Skip to content

Commit

Permalink
Replace __cmp__ with Python 3 compatible ordering methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
hannosch committed Sep 12, 2016
1 parent ecc1827 commit 3c66988
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 38 deletions.
23 changes: 14 additions & 9 deletions src/App/Extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
Extensions currently include external methods.
"""
from functools import total_ordering
import imp
import os

Expand All @@ -23,20 +24,24 @@
from zExceptions import NotFound


class FuncCode:
@total_ordering
class FuncCode(object):

def __init__(self, f, im=0):
self.co_varnames = f.func_code.co_varnames[im:]
self.co_argcount = f.func_code.co_argcount - im

def __cmp__(self, other):
if other is None:
return 1
try:
return cmp((self.co_argcount, self.co_varnames),
(other.co_argcount, other.co_varnames))
except Exception:
return 1
def __eq__(self, other):
if not isinstance(other, FuncCode):
return False
return ((self.co_argcount, self.co_varnames) ==
(other.co_argcount, other.co_varnames))

def __lt__(self, other):
if not isinstance(other, FuncCode):
return False
return ((self.co_argcount, self.co_varnames) <
(other.co_argcount, other.co_varnames))


def _getPath(home, prefix, name, suffixes):
Expand Down
5 changes: 3 additions & 2 deletions src/Products/Five/browser/tests/test_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ def test_menu():
Sort menu items by title so we get a stable testable result:
>>> menu.sort(lambda x, y: cmp(x['title'], y['title']))
>>> from operator import itemgetter
>>> menu.sort(key=itemgetter('title'))
>>> from pprint import pprint
>>> pprint(menu[0])
{'action': '@@cockatiel_menu_public.html',
Expand Down Expand Up @@ -99,7 +100,7 @@ def test_menu():
>>> len(menu)
7
>>> menu.sort(lambda x, y: cmp(x['title'], y['title']))
>>> menu.sort(key=itemgetter('title'))
>>> pprint(menu[0])
{'action': '@@cockatiel_menu_protected.html',
'description': u'',
Expand Down
11 changes: 6 additions & 5 deletions src/Products/Five/viewlet/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,8 @@ two features are not part of the view logic, they should be treated with
independent components. In this example, we are going to only implement
sorting using a simple utility:

>>> from operator import attrgetter

>>> class ISorter(zope.interface.Interface):
...
... def sort(values):
Expand All @@ -763,18 +765,17 @@ sorting using a simple utility:
... zope.interface.implements(ISorter)
...
... def sort(self, values):
... return sorted(values, lambda x, y: cmp(x.__name__, y.__name__))
... return sorted(values, key=attrgetter('__name__'))

>>> zope.component.provideUtility(SortByName(), name='name')

>>> class SortBySize(object):
... zope.interface.implements(ISorter)
...
... def sort(self, values):
... return sorted(
... values,
... lambda x, y: cmp(size.interfaces.ISized(x).sizeForSorting(),
... size.interfaces.ISized(y).sizeForSorting()))
... def _key(value):
... return size.interfaces.ISized(value).sizeForSorting()
... return sorted(values, key=_key)

>>> zope.component.provideUtility(SortBySize(), name='size')

Expand Down
2 changes: 1 addition & 1 deletion src/Products/PageTemplates/tests/rr.pt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<html>
<body>
<ul tal:define="refs options/refs;
refs python:sorted(refs, lambda x,y: cmp(x.order, y.order))">
refs python:sorted(refs, key=lambda x: x.order)">
<li tal:repeat="ref refs" tal:content="ref" />
</ul>
</body>
Expand Down
25 changes: 15 additions & 10 deletions src/Shared/DC/Scripts/Signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,31 @@
This provides support for simulating function signatures
"""

from functools import total_ordering

class FuncCode:

@total_ordering
class FuncCode(object):

def __init__(self, varnames, argcount):
self.co_varnames = varnames
self.co_argcount = argcount

def __cmp__(self, other):
if other is None:
return 1
try:
return cmp((self.co_argcount, self.co_varnames),
(other.co_argcount, other.co_varnames))
except Exception:
return 1
def __eq__(self, other):
if not isinstance(other, FuncCode):
return False
return ((self.co_argcount, self.co_varnames) ==
(other.co_argcount, other.co_varnames))

# This is meant to be imported directly into a class.
def __lt__(self, other):
if not isinstance(other, FuncCode):
return False
return ((self.co_argcount, self.co_varnames) <
(other.co_argcount, other.co_varnames))


def _setFuncSignature(self, defaults=None, varnames=(), argcount=-1):
# This is meant to be imported directly into a class.
# Simplify calls.
if argcount < 0 and varnames:
argcount = len(varnames)
Expand Down
20 changes: 9 additions & 11 deletions src/ZPublisher/HTTPRequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1743,7 +1743,7 @@ def parse_cookie(text,
return parse_cookie(text[l:], result)


class record:
class record(object):

# Allow access to record methods and values from DTML
__allow_access_to_unprotected_subobjects__ = 1
Expand All @@ -1766,21 +1766,19 @@ def __getitem__(self, key):
return self.__dict__[key]

def __str__(self):
L1 = self.__dict__.items()
L1.sort()
return ", ".join("%s: %s" % item for item in L1)
return ", ".join("%s: %s" % item for item in
sorted(self.__dict__.items()))

def __repr__(self):
# return repr( self.__dict__ )
L1 = self.__dict__.items()
L1.sort()
return '{%s}' % ', '.join(
"'%s': %s" % (item[0], repr(item[1])) for item in L1)
"'%s': %s" % (item[0], repr(item[1])) for item in
sorted(self.__dict__.items()))

def __cmp__(self, other):
return (cmp(type(self), type(other)) or
cmp(self.__class__, other.__class__) or
cmp(self.__dict__.items(), other.__dict__.items()))
def __eq__(self, other):
if not isinstance(other, record):
return False
return self.__dict__.items() == other.__dict__.items()


def _filterPasswordFields(items):
Expand Down
1 change: 1 addition & 0 deletions src/ZTUtils/Tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ def decodeExpansion(s, nth=None, maxsize=8192):
nth_pair = None
if nth is not None:
nth_pair = (None, None)
obid = None
for step in s.split(':'):
if step.startswith('_'):
pop = len(step) - 1
Expand Down

0 comments on commit 3c66988

Please sign in to comment.