Skip to content

Commit

Permalink
Zope 4: Restore filename on code objects: `App.Extensions.getObject()…
Browse files Browse the repository at this point in the history
…`. (#1139)

* Port of #1138 to Zope 4.
* Get back Python 2.7.
* Fix builds: Build on pushes to every Zope 4.x branch. Leave out pull requests as they were are disabled later on.
  • Loading branch information
Michael Howitz committed Jun 19, 2023
1 parent 828b197 commit 601c8f7
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 5 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ name: tests

on:
push:
branches: [ 4.x ]
pull_request:
schedule:
- cron: '0 12 * * 0' # run once a week on Sunday
# Allow to run this workflow manually from the Actions tab
Expand Down Expand Up @@ -35,15 +33,15 @@ jobs:
- { os: ["windows", "windows-latest"], config: ["3.8", "lint"] }
- { os: ["windows", "windows-latest"], config: ["3.8", "docs"] }
- { os: ["windows", "windows-latest"], config: ["3.8", "coverage"] }
- { os: ["windows", "windows-latest"], config: ["2.7", "py27"] }
- { os: ["windows", "windows-latest"], config: ["2.7", "py27-zserver"] }

runs-on: ${{ matrix.os[1] }}
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name
name: ${{ matrix.os[0] }}-${{ matrix.config[1] }}
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
uses: MatteoH2O1999/setup-python@v1
with:
python-version: ${{ matrix.config[0] }}
- name: Pip cache
Expand Down
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ https://zope.readthedocs.io/en/2.13/CHANGES.html
- Sanitize tainting fixing
`#1095 <https://github.com/zopefoundation/Zope/issues/1095>`_

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


4.8.7 (2023-01-10)
------------------
Expand Down
3 changes: 2 additions & 1 deletion src/App/Extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,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
40 changes: 40 additions & 0 deletions src/App/tests/test_Extensions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import os.path
import types
import unittest

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 = os.path.join(os.path.dirname(__file__), '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(
os.path.sep.join(
['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 601c8f7

Please sign in to comment.