Skip to content

Commit

Permalink
Merge pull request #32 from zopefoundation/remove_u
Browse files Browse the repository at this point in the history
Simplify _compat.py now that we only run on newer Pythons
  • Loading branch information
jamadden committed Sep 8, 2017
2 parents ed4d2b7 + a0372d5 commit d8565d6
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 180 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ Changes
default. In addition, iteration of all the custom iterator types
defined in itertools are also allowed by default.

- Simplify the internal ``_compat.py`` module now that we only run on
newer Python versions. See `PR 32 <https://github.com/zopefoundation/zope.security/pull/32>`_.

4.1.1 (2017-05-17)
------------------

Expand Down
27 changes: 4 additions & 23 deletions src/zope/security/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,23 @@

py_impl = getattr(platform, 'python_implementation', lambda: None)
PYPY = py_impl() == 'PyPy'
PURE_PYTHON = os.environ.get('PURE_PYTHON', False)
PURE_PYTHON = os.environ.get('PURE_PYTHON', PYPY)

if sys.version_info[0] < 3: #pragma NO COVER

from StringIO import StringIO
import cPickle as _pickle

reload = reload

def _u(s):
return unicode(s, 'unicode_escape')
if sys.version_info[0] < 3: # pragma: no cover

CLASS_TYPES = (type, types.ClassType)
_BUILTINS = '__builtin__'

PYTHON3 = False
PYTHON2 = True

TEXT = unicode

else: #pragma NO COVER

from io import StringIO
import pickle as _pickle

from imp import reload

def _u(s):
return s
else: # pragma: no cover

CLASS_TYPES = (type,)
_BUILTINS = 'builtins'

PYTHON3 = True
PYTHON2 = False

TEXT = str

_BLANK = _u('')
_BLANK = u''
7 changes: 3 additions & 4 deletions src/zope/security/_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@
import zope.interface

from zope.security import interfaces
from zope.security._compat import _u

thread_local = threading.local()

@zope.interface.provider(interfaces.IPrincipal)
class system_user(object):
id = _u('zope.security.management.system_user')
title = _u('System')
description = _u('')
id = u'zope.security.management.system_user'
title = u'System'
description = u''
3 changes: 1 addition & 2 deletions src/zope/security/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
from zope.security._definitions import thread_local
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
from zope.security.proxy import getChecker

Expand Down Expand Up @@ -687,7 +686,7 @@ def update(self, d):
}

if PYTHON2:
BasicTypes_examples[unicode] = _u('uabc')
BasicTypes_examples[unicode] = u'uabc'
BasicTypes_examples[long] = long(65536)


Expand Down
33 changes: 16 additions & 17 deletions src/zope/security/metadirectives.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import zope.security.zcml
from zope.security.i18n import ZopeMessageFactory as _
from zope.security.zcml import Permission
from zope.security._compat import _u

class IClassDirective(zope.interface.Interface):
"""Make statements about a class"""
Expand Down Expand Up @@ -149,8 +148,8 @@ class IModule(Interface):
"""Group security declarations about a module"""

module = GlobalObject(
title=_u("Module"),
description=_u("Pointer to the module object."),
title=u"Module",
description=u"Pointer to the module object.",
required=True)


Expand All @@ -163,17 +162,17 @@ class IAllow(Interface):
"""

attributes = Tokens(
title=_u("Attributes"),
description=_u("The attributes to provide access to."),
value_type = PythonIdentifier(),
title=u"Attributes",
description=u"The attributes to provide access to.",
value_type=PythonIdentifier(),
required=False)

interface = Tokens(
title=_u("Interface"),
description=_u("Interfaces whos names to provide access to. Access "
"will be provided to all of the names defined by the "
"interface(s). Multiple interfaces can be supplied."),
value_type = GlobalInterface(),
title=u"Interface",
description=(u"Interfaces whos names to provide access to. Access "
u"will be provided to all of the names defined by the "
u"interface(s). Multiple interfaces can be supplied."),
value_type=GlobalInterface(),
required=False)


Expand All @@ -182,15 +181,15 @@ class IRequire(Interface):
The given permission is required to access any names provided
directly in the attributes attribute or any names defined by
interfaces listed in the interface attribute.
interfaces listed in the interface attribute.
"""

attributes = Tokens(
title=_u("Attributes"),
description=_u("The attributes to require permission for."),
value_type = PythonIdentifier(),
title=u"Attributes",
description=u"The attributes to require permission for.",
value_type=PythonIdentifier(),
required=False)

permission = Permission(
title=_u("Permission ID"),
description=_u("The id of the permission to require."))
title=u"Permission ID",
description=u"The id of the permission to require.")
5 changes: 2 additions & 3 deletions src/zope/security/permission.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

from zope.security.checker import CheckerPublic
from zope.security.interfaces import IPermission
from zope.security._compat import _u

@implementer(IPermission)
class Permission(object):
Expand All @@ -49,7 +48,7 @@ def allPermissions(context=None):
"""Get the ids of all defined permissions
"""
for id, permission in getUtilitiesFor(IPermission, context):
if id != _u('zope.Public'):
if id != u'zope.Public':
yield id

def PermissionsVocabulary(context=None):
Expand Down Expand Up @@ -85,7 +84,7 @@ def PermissionIdsVocabulary(context=None):
terms.append(SimpleTerm(name, name, name))
terms = sorted(terms, key=operator.attrgetter('title'))
if has_public:
terms.insert(0, SimpleTerm(CheckerPublic, 'zope.Public', _u('Public')))
terms.insert(0, SimpleTerm(CheckerPublic, 'zope.Public', u'Public'))
return SimpleVocabulary(terms)

directlyProvides(PermissionIdsVocabulary, IVocabularyFactory)
13 changes: 5 additions & 8 deletions src/zope/security/tests/test_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -868,13 +868,12 @@ def tearDown(self):
def test_w_basic_types_NoProxy(self):
import datetime
from zope.i18nmessageid import Message
from zope.security._compat import _u
msg = Message('msg')
for obj in [object(),
42,
3.14,
None,
_u('text'),
u'text',
b'binary',
msg,
True,
Expand Down Expand Up @@ -1044,13 +1043,12 @@ def _callFUT(self, type_, checker):
return defineChecker(type_, checker)

def test_w_wrong_type(self):
from zope.security._compat import _u
checker = object()
for obj in [object(),
42,
3.14,
None,
_u('text'),
u'text',
b'binary',
True,
]:
Expand Down Expand Up @@ -2108,11 +2106,10 @@ class SomeClass(object):
class TestCheckerPublic(unittest.TestCase):

def test_that_pickling_CheckerPublic_retains_identity(self):
from zope.security._compat import _pickle
import pickle
from zope.security.checker import CheckerPublic
self.assertTrue(_pickle.loads(_pickle.dumps(CheckerPublic))
is
CheckerPublic)
self.assertIs(pickle.loads(pickle.dumps(CheckerPublic)),
CheckerPublic)

def test_that_CheckerPublic_identity_works_even_when_proxied(self):
from zope.security.checker import ProxyFactory
Expand Down
14 changes: 6 additions & 8 deletions src/zope/security/tests/test_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,15 @@ def checkPermission(s, p, o):

def test_system_user(self):
from zope.security.management import system_user
from zope.security._compat import TEXT
from zope.security._compat import _u

self.assertEqual(system_user.id,
_u('zope.security.management.system_user'))
u'zope.security.management.system_user')

self.assertEqual(system_user.title, _u('System'))
self.assertEqual(system_user.title, u'System')

for name in 'id', 'title', 'description':
self.assertTrue(isinstance(getattr(system_user, name), TEXT))
self.assertIsInstance(getattr(system_user, name),
type(u''))

def test_suite():
return unittest.TestSuite((
unittest.makeSuite(Test),
))
return unittest.defaultTestLoader.loadTestsFromName(__name__)
17 changes: 4 additions & 13 deletions src/zope/security/tests/test_metaconfigure.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,9 @@ def test_require_w_set_schema_normal_fields(self):
from zope.schema import Field
from zope.interface import Interface
from zope.security.protectclass import protectSetAttribute
from zope.security._compat import _u
class IFoo(Interface):
bar = Field(_u("Bar"))
baz = Field(_u("Baz"))
bar = Field(u"Bar")
baz = Field(u"Baz")
context = DummyZCMLContext()
directive = self._makeOne(context, Foo)
directive.require(context, permission='testing', set_schema=[IFoo])
Expand Down Expand Up @@ -258,9 +257,8 @@ def test_require_w_set_schema_ignores_readonly_fields(self):
from zope.component.interface import provideInterface
from zope.schema import Field
from zope.interface import Interface
from zope.security._compat import _u
class IFoo(Interface):
bar = Field(_u("Bar"), readonly=True)
bar = Field(u"Bar", readonly=True)
context = DummyZCMLContext()
directive = self._makeOne(context, Foo)
directive.require(context, permission='testing', set_schema=[IFoo])
Expand Down Expand Up @@ -650,11 +648,4 @@ def action(self, **kw):


def test_suite():
return unittest.TestSuite([
unittest.makeSuite(Test_dottedName),
unittest.makeSuite(ClassDirectiveTests),
unittest.makeSuite(Test_protectModule),
unittest.makeSuite(Test_allow),
unittest.makeSuite(Test_allow),
])

return unittest.defaultTestLoader.loadTestsFromName(__name__)
Loading

0 comments on commit d8565d6

Please sign in to comment.