Skip to content

Commit

Permalink
Fix handling of globals and locals in zconsole scripts. (#374)
Browse files Browse the repository at this point in the history
* Fix handling of globals and locals in zconsole scripts.

In case `exec` is called without arguments, the current scope is used. This
results in a overfull `globals` missing the `app` which is only in `locals`.
  • Loading branch information
sallner authored and Michael Howitz committed Oct 16, 2018
1 parent fae058e commit f0de1c8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
6 changes: 5 additions & 1 deletion CHANGES.rst
Expand Up @@ -11,7 +11,11 @@ https://github.com/zopefoundation/Zope/blob/4.0a6/CHANGES.rst
4.0b7 (unreleased)
------------------

- Nothing changed yet.
Bugfixes
++++++++

- Fix a bug with scopes in scripts with zconsole, which made it impossible to
reach global imports in the script within a function.


4.0b6 (2018-10-11)
Expand Down
14 changes: 12 additions & 2 deletions src/Zope2/utilities/tests/test_zconsole.py
Expand Up @@ -21,12 +21,20 @@

test_script = """
import sys
import OFS.PropertyManager
def print_info():
# This tests the availability of global variables and imports.
print(sys.argv[1:])
# 'PropertyManager'
print(OFS.PropertyManager.PropertyManager.__name__)
if __name__ == '__main__':
app.foo = '42'
print(app.foo)
print(sys.argv[1:])
print_info()
"""


Expand Down Expand Up @@ -75,5 +83,7 @@ def test_runscript(self):
finally:
sys.argv = self.stored_sys_argv
sys.stdout = self.stored_stdout
expected = "42\n['run', '{}', '{}', 'bar', 'baz']\n".format(self.zopeconf, script) # noqa: E501
expected = (
"42\n['run', '{}', '{}', 'bar', 'baz']\nPropertyManager\n").format(
self.zopeconf, script)
self.assertEqual(expected, got)
5 changes: 2 additions & 3 deletions src/Zope2/utilities/zconsole.py
Expand Up @@ -15,11 +15,10 @@ def runscript(zopeconf, script_name, *extra_args):
app.REQUEST['PARENTS'] = [app]
setRequest(app.REQUEST)
newSecurityManager(None, user)
scriptglobals = globals()
scriptglobals.update(__name__='__main__')
scriptglobals = {'__name__': '__main__', 'app': app}
with open(script_name) as script:
scriptcode = script.read()
exec(compile(scriptcode, script_name, 'exec'))
exec(compile(scriptcode, script_name, 'exec'), scriptglobals)


def debug(zopeconf):
Expand Down

0 comments on commit f0de1c8

Please sign in to comment.