Browse Source

Define cmp() for Python 3 (#3517)

* Define cmp() for Python 3

http://python-future.org/compatible_idioms.html?highlight=cmp#cmp

* Define cmp() for Python 3

http://python-future.org/compatible_idioms.html?highlight=cmp#cmp
cclauss 8 years ago
parent
commit
a04eb8c191

+ 5 - 0
python/compatibility_tests/v2.5.0/tests/google/protobuf/internal/message_test.py

@@ -55,6 +55,11 @@ from google.protobuf.internal import api_implementation
 from google.protobuf.internal import test_util
 from google.protobuf.internal import test_util
 from google.protobuf import message
 from google.protobuf import message
 
 
+try:
+  cmp                                    # Python 2
+except NameError:
+  cmp = lambda(x, y): (x > y) - (x < y)  # Python 3
+
 # Python pre-2.6 does not have isinf() or isnan() functions, so we have
 # Python pre-2.6 does not have isinf() or isnan() functions, so we have
 # to provide our own.
 # to provide our own.
 def isnan(val):
 def isnan(val):

+ 5 - 1
python/google/protobuf/internal/message_test.py

@@ -53,9 +53,13 @@ import six
 import sys
 import sys
 
 
 try:
 try:
-  import unittest2 as unittest  #PY26
+  import unittest2 as unittest  # PY26
 except ImportError:
 except ImportError:
   import unittest
   import unittest
+try:
+  cmp                                    # Python 2
+except NameError:
+  cmp = lambda(x, y): (x > y) - (x < y)  # Python 3
 
 
 from google.protobuf import map_unittest_pb2
 from google.protobuf import map_unittest_pb2
 from google.protobuf import unittest_pb2
 from google.protobuf import unittest_pb2