From 8cce64bbef274d776ac4856843ebe25b7d9cb51f Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Mon, 11 Mar 2013 00:08:02 -0400 Subject: [PATCH] Start of PyPy support. --- src/zope/security/__init__.py | 6 ++++++ src/zope/security/_compat.py | 4 ++++ src/zope/security/checker.py | 8 ++++++-- src/zope/security/proxy.py | 27 +++++++++++++++------------ src/zope/security/tests/test_proxy.py | 6 +++--- tox.ini | 2 +- 6 files changed, 35 insertions(+), 18 deletions(-) diff --git a/src/zope/security/__init__.py b/src/zope/security/__init__.py index 45c660a..5f079f6 100644 --- a/src/zope/security/__init__.py +++ b/src/zope/security/__init__.py @@ -16,3 +16,9 @@ """ from zope.security.management import checkPermission from zope.security.checker import canWrite, canAccess + +# We need the injection of DecoratedSecurityCheckerDescriptor into +# zope.location's LocationProxy as soon someone uses security proxies by +# importing zope.security.proxy: +import zope.security.decorator + diff --git a/src/zope/security/_compat.py b/src/zope/security/_compat.py index 414c290..6de4758 100644 --- a/src/zope/security/_compat.py +++ b/src/zope/security/_compat.py @@ -13,9 +13,13 @@ ############################################################################## """ Python 2 / 3 compatibility """ +import platform import sys import types +py_impl = getattr(platform, 'python_implementation', lambda: None) +PYPY = py_impl() == 'PyPy' + if sys.version_info[0] < 3: #pragma NO COVER from StringIO import StringIO diff --git a/src/zope/security/checker.py b/src/zope/security/checker.py index 5f1d7df..061cc44 100644 --- a/src/zope/security/checker.py +++ b/src/zope/security/checker.py @@ -50,8 +50,8 @@ from zope.security._compat import CLASS_TYPES from zope.security._compat import PYTHON2 from zope.security._compat import _u -from zope.security._proxy import _Proxy as Proxy -from zope.security._proxy import getChecker +from zope.security.proxy import Proxy +from zope.security.proxy import getChecker try: from zope.exceptions import DuplicationError @@ -98,6 +98,10 @@ def ProxyFactory(object, checker=None): directlyProvides(ProxyFactory, ISecurityProxyFactory) +# This import represents part of the API for the proxy module +from . import proxy +proxy.ProxyFactory = ProxyFactory + def canWrite(obj, name): """Check whether the interaction may write an attribute named name on obj. diff --git a/src/zope/security/proxy.py b/src/zope/security/proxy.py index 9457ab1..8291045 100644 --- a/src/zope/security/proxy.py +++ b/src/zope/security/proxy.py @@ -19,6 +19,7 @@ import sys from zope.proxy import PyProxyBase +from zope.security._compat import PYPY from zope.security.interfaces import ForbiddenAttribute @@ -272,28 +273,27 @@ def __repr__(self): meth = getattr(PyProxyBase, name) setattr(ProxyPy, name, _check_name_inplace(meth)) +def getCheckerPy(proxy): + return proxy._checker + +def getObjectPy(proxy): + # Aem, if this works, how is the Python implementation providing any + # security? + return proxy._wrapped + try: from zope.security._proxy import _Proxy except ImportError: #pragma NO COVER PyPy - #getChecker = getCheckerPy - #getObject = getObjectPy + getChecker = getCheckerPy + getObject = getObjectPy Proxy = ProxyPy else: #pragma NO COVER CPython from zope.security._proxy import getChecker from zope.security._proxy import getObject Proxy = _Proxy -# We need the injection of DecoratedSecurityCheckerDescriptor into -# zope.location's LocationProxy as soon someone uses security proxies by -# importing zope.security.proxy: -import zope.security.decorator - - removeSecurityProxy = getObject -# This import represents part of the API for this module -from zope.security.checker import ProxyFactory - def getTestProxyItems(proxy): """Return a sorted sequence of checker names and permissions for testing """ @@ -309,7 +309,10 @@ def isinstance(object, cls): """ global builtin_isinstance if builtin_isinstance is None: - builtin_isinstance = __builtins__['isinstance'] + if PYPY: + builtin_isinstance = getattr(__builtins__, 'isinstance') + else: + builtin_isinstance = __builtins__['isinstance'] # The removeSecurityProxy call is OK here because it is *only* # being used for isinstance return builtin_isinstance(removeSecurityProxy(object), cls) diff --git a/src/zope/security/tests/test_proxy.py b/src/zope/security/tests/test_proxy.py index 38663cc..7c15d52 100644 --- a/src/zope/security/tests/test_proxy.py +++ b/src/zope/security/tests/test_proxy.py @@ -1887,13 +1887,13 @@ def test_using_mapping_slots_hack(): class LocationProxySecurityCheckerTests(unittest.TestCase): - def test_LocationProxy_gets_a_security_checker_when_importing_z_s_proxy( + def test_LocationProxy_gets_a_security_checker_when_importing_z_security( self): # Regression test for a problem introduced in 3.8.1 and fixed in # 3.8.3. For details see change log. import sys from zope.location.location import LocationProxy - import zope.security.proxy + import zope.security from zope.security._compat import reload # This attribute is set when zope.security.decorator is imported, to # show that it will be set too, if zope.security.proxy is imported @@ -1904,7 +1904,7 @@ def test_LocationProxy_gets_a_security_checker_when_importing_z_s_proxy( # After deleting zope.security.decorator and reloading # zope.security.proxy the attribute is set again: del sys.modules["zope.security.decorator"] - reload(zope.security.proxy) + reload(zope.security) self.assertTrue( hasattr(LocationProxy, '__Security_checker__')) diff --git a/tox.ini b/tox.ini index ee015f0..a2df416 100644 --- a/tox.ini +++ b/tox.ini @@ -3,7 +3,7 @@ envlist = # Jython support pending 2.7 support, due 2012-07-15 or so. See: # http://fwierzbicki.blogspot.com/2012/03/adconion-to-fund-jython-27.html # py26,py27,pypy,jython,py32,py33,coverage,docs - py26,py27,py32,py33,coverage,docs + py26,py27,pypy,py32,py33,coverage,docs [testenv] deps =