Skip to content

Commit

Permalink
Fix various Python 3 compatibility problems.
Browse files Browse the repository at this point in the history
These are just simple things caught by flake8. Since a lot of the
dependencies aren't Python 3 compatible yet, real Python 3 compatibility
is a ways off.
  • Loading branch information
hannosch committed Apr 3, 2016
1 parent 2f07c78 commit ce63dcf
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 22 deletions.
7 changes: 4 additions & 3 deletions src/Products/PluginIndexes/DateRangeIndex/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#
##############################################################################

import operator
import unittest


Expand Down Expand Up @@ -107,14 +108,14 @@ def test_retrieval(self):
self.assertEqual(used, ('start', 'stop'))
self.assertEqual(len(matches), len(results))

matches.sort(lambda x, y: cmp(x.name(), y.name()))
matches = sorted(matches, key=operator.methodcaller('name'))

for result, match in map(None, results, matches):
self.assertEqual(index.getEntryForObject(result), match.datum())

def test_longdates(self):
too_large = long(2**31)
too_small = - long(2**31)
too_large = 2 ** 31
too_small = -2 ** 31
index = self._makeOne('work', 'start', 'stop')
bad = Dummy('bad', too_large, too_large)
self.assertRaises(OverflowError, index.index_object, 0, bad)
Expand Down
6 changes: 6 additions & 0 deletions src/Products/PluginIndexes/KeywordIndex/KeywordIndex.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@

LOG = getLogger('Zope.KeywordIndex')

try:
basestring
except NameError:
# Python 3 compatibility
basestring = (bytes, str)


class KeywordIndex(UnIndex):
"""Like an UnIndex only it indexes sequences of items.
Expand Down
2 changes: 1 addition & 1 deletion src/Products/PluginIndexes/PathIndex/PathIndex.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ def _search(self, path, default_level=0):
# Search at every level, return the union of all results
return multiunion(
[self._search(path, level)
for level in xrange(self._depth + 1)])
for level in range(self._depth + 1)])

comps = filter(None, path.split('/'))

Expand Down
1 change: 0 additions & 1 deletion src/Products/PluginIndexes/common/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@
# FOR A PARTICULAR PURPOSE
#
#############################################################################

6 changes: 5 additions & 1 deletion src/Products/PluginIndexes/common/tests/test_UnIndex.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ def _makeOne(self, *args, **kw):

def _makeConflicted(self):
from ZODB.POSException import ConflictError

class Conflicted:

def __str__(self):
return 'Conflicted'
__repr__ = __str__

def __getattr__(self, id, default=object()):
raise ConflictError, 'testing'
raise ConflictError('testing')

return Conflicted()

def test_empty(self):
Expand Down
23 changes: 21 additions & 2 deletions src/Products/ZCatalog/Catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@
from .plan import CatalogPlan
from .ProgressHandler import ZLogHandler

try:
from functools import cmp_to_key
except ImportError:
cmp_to_key = None

try:
xrange
except NameError:
# Python 3 compatibility
xrange = range

LOG = logging.getLogger('Zope.ZCatalog')


Expand Down Expand Up @@ -1176,9 +1187,17 @@ def multisort(items, sort_spec):

def comparer(left, right):
for func, order in comparers:
result = cmp(func(left[0]), func(right[0]))
# emulate cmp even in Python 3
a = func(left[0])
b = func(right[0])
result = ((a > b) - (a < b))
if result:
return order * result
return 0
items.sort(cmp=comparer)

if cmp_to_key is None:
items.sort(cmp=comparer)
else:
items.sort(key=cmp_to_key(comparer))

return items
18 changes: 12 additions & 6 deletions src/Products/ZCatalog/ZCatalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@
from Products.ZCatalog.ZCatalogIndexes import ZCatalogIndexes
from .plan import PriorityMap

try:
xrange
except NameError:
# Python 3 compatibility
xrange = range

LOG = logging.getLogger('Zope.ZCatalog')

manage_addZCatalogForm = DTMLFile('dtml/addZCatalog', globals())
Expand Down Expand Up @@ -243,8 +249,8 @@ def manage_catalogReindex(self, REQUEST, RESPONSE, URL1):
URL1 +
'/manage_catalogAdvanced?manage_tabs_message=' +
urllib.quote('Catalog Updated \n'
'Total time: %s\n'
'Total CPU time: %s' % (`elapse`, `c_elapse`)))
'Total time: %r\n'
'Total CPU time: %r' % (elapse, c_elapse)))

security.declareProtected(manage_zcatalog_entries, 'refreshCatalog')
def refreshCatalog(self, clear=0, pghandler=None):
Expand Down Expand Up @@ -328,9 +334,9 @@ def manage_catalogFoundItems(self, REQUEST, RESPONSE, URL2, URL1,
URL1 +
'/manage_catalogView?manage_tabs_message=' +
urllib.quote('Catalog Updated\n'
'Total time: %s\n'
'Total CPU time: %s'
% (`elapse`, `c_elapse`)))
'Total time: %r\n'
'Total CPU time: %r'
% (elapse, c_elapse)))

security.declareProtected(manage_zcatalog_entries, 'manage_addColumn')
def manage_addColumn(self, name, REQUEST=None, RESPONSE=None, URL1=None):
Expand Down Expand Up @@ -706,7 +712,7 @@ def ZopeFindAndApply(self, obj, obj_ids=None, obj_metatypes=None,
p = id

dflag = 0
if hasattr(ob, '_p_changed') and (ob._p_changed == None):
if hasattr(ob, '_p_changed') and (ob._p_changed is None):
dflag = 1

bs = aq_base(ob)
Expand Down
5 changes: 4 additions & 1 deletion src/Products/ZCatalog/plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ def load_from_path(cls, path):
path = os.path.abspath(path)
_globals = {}
_locals = {}
execfile(path, _globals, _locals)

with open(path, 'rb') as fd:
exec(fd.read(), _globals, _locals)

pmap = _locals['queryplan'].copy()
cls.load_pmap(path, pmap)

Expand Down
2 changes: 1 addition & 1 deletion src/Products/ZCatalog/tests/queryplan.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
'index2': (1.5, 2, False),
},
}
}
}
4 changes: 2 additions & 2 deletions src/Products/ZCatalog/tests/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def test_add_brains(self):
catalog = self._make_one()
catalog.addColumn('col1')
catalog.addColumn('col3')
for i in xrange(3):
for i in range(3):
catalog.catalogObject(dummy(3), repr(i))
self.assertTrue('col2' not in catalog.data.values()[0])
catalog.addColumn('col2', default_value='new')
Expand All @@ -114,7 +114,7 @@ def test_del_brains(self):
catalog.addColumn('col1')
catalog.addColumn('col2')
catalog.addColumn('col3')
for i in xrange(3):
for i in range(3):
catalog.catalogObject(dummy(3), repr(i))
self.assertTrue('col2' in catalog.data.values()[0])
catalog.delColumn('col2')
Expand Down
8 changes: 4 additions & 4 deletions src/Products/ZCatalog/tests/test_lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,17 @@ def test_init_nested(self):
seq1 = range(10)
seq2 = list(hexdigits)
seq3 = list(letters)
lcat = apply(self._createLSeq,
[self._createLSeq(seq) for seq in (seq1, seq2, seq3)])
lcat = self._createLSeq(
*[self._createLSeq(seq) for seq in (seq1, seq2, seq3)])
self._compare(lcat, seq1 + seq2 + seq3)

def test_slicing(self):
from string import hexdigits, letters
seq1 = range(10)
seq2 = list(hexdigits)
seq3 = list(letters)
lcat = apply(self._createLSeq,
[self._createLSeq(seq) for seq in (seq1, seq2, seq3)])
lcat = self._createLSeq(
*[self._createLSeq(seq) for seq in (seq1, seq2, seq3)])
self._compare(lcat[5:-5], seq1[5:] + seq2 + seq3[:-5])

def test_length(self):
Expand Down

0 comments on commit ce63dcf

Please sign in to comment.