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.
# Display the input we provided
self.addMessage(scriptstr, 1)
# Replace the old writer (for now)
sysout, sys.stdout = sys.stdout, self
try: try:
code = compile(scriptstr + '\n', "<string>", "single") # Replace the old writer (for now)
result = eval(code, env) sysout, sys.stdout = sys.stdout, self
except: try:
# Something went wrong. code = compile(scriptstr + '\n', "<string>", "single")
#~logging.exception("Exception: %s", err, exc_info=sys.exc_info()) # Will using cenv instead of env cause problems?...
self.addTraceback(sys.exc_info()[2]) result = eval(code, cenv)
else: except:
# No errors. # Something went wrong.
if result is not None: self.addTraceback(sys.exc_info()[2])
#~self.addMessage(repr(result), -1) else:
print repr(result) # No errors.
if result is not None:
print repr(result)
finally:
# Restore system output.
sys.stdout = sysout
finally: finally:
# Restore system output.
sys.stdout = sysout
# 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: