Skip to content

Commit 7700c0f

Browse files
committed
Remove cmp() from compatibility
1 parent 32636ab commit 7700c0f

File tree

7 files changed

+28
-36
lines changed

7 files changed

+28
-36
lines changed

sympy/core/basic.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
from sympy.core.cache import cacheit
44
from sympy.core.core import BasicType, C
55
from sympy.core.sympify import _sympify, sympify, SympifyError
6-
from sympy.core.compatibility import (reduce, cmp, iterable,
7-
Iterator, ordered, string_types, with_metaclass)
6+
from sympy.core.compatibility import (reduce, iterable, Iterator, ordered,
7+
string_types, with_metaclass)
88
from sympy.core.decorators import deprecated
99
from sympy.core.singleton import S
1010

@@ -172,16 +172,18 @@ def compare(self, other):
172172
173173
"""
174174
# all redefinitions of __cmp__ method should start with the
175-
# following three lines:
175+
# following lines:
176176
if self is other:
177177
return 0
178-
c = cmp(self.__class__, other.__class__)
178+
n1 = self.__class__
179+
n2 = other.__class__
180+
c = (n1 > n2) - (n1 < n2)
179181
if c:
180182
return c
181183
#
182184
st = self._hashable_content()
183185
ot = other._hashable_content()
184-
c = cmp(len(st), len(ot))
186+
c = (len(st) > len(ot)) - (len(st) < len(ot))
185187
if c:
186188
return c
187189
for l, r in zip(st, ot):
@@ -190,7 +192,7 @@ def compare(self, other):
190192
elif isinstance(l, frozenset):
191193
c = 0
192194
else:
193-
c = cmp(l, r)
195+
c = (l > r) - (l < r)
194196
if c:
195197
return c
196198
return 0
@@ -204,7 +206,9 @@ def _compare_pretty(a, b):
204206
return -1
205207

206208
if a.is_Rational and b.is_Rational:
207-
return cmp(a.p*b.q, b.p*a.q)
209+
l = a.p * b.q
210+
r = b.p * a.q
211+
return (l > r) - (l < r)
208212
else:
209213
from sympy.core.symbol import Wild
210214
p1, p2, p3 = Wild("p1"), Wild("p2"), Wild("p3")
@@ -269,18 +273,13 @@ def compare_pretty(a, b):
269273
except SympifyError:
270274
pass
271275

272-
# both objects are non-SymPy
273-
if (not isinstance(a, Basic)) and (not isinstance(b, Basic)):
274-
return cmp(a, b)
275-
276-
if not isinstance(a, Basic):
277-
return -1 # other < sympy
278-
279276
if not isinstance(b, Basic):
280277
return +1 # sympy > other
281278

282279
# now both objects are from SymPy, so we can proceed to usual comparison
283-
return cmp(a.sort_key(), b.sort_key())
280+
a = a.sort_key()
281+
b = b.sort_key()
282+
return (a > b) - (a < b)
284283

285284
@classmethod
286285
def fromiter(cls, args, **assumptions):

sympy/core/compatibility.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,6 @@ def u_decode(x):
7979
get_function_name = operator.attrgetter("__name__")
8080

8181
import builtins
82-
def cmp(a, b):
83-
return (a > b) - (a < b)
8482
# This is done to make filter importable
8583
from functools import reduce
8684
from io import StringIO
@@ -113,7 +111,6 @@ def next(self):
113111
get_function_name = operator.attrgetter("func_name")
114112

115113
import __builtin__ as builtins
116-
cmp = cmp
117114
reduce = reduce
118115
from StringIO import StringIO
119116
from cStringIO import StringIO as cStringIO

sympy/core/core.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
""" The core's core. """
2-
from sympy.core.compatibility import cmp
3-
42
# used for canonical ordering of symbolic sequences
53
# via __cmp__ method:
64
# FIXME this is *so* irrelevant and outdated!
@@ -108,8 +106,7 @@ def __cmp__(cls, other):
108106
return -1
109107
n1 = cls.__name__
110108
n2 = other.__name__
111-
c = cmp(n1, n2)
112-
if not c:
109+
if n1 == n2:
113110
return 0
114111

115112
UNKNOWN = len(ordering_of_classes) + 1
@@ -122,8 +119,8 @@ def __cmp__(cls, other):
122119
except ValueError:
123120
i2 = UNKNOWN
124121
if i1 == UNKNOWN and i2 == UNKNOWN:
125-
return c
126-
return cmp(i1, i2)
122+
return (n1 > n2) - (n1 < n2)
123+
return (i1 > i2) - (i1 < i2)
127124

128125
def __lt__(cls, other):
129126
if cls.__cmp__(other) == -1:

sympy/core/logic.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
at present this is mainly needed for facts.py , feel free however to improve
77
this stuff for general purpose.
88
"""
9-
from sympy.core.compatibility import iterable, cmp
9+
from sympy.core.compatibility import iterable
1010

1111

1212
def fuzzy_bool(x):
@@ -128,10 +128,12 @@ def __lt__(cls, other):
128128

129129
def __cmp__(a, b):
130130
if type(a) is not type(b):
131-
return cmp( str(type(a)), str(type(b)) )
132-
131+
a = str(type(a))
132+
b = str(type(b))
133133
else:
134-
return cmp(a.args, b.args)
134+
a = a.args
135+
b = b.args
136+
return (a > b) - (a < b)
135137

136138
def __str__(self):
137139
return '%s(%s)' % (self.__class__.__name__, ', '.join(str(a) for a in self.args))

sympy/core/operations.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from sympy.core.sympify import _sympify, sympify
33
from sympy.core.basic import Basic, _aresame
44
from sympy.core.cache import cacheit
5-
from sympy.core.compatibility import cmp, ordered
5+
from sympy.core.compatibility import ordered
66
from sympy.core.logic import fuzzy_and
77

88

@@ -469,4 +469,4 @@ def args(self):
469469

470470
@staticmethod
471471
def _compare_pretty(a, b):
472-
return cmp(str(a), str(b))
472+
return (str(a) > str(b)) - (str(a) < str(b))

sympy/core/tests/test_logic.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
from sympy.core.logic import fuzzy_not, Logic, And, Or, Not, fuzzy_and
22
from sympy.utilities.pytest import raises
33

4-
from sympy.core.compatibility import cmp
5-
64
T = True
75
F = False
86
U = None
@@ -39,7 +37,6 @@ def test_logic_cmp():
3937
assert hash(l1) == hash(l2)
4038
assert (l1 == l2) == T
4139
assert (l1 != l2) == F
42-
assert cmp(l1, l2) == 0
4340

4441
assert And('a', 'b', 'c') == And('b', 'a', 'c')
4542
assert And('a', 'b', 'c') == And('c', 'b', 'a')

sympy/geometry/entity.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
88
"""
99

10-
from sympy.core.compatibility import cmp, is_sequence
10+
from sympy.core.compatibility import is_sequence
1111
from sympy.core.basic import Basic
1212
from sympy.core.sympify import sympify
1313
from sympy.functions import cos, sin
@@ -286,7 +286,7 @@ def __cmp__(self, other):
286286
"""Comparison of two GeometryEntities."""
287287
n1 = self.__class__.__name__
288288
n2 = other.__class__.__name__
289-
c = cmp(n1, n2)
289+
c = (n1 > n2) - (n1 < n2)
290290
if not c:
291291
return 0
292292

@@ -310,7 +310,7 @@ def __cmp__(self, other):
310310
if i2 == -1:
311311
return c
312312

313-
return cmp(i1, i2)
313+
return (i1 > i2) - (i1 < i2)
314314

315315
def __contains__(self, other):
316316
"""Subclasses should implement this method for anything more complex than equality."""

0 commit comments

Comments
 (0)