From ce63dcf59ae07b69e1b9a4e1f6afff20c7166d5b Mon Sep 17 00:00:00 2001 From: Hanno Schlichting Date: Sun, 3 Apr 2016 13:01:04 +0200 Subject: [PATCH] Fix various Python 3 compatibility problems. 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. --- .../PluginIndexes/DateRangeIndex/tests.py | 7 +++--- .../KeywordIndex/KeywordIndex.py | 6 +++++ .../PluginIndexes/PathIndex/PathIndex.py | 2 +- .../PluginIndexes/common/tests/__init__.py | 1 - .../common/tests/test_UnIndex.py | 6 ++++- src/Products/ZCatalog/Catalog.py | 23 +++++++++++++++++-- src/Products/ZCatalog/ZCatalog.py | 18 ++++++++++----- src/Products/ZCatalog/plan.py | 5 +++- src/Products/ZCatalog/tests/queryplan.py | 2 +- src/Products/ZCatalog/tests/test_catalog.py | 4 ++-- src/Products/ZCatalog/tests/test_lazy.py | 8 +++---- 11 files changed, 60 insertions(+), 22 deletions(-) diff --git a/src/Products/PluginIndexes/DateRangeIndex/tests.py b/src/Products/PluginIndexes/DateRangeIndex/tests.py index c0c0c979..7afae6ec 100644 --- a/src/Products/PluginIndexes/DateRangeIndex/tests.py +++ b/src/Products/PluginIndexes/DateRangeIndex/tests.py @@ -11,6 +11,7 @@ # ############################################################################## +import operator import unittest @@ -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) diff --git a/src/Products/PluginIndexes/KeywordIndex/KeywordIndex.py b/src/Products/PluginIndexes/KeywordIndex/KeywordIndex.py index c7b07073..6d249972 100644 --- a/src/Products/PluginIndexes/KeywordIndex/KeywordIndex.py +++ b/src/Products/PluginIndexes/KeywordIndex/KeywordIndex.py @@ -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. diff --git a/src/Products/PluginIndexes/PathIndex/PathIndex.py b/src/Products/PluginIndexes/PathIndex/PathIndex.py index 1300cc3f..666222ee 100644 --- a/src/Products/PluginIndexes/PathIndex/PathIndex.py +++ b/src/Products/PluginIndexes/PathIndex/PathIndex.py @@ -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('/')) diff --git a/src/Products/PluginIndexes/common/tests/__init__.py b/src/Products/PluginIndexes/common/tests/__init__.py index 3dc42bca..e5156dfc 100644 --- a/src/Products/PluginIndexes/common/tests/__init__.py +++ b/src/Products/PluginIndexes/common/tests/__init__.py @@ -10,4 +10,3 @@ # FOR A PARTICULAR PURPOSE # ############################################################################# - diff --git a/src/Products/PluginIndexes/common/tests/test_UnIndex.py b/src/Products/PluginIndexes/common/tests/test_UnIndex.py index 883242c3..9492e56a 100644 --- a/src/Products/PluginIndexes/common/tests/test_UnIndex.py +++ b/src/Products/PluginIndexes/common/tests/test_UnIndex.py @@ -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): diff --git a/src/Products/ZCatalog/Catalog.py b/src/Products/ZCatalog/Catalog.py index 42f9b81c..1acd7f65 100644 --- a/src/Products/ZCatalog/Catalog.py +++ b/src/Products/ZCatalog/Catalog.py @@ -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') @@ -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 diff --git a/src/Products/ZCatalog/ZCatalog.py b/src/Products/ZCatalog/ZCatalog.py index e6792f26..7fea1552 100644 --- a/src/Products/ZCatalog/ZCatalog.py +++ b/src/Products/ZCatalog/ZCatalog.py @@ -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()) @@ -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): @@ -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): @@ -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) diff --git a/src/Products/ZCatalog/plan.py b/src/Products/ZCatalog/plan.py index 6db5db72..9a3cf9bc 100644 --- a/src/Products/ZCatalog/plan.py +++ b/src/Products/ZCatalog/plan.py @@ -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) diff --git a/src/Products/ZCatalog/tests/queryplan.py b/src/Products/ZCatalog/tests/queryplan.py index 9c011bfd..bbf0cdbc 100644 --- a/src/Products/ZCatalog/tests/queryplan.py +++ b/src/Products/ZCatalog/tests/queryplan.py @@ -8,4 +8,4 @@ 'index2': (1.5, 2, False), }, } -} \ No newline at end of file +} diff --git a/src/Products/ZCatalog/tests/test_catalog.py b/src/Products/ZCatalog/tests/test_catalog.py index 3b5403d9..0fd7fb28 100644 --- a/src/Products/ZCatalog/tests/test_catalog.py +++ b/src/Products/ZCatalog/tests/test_catalog.py @@ -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') @@ -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') diff --git a/src/Products/ZCatalog/tests/test_lazy.py b/src/Products/ZCatalog/tests/test_lazy.py index e2f7640f..85c7b650 100644 --- a/src/Products/ZCatalog/tests/test_lazy.py +++ b/src/Products/ZCatalog/tests/test_lazy.py @@ -88,8 +88,8 @@ 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): @@ -97,8 +97,8 @@ def test_slicing(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[5:-5], seq1[5:] + seq2 + seq3[:-5]) def test_length(self):