Skip to content

Commit

Permalink
Upgrade syntax to be mostly idiomatic python 2.7.
Browse files Browse the repository at this point in the history
So we have a baseline from which to bootstrap python 3 porting while retaining python 2.7 compatibility.
  • Loading branch information
dwt committed Sep 30, 2016
1 parent 3ffdc3a commit db92b78
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/AccessControl/ImplPython.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ def guarded_getattr(inst, name, default=_marker):
raise

try:
container = v.im_self
container = v.__self__
except AttributeError:
container = aq_parent(aq_inner(v)) or inst

Expand Down Expand Up @@ -826,7 +826,7 @@ def verifyAcquisitionContext(user, object, object_roles=None):
return 1
if hasattr(object, 'im_self'):
# This is a method. Grab its self.
object=object.im_self
object=object.__self__
if not aq_inContextOf(object, ucontext, 1):
if 'Shared' in object_roles:
# Old role setting. Waaa
Expand Down
2 changes: 1 addition & 1 deletion src/AccessControl/Permission.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def getRoles(self, default=_marker):
if hasattr(obj, name):
attr = getattr(obj, name)
if hasattr(attr, 'im_self'):
attr = attr.im_self
attr = attr.__self__
if hasattr(attr, '__dict__'):
attr = attr.__dict__
name = name + '__roles__'
Expand Down
4 changes: 2 additions & 2 deletions src/AccessControl/PermissionMapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def getPermissionMapping(name, obj, st=type('')):
def setPermissionMapping(name, obj, v):
name=getPermissionIdentifier(name)
if v: setattr(obj, name, getPermissionIdentifier(v))
elif obj.__dict__.has_key(name): delattr(obj, name)
elif name in obj.__dict__: delattr(obj, name)

class PM(Base):
_owner=UnownableOwner
Expand Down Expand Up @@ -154,4 +154,4 @@ def __call__(self, *args, **kw):
self=m.__of__(
ImplicitAcquisitionWrapper(
w, parent))
return apply(self, args, kw)
return self(*args, **kw)
1 change: 1 addition & 0 deletions src/AccessControl/ZopeGuards.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from .SecurityInfo import secureModule
from .SimpleObjectPolicies import Containers, ContainerAssertions
from zExceptions import Unauthorized
from functools import reduce

_marker = [] # Create a new marker object.

Expand Down
2 changes: 1 addition & 1 deletion src/AccessControl/ZopeSecurityPolicy.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def getRoles(container, name, value, default):
return default

if type(value) is MethodType:
container = value.im_self
container = value.__self__

cls = getattr(container, '__class__', None)
if cls is None:
Expand Down
4 changes: 2 additions & 2 deletions src/AccessControl/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
def getSecurityInfo(klass):
sec = {}
info = vars(klass)
if info.has_key('__ac_permissions__'):
if '__ac_permissions__' in info:
sec['__ac_permissions__'] = info['__ac_permissions__']
for k, v in info.items():
if k.endswith('__roles__'):
Expand All @@ -53,7 +53,7 @@ def getSecurityInfo(klass):

def clearSecurityInfo(klass):
info = vars(klass)
if info.has_key('__ac_permissions__'):
if '__ac_permissions__' in info:
delattr(klass, '__ac_permissions__')
for k, v in info.items():
if k.endswith('__roles__'):
Expand Down
6 changes: 3 additions & 3 deletions src/AccessControl/tests/actual_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def display(self):
f6()

def f7():
d = apply(dict, [((1, 2), (3, 4))]) # {1: 2, 3: 4}
d = dict(*[((1, 2), (3, 4))]) # {1: 2, 3: 4}
expected = {'k': [1, 3],
'v': [2, 4],
'i': [(1, 2), (3, 4)]}
Expand Down Expand Up @@ -138,7 +138,7 @@ def f8():
# So do something to touch them.
def f9():
d = DateTime()
print d # this one provoked _print_
print(d) # this one provoked _print_

# Funky. This probably isn't an intended use of reorder, but I'm
# not sure why it exists.
Expand All @@ -155,7 +155,7 @@ def f9():
f9()

def f10():
assert iter(enumerate(iter(iter(range(9))))).next() == (0, 0)
assert next(iter(enumerate(iter(iter(range(9)))))) == (0, 0)
f10()

def f11():
Expand Down
10 changes: 5 additions & 5 deletions src/AccessControl/tests/testZopeGuards.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def test_iterkeys_validates(self):
old = self.setSecurityManager(sm)
iterkeys = get_iter({GuardTestCase: 1}, 'iterkeys')
try:
iterkeys().next()
next(iterkeys())
finally:
self.setSecurityManager(old)
self.assert_(sm.calls)
Expand All @@ -227,7 +227,7 @@ def test_itervalues_validates(self):
old = self.setSecurityManager(sm)
itervalues = get_iter({GuardTestCase: 1}, 'itervalues')
try:
itervalues().next()
next(itervalues())
finally:
self.setSecurityManager(old)
self.assert_(sm.calls)
Expand Down Expand Up @@ -404,9 +404,9 @@ def test_enumerate_succeeds(self):
sm = SecurityManager() # accepts
old = self.setSecurityManager(sm)
enum = guarded_enumerate([1,2,3])
self.assertEqual(enum.next(), (0,1))
self.assertEqual(enum.next(), (1,2))
self.assertEqual(enum.next(), (2,3))
self.assertEqual(next(enum), (0,1))
self.assertEqual(next(enum), (1,2))
self.assertEqual(next(enum), (2,3))
self.assertRaises(StopIteration, enum.next)
self.setSecurityManager(old)

Expand Down
2 changes: 1 addition & 1 deletion src/AccessControl/userfolder.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ def _getobcontext(self, v, request):
c = innerparent
elif hasattr(v, 'im_self'):
# this is a method, we need to treat it specially
c = v.im_self
c = v.__self__
c = getattr(v, 'aq_inner', v)
request_container = getattr(request['PARENTS'][-1], '__parent__', [])
# if pub's __parent__ or container is the request container, it
Expand Down
6 changes: 3 additions & 3 deletions src/AccessControl/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def getRolesInContext(self, object):
object = parent
continue
if hasattr(object, 'im_self'):
object=object.im_self
object=object.__self__
object=getattr(object, 'aq_inner', object)
continue
break
Expand Down Expand Up @@ -140,7 +140,7 @@ def _check_context(self, object):
return 1
if hasattr(object, 'im_self'):
# This is a method. Grab its self.
object = object.im_self
object = object.__self__
return aq_inContextOf(object, context, 1)

# This is lame, but required to keep existing behavior.
Expand Down Expand Up @@ -201,7 +201,7 @@ def allowed(self, object, object_roles=None):
inner_obj = parent
continue
if hasattr(inner_obj, 'im_self'):
inner_obj = inner_obj.im_self
inner_obj = inner_obj.__self__
inner_obj = getattr(inner_obj, 'aq_inner', inner_obj)
continue
break
Expand Down

0 comments on commit db92b78

Please sign in to comment.