Commit 30806110 authored by Kazuhiko Shiozaki's avatar Kazuhiko Shiozaki Committed by Jérome Perrin

py2/py3: define ERP5Type.Utils.cmp.

parent a548c8d6
...@@ -29,7 +29,6 @@ ...@@ -29,7 +29,6 @@
# Required modules - some modules are imported later to prevent circular deadlocks # Required modules - some modules are imported later to prevent circular deadlocks
from __future__ import absolute_import from __future__ import absolute_import
from past.builtins import cmp
from six import int2byte as chr from six import int2byte as chr
from six import string_types as basestring from six import string_types as basestring
from six.moves import xrange from six.moves import xrange
...@@ -125,6 +124,22 @@ from Products.ERP5Type.Globals import get_request ...@@ -125,6 +124,22 @@ from Products.ERP5Type.Globals import get_request
from .Accessor.TypeDefinition import type_definition from .Accessor.TypeDefinition import type_definition
from .Accessor.TypeDefinition import list_types from .Accessor.TypeDefinition import list_types
if six.PY3:
def cmp(a, b):
try:
return (a > b) - (a < b)
except TypeError:
if a is None:
return -1
elif b is None:
return 1
type_a = '' if isinstance(a, (int, float)) else type(a).__name__
type_b = '' if isinstance(b, (int, float)) else type(b).__name__
return (type_a > type_b) - (type_a < type_b)
else:
import __builtin__
cmp = __builtin__.cmp
##################################################### #####################################################
# Generic sort method # Generic sort method
##################################################### #####################################################
......
...@@ -38,6 +38,7 @@ from Acquisition import aq_base ...@@ -38,6 +38,7 @@ from Acquisition import aq_base
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from AccessControl.ZopeGuards import guarded_import from AccessControl.ZopeGuards import guarded_import
from Products.ERP5Type.tests.utils import LogInterceptor, createZODBPythonScript from Products.ERP5Type.tests.utils import LogInterceptor, createZODBPythonScript
from Products.ERP5Type.Utils import cmp
class TestERP5Type(ERP5TypeTestCase, LogInterceptor): class TestERP5Type(ERP5TypeTestCase, LogInterceptor):
""" """
...@@ -290,6 +291,11 @@ class TestERP5Type(ERP5TypeTestCase, LogInterceptor): ...@@ -290,6 +291,11 @@ class TestERP5Type(ERP5TypeTestCase, LogInterceptor):
self.assertIn(script, person.objectValues()) self.assertIn(script, person.objectValues())
self.assertNotIn(script, person.objectValues(portal_type='Person')) self.assertNotIn(script, person.objectValues(portal_type='Person'))
def test_cmp(self):
self.assertEqual(cmp(None, 0), -1)
self.assertEqual(cmp(None, ''), -1)
self.assertEqual(cmp(0, ''), -1)
def test_suite(): def test_suite():
suite = unittest.TestSuite() suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestERP5Type)) suite.addTest(unittest.makeSuite(TestERP5Type))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment