You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by jd...@apache.org on 2023/09/30 08:33:56 UTC
[qpid-python] 02/05: QPID-8631: add py3compat.py helper; fill in for the removed `__cmp__` in Python 3
This is an automated email from the ASF dual-hosted git repository.
jdanek pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/qpid-python.git
commit 90ea5638bf456d4e71a9296309a8d864c08222c6
Author: Jiri Daněk <jd...@redhat.com>
AuthorDate: Mon Apr 10 13:52:26 2023 +0200
QPID-8631: add py3compat.py helper; fill in for the removed `__cmp__` in Python 3
---
qpid/datatypes.py | 7 ++++++
qpid/disp.py | 2 ++
qpid/messaging/constants.py | 9 ++++++++
qpid/py3compat.py | 52 +++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 70 insertions(+)
diff --git a/qpid/datatypes.py b/qpid/datatypes.py
index ec42eff..69e27f1 100644
--- a/qpid/datatypes.py
+++ b/qpid/datatypes.py
@@ -20,7 +20,12 @@
from __future__ import absolute_import
import threading, struct, datetime, time
from .exceptions import Timeout
+from .py3compat import PY3__cmp__, cmp
+try:
+ long
+except NameError:
+ long = int
class Struct:
def __init__(self, _type, *args, **kwargs):
@@ -124,6 +129,7 @@ def serial(o):
else:
return Serial(o)
+@PY3__cmp__
class Serial:
def __init__(self, value):
@@ -290,6 +296,7 @@ try:
from uuid import uuid4
from uuid import UUID
except ImportError:
+ @PY3__cmp__
class UUID:
def __init__(self, hex=None, bytes=None):
if [hex, bytes].count(None) != 1:
diff --git a/qpid/disp.py b/qpid/disp.py
index 67031c7..e655a83 100644
--- a/qpid/disp.py
+++ b/qpid/disp.py
@@ -22,6 +22,7 @@
from __future__ import absolute_import
from __future__ import print_function
from time import strftime, gmtime
+from qpid.py3compat import PY3__cmp__
class Header:
""" """
@@ -195,6 +196,7 @@ class Display:
result += "%ds" % (sec % 60)
return result
+@PY3__cmp__
class Sortable:
""" """
def __init__(self, row, sortIndex):
diff --git a/qpid/messaging/constants.py b/qpid/messaging/constants.py
index 2c00703..67958cc 100644
--- a/qpid/messaging/constants.py
+++ b/qpid/messaging/constants.py
@@ -17,8 +17,12 @@
# under the License.
#
+from __future__ import absolute_import
+import qpid.py3compat
+
__SELF__ = object()
+@qpid.py3compat.PY3__cmp__
class Constant:
def __init__(self, name, value=__SELF__):
@@ -31,6 +35,11 @@ class Constant:
def __repr__(self):
return self.name
+ # NOTE: this method was not implemented in the Python 2 version,
+ # filling in the default Python 2 __cmp__ to preserve behavior
+ def __cmp__(self, other):
+ return qpid.py3compat.cmp(id(self), id(other))
+
AMQP_PORT = 5672
AMQPS_PORT = 5671
diff --git a/qpid/py3compat.py b/qpid/py3compat.py
new file mode 100644
index 0000000..5e85c2f
--- /dev/null
+++ b/qpid/py3compat.py
@@ -0,0 +1,52 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+"""Python 3 compatibility helpers"""
+
+import sys
+
+PY2 = sys.version_info.major == 2
+PY3 = sys.version_info.major == 3
+
+
+def cmp(a, b):
+ return (a > b) - (a < b)
+
+
+_convert = {
+ '__eq__': lambda self, other: self.__cmp__(other) == 0,
+ '__ne__': lambda self, other: self.__cmp__(other) != 0,
+ '__lt__': lambda self, other: self.__cmp__(other) < 0,
+ '__le__': lambda self, other: self.__cmp__(other) <= 0,
+ '__gt__': lambda self, other: self.__cmp__(other) > 0,
+ '__ge__': lambda self, other: self.__cmp__(other) >= 0,
+}
+
+
+def PY3__cmp__(cls):
+ """Class decorator that fills in missing ordering methods when Python2's __cmp__ is provided."""
+ if not hasattr(cls, '__cmp__'):
+ raise ValueError('must define the __cmp__ Python2 operation')
+ if sys.version_info < (3, 0, 0):
+ return cls
+ for op, opfunc in _convert.items():
+ # Overwrite `raise NotImplemented` comparisons inherited from object
+ if getattr(cls, op, None) is getattr(object, op, None):
+ setattr(cls, op, opfunc)
+ return cls
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org