Minor console code tweaks

This commit is contained in:
karxi 2016-12-23 07:21:23 -05:00
parent 2c594f47b3
commit 8da877cf85

View file

@ -118,7 +118,6 @@ class ConsoleWindow(QtGui.QDialog):
env = pchum._retrieveGlobals() env = pchum._retrieveGlobals()
# Modify the environment the script will execute in. # Modify the environment the script will execute in.
# NOTE: This doesn't presently work, for some reason.
_CUSTOM_ENV = { _CUSTOM_ENV = {
"CONSOLE": self, "CONSOLE": self,
"MAINWIN": self.mainwindow, "MAINWIN": self.mainwindow,
@ -126,9 +125,16 @@ class ConsoleWindow(QtGui.QDialog):
} }
_CUSTOM_ENV_USED = [] _CUSTOM_ENV_USED = []
cenv = pchum.__dict__ cenv = pchum.__dict__
# Display the input we provided
# We do this here, *before* we do our variable injection, so that it
# doesn't have to be part of the try statement, where it could
# potentially complicate matters/give false positives.
self.addMessage(scriptstr, 1)
for k in _CUSTOM_ENV: for k in _CUSTOM_ENV:
if k not in cenv: if k not in cenv:
# Inject the variable for ease of use.
cenv[k] = _CUSTOM_ENV[k] cenv[k] = _CUSTOM_ENV[k]
# Record that we injected it.
_CUSTOM_ENV_USED.append(k) _CUSTOM_ENV_USED.append(k)
else: else:
# Don't overwrite anything! # Don't overwrite anything!
@ -137,27 +143,24 @@ class ConsoleWindow(QtGui.QDialog):
logging.warning(warn) logging.warning(warn)
# Because all we did was change a linked AttrDict, we should be fine # Because all we did was change a linked AttrDict, we should be fine
# here. # here.
try:
# Display the input we provided
self.addMessage(scriptstr, 1)
# Replace the old writer (for now) # Replace the old writer (for now)
sysout, sys.stdout = sys.stdout, self sysout, sys.stdout = sys.stdout, self
try: try:
code = compile(scriptstr + '\n', "<string>", "single") code = compile(scriptstr + '\n', "<string>", "single")
result = eval(code, env) # Will using cenv instead of env cause problems?...
result = eval(code, cenv)
except: except:
# Something went wrong. # Something went wrong.
#~logging.exception("Exception: %s", err, exc_info=sys.exc_info())
self.addTraceback(sys.exc_info()[2]) self.addTraceback(sys.exc_info()[2])
else: else:
# No errors. # No errors.
if result is not None: if result is not None:
#~self.addMessage(repr(result), -1)
print repr(result) print repr(result)
finally: finally:
# Restore system output. # Restore system output.
sys.stdout = sysout sys.stdout = sysout
finally:
# Try to clean us out of globals - this might be disabled # Try to clean us out of globals - this might be disabled
# later. # later.
for k in _CUSTOM_ENV_USED: for k in _CUSTOM_ENV_USED: