From 8aae8b885f12c38843a2f20cc7009d4a8b4f9b96 Mon Sep 17 00:00:00 2001 From: Steffen Allner Date: Fri, 19 Nov 2021 16:44:15 +0100 Subject: [PATCH 1/4] Use `AC_PURE_PYTHON` to switch default implementation to python. Use it only for tests. --- .github/workflows/tests.yml | 8 +++----- CHANGES.rst | 2 ++ src/AccessControl/ImplPython.py | 4 ++-- src/AccessControl/Implementation.py | 13 ++++++++++++- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 44492eb..fd2c6fc 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -229,11 +229,9 @@ jobs: if: ${{ !startsWith(matrix.python-version, 'pypy') }} run: | python -m coverage run -p -m zope.testrunner --test-path=src --auto-color --auto-progress -# This is not supported -# - name: Run tests without C extensions -# run: -# # coverage makes PyPy run about 3x slower! -# PURE_PYTHON=1 python -m coverage run -p -m zope.testrunner --test-path=src --auto-color --auto-progress + - name: Run tests without C extensions + run: + AC_PURE_PYTHON=1 python -m coverage run -p -m zope.testrunner --test-path=src --auto-color --auto-progress - name: Report Coverage run: | coverage combine diff --git a/CHANGES.rst b/CHANGES.rst index ec950c6..f6a7e9e 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -8,6 +8,8 @@ For changes before version 3.0, see ``HISTORY.rst``. - Provide ``AccessControl.get_safe_globals`` to facilitate safe use. +- Use ``AC_PURE_PYTHON`` as switch to enable python implementation as default. + This should not be used except for tests and debugging. 5.2 (2021-07-30) diff --git a/src/AccessControl/ImplPython.py b/src/AccessControl/ImplPython.py index edd02f0..61fd120 100644 --- a/src/AccessControl/ImplPython.py +++ b/src/AccessControl/ImplPython.py @@ -76,8 +76,8 @@ def rolesForPermissionOn(perm, object, default=_default_roles, n=None): roles = getattr(object, n) if roles is None: if _embed_permission_in_roles: - return ('Anonymous', n) - return 'Anonymous' + return (('Anonymous',), n) + return ('Anonymous',) t = type(roles) if t is tuple: diff --git a/src/AccessControl/Implementation.py b/src/AccessControl/Implementation.py index bf54394..2defee7 100644 --- a/src/AccessControl/Implementation.py +++ b/src/AccessControl/Implementation.py @@ -25,6 +25,13 @@ """ from __future__ import absolute_import +import os + +# We cannot use `PURE_PYTHON` as this would be propagated to `ExtensionClass` +# and `Acquisition`. Due to restrictions of python implementation concerning +# proxies it is not possible to run all with pure python and we influence only +# the AccessControl implementation here. +CAPI = not int(os.environ.get('AC_PURE_PYTHON', '0')) def getImplementationName(): @@ -96,7 +103,11 @@ def setImplementation(name): ), } -setImplementation(_default_implementation) + +if CAPI: + setImplementation(_default_implementation) +else: + setImplementation('PYTHON') # allow the implementation to change from the default _implementation_set = 0 From 90ea12ef0f8516a9369f46b3097ba31f86b7573d Mon Sep 17 00:00:00 2001 From: Steffen Allner Date: Fri, 19 Nov 2021 16:51:11 +0100 Subject: [PATCH 2/4] Fix syntax. --- .github/workflows/tests.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index fd2c6fc..41563c0 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -229,9 +229,9 @@ jobs: if: ${{ !startsWith(matrix.python-version, 'pypy') }} run: | python -m coverage run -p -m zope.testrunner --test-path=src --auto-color --auto-progress - - name: Run tests without C extensions - run: - AC_PURE_PYTHON=1 python -m coverage run -p -m zope.testrunner --test-path=src --auto-color --auto-progress + - name: Run tests without C extensions + run: | + AC_PURE_PYTHON=1 python -m coverage run -p -m zope.testrunner --test-path=src --auto-color --auto-progress - name: Report Coverage run: | coverage combine From aa992014daf8a09914ed49550cffe77acc37483a Mon Sep 17 00:00:00 2001 From: Michael Howitz Date: Wed, 16 Feb 2022 08:48:33 +0100 Subject: [PATCH 3/4] Support PURE_PYTHON instead of AC_PURE_PYTHON (#120) * Do not import from cAccessControl in ImplPython. * add `gc.collect` calls to let the `testIdentityProxy` test succeed * Do not fail fast but show all failures. * Make the linter a bit more happy. * Handle garbage collector in TestGuardedHasattr as done in TestGuardedGetattr. * Drop AC_PURE_PYTHON. Co-authored-by: dieter --- .github/workflows/tests.yml | 4 +++- CHANGES.rst | 4 ++-- buildout.cfg | 1 - src/AccessControl/ImplPython.py | 9 ++------- src/AccessControl/Implementation.py | 9 ++++----- src/AccessControl/tests/testSecurityManager.py | 2 ++ src/AccessControl/tests/testZopeGuards.py | 2 ++ src/AccessControl/tests/testZopeSecurityPolicy.py | 6 ++++-- tox.ini | 2 ++ 9 files changed, 21 insertions(+), 18 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 41563c0..8f898e4 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -92,6 +92,7 @@ jobs: # with `test`, and `docs` must use a subset. runs-on: ${{ matrix.os }} strategy: + fail-fast: false matrix: python-version: - 2.7 @@ -171,6 +172,7 @@ jobs: needs: build-package runs-on: ${{ matrix.os }} strategy: + fail-fast: false matrix: python-version: - 2.7 @@ -231,7 +233,7 @@ jobs: python -m coverage run -p -m zope.testrunner --test-path=src --auto-color --auto-progress - name: Run tests without C extensions run: | - AC_PURE_PYTHON=1 python -m coverage run -p -m zope.testrunner --test-path=src --auto-color --auto-progress + PURE_PYTHON=1 python -m coverage run -p -m zope.testrunner --test-path=src --auto-color --auto-progress - name: Report Coverage run: | coverage combine diff --git a/CHANGES.rst b/CHANGES.rst index f6a7e9e..e481d49 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -8,8 +8,8 @@ For changes before version 3.0, see ``HISTORY.rst``. - Provide ``AccessControl.get_safe_globals`` to facilitate safe use. -- Use ``AC_PURE_PYTHON`` as switch to enable python implementation as default. - This should not be used except for tests and debugging. +- Honor ``PURE_PYTHON`` environment variable to enable python implementation + during runtime. 5.2 (2021-07-30) diff --git a/buildout.cfg b/buildout.cfg index 0b4aeda..afbc7c2 100644 --- a/buildout.cfg +++ b/buildout.cfg @@ -7,7 +7,6 @@ parts = interpreter test coverage [versions] AccessControl = -RestrictedPython = [interpreter] recipe = zc.recipe.egg diff --git a/src/AccessControl/ImplPython.py b/src/AccessControl/ImplPython.py index 61fd120..00f3785 100644 --- a/src/AccessControl/ImplPython.py +++ b/src/AccessControl/ImplPython.py @@ -26,13 +26,8 @@ from ExtensionClass import Base from zope.interface import implementer -# This is used when a permission maps explicitly to no permission. We -# try and get this from cAccessControl first to make sure that if both -# security implementations exist, we can switch between them later. -try: - from AccessControl.cAccessControl import _what_not_even_god_should_do -except ImportError: - _what_not_even_god_should_do = [] +# We define our own `_what_not_even_god_should_do` to allow using PURE_PYTHON=1 +_what_not_even_god_should_do = [] from AccessControl.interfaces import ISecurityManager from AccessControl.interfaces import ISecurityPolicy diff --git a/src/AccessControl/Implementation.py b/src/AccessControl/Implementation.py index 2defee7..638a5a0 100644 --- a/src/AccessControl/Implementation.py +++ b/src/AccessControl/Implementation.py @@ -25,13 +25,12 @@ """ from __future__ import absolute_import + import os -# We cannot use `PURE_PYTHON` as this would be propagated to `ExtensionClass` -# and `Acquisition`. Due to restrictions of python implementation concerning -# proxies it is not possible to run all with pure python and we influence only -# the AccessControl implementation here. -CAPI = not int(os.environ.get('AC_PURE_PYTHON', '0')) + +PURE_PYTHON = int(os.environ.get('PURE_PYTHON', '0')) +CAPI = not PURE_PYTHON def getImplementationName(): diff --git a/src/AccessControl/tests/testSecurityManager.py b/src/AccessControl/tests/testSecurityManager.py index 40058a0..a1ae9cd 100644 --- a/src/AccessControl/tests/testSecurityManager.py +++ b/src/AccessControl/tests/testSecurityManager.py @@ -14,6 +14,7 @@ """Tests for the SecurityManager implementations """ +import os import unittest @@ -250,6 +251,7 @@ def _getTargetClass(self): return SecurityManager +@unittest.skipIf(os.environ.get('PURE_PYTHON'), reason="Test expects C impl.") class C_SecurityManagerTests(SecurityManagerTestBase, ISecurityManagerConformance, unittest.TestCase): diff --git a/src/AccessControl/tests/testZopeGuards.py b/src/AccessControl/tests/testZopeGuards.py index 7611232..b8ec634 100644 --- a/src/AccessControl/tests/testZopeGuards.py +++ b/src/AccessControl/tests/testZopeGuards.py @@ -167,9 +167,11 @@ class TestGuardedHasattr(GuardTestCase): def setUp(self): self.__sm = SecurityManager() self.__old = self.setSecurityManager(self.__sm) + gc.disable() def tearDown(self): self.setSecurityManager(self.__old) + gc.enable() def test_miss(self): from AccessControl.ZopeGuards import guarded_hasattr diff --git a/src/AccessControl/tests/testZopeSecurityPolicy.py b/src/AccessControl/tests/testZopeSecurityPolicy.py index c81face..eecfe6b 100644 --- a/src/AccessControl/tests/testZopeSecurityPolicy.py +++ b/src/AccessControl/tests/testZopeSecurityPolicy.py @@ -11,6 +11,7 @@ # ############################################################################## +import os import sys import unittest from doctest import DocTestSuite @@ -775,9 +776,10 @@ def loop(): def test_suite(): suite = unittest.TestSuite() suite.addTest(unittest.makeSuite(Python_ZSPTests)) - suite.addTest(unittest.makeSuite(C_ZSPTests)) suite.addTest(unittest.makeSuite(Python_SMTests)) - suite.addTest(unittest.makeSuite(C_SMTests)) + if not os.environ.get('PURE_PYTHON'): + suite.addTest(unittest.makeSuite(C_ZSPTests)) + suite.addTest(unittest.makeSuite(C_SMTests)) suite.addTest(DocTestSuite()) suite.addTest(unittest.makeSuite(GetRolesWithMultiThreadTest)) return suite diff --git a/tox.ini b/tox.ini index 079a38c..8f8fe65 100644 --- a/tox.ini +++ b/tox.ini @@ -19,6 +19,8 @@ deps = setuptools < 52 zc.buildout skip_install = true +setenv = + PURE_PYTHON=1 [testenv:coverage] basepython = python3 From c1eea9015509c3b526153f4744c2318a90814db7 Mon Sep 17 00:00:00 2001 From: Michael Howitz Date: Tue, 22 Feb 2022 08:21:58 +0100 Subject: [PATCH 4/4] Try to use c implementation if suitable. --- src/AccessControl/ImplPython.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/AccessControl/ImplPython.py b/src/AccessControl/ImplPython.py index 00f3785..88b952f 100644 --- a/src/AccessControl/ImplPython.py +++ b/src/AccessControl/ImplPython.py @@ -26,8 +26,12 @@ from ExtensionClass import Base from zope.interface import implementer -# We define our own `_what_not_even_god_should_do` to allow using PURE_PYTHON=1 -_what_not_even_god_should_do = [] +PURE_PYTHON = int(os.environ.get('PURE_PYTHON', '0')) +if PURE_PYTHON: + # We need our own to not depend on the C implementation: + _what_not_even_god_should_do = [] +else: + from AccessControl.cAccessControl import _what_not_even_god_should_do from AccessControl.interfaces import ISecurityManager from AccessControl.interfaces import ISecurityPolicy @@ -37,6 +41,8 @@ from AccessControl.SimpleObjectPolicies import _noroles from AccessControl.unauthorized import Unauthorized from AccessControl.ZopeGuards import guarded_getitem # NOQA + + # AccessControl.ZopeSecurityPolicy # -------------------------------- #