Skip to content

Commit

Permalink
Restore filename on code objects: App.Extensions.getObject(). (#1138)
Browse files Browse the repository at this point in the history
Since 4.0a6 the filename is missing there, resulting in problems while
debugging (debugger is unable to show the code line) and coverage of
Python modules loaded via Products.ExternalMethods.
  • Loading branch information
Michael Howitz committed Jun 19, 2023
1 parent 1f2a5a6 commit b0bb108
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ https://github.com/zopefoundation/Zope/blob/4.x/CHANGES.rst
For details, see
`#1136 <https://github.com/zopefoundation/Zope/issues/1136>`_.

- Restore filename on code objects of objects returned from
``App.Extensions.getObject()``. This got lost in 4.0a6.

- Update to newest compatible versions of dependencies.


Expand Down
3 changes: 2 additions & 1 deletion src/App/Extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,9 @@ def getObject(module, name, reload=0):
except Exception:
raise NotFound("The specified module, '%s', "
"couldn't be opened." % module)
execcode = compile(execsrc, path, 'exec')
module_dict = {}
exec(execsrc, module_dict)
exec(execcode, module_dict)

if old is not None:
# XXX Accretive??
Expand Down
2 changes: 2 additions & 0 deletions src/App/tests/fixtures/error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# This file intentionally contains a SyntaxError
a = # noqa
5 changes: 5 additions & 0 deletions src/App/tests/fixtures/getObject.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import sys


def f1():
return sys._getframe(0).f_code.co_filename
38 changes: 38 additions & 0 deletions src/App/tests/test_Extensions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import types
import unittest
from pathlib import Path

import App.config
from App.Extensions import getObject


class GetObjectTests(unittest.TestCase):
"""Testing ..Extensions.getObject()."""

def setUp(self):
cfg = App.config.getConfiguration()
assert not hasattr(cfg, 'extensions')
cfg.extensions = Path(__file__).parent / 'fixtures'

def tearDown(self):
cfg = App.config.getConfiguration()
del cfg.extensions

def test_Extensions__getObject__1(self):
"""Check that "getObject" returns the requested function and ...
that its code object has the path set.
"""
obj = getObject('getObject', 'f1')
self.assertIsInstance(obj, types.FunctionType)
self.assertEqual(obj.__name__, 'f1')
path = obj()
self.assertTrue(
path.endswith(str(Path('App/tests/fixtures/getObject.py'))))

def test_Extensions__getObject__2(self):
"""It raises a SyntaxError if necessary."""
try:
getObject('error', 'f1')
except SyntaxError as e:
self.assertEqual(str(e), 'invalid syntax (error.py, line 2)')

0 comments on commit b0bb108

Please sign in to comment.