mirror of https://github.com/python/cpython
Restructure comparison dramatically. There is no longer a default
*ordering* between objects; there is only a default equality test (defined by an object being equal to itself only). Read the comment in object.c. The current implementation never uses a three-way comparison to compute a rich comparison, but it does use a rich comparison to compute a three-way comparison. I'm not quite done ripping out all the calls to PyObject_Compare/Cmp, or replacing tp_compare implementations with tp_richcompare implementations; but much of that has happened (to make most unit tests pass). The following tests still fail, because I need help deciding or understanding: test_codeop -- depends on comparing code objects test_datetime -- need Tim Peters' opinion test_marshal -- depends on comparing code objects test_mutants -- need help understanding it The problem with test_codeop and test_marshal is this: these tests compare two different code objects and expect them to be equal. Is that still a feature we'd like to support? I've temporarily removed the comparison and hash code from code objects, so they use the default (equality by pointer only) comparison. For the other two tests, run them to see for yourself. (There may be more failing test with "-u all".) A general problem with getting lots of these tests to pass is the reality that for object types that have a natural total ordering, implementing __cmp__ is much more convenient than implementing __eq__, __ne__, __lt__, and so on. Should we go back to allowing __cmp__ to provide a total ordering? Should we provide some other way to implement rich comparison with a single method override? Alex proposed a __key__() method; I've considered a __richcmp__() method. Or perhaps __cmp__() just shouldn't be killed off...
This commit is contained in:
parent
9a6e62b947
commit
47b9ff6ba1
|
@ -379,6 +379,7 @@ PyAPI_FUNC(PyObject *) PyObject_Unicode(PyObject *);
|
||||||
PyAPI_FUNC(int) PyObject_Compare(PyObject *, PyObject *);
|
PyAPI_FUNC(int) PyObject_Compare(PyObject *, PyObject *);
|
||||||
PyAPI_FUNC(PyObject *) PyObject_RichCompare(PyObject *, PyObject *, int);
|
PyAPI_FUNC(PyObject *) PyObject_RichCompare(PyObject *, PyObject *, int);
|
||||||
PyAPI_FUNC(int) PyObject_RichCompareBool(PyObject *, PyObject *, int);
|
PyAPI_FUNC(int) PyObject_RichCompareBool(PyObject *, PyObject *, int);
|
||||||
|
PyAPI_FUNC(PyObject *) Py_CmpToRich(int op, int cmp);
|
||||||
PyAPI_FUNC(PyObject *) PyObject_GetAttrString(PyObject *, const char *);
|
PyAPI_FUNC(PyObject *) PyObject_GetAttrString(PyObject *, const char *);
|
||||||
PyAPI_FUNC(int) PyObject_SetAttrString(PyObject *, const char *, PyObject *);
|
PyAPI_FUNC(int) PyObject_SetAttrString(PyObject *, const char *, PyObject *);
|
||||||
PyAPI_FUNC(int) PyObject_HasAttrString(PyObject *, const char *);
|
PyAPI_FUNC(int) PyObject_HasAttrString(PyObject *, const char *);
|
||||||
|
|
|
@ -116,7 +116,7 @@ class StringIO:
|
||||||
_complain_ifclosed(self.closed)
|
_complain_ifclosed(self.closed)
|
||||||
return self.pos
|
return self.pos
|
||||||
|
|
||||||
def read(self, n = -1):
|
def read(self, n=None):
|
||||||
"""Read at most size bytes from the file
|
"""Read at most size bytes from the file
|
||||||
(less if the read hits EOF before obtaining size bytes).
|
(less if the read hits EOF before obtaining size bytes).
|
||||||
|
|
||||||
|
@ -128,6 +128,8 @@ class StringIO:
|
||||||
if self.buflist:
|
if self.buflist:
|
||||||
self.buf += ''.join(self.buflist)
|
self.buf += ''.join(self.buflist)
|
||||||
self.buflist = []
|
self.buflist = []
|
||||||
|
if n is None:
|
||||||
|
n = -1
|
||||||
if n < 0:
|
if n < 0:
|
||||||
newpos = self.len
|
newpos = self.len
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -8,11 +8,16 @@ class UserDict:
|
||||||
if len(kwargs):
|
if len(kwargs):
|
||||||
self.update(kwargs)
|
self.update(kwargs)
|
||||||
def __repr__(self): return repr(self.data)
|
def __repr__(self): return repr(self.data)
|
||||||
def __cmp__(self, dict):
|
def __eq__(self, dict):
|
||||||
if isinstance(dict, UserDict):
|
if isinstance(dict, UserDict):
|
||||||
return cmp(self.data, dict.data)
|
return self.data == dict.data
|
||||||
else:
|
else:
|
||||||
return cmp(self.data, dict)
|
return self.data == dict
|
||||||
|
def __ne__(self, dict):
|
||||||
|
if isinstance(dict, UserDict):
|
||||||
|
return self.data != dict.data
|
||||||
|
else:
|
||||||
|
return self.data != dict
|
||||||
def __len__(self): return len(self.data)
|
def __len__(self): return len(self.data)
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
if key in self.data:
|
if key in self.data:
|
||||||
|
@ -162,11 +167,13 @@ class DictMixin:
|
||||||
return default
|
return default
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return repr(dict(self.iteritems()))
|
return repr(dict(self.iteritems()))
|
||||||
def __cmp__(self, other):
|
def __eq__(self, other):
|
||||||
if other is None:
|
|
||||||
return 1
|
|
||||||
if isinstance(other, DictMixin):
|
if isinstance(other, DictMixin):
|
||||||
other = dict(other.iteritems())
|
other = dict(other.iteritems())
|
||||||
return cmp(dict(self.iteritems()), other)
|
return dict(self.iteritems()) == other
|
||||||
|
def __ne__(self, other):
|
||||||
|
if isinstance(other, DictMixin):
|
||||||
|
other = dict(other.iteritems())
|
||||||
|
return dict(self.iteritems()) != other
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return len(self.keys())
|
return len(self.keys())
|
||||||
|
|
|
@ -25,11 +25,37 @@ class UserString:
|
||||||
def __complex__(self): return complex(self.data)
|
def __complex__(self): return complex(self.data)
|
||||||
def __hash__(self): return hash(self.data)
|
def __hash__(self): return hash(self.data)
|
||||||
|
|
||||||
def __cmp__(self, string):
|
def __eq__(self, string):
|
||||||
if isinstance(string, UserString):
|
if isinstance(string, UserString):
|
||||||
return cmp(self.data, string.data)
|
return self.data == string.data
|
||||||
else:
|
else:
|
||||||
return cmp(self.data, string)
|
return self.data == string
|
||||||
|
def __ne__(self, string):
|
||||||
|
if isinstance(string, UserString):
|
||||||
|
return self.data != string.data
|
||||||
|
else:
|
||||||
|
return self.data != string
|
||||||
|
def __lt__(self, string):
|
||||||
|
if isinstance(string, UserString):
|
||||||
|
return self.data < string.data
|
||||||
|
else:
|
||||||
|
return self.data < string
|
||||||
|
def __le__(self, string):
|
||||||
|
if isinstance(string, UserString):
|
||||||
|
return self.data <= string.data
|
||||||
|
else:
|
||||||
|
return self.data <= string
|
||||||
|
def __gt__(self, string):
|
||||||
|
if isinstance(string, UserString):
|
||||||
|
return self.data > string.data
|
||||||
|
else:
|
||||||
|
return self.data > string
|
||||||
|
def __ge__(self, string):
|
||||||
|
if isinstance(string, UserString):
|
||||||
|
return self.data >= string.data
|
||||||
|
else:
|
||||||
|
return self.data >= string
|
||||||
|
|
||||||
def __contains__(self, char):
|
def __contains__(self, char):
|
||||||
return char in self.data
|
return char in self.data
|
||||||
|
|
||||||
|
|
|
@ -2,10 +2,10 @@ import unittest
|
||||||
from ctypes import *
|
from ctypes import *
|
||||||
|
|
||||||
class MyInt(c_int):
|
class MyInt(c_int):
|
||||||
def __cmp__(self, other):
|
def __eq__(self, other):
|
||||||
if type(other) != MyInt:
|
if type(other) != MyInt:
|
||||||
return -1
|
return NotImplementedError
|
||||||
return cmp(self.value, other.value)
|
return self.value == other.value
|
||||||
|
|
||||||
class Test(unittest.TestCase):
|
class Test(unittest.TestCase):
|
||||||
|
|
||||||
|
|
|
@ -706,6 +706,26 @@ class Decimal(object):
|
||||||
return NotImplemented
|
return NotImplemented
|
||||||
return self.__cmp__(other) != 0
|
return self.__cmp__(other) != 0
|
||||||
|
|
||||||
|
def __lt__(self, other):
|
||||||
|
if not isinstance(other, (Decimal, int, long)):
|
||||||
|
return NotImplemented
|
||||||
|
return self.__cmp__(other) < 0
|
||||||
|
|
||||||
|
def __le__(self, other):
|
||||||
|
if not isinstance(other, (Decimal, int, long)):
|
||||||
|
return NotImplemented
|
||||||
|
return self.__cmp__(other) <= 0
|
||||||
|
|
||||||
|
def __gt__(self, other):
|
||||||
|
if not isinstance(other, (Decimal, int, long)):
|
||||||
|
return NotImplemented
|
||||||
|
return self.__cmp__(other) > 0
|
||||||
|
|
||||||
|
def __ge__(self, other):
|
||||||
|
if not isinstance(other, (Decimal, int, long)):
|
||||||
|
return NotImplemented
|
||||||
|
return self.__cmp__(other) >= 0
|
||||||
|
|
||||||
def compare(self, other, context=None):
|
def compare(self, other, context=None):
|
||||||
"""Compares one to another.
|
"""Compares one to another.
|
||||||
|
|
||||||
|
@ -1894,6 +1914,7 @@ class Decimal(object):
|
||||||
ans = self._check_nans(context=context)
|
ans = self._check_nans(context=context)
|
||||||
if ans:
|
if ans:
|
||||||
return ans
|
return ans
|
||||||
|
return self
|
||||||
if self._exp >= 0:
|
if self._exp >= 0:
|
||||||
return self
|
return self
|
||||||
if context is None:
|
if context is None:
|
||||||
|
|
|
@ -32,7 +32,8 @@ from types import StringType
|
||||||
class Version:
|
class Version:
|
||||||
"""Abstract base class for version numbering classes. Just provides
|
"""Abstract base class for version numbering classes. Just provides
|
||||||
constructor (__init__) and reproducer (__repr__), because those
|
constructor (__init__) and reproducer (__repr__), because those
|
||||||
seem to be the same for all version numbering classes.
|
seem to be the same for all version numbering classes; and route
|
||||||
|
rich comparisons to __cmp__.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__ (self, vstring=None):
|
def __init__ (self, vstring=None):
|
||||||
|
@ -42,6 +43,42 @@ class Version:
|
||||||
def __repr__ (self):
|
def __repr__ (self):
|
||||||
return "%s ('%s')" % (self.__class__.__name__, str(self))
|
return "%s ('%s')" % (self.__class__.__name__, str(self))
|
||||||
|
|
||||||
|
def __eq__(self, other):
|
||||||
|
c = self.__cmp__(other)
|
||||||
|
if c is NotImplemented:
|
||||||
|
return c
|
||||||
|
return c == 0
|
||||||
|
|
||||||
|
def __ne__(self, other):
|
||||||
|
c = self.__cmp__(other)
|
||||||
|
if c is NotImplemented:
|
||||||
|
return c
|
||||||
|
return c != 0
|
||||||
|
|
||||||
|
def __lt__(self, other):
|
||||||
|
c = self.__cmp__(other)
|
||||||
|
if c is NotImplemented:
|
||||||
|
return c
|
||||||
|
return c < 0
|
||||||
|
|
||||||
|
def __le__(self, other):
|
||||||
|
c = self.__cmp__(other)
|
||||||
|
if c is NotImplemented:
|
||||||
|
return c
|
||||||
|
return c <= 0
|
||||||
|
|
||||||
|
def __gt__(self, other):
|
||||||
|
c = self.__cmp__(other)
|
||||||
|
if c is NotImplemented:
|
||||||
|
return c
|
||||||
|
return c > 0
|
||||||
|
|
||||||
|
def __ge__(self, other):
|
||||||
|
c = self.__cmp__(other)
|
||||||
|
if c is NotImplemented:
|
||||||
|
return c
|
||||||
|
return c >= 0
|
||||||
|
|
||||||
|
|
||||||
# Interface for version-number classes -- must be implemented
|
# Interface for version-number classes -- must be implemented
|
||||||
# by the following classes (the concrete ones -- Version should
|
# by the following classes (the concrete ones -- Version should
|
||||||
|
|
|
@ -469,11 +469,12 @@ class DocTest:
|
||||||
|
|
||||||
|
|
||||||
# This lets us sort tests by name:
|
# This lets us sort tests by name:
|
||||||
def __cmp__(self, other):
|
def __lt__(self, other):
|
||||||
if not isinstance(other, DocTest):
|
if not isinstance(other, DocTest):
|
||||||
return -1
|
return NotImplemented
|
||||||
return cmp((self.name, self.filename, self.lineno, id(self)),
|
return ((self.name, self.filename, self.lineno, id(self))
|
||||||
(other.name, other.filename, other.lineno, id(other)))
|
<
|
||||||
|
(other.name, other.filename, other.lineno, id(other)))
|
||||||
|
|
||||||
######################################################################
|
######################################################################
|
||||||
## 3. DocTestParser
|
## 3. DocTestParser
|
||||||
|
|
|
@ -838,13 +838,13 @@ class Values:
|
||||||
|
|
||||||
__repr__ = _repr
|
__repr__ = _repr
|
||||||
|
|
||||||
def __cmp__(self, other):
|
def __eq__(self, other):
|
||||||
if isinstance(other, Values):
|
if isinstance(other, Values):
|
||||||
return cmp(self.__dict__, other.__dict__)
|
return self.__dict__ == other.__dict__
|
||||||
elif isinstance(other, types.DictType):
|
elif isinstance(other, types.DictType):
|
||||||
return cmp(self.__dict__, other)
|
return self.__dict__ == other
|
||||||
else:
|
else:
|
||||||
return -1
|
return NotImplemented
|
||||||
|
|
||||||
def _update_careful(self, dict):
|
def _update_careful(self, dict):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -374,13 +374,13 @@ class Data:
|
||||||
def asBase64(self, maxlinelength=76):
|
def asBase64(self, maxlinelength=76):
|
||||||
return _encodeBase64(self.data, maxlinelength)
|
return _encodeBase64(self.data, maxlinelength)
|
||||||
|
|
||||||
def __cmp__(self, other):
|
def __eq__(self, other):
|
||||||
if isinstance(other, self.__class__):
|
if isinstance(other, self.__class__):
|
||||||
return cmp(self.data, other.data)
|
return self.data == other.data
|
||||||
elif isinstance(other, str):
|
elif isinstance(other, str):
|
||||||
return cmp(self.data, other)
|
return self.data == other
|
||||||
else:
|
else:
|
||||||
return cmp(id(self), id(other))
|
return id(self) == id(other)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "%s(%s)" % (self.__class__.__name__, repr(self.data))
|
return "%s(%s)" % (self.__class__.__name__, repr(self.data))
|
||||||
|
|
|
@ -246,7 +246,12 @@ def _safe_repr(object, context, maxlevels, level):
|
||||||
append = components.append
|
append = components.append
|
||||||
level += 1
|
level += 1
|
||||||
saferepr = _safe_repr
|
saferepr = _safe_repr
|
||||||
for k, v in sorted(object.items()):
|
items = object.items()
|
||||||
|
try:
|
||||||
|
items = sorted(items)
|
||||||
|
except TypeError:
|
||||||
|
items = sorted(items, key=lambda (k, v): (str(type(k)), k, v))
|
||||||
|
for k, v in items:
|
||||||
krepr, kreadable, krecur = saferepr(k, context, maxlevels, level)
|
krepr, kreadable, krecur = saferepr(k, context, maxlevels, level)
|
||||||
vrepr, vreadable, vrecur = saferepr(v, context, maxlevels, level)
|
vrepr, vreadable, vrecur = saferepr(v, context, maxlevels, level)
|
||||||
append("%s: %s" % (krepr, vrepr))
|
append("%s: %s" % (krepr, vrepr))
|
||||||
|
|
|
@ -86,6 +86,12 @@ class DeclTypesTests(unittest.TestCase):
|
||||||
else:
|
else:
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
def __eq__(self, other):
|
||||||
|
c = self.__cmp__(other)
|
||||||
|
if c is NotImplemented:
|
||||||
|
return c
|
||||||
|
return c == 0
|
||||||
|
|
||||||
def __conform__(self, protocol):
|
def __conform__(self, protocol):
|
||||||
if protocol is sqlite.PrepareProtocol:
|
if protocol is sqlite.PrepareProtocol:
|
||||||
return self.val
|
return self.val
|
||||||
|
|
|
@ -60,10 +60,10 @@ class BasicTestMappingProtocol(unittest.TestCase):
|
||||||
for k in self.other:
|
for k in self.other:
|
||||||
self.failIf(k in d)
|
self.failIf(k in d)
|
||||||
#cmp
|
#cmp
|
||||||
self.assertEqual(cmp(p,p), 0)
|
self.assertEqual(p, p)
|
||||||
self.assertEqual(cmp(d,d), 0)
|
self.assertEqual(d, d)
|
||||||
self.assertEqual(cmp(p,d), -1)
|
self.assertNotEqual(p, d)
|
||||||
self.assertEqual(cmp(d,p), 1)
|
self.assertNotEqual(d, p)
|
||||||
#__non__zero__
|
#__non__zero__
|
||||||
if p: self.fail("Empty mapping must compare to False")
|
if p: self.fail("Empty mapping must compare to False")
|
||||||
if not d: self.fail("Full mapping must compare to True")
|
if not d: self.fail("Full mapping must compare to True")
|
||||||
|
@ -623,9 +623,10 @@ class TestHashMappingProtocol(TestMappingProtocol):
|
||||||
d = self._full_mapping({1: BadRepr()})
|
d = self._full_mapping({1: BadRepr()})
|
||||||
self.assertRaises(Exc, repr, d)
|
self.assertRaises(Exc, repr, d)
|
||||||
|
|
||||||
def test_le(self):
|
def test_eq(self):
|
||||||
self.assert_(not (self._empty_mapping() < self._empty_mapping()))
|
self.assertEqual(self._empty_mapping(), self._empty_mapping())
|
||||||
self.assert_(not (self._full_mapping({1: 2}) < self._full_mapping({1L: 2L})))
|
self.assertEqual(self._full_mapping({1: 2}),
|
||||||
|
self._full_mapping({1L: 2L}))
|
||||||
|
|
||||||
class Exc(Exception): pass
|
class Exc(Exception): pass
|
||||||
|
|
||||||
|
@ -633,16 +634,12 @@ class TestHashMappingProtocol(TestMappingProtocol):
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
raise Exc()
|
raise Exc()
|
||||||
def __hash__(self):
|
def __hash__(self):
|
||||||
return 42
|
return 1
|
||||||
|
|
||||||
d1 = self._full_mapping({BadCmp(): 1})
|
d1 = self._full_mapping({BadCmp(): 1})
|
||||||
d2 = self._full_mapping({1: 1})
|
d2 = self._full_mapping({1: 1})
|
||||||
try:
|
self.assertRaises(Exc, lambda: BadCmp()==1)
|
||||||
d1 < d2
|
self.assertRaises(Exc, lambda: d1==d2)
|
||||||
except Exc:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
self.fail("< didn't raise Exc")
|
|
||||||
|
|
||||||
def test_setdefault(self):
|
def test_setdefault(self):
|
||||||
TestMappingProtocol.test_setdefault(self)
|
TestMappingProtocol.test_setdefault(self)
|
||||||
|
|
|
@ -51,16 +51,16 @@ __hex__: ()
|
||||||
__hash__: ()
|
__hash__: ()
|
||||||
__repr__: ()
|
__repr__: ()
|
||||||
__str__: ()
|
__str__: ()
|
||||||
__cmp__: (1,)
|
__eq__: (1,)
|
||||||
__cmp__: (1,)
|
__lt__: (1,)
|
||||||
__cmp__: (1,)
|
__gt__: (1,)
|
||||||
__cmp__: (1,)
|
__ne__: (1,)
|
||||||
__cmp__: (1,)
|
__ne__: (1,)
|
||||||
__cmp__: (1,)
|
__eq__: (1,)
|
||||||
__cmp__: (1,)
|
__gt__: (1,)
|
||||||
__cmp__: (1,)
|
__lt__: (1,)
|
||||||
__cmp__: (1,)
|
__ne__: (1,)
|
||||||
__cmp__: (1,)
|
__ne__: (1,)
|
||||||
__del__: ()
|
__del__: ()
|
||||||
__getattr__: ('spam',)
|
__getattr__: ('spam',)
|
||||||
__setattr__: ('eggs', 'spam, spam, spam and ham')
|
__setattr__: ('eggs', 'spam, spam, spam and ham')
|
||||||
|
|
|
@ -65,8 +65,8 @@ class ExtensionSaver:
|
||||||
copy_reg.add_extension(pair[0], pair[1], code)
|
copy_reg.add_extension(pair[0], pair[1], code)
|
||||||
|
|
||||||
class C:
|
class C:
|
||||||
def __cmp__(self, other):
|
def __eq__(self, other):
|
||||||
return cmp(self.__dict__, other.__dict__)
|
return self.__dict__ == other.__dict__
|
||||||
|
|
||||||
import __main__
|
import __main__
|
||||||
__main__.C = C
|
__main__.C = C
|
||||||
|
|
|
@ -174,8 +174,13 @@ class GetOnly:
|
||||||
|
|
||||||
class CmpErr:
|
class CmpErr:
|
||||||
"Dummy element that always raises an error during comparison"
|
"Dummy element that always raises an error during comparison"
|
||||||
def __cmp__(self, other):
|
def __lt__(self, other):
|
||||||
raise ZeroDivisionError
|
raise ZeroDivisionError
|
||||||
|
__gt__ = __lt__
|
||||||
|
__le__ = __lt__
|
||||||
|
__ge__ = __lt__
|
||||||
|
__eq__ = __lt__
|
||||||
|
__ne__ = __lt__
|
||||||
|
|
||||||
class TestErrorHandling(unittest.TestCase):
|
class TestErrorHandling(unittest.TestCase):
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,44 @@
|
||||||
|
"""Unit tests for buffer objects.
|
||||||
|
|
||||||
|
For now, we just test (the brand new) rich comparison.
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
from test import test_support
|
||||||
|
|
||||||
|
class BufferTests(unittest.TestCase):
|
||||||
|
|
||||||
|
def test_comparison(self):
|
||||||
|
a = buffer("a.b.c")
|
||||||
|
b = buffer("a.b" + ".c")
|
||||||
|
self.assert_(a == b)
|
||||||
|
self.assert_(a <= b)
|
||||||
|
self.assert_(a >= b)
|
||||||
|
self.assert_(a == "a.b.c")
|
||||||
|
self.assert_(a <= "a.b.c")
|
||||||
|
self.assert_(a >= "a.b.c")
|
||||||
|
b = buffer("a.b.c.d")
|
||||||
|
self.assert_(a != b)
|
||||||
|
self.assert_(a <= b)
|
||||||
|
self.assert_(a < b)
|
||||||
|
self.assert_(a != "a.b.c.d")
|
||||||
|
self.assert_(a < "a.b.c.d")
|
||||||
|
self.assert_(a <= "a.b.c.d")
|
||||||
|
b = buffer("a.b")
|
||||||
|
self.assert_(a != b)
|
||||||
|
self.assert_(a >= b)
|
||||||
|
self.assert_(a > b)
|
||||||
|
self.assert_(a != "a.b")
|
||||||
|
self.assert_(a > "a.b")
|
||||||
|
self.assert_(a >= "a.b")
|
||||||
|
b = object()
|
||||||
|
self.assert_(a != b)
|
||||||
|
self.failIf(a == b)
|
||||||
|
self.assertRaises(TypeError, lambda: a < b)
|
||||||
|
|
||||||
|
def test_main():
|
||||||
|
test_support.run_unittest(BufferTests)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
test_main()
|
|
@ -179,7 +179,8 @@ class BuiltinTest(unittest.TestCase):
|
||||||
self.assertRaises(ValueError, chr, 256)
|
self.assertRaises(ValueError, chr, 256)
|
||||||
self.assertRaises(TypeError, chr)
|
self.assertRaises(TypeError, chr)
|
||||||
|
|
||||||
def test_cmp(self):
|
def XXX_test_cmp(self):
|
||||||
|
# cmp() is no longer supported
|
||||||
self.assertEqual(cmp(-1, 1), -1)
|
self.assertEqual(cmp(-1, 1), -1)
|
||||||
self.assertEqual(cmp(1, -1), 1)
|
self.assertEqual(cmp(1, -1), 1)
|
||||||
self.assertEqual(cmp(1, 1), 0)
|
self.assertEqual(cmp(1, 1), 0)
|
||||||
|
@ -1132,8 +1133,14 @@ class BuiltinTest(unittest.TestCase):
|
||||||
map(None, Squares(3), Squares(2)),
|
map(None, Squares(3), Squares(2)),
|
||||||
[(0,0), (1,1), (4,None)]
|
[(0,0), (1,1), (4,None)]
|
||||||
)
|
)
|
||||||
|
def Max(a, b):
|
||||||
|
if a is None:
|
||||||
|
return b
|
||||||
|
if b is None:
|
||||||
|
return a
|
||||||
|
return max(a, b)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
map(max, Squares(3), Squares(2)),
|
map(Max, Squares(3), Squares(2)),
|
||||||
[0, 1, 4]
|
[0, 1, 4]
|
||||||
)
|
)
|
||||||
self.assertRaises(TypeError, map)
|
self.assertRaises(TypeError, map)
|
||||||
|
@ -1201,7 +1208,7 @@ class BuiltinTest(unittest.TestCase):
|
||||||
class BadNumber:
|
class BadNumber:
|
||||||
def __cmp__(self, other):
|
def __cmp__(self, other):
|
||||||
raise ValueError
|
raise ValueError
|
||||||
self.assertRaises(ValueError, min, (42, BadNumber()))
|
self.assertRaises(TypeError, min, (42, BadNumber()))
|
||||||
|
|
||||||
for stmt in (
|
for stmt in (
|
||||||
"min(key=int)", # no args
|
"min(key=int)", # no args
|
||||||
|
@ -1379,8 +1386,11 @@ class BuiltinTest(unittest.TestCase):
|
||||||
self.assertRaises(ValueError, range, a, a + 1, long(0))
|
self.assertRaises(ValueError, range, a, a + 1, long(0))
|
||||||
|
|
||||||
class badzero(int):
|
class badzero(int):
|
||||||
def __cmp__(self, other):
|
def __eq__(self, other):
|
||||||
raise RuntimeError
|
raise RuntimeError
|
||||||
|
__ne__ = __lt__ = __gt__ = __le__ = __ge__ = __eq__
|
||||||
|
|
||||||
|
# XXX This won't (but should!) raise RuntimeError if a is an int...
|
||||||
self.assertRaises(RuntimeError, range, a, a + 1, badzero(1))
|
self.assertRaises(RuntimeError, range, a, a + 1, badzero(1))
|
||||||
|
|
||||||
# Reject floats when it would require PyLongs to represent.
|
# Reject floats when it would require PyLongs to represent.
|
||||||
|
@ -1594,7 +1604,7 @@ class TestSorted(unittest.TestCase):
|
||||||
|
|
||||||
data.reverse()
|
data.reverse()
|
||||||
random.shuffle(copy)
|
random.shuffle(copy)
|
||||||
self.assertEqual(data, sorted(copy, cmp=lambda x, y: cmp(y,x)))
|
self.assertEqual(data, sorted(copy, cmp=lambda x, y: (x < y) - (x > y)))
|
||||||
self.assertNotEqual(data, copy)
|
self.assertNotEqual(data, copy)
|
||||||
random.shuffle(copy)
|
random.shuffle(copy)
|
||||||
self.assertEqual(data, sorted(copy, key=lambda x: -x))
|
self.assertEqual(data, sorted(copy, key=lambda x: -x))
|
||||||
|
|
|
@ -212,7 +212,7 @@ class CalendarTestCase(unittest.TestCase):
|
||||||
self.assertEqual(calendar.isleap(2003), 0)
|
self.assertEqual(calendar.isleap(2003), 0)
|
||||||
|
|
||||||
def test_setfirstweekday(self):
|
def test_setfirstweekday(self):
|
||||||
self.assertRaises(ValueError, calendar.setfirstweekday, 'flabber')
|
self.assertRaises(TypeError, calendar.setfirstweekday, 'flabber')
|
||||||
self.assertRaises(ValueError, calendar.setfirstweekday, -1)
|
self.assertRaises(ValueError, calendar.setfirstweekday, -1)
|
||||||
self.assertRaises(ValueError, calendar.setfirstweekday, 200)
|
self.assertRaises(ValueError, calendar.setfirstweekday, 200)
|
||||||
orig = calendar.firstweekday()
|
orig = calendar.firstweekday()
|
||||||
|
|
|
@ -25,13 +25,11 @@ class ComparableException:
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return str(self.err)
|
return str(self.err)
|
||||||
|
|
||||||
def __cmp__(self, anExc):
|
def __eq__(self, anExc):
|
||||||
if not isinstance(anExc, Exception):
|
if not isinstance(anExc, Exception):
|
||||||
return -1
|
return NotImplemented
|
||||||
x = cmp(self.err.__class__, anExc.__class__)
|
return (self.err.__class__ == anExc.__class__ and
|
||||||
if x != 0:
|
self.err.args == anExc.args)
|
||||||
return x
|
|
||||||
return cmp(self.err.args, anExc.args)
|
|
||||||
|
|
||||||
def __getattr__(self, attr):
|
def __getattr__(self, attr):
|
||||||
return getattr(self.err, attr)
|
return getattr(self.err, attr)
|
||||||
|
@ -118,10 +116,10 @@ parse_strict_test_cases = [
|
||||||
})
|
})
|
||||||
]
|
]
|
||||||
|
|
||||||
def norm(list):
|
def norm(seq):
|
||||||
if type(list) == type([]):
|
if isinstance(seq, list):
|
||||||
list.sort()
|
seq.sort(key=repr)
|
||||||
return list
|
return seq
|
||||||
|
|
||||||
def first_elts(list):
|
def first_elts(list):
|
||||||
return map(lambda x:x[0], list)
|
return map(lambda x:x[0], list)
|
||||||
|
|
|
@ -100,6 +100,30 @@ class AllTests:
|
||||||
print "__cmp__:", args
|
print "__cmp__:", args
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
def __eq__(self, *args):
|
||||||
|
print "__eq__:", args
|
||||||
|
return True
|
||||||
|
|
||||||
|
def __ne__(self, *args):
|
||||||
|
print "__ne__:", args
|
||||||
|
return False
|
||||||
|
|
||||||
|
def __lt__(self, *args):
|
||||||
|
print "__lt__:", args
|
||||||
|
return False
|
||||||
|
|
||||||
|
def __le__(self, *args):
|
||||||
|
print "__le__:", args
|
||||||
|
return True
|
||||||
|
|
||||||
|
def __gt__(self, *args):
|
||||||
|
print "__gt__:", args
|
||||||
|
return False
|
||||||
|
|
||||||
|
def __ge__(self, *args):
|
||||||
|
print "__ge__:", args
|
||||||
|
return True
|
||||||
|
|
||||||
def __del__(self, *args):
|
def __del__(self, *args):
|
||||||
print "__del__:", args
|
print "__del__:", args
|
||||||
|
|
||||||
|
|
|
@ -13,8 +13,8 @@ class Cmp:
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<Cmp %s>' % self.arg
|
return '<Cmp %s>' % self.arg
|
||||||
|
|
||||||
def __cmp__(self, other):
|
def __eq__(self, other):
|
||||||
return cmp(self.arg, other)
|
return self.arg == other
|
||||||
|
|
||||||
class ComparisonTest(unittest.TestCase):
|
class ComparisonTest(unittest.TestCase):
|
||||||
set1 = [2, 2.0, 2L, 2+0j, Cmp(2.0)]
|
set1 = [2, 2.0, 2L, 2+0j, Cmp(2.0)]
|
||||||
|
@ -36,7 +36,7 @@ class ComparisonTest(unittest.TestCase):
|
||||||
L.insert(len(L)//2, Empty())
|
L.insert(len(L)//2, Empty())
|
||||||
for a in L:
|
for a in L:
|
||||||
for b in L:
|
for b in L:
|
||||||
self.assertEqual(cmp(a, b), cmp(id(a), id(b)),
|
self.assertEqual(a == b, id(a) == id(b),
|
||||||
'a=%r, b=%r' % (a, b))
|
'a=%r, b=%r' % (a, b))
|
||||||
|
|
||||||
def test_main():
|
def test_main():
|
||||||
|
|
|
@ -104,8 +104,8 @@ class TestCopy(unittest.TestCase):
|
||||||
class C:
|
class C:
|
||||||
def __init__(self, foo):
|
def __init__(self, foo):
|
||||||
self.foo = foo
|
self.foo = foo
|
||||||
def __cmp__(self, other):
|
def __eq__(self, other):
|
||||||
return cmp(self.foo, other.foo)
|
return self.foo == other.foo
|
||||||
x = C(42)
|
x = C(42)
|
||||||
self.assertEqual(copy.copy(x), x)
|
self.assertEqual(copy.copy(x), x)
|
||||||
|
|
||||||
|
@ -115,8 +115,8 @@ class TestCopy(unittest.TestCase):
|
||||||
self.foo = foo
|
self.foo = foo
|
||||||
def __copy__(self):
|
def __copy__(self):
|
||||||
return C(self.foo)
|
return C(self.foo)
|
||||||
def __cmp__(self, other):
|
def __eq__(self, other):
|
||||||
return cmp(self.foo, other.foo)
|
return self.foo == other.foo
|
||||||
x = C(42)
|
x = C(42)
|
||||||
self.assertEqual(copy.copy(x), x)
|
self.assertEqual(copy.copy(x), x)
|
||||||
|
|
||||||
|
@ -126,8 +126,8 @@ class TestCopy(unittest.TestCase):
|
||||||
self.foo = foo
|
self.foo = foo
|
||||||
def __getinitargs__(self):
|
def __getinitargs__(self):
|
||||||
return (self.foo,)
|
return (self.foo,)
|
||||||
def __cmp__(self, other):
|
def __eq__(self, other):
|
||||||
return cmp(self.foo, other.foo)
|
return self.foo == other.foo
|
||||||
x = C(42)
|
x = C(42)
|
||||||
self.assertEqual(copy.copy(x), x)
|
self.assertEqual(copy.copy(x), x)
|
||||||
|
|
||||||
|
@ -137,8 +137,8 @@ class TestCopy(unittest.TestCase):
|
||||||
self.foo = foo
|
self.foo = foo
|
||||||
def __getstate__(self):
|
def __getstate__(self):
|
||||||
return {"foo": self.foo}
|
return {"foo": self.foo}
|
||||||
def __cmp__(self, other):
|
def __eq__(self, other):
|
||||||
return cmp(self.foo, other.foo)
|
return self.foo == other.foo
|
||||||
x = C(42)
|
x = C(42)
|
||||||
self.assertEqual(copy.copy(x), x)
|
self.assertEqual(copy.copy(x), x)
|
||||||
|
|
||||||
|
@ -148,8 +148,8 @@ class TestCopy(unittest.TestCase):
|
||||||
self.foo = foo
|
self.foo = foo
|
||||||
def __setstate__(self, state):
|
def __setstate__(self, state):
|
||||||
self.foo = state["foo"]
|
self.foo = state["foo"]
|
||||||
def __cmp__(self, other):
|
def __eq__(self, other):
|
||||||
return cmp(self.foo, other.foo)
|
return self.foo == other.foo
|
||||||
x = C(42)
|
x = C(42)
|
||||||
self.assertEqual(copy.copy(x), x)
|
self.assertEqual(copy.copy(x), x)
|
||||||
|
|
||||||
|
@ -161,8 +161,8 @@ class TestCopy(unittest.TestCase):
|
||||||
return self.foo
|
return self.foo
|
||||||
def __setstate__(self, state):
|
def __setstate__(self, state):
|
||||||
self.foo = state
|
self.foo = state
|
||||||
def __cmp__(self, other):
|
def __eq__(self, other):
|
||||||
return cmp(self.foo, other.foo)
|
return self.foo == other.foo
|
||||||
x = C(42)
|
x = C(42)
|
||||||
self.assertEqual(copy.copy(x), x)
|
self.assertEqual(copy.copy(x), x)
|
||||||
|
|
||||||
|
@ -304,7 +304,7 @@ class TestCopy(unittest.TestCase):
|
||||||
x = {}
|
x = {}
|
||||||
x['foo'] = x
|
x['foo'] = x
|
||||||
y = copy.deepcopy(x)
|
y = copy.deepcopy(x)
|
||||||
self.assertRaises(RuntimeError, cmp, y, x)
|
self.assertRaises(TypeError, cmp, y, x)
|
||||||
self.assert_(y is not x)
|
self.assert_(y is not x)
|
||||||
self.assert_(y['foo'] is y)
|
self.assert_(y['foo'] is y)
|
||||||
self.assertEqual(len(y), 1)
|
self.assertEqual(len(y), 1)
|
||||||
|
@ -319,8 +319,8 @@ class TestCopy(unittest.TestCase):
|
||||||
class C:
|
class C:
|
||||||
def __init__(self, foo):
|
def __init__(self, foo):
|
||||||
self.foo = foo
|
self.foo = foo
|
||||||
def __cmp__(self, other):
|
def __eq__(self, other):
|
||||||
return cmp(self.foo, other.foo)
|
return self.foo == other.foo
|
||||||
x = C([42])
|
x = C([42])
|
||||||
y = copy.deepcopy(x)
|
y = copy.deepcopy(x)
|
||||||
self.assertEqual(y, x)
|
self.assertEqual(y, x)
|
||||||
|
@ -332,8 +332,8 @@ class TestCopy(unittest.TestCase):
|
||||||
self.foo = foo
|
self.foo = foo
|
||||||
def __deepcopy__(self, memo):
|
def __deepcopy__(self, memo):
|
||||||
return C(copy.deepcopy(self.foo, memo))
|
return C(copy.deepcopy(self.foo, memo))
|
||||||
def __cmp__(self, other):
|
def __eq__(self, other):
|
||||||
return cmp(self.foo, other.foo)
|
return self.foo == other.foo
|
||||||
x = C([42])
|
x = C([42])
|
||||||
y = copy.deepcopy(x)
|
y = copy.deepcopy(x)
|
||||||
self.assertEqual(y, x)
|
self.assertEqual(y, x)
|
||||||
|
@ -346,8 +346,8 @@ class TestCopy(unittest.TestCase):
|
||||||
self.foo = foo
|
self.foo = foo
|
||||||
def __getinitargs__(self):
|
def __getinitargs__(self):
|
||||||
return (self.foo,)
|
return (self.foo,)
|
||||||
def __cmp__(self, other):
|
def __eq__(self, other):
|
||||||
return cmp(self.foo, other.foo)
|
return self.foo == other.foo
|
||||||
x = C([42])
|
x = C([42])
|
||||||
y = copy.deepcopy(x)
|
y = copy.deepcopy(x)
|
||||||
self.assertEqual(y, x)
|
self.assertEqual(y, x)
|
||||||
|
@ -360,8 +360,8 @@ class TestCopy(unittest.TestCase):
|
||||||
self.foo = foo
|
self.foo = foo
|
||||||
def __getstate__(self):
|
def __getstate__(self):
|
||||||
return {"foo": self.foo}
|
return {"foo": self.foo}
|
||||||
def __cmp__(self, other):
|
def __eq__(self, other):
|
||||||
return cmp(self.foo, other.foo)
|
return self.foo == other.foo
|
||||||
x = C([42])
|
x = C([42])
|
||||||
y = copy.deepcopy(x)
|
y = copy.deepcopy(x)
|
||||||
self.assertEqual(y, x)
|
self.assertEqual(y, x)
|
||||||
|
@ -374,8 +374,8 @@ class TestCopy(unittest.TestCase):
|
||||||
self.foo = foo
|
self.foo = foo
|
||||||
def __setstate__(self, state):
|
def __setstate__(self, state):
|
||||||
self.foo = state["foo"]
|
self.foo = state["foo"]
|
||||||
def __cmp__(self, other):
|
def __eq__(self, other):
|
||||||
return cmp(self.foo, other.foo)
|
return self.foo == other.foo
|
||||||
x = C([42])
|
x = C([42])
|
||||||
y = copy.deepcopy(x)
|
y = copy.deepcopy(x)
|
||||||
self.assertEqual(y, x)
|
self.assertEqual(y, x)
|
||||||
|
@ -390,8 +390,8 @@ class TestCopy(unittest.TestCase):
|
||||||
return self.foo
|
return self.foo
|
||||||
def __setstate__(self, state):
|
def __setstate__(self, state):
|
||||||
self.foo = state
|
self.foo = state
|
||||||
def __cmp__(self, other):
|
def __eq__(self, other):
|
||||||
return cmp(self.foo, other.foo)
|
return self.foo == other.foo
|
||||||
x = C([42])
|
x = C([42])
|
||||||
y = copy.deepcopy(x)
|
y = copy.deepcopy(x)
|
||||||
self.assertEqual(y, x)
|
self.assertEqual(y, x)
|
||||||
|
@ -434,8 +434,8 @@ class TestCopy(unittest.TestCase):
|
||||||
class C(object):
|
class C(object):
|
||||||
def __reduce__(self):
|
def __reduce__(self):
|
||||||
return (C, (), self.__dict__)
|
return (C, (), self.__dict__)
|
||||||
def __cmp__(self, other):
|
def __eq__(self, other):
|
||||||
return cmp(self.__dict__, other.__dict__)
|
return self.__dict__ == other.__dict__
|
||||||
x = C()
|
x = C()
|
||||||
x.foo = [42]
|
x.foo = [42]
|
||||||
y = copy.copy(x)
|
y = copy.copy(x)
|
||||||
|
@ -450,8 +450,8 @@ class TestCopy(unittest.TestCase):
|
||||||
return (C, (), self.__dict__)
|
return (C, (), self.__dict__)
|
||||||
def __setstate__(self, state):
|
def __setstate__(self, state):
|
||||||
self.__dict__.update(state)
|
self.__dict__.update(state)
|
||||||
def __cmp__(self, other):
|
def __eq__(self, other):
|
||||||
return cmp(self.__dict__, other.__dict__)
|
return self.__dict__ == other.__dict__
|
||||||
x = C()
|
x = C()
|
||||||
x.foo = [42]
|
x.foo = [42]
|
||||||
y = copy.copy(x)
|
y = copy.copy(x)
|
||||||
|
@ -475,9 +475,9 @@ class TestCopy(unittest.TestCase):
|
||||||
class C(list):
|
class C(list):
|
||||||
def __reduce__(self):
|
def __reduce__(self):
|
||||||
return (C, (), self.__dict__, iter(self))
|
return (C, (), self.__dict__, iter(self))
|
||||||
def __cmp__(self, other):
|
def __eq__(self, other):
|
||||||
return (cmp(list(self), list(other)) or
|
return (list(self) == list(other) and
|
||||||
cmp(self.__dict__, other.__dict__))
|
self.__dict__ == other.__dict__)
|
||||||
x = C([[1, 2], 3])
|
x = C([[1, 2], 3])
|
||||||
y = copy.copy(x)
|
y = copy.copy(x)
|
||||||
self.assertEqual(x, y)
|
self.assertEqual(x, y)
|
||||||
|
@ -492,9 +492,9 @@ class TestCopy(unittest.TestCase):
|
||||||
class C(dict):
|
class C(dict):
|
||||||
def __reduce__(self):
|
def __reduce__(self):
|
||||||
return (C, (), self.__dict__, None, self.iteritems())
|
return (C, (), self.__dict__, None, self.iteritems())
|
||||||
def __cmp__(self, other):
|
def __eq__(self, other):
|
||||||
return (cmp(dict(self), list(dict)) or
|
return (dict(self) == dict(other) and
|
||||||
cmp(self.__dict__, other.__dict__))
|
self.__dict__ == other.__dict__)
|
||||||
x = C([("foo", [1, 2]), ("bar", 3)])
|
x = C([("foo", [1, 2]), ("bar", 3)])
|
||||||
y = copy.copy(x)
|
y = copy.copy(x)
|
||||||
self.assertEqual(x, y)
|
self.assertEqual(x, y)
|
||||||
|
|
|
@ -276,8 +276,8 @@ class DecimalTest(unittest.TestCase):
|
||||||
myexceptions = self.getexceptions()
|
myexceptions = self.getexceptions()
|
||||||
self.context.clear_flags()
|
self.context.clear_flags()
|
||||||
|
|
||||||
myexceptions.sort()
|
myexceptions.sort(key=repr)
|
||||||
theirexceptions.sort()
|
theirexceptions.sort(key=repr)
|
||||||
|
|
||||||
self.assertEqual(result, ans,
|
self.assertEqual(result, ans,
|
||||||
'Incorrect answer for ' + s + ' -- got ' + result)
|
'Incorrect answer for ' + s + ' -- got ' + result)
|
||||||
|
|
|
@ -151,7 +151,7 @@ def lists():
|
||||||
|
|
||||||
def dicts():
|
def dicts():
|
||||||
if verbose: print "Testing dict operations..."
|
if verbose: print "Testing dict operations..."
|
||||||
testbinop({1:2}, {2:1}, -1, "cmp(a,b)", "__cmp__")
|
##testbinop({1:2}, {2:1}, -1, "cmp(a,b)", "__cmp__")
|
||||||
testbinop({1:2,3:4}, 1, 1, "b in a", "__contains__")
|
testbinop({1:2,3:4}, 1, 1, "b in a", "__contains__")
|
||||||
testbinop({1:2,3:4}, 2, 0, "b in a", "__contains__")
|
testbinop({1:2,3:4}, 2, 0, "b in a", "__contains__")
|
||||||
testbinop({1:2,3:4}, 1, 2, "a[b]", "__getitem__")
|
testbinop({1:2,3:4}, 1, 2, "a[b]", "__getitem__")
|
||||||
|
@ -523,7 +523,7 @@ def spamdicts():
|
||||||
# This is an ugly hack:
|
# This is an ugly hack:
|
||||||
copy._deepcopy_dispatch[spam.spamdict] = spamdict
|
copy._deepcopy_dispatch[spam.spamdict] = spamdict
|
||||||
|
|
||||||
testbinop(spamdict({1:2}), spamdict({2:1}), -1, "cmp(a,b)", "__cmp__")
|
##testbinop(spamdict({1:2}), spamdict({2:1}), -1, "cmp(a,b)", "__cmp__")
|
||||||
testbinop(spamdict({1:2,3:4}), 1, 1, "b in a", "__contains__")
|
testbinop(spamdict({1:2,3:4}), 1, 1, "b in a", "__contains__")
|
||||||
testbinop(spamdict({1:2,3:4}), 2, 0, "b in a", "__contains__")
|
testbinop(spamdict({1:2,3:4}), 2, 0, "b in a", "__contains__")
|
||||||
testbinop(spamdict({1:2,3:4}), 1, 2, "a[b]", "__getitem__")
|
testbinop(spamdict({1:2,3:4}), 1, 2, "a[b]", "__getitem__")
|
||||||
|
@ -1641,7 +1641,7 @@ def specials():
|
||||||
verify(id(c1) != id(c2))
|
verify(id(c1) != id(c2))
|
||||||
hash(c1)
|
hash(c1)
|
||||||
hash(c2)
|
hash(c2)
|
||||||
vereq(cmp(c1, c2), cmp(id(c1), id(c2)))
|
##vereq(cmp(c1, c2), cmp(id(c1), id(c2)))
|
||||||
vereq(c1, c1)
|
vereq(c1, c1)
|
||||||
verify(c1 != c2)
|
verify(c1 != c2)
|
||||||
verify(not c1 != c1)
|
verify(not c1 != c1)
|
||||||
|
@ -1665,7 +1665,7 @@ def specials():
|
||||||
verify(id(d1) != id(d2))
|
verify(id(d1) != id(d2))
|
||||||
hash(d1)
|
hash(d1)
|
||||||
hash(d2)
|
hash(d2)
|
||||||
vereq(cmp(d1, d2), cmp(id(d1), id(d2)))
|
##vereq(cmp(d1, d2), cmp(id(d1), id(d2)))
|
||||||
vereq(d1, d1)
|
vereq(d1, d1)
|
||||||
verify(d1 != d2)
|
verify(d1 != d2)
|
||||||
verify(not d1 != d1)
|
verify(not d1 != d1)
|
||||||
|
@ -1758,21 +1758,21 @@ def specials():
|
||||||
for i in range(10):
|
for i in range(10):
|
||||||
verify(i in p10)
|
verify(i in p10)
|
||||||
verify(10 not in p10)
|
verify(10 not in p10)
|
||||||
# Safety test for __cmp__
|
## # Safety test for __cmp__
|
||||||
def unsafecmp(a, b):
|
## def unsafecmp(a, b):
|
||||||
try:
|
## try:
|
||||||
a.__class__.__cmp__(a, b)
|
## a.__class__.__cmp__(a, b)
|
||||||
except TypeError:
|
## except TypeError:
|
||||||
pass
|
## pass
|
||||||
else:
|
## else:
|
||||||
raise TestFailed, "shouldn't allow %s.__cmp__(%r, %r)" % (
|
## raise TestFailed, "shouldn't allow %s.__cmp__(%r, %r)" % (
|
||||||
a.__class__, a, b)
|
## a.__class__, a, b)
|
||||||
unsafecmp(u"123", "123")
|
## unsafecmp(u"123", "123")
|
||||||
unsafecmp("123", u"123")
|
## unsafecmp("123", u"123")
|
||||||
unsafecmp(1, 1.0)
|
## unsafecmp(1, 1.0)
|
||||||
unsafecmp(1.0, 1)
|
## unsafecmp(1.0, 1)
|
||||||
unsafecmp(1, 1L)
|
## unsafecmp(1, 1L)
|
||||||
unsafecmp(1L, 1)
|
## unsafecmp(1L, 1)
|
||||||
|
|
||||||
class Letter(str):
|
class Letter(str):
|
||||||
def __new__(cls, letter):
|
def __new__(cls, letter):
|
||||||
|
@ -2469,12 +2469,43 @@ def classic_comparisons():
|
||||||
class C(base):
|
class C(base):
|
||||||
def __init__(self, value):
|
def __init__(self, value):
|
||||||
self.value = int(value)
|
self.value = int(value)
|
||||||
def __cmp__(self, other):
|
def __eq__(self, other):
|
||||||
if isinstance(other, C):
|
if isinstance(other, C):
|
||||||
return cmp(self.value, other.value)
|
return self.value == other.value
|
||||||
if isinstance(other, int) or isinstance(other, long):
|
if isinstance(other, int) or isinstance(other, long):
|
||||||
return cmp(self.value, other)
|
return self.value == other
|
||||||
return NotImplemented
|
return NotImplemented
|
||||||
|
def __ne__(self, other):
|
||||||
|
if isinstance(other, C):
|
||||||
|
return self.value != other.value
|
||||||
|
if isinstance(other, int) or isinstance(other, long):
|
||||||
|
return self.value != other
|
||||||
|
return NotImplemented
|
||||||
|
def __lt__(self, other):
|
||||||
|
if isinstance(other, C):
|
||||||
|
return self.value < other.value
|
||||||
|
if isinstance(other, int) or isinstance(other, long):
|
||||||
|
return self.value < other
|
||||||
|
return NotImplemented
|
||||||
|
def __le__(self, other):
|
||||||
|
if isinstance(other, C):
|
||||||
|
return self.value <= other.value
|
||||||
|
if isinstance(other, int) or isinstance(other, long):
|
||||||
|
return self.value <= other
|
||||||
|
return NotImplemented
|
||||||
|
def __gt__(self, other):
|
||||||
|
if isinstance(other, C):
|
||||||
|
return self.value > other.value
|
||||||
|
if isinstance(other, int) or isinstance(other, long):
|
||||||
|
return self.value > other
|
||||||
|
return NotImplemented
|
||||||
|
def __ge__(self, other):
|
||||||
|
if isinstance(other, C):
|
||||||
|
return self.value >= other.value
|
||||||
|
if isinstance(other, int) or isinstance(other, long):
|
||||||
|
return self.value >= other
|
||||||
|
return NotImplemented
|
||||||
|
|
||||||
c1 = C(1)
|
c1 = C(1)
|
||||||
c2 = C(2)
|
c2 = C(2)
|
||||||
c3 = C(3)
|
c3 = C(3)
|
||||||
|
@ -2482,12 +2513,12 @@ def classic_comparisons():
|
||||||
c = {1: c1, 2: c2, 3: c3}
|
c = {1: c1, 2: c2, 3: c3}
|
||||||
for x in 1, 2, 3:
|
for x in 1, 2, 3:
|
||||||
for y in 1, 2, 3:
|
for y in 1, 2, 3:
|
||||||
verify(cmp(c[x], c[y]) == cmp(x, y), "x=%d, y=%d" % (x, y))
|
##verify(cmp(c[x], c[y]) == cmp(x, y), "x=%d, y=%d" % (x, y))
|
||||||
for op in "<", "<=", "==", "!=", ">", ">=":
|
for op in "<", "<=", "==", "!=", ">", ">=":
|
||||||
verify(eval("c[x] %s c[y]" % op) == eval("x %s y" % op),
|
verify(eval("c[x] %s c[y]" % op) == eval("x %s y" % op),
|
||||||
"x=%d, y=%d" % (x, y))
|
"x=%d, y=%d" % (x, y))
|
||||||
verify(cmp(c[x], y) == cmp(x, y), "x=%d, y=%d" % (x, y))
|
##verify(cmp(c[x], y) == cmp(x, y), "x=%d, y=%d" % (x, y))
|
||||||
verify(cmp(x, c[y]) == cmp(x, y), "x=%d, y=%d" % (x, y))
|
##verify(cmp(x, c[y]) == cmp(x, y), "x=%d, y=%d" % (x, y))
|
||||||
|
|
||||||
def rich_comparisons():
|
def rich_comparisons():
|
||||||
if verbose:
|
if verbose:
|
||||||
|
@ -3875,6 +3906,8 @@ def methodwrapper():
|
||||||
if verbose:
|
if verbose:
|
||||||
print "Testing method-wrapper objects..."
|
print "Testing method-wrapper objects..."
|
||||||
|
|
||||||
|
return # XXX should methods really support __eq__?
|
||||||
|
|
||||||
l = []
|
l = []
|
||||||
vereq(l.__add__, l.__add__)
|
vereq(l.__add__, l.__add__)
|
||||||
vereq(l.__add__, [].__add__)
|
vereq(l.__add__, [].__add__)
|
||||||
|
|
|
@ -65,14 +65,11 @@ We can also use the new type in contexts where classic only allows "real"
|
||||||
dictionaries, such as the locals/globals dictionaries for the exec
|
dictionaries, such as the locals/globals dictionaries for the exec
|
||||||
statement or the built-in function eval():
|
statement or the built-in function eval():
|
||||||
|
|
||||||
>>> def sorted(seq):
|
|
||||||
... seq.sort()
|
|
||||||
... return seq
|
|
||||||
>>> print sorted(a.keys())
|
>>> print sorted(a.keys())
|
||||||
[1, 2]
|
[1, 2]
|
||||||
>>> exec "x = 3; print x" in a
|
>>> exec "x = 3; print x" in a
|
||||||
3
|
3
|
||||||
>>> print sorted(a.keys())
|
>>> print sorted(a.keys(), key=lambda x: (str(type(x)), x))
|
||||||
[1, 2, '__builtins__', 'x']
|
[1, 2, '__builtins__', 'x']
|
||||||
>>> print a['x']
|
>>> print a['x']
|
||||||
3
|
3
|
||||||
|
|
|
@ -5,6 +5,7 @@ import sys, UserDict, cStringIO
|
||||||
|
|
||||||
|
|
||||||
class DictTest(unittest.TestCase):
|
class DictTest(unittest.TestCase):
|
||||||
|
|
||||||
def test_constructor(self):
|
def test_constructor(self):
|
||||||
# calling built-in types without argument must return empty
|
# calling built-in types without argument must return empty
|
||||||
self.assertEqual(dict(), {})
|
self.assertEqual(dict(), {})
|
||||||
|
@ -368,9 +369,9 @@ class DictTest(unittest.TestCase):
|
||||||
d = {1: BadRepr()}
|
d = {1: BadRepr()}
|
||||||
self.assertRaises(Exc, repr, d)
|
self.assertRaises(Exc, repr, d)
|
||||||
|
|
||||||
def test_le(self):
|
def test_eq(self):
|
||||||
self.assert_(not ({} < {}))
|
self.assertEqual({}, {})
|
||||||
self.assert_(not ({1: 2} < {1L: 2L}))
|
self.assertEqual({1: 2}, {1L: 2L})
|
||||||
|
|
||||||
class Exc(Exception): pass
|
class Exc(Exception): pass
|
||||||
|
|
||||||
|
@ -378,12 +379,12 @@ class DictTest(unittest.TestCase):
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
raise Exc()
|
raise Exc()
|
||||||
def __hash__(self):
|
def __hash__(self):
|
||||||
return 42
|
return 1
|
||||||
|
|
||||||
d1 = {BadCmp(): 1}
|
d1 = {BadCmp(): 1}
|
||||||
d2 = {1: 1}
|
d2 = {1: 1}
|
||||||
try:
|
try:
|
||||||
d1 < d2
|
d1 == d2
|
||||||
except Exc:
|
except Exc:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -681,7 +681,7 @@ d[1,] = 2
|
||||||
d[1,2] = 3
|
d[1,2] = 3
|
||||||
d[1,2,3] = 4
|
d[1,2,3] = 4
|
||||||
L = list(d)
|
L = list(d)
|
||||||
L.sort()
|
L.sort(key=lambda x: x if isinstance(x, tuple) else ())
|
||||||
print L
|
print L
|
||||||
|
|
||||||
|
|
||||||
|
@ -741,7 +741,7 @@ print [(i, s) for i in nums for s in [f for f in strs if "n" in f]]
|
||||||
print [(lambda a:[a**i for i in range(a+1)])(j) for j in range(5)]
|
print [(lambda a:[a**i for i in range(a+1)])(j) for j in range(5)]
|
||||||
|
|
||||||
def test_in_func(l):
|
def test_in_func(l):
|
||||||
return [None < x < 3 for x in l if x > 2]
|
return [0 < x < 3 for x in l if x > 2]
|
||||||
|
|
||||||
print test_in_func(nums)
|
print test_in_func(nums)
|
||||||
|
|
||||||
|
|
|
@ -136,6 +136,7 @@ class CmpErr:
|
||||||
"Dummy element that always raises an error during comparison"
|
"Dummy element that always raises an error during comparison"
|
||||||
def __cmp__(self, other):
|
def __cmp__(self, other):
|
||||||
raise ZeroDivisionError
|
raise ZeroDivisionError
|
||||||
|
__eq__ = __ne__ = __lt__ = __le__ = __gt__ = __ge__ = __cmp__
|
||||||
|
|
||||||
def R(seqn):
|
def R(seqn):
|
||||||
'Regular generator'
|
'Regular generator'
|
||||||
|
@ -253,7 +254,7 @@ class TestErrorHandling(unittest.TestCase):
|
||||||
|
|
||||||
def test_iterable_args(self):
|
def test_iterable_args(self):
|
||||||
for f in (nlargest, nsmallest):
|
for f in (nlargest, nsmallest):
|
||||||
for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)):
|
for s in ("123", "", range(1000), (1, 1.2), xrange(2000,2200,5)):
|
||||||
for g in (G, I, Ig, L, R):
|
for g in (G, I, Ig, L, R):
|
||||||
self.assertEqual(f(2, g(s)), f(2,s))
|
self.assertEqual(f(2, g(s)), f(2,s))
|
||||||
self.assertEqual(f(2, S(s)), [])
|
self.assertEqual(f(2, S(s)), [])
|
||||||
|
|
|
@ -132,13 +132,13 @@ class TestBasicOps(unittest.TestCase):
|
||||||
|
|
||||||
# __cmp__ failure
|
# __cmp__ failure
|
||||||
class DummyCmp:
|
class DummyCmp:
|
||||||
def __cmp__(self, dst):
|
def __eq__(self, dst):
|
||||||
raise ExpectedError
|
raise ExpectedError
|
||||||
s = [DummyCmp(), DummyCmp(), None]
|
s = [DummyCmp(), DummyCmp(), None]
|
||||||
|
|
||||||
# __cmp__ failure on outer object
|
# __eq__ failure on outer object
|
||||||
self.assertRaises(ExpectedError, gulp, s, func=id)
|
self.assertRaises(ExpectedError, gulp, s, func=id)
|
||||||
# __cmp__ failure on inner object
|
# __eq__ failure on inner object
|
||||||
self.assertRaises(ExpectedError, gulp, s)
|
self.assertRaises(ExpectedError, gulp, s)
|
||||||
|
|
||||||
# keyfunc failure
|
# keyfunc failure
|
||||||
|
|
|
@ -27,7 +27,7 @@ import os
|
||||||
# ran it. Indeed, at the start, the driver never got beyond 6 iterations
|
# ran it. Indeed, at the start, the driver never got beyond 6 iterations
|
||||||
# before the test died.
|
# before the test died.
|
||||||
|
|
||||||
# The dicts are global to make it easy to mutate tham from within functions.
|
# The dicts are global to make it easy to mutate them from within functions.
|
||||||
dict1 = {}
|
dict1 = {}
|
||||||
dict2 = {}
|
dict2 = {}
|
||||||
|
|
||||||
|
@ -93,9 +93,9 @@ class Horrid:
|
||||||
def __hash__(self):
|
def __hash__(self):
|
||||||
return self.hashcode
|
return self.hashcode
|
||||||
|
|
||||||
def __cmp__(self, other):
|
def __eq__(self, other):
|
||||||
maybe_mutate() # The point of the test.
|
maybe_mutate() # The point of the test.
|
||||||
return cmp(self.i, other.i)
|
return self.i == other.i
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "Horrid(%d)" % self.i
|
return "Horrid(%d)" % self.i
|
||||||
|
@ -132,7 +132,8 @@ def test_one(n):
|
||||||
while dict1 and len(dict1) == len(dict2):
|
while dict1 and len(dict1) == len(dict2):
|
||||||
if verbose:
|
if verbose:
|
||||||
print ".",
|
print ".",
|
||||||
c = cmp(dict1, dict2)
|
c = dict1 == dict2
|
||||||
|
XXX # Can't figure out how to make this work
|
||||||
if verbose:
|
if verbose:
|
||||||
print
|
print
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ class BadDictKey:
|
||||||
def __hash__(self):
|
def __hash__(self):
|
||||||
return hash(self.__class__)
|
return hash(self.__class__)
|
||||||
|
|
||||||
def __cmp__(self, other):
|
def __eq__(self, other):
|
||||||
if isinstance(other, self.__class__):
|
if isinstance(other, self.__class__):
|
||||||
print "raising error"
|
print "raising error"
|
||||||
raise RuntimeError, "gotcha"
|
raise RuntimeError, "gotcha"
|
||||||
|
@ -34,7 +34,7 @@ for stmt in ['d[x2] = 2',
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
print "%s: caught the RuntimeError outside" % (stmt,)
|
print "%s: caught the RuntimeError outside" % (stmt,)
|
||||||
else:
|
else:
|
||||||
print "%s: No exception passed through!" # old CPython behavior
|
print "%s: No exception passed through!" % (stmt,) # old CPython behavior
|
||||||
|
|
||||||
|
|
||||||
# Dict resizing bug, found by Jack Jansen in 2.2 CVS development.
|
# Dict resizing bug, found by Jack Jansen in 2.2 CVS development.
|
||||||
|
|
|
@ -18,7 +18,7 @@ def check_pass_thru():
|
||||||
class BadCmp:
|
class BadCmp:
|
||||||
def __hash__(self):
|
def __hash__(self):
|
||||||
return 1
|
return 1
|
||||||
def __cmp__(self, other):
|
def __eq__(self, other):
|
||||||
raise RuntimeError
|
raise RuntimeError
|
||||||
|
|
||||||
class TestJointOps(unittest.TestCase):
|
class TestJointOps(unittest.TestCase):
|
||||||
|
@ -781,11 +781,8 @@ class TestBinaryOps(unittest.TestCase):
|
||||||
a, b = set('a'), set('b')
|
a, b = set('a'), set('b')
|
||||||
self.assertRaises(TypeError, cmp, a, b)
|
self.assertRaises(TypeError, cmp, a, b)
|
||||||
|
|
||||||
# You can view this as a buglet: cmp(a, a) does not raise TypeError,
|
# In py3k, this works!
|
||||||
# because __eq__ is tried before __cmp__, and a.__eq__(a) returns True,
|
self.assertRaises(TypeError, cmp, a, a)
|
||||||
# which Python thinks is good enough to synthesize a cmp() result
|
|
||||||
# without calling __cmp__.
|
|
||||||
self.assertEqual(cmp(a, a), 0)
|
|
||||||
|
|
||||||
self.assertRaises(TypeError, cmp, a, 12)
|
self.assertRaises(TypeError, cmp, a, 12)
|
||||||
self.assertRaises(TypeError, cmp, "abc", a)
|
self.assertRaises(TypeError, cmp, "abc", a)
|
||||||
|
@ -1201,8 +1198,8 @@ class TestCopying(unittest.TestCase):
|
||||||
|
|
||||||
def test_copy(self):
|
def test_copy(self):
|
||||||
dup = self.set.copy()
|
dup = self.set.copy()
|
||||||
dup_list = list(dup); dup_list.sort()
|
dup_list = sorted(dup, key=repr)
|
||||||
set_list = list(self.set); set_list.sort()
|
set_list = sorted(self.set, key=repr)
|
||||||
self.assertEqual(len(dup_list), len(set_list))
|
self.assertEqual(len(dup_list), len(set_list))
|
||||||
for i in range(len(dup_list)):
|
for i in range(len(dup_list)):
|
||||||
self.failUnless(dup_list[i] is set_list[i])
|
self.failUnless(dup_list[i] is set_list[i])
|
||||||
|
@ -1210,8 +1207,8 @@ class TestCopying(unittest.TestCase):
|
||||||
def test_deep_copy(self):
|
def test_deep_copy(self):
|
||||||
dup = copy.deepcopy(self.set)
|
dup = copy.deepcopy(self.set)
|
||||||
##print type(dup), repr(dup)
|
##print type(dup), repr(dup)
|
||||||
dup_list = list(dup); dup_list.sort()
|
dup_list = sorted(dup, key=repr)
|
||||||
set_list = list(self.set); set_list.sort()
|
set_list = sorted(self.set, key=repr)
|
||||||
self.assertEqual(len(dup_list), len(set_list))
|
self.assertEqual(len(dup_list), len(set_list))
|
||||||
for i in range(len(dup_list)):
|
for i in range(len(dup_list)):
|
||||||
self.assertEqual(dup_list[i], set_list[i])
|
self.assertEqual(dup_list[i], set_list[i])
|
||||||
|
@ -1374,7 +1371,7 @@ class TestVariousIteratorArgs(unittest.TestCase):
|
||||||
for cons in (set, frozenset):
|
for cons in (set, frozenset):
|
||||||
for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)):
|
for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)):
|
||||||
for g in (G, I, Ig, S, L, R):
|
for g in (G, I, Ig, S, L, R):
|
||||||
self.assertEqual(sorted(cons(g(s))), sorted(g(s)))
|
self.assertEqual(sorted(cons(g(s)), key=repr), sorted(g(s), key=repr))
|
||||||
self.assertRaises(TypeError, cons , X(s))
|
self.assertRaises(TypeError, cons , X(s))
|
||||||
self.assertRaises(TypeError, cons , N(s))
|
self.assertRaises(TypeError, cons , N(s))
|
||||||
self.assertRaises(ZeroDivisionError, cons , E(s))
|
self.assertRaises(ZeroDivisionError, cons , E(s))
|
||||||
|
@ -1386,7 +1383,7 @@ class TestVariousIteratorArgs(unittest.TestCase):
|
||||||
for g in (G, I, Ig, L, R):
|
for g in (G, I, Ig, L, R):
|
||||||
expected = meth(data)
|
expected = meth(data)
|
||||||
actual = meth(G(data))
|
actual = meth(G(data))
|
||||||
self.assertEqual(sorted(actual), sorted(expected))
|
self.assertEqual(sorted(actual, key=repr), sorted(expected, key=repr))
|
||||||
self.assertRaises(TypeError, meth, X(s))
|
self.assertRaises(TypeError, meth, X(s))
|
||||||
self.assertRaises(TypeError, meth, N(s))
|
self.assertRaises(TypeError, meth, N(s))
|
||||||
self.assertRaises(ZeroDivisionError, meth, E(s))
|
self.assertRaises(ZeroDivisionError, meth, E(s))
|
||||||
|
@ -1400,7 +1397,7 @@ class TestVariousIteratorArgs(unittest.TestCase):
|
||||||
t = s.copy()
|
t = s.copy()
|
||||||
getattr(s, methname)(list(g(data)))
|
getattr(s, methname)(list(g(data)))
|
||||||
getattr(t, methname)(g(data))
|
getattr(t, methname)(g(data))
|
||||||
self.assertEqual(sorted(s), sorted(t))
|
self.assertEqual(sorted(s, key=repr), sorted(t, key=repr))
|
||||||
|
|
||||||
self.assertRaises(TypeError, getattr(set('january'), methname), X(data))
|
self.assertRaises(TypeError, getattr(set('january'), methname), X(data))
|
||||||
self.assertRaises(TypeError, getattr(set('january'), methname), N(data))
|
self.assertRaises(TypeError, getattr(set('january'), methname), N(data))
|
||||||
|
|
|
@ -234,11 +234,8 @@ class TestBinaryOps(unittest.TestCase):
|
||||||
a, b = Set('a'), Set('b')
|
a, b = Set('a'), Set('b')
|
||||||
self.assertRaises(TypeError, cmp, a, b)
|
self.assertRaises(TypeError, cmp, a, b)
|
||||||
|
|
||||||
# You can view this as a buglet: cmp(a, a) does not raise TypeError,
|
# In py3k, this works!
|
||||||
# because __eq__ is tried before __cmp__, and a.__eq__(a) returns True,
|
self.assertRaises(TypeError, cmp, a, a)
|
||||||
# which Python thinks is good enough to synthesize a cmp() result
|
|
||||||
# without calling __cmp__.
|
|
||||||
self.assertEqual(cmp(a, a), 0)
|
|
||||||
|
|
||||||
self.assertRaises(TypeError, cmp, a, 12)
|
self.assertRaises(TypeError, cmp, a, 12)
|
||||||
self.assertRaises(TypeError, cmp, "abc", a)
|
self.assertRaises(TypeError, cmp, "abc", a)
|
||||||
|
@ -675,8 +672,8 @@ class TestCopying(unittest.TestCase):
|
||||||
|
|
||||||
def test_copy(self):
|
def test_copy(self):
|
||||||
dup = self.set.copy()
|
dup = self.set.copy()
|
||||||
dup_list = list(dup); dup_list.sort()
|
dup_list = sorted(dup, key=repr)
|
||||||
set_list = list(self.set); set_list.sort()
|
set_list = sorted(self.set, key=repr)
|
||||||
self.assertEqual(len(dup_list), len(set_list))
|
self.assertEqual(len(dup_list), len(set_list))
|
||||||
for i in range(len(dup_list)):
|
for i in range(len(dup_list)):
|
||||||
self.failUnless(dup_list[i] is set_list[i])
|
self.failUnless(dup_list[i] is set_list[i])
|
||||||
|
@ -684,8 +681,8 @@ class TestCopying(unittest.TestCase):
|
||||||
def test_deep_copy(self):
|
def test_deep_copy(self):
|
||||||
dup = copy.deepcopy(self.set)
|
dup = copy.deepcopy(self.set)
|
||||||
##print type(dup), repr(dup)
|
##print type(dup), repr(dup)
|
||||||
dup_list = list(dup); dup_list.sort()
|
dup_list = sorted(dup, key=repr)
|
||||||
set_list = list(self.set); set_list.sort()
|
set_list = sorted(self.set, key=repr)
|
||||||
self.assertEqual(len(dup_list), len(set_list))
|
self.assertEqual(len(dup_list), len(set_list))
|
||||||
for i in range(len(dup_list)):
|
for i in range(len(dup_list)):
|
||||||
self.assertEqual(dup_list[i], set_list[i])
|
self.assertEqual(dup_list[i], set_list[i])
|
||||||
|
|
|
@ -35,18 +35,18 @@ class SliceTest(unittest.TestCase):
|
||||||
|
|
||||||
s1 = slice(BadCmp())
|
s1 = slice(BadCmp())
|
||||||
s2 = slice(BadCmp())
|
s2 = slice(BadCmp())
|
||||||
self.assertRaises(Exc, cmp, s1, s2)
|
|
||||||
self.assertEqual(s1, s1)
|
self.assertEqual(s1, s1)
|
||||||
|
self.assertRaises(Exc, lambda: s1 == s2)
|
||||||
|
|
||||||
s1 = slice(1, BadCmp())
|
s1 = slice(1, BadCmp())
|
||||||
s2 = slice(1, BadCmp())
|
s2 = slice(1, BadCmp())
|
||||||
self.assertEqual(s1, s1)
|
self.assertEqual(s1, s1)
|
||||||
self.assertRaises(Exc, cmp, s1, s2)
|
self.assertRaises(Exc, lambda: s1 == s2)
|
||||||
|
|
||||||
s1 = slice(1, 2, BadCmp())
|
s1 = slice(1, 2, BadCmp())
|
||||||
s2 = slice(1, 2, BadCmp())
|
s2 = slice(1, 2, BadCmp())
|
||||||
self.assertEqual(s1, s1)
|
self.assertEqual(s1, s1)
|
||||||
self.assertRaises(Exc, cmp, s1, s2)
|
self.assertRaises(Exc, lambda: s1 == s2)
|
||||||
|
|
||||||
def test_members(self):
|
def test_members(self):
|
||||||
s = slice(1)
|
s = slice(1)
|
||||||
|
|
|
@ -68,8 +68,8 @@ class TestBase(unittest.TestCase):
|
||||||
self.key = key
|
self.key = key
|
||||||
self.index = i
|
self.index = i
|
||||||
|
|
||||||
def __cmp__(self, other):
|
def __lt__(self, other):
|
||||||
return cmp(self.key, other.key)
|
return self.key < other.key
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "Stable(%d, %d)" % (self.key, self.index)
|
return "Stable(%d, %d)" % (self.key, self.index)
|
||||||
|
@ -225,6 +225,8 @@ class TestDecorateSortUndecorate(unittest.TestCase):
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
del data[:]
|
del data[:]
|
||||||
data[:] = range(20)
|
data[:] = range(20)
|
||||||
|
def __lt__(self, other):
|
||||||
|
return id(self) < id(other)
|
||||||
self.assertRaises(ValueError, data.sort, key=SortKiller)
|
self.assertRaises(ValueError, data.sort, key=SortKiller)
|
||||||
|
|
||||||
def test_key_with_mutating_del_and_exception(self):
|
def test_key_with_mutating_del_and_exception(self):
|
||||||
|
|
|
@ -122,8 +122,8 @@ def fcmp(x, y): # fuzzy comparison function
|
||||||
outcome = fcmp(x[i], y[i])
|
outcome = fcmp(x[i], y[i])
|
||||||
if outcome != 0:
|
if outcome != 0:
|
||||||
return outcome
|
return outcome
|
||||||
return cmp(len(x), len(y))
|
return (len(x) > len(y)) - (len(x) < len(y))
|
||||||
return cmp(x, y)
|
return (x > y) - (x < y)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
unicode
|
unicode
|
||||||
|
|
|
@ -233,7 +233,6 @@ print 'Buffers'
|
||||||
try: buffer('asdf', -1)
|
try: buffer('asdf', -1)
|
||||||
except ValueError: pass
|
except ValueError: pass
|
||||||
else: raise TestFailed, "buffer('asdf', -1) should raise ValueError"
|
else: raise TestFailed, "buffer('asdf', -1) should raise ValueError"
|
||||||
cmp(buffer("abc"), buffer("def")) # used to raise a warning: tp_compare didn't return -1, 0, or 1
|
|
||||||
|
|
||||||
try: buffer(None)
|
try: buffer(None)
|
||||||
except TypeError: pass
|
except TypeError: pass
|
||||||
|
|
|
@ -52,7 +52,7 @@ class UserDictTest(mapping_tests.TestHashMappingProtocol):
|
||||||
all = [d0, d1, d2, u, u0, u1, u2, uu, uu0, uu1, uu2]
|
all = [d0, d1, d2, u, u0, u1, u2, uu, uu0, uu1, uu2]
|
||||||
for a in all:
|
for a in all:
|
||||||
for b in all:
|
for b in all:
|
||||||
self.assertEqual(cmp(a, b), cmp(len(a), len(b)))
|
self.assertEqual(a == b, len(a) == len(b))
|
||||||
|
|
||||||
# Test __getitem__
|
# Test __getitem__
|
||||||
self.assertEqual(u2["one"], 1)
|
self.assertEqual(u2["one"], 1)
|
||||||
|
|
|
@ -701,6 +701,12 @@ class Object:
|
||||||
self.arg = arg
|
self.arg = arg
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<Object %r>" % self.arg
|
return "<Object %r>" % self.arg
|
||||||
|
def __lt__(self, other):
|
||||||
|
if isinstance(other, Object):
|
||||||
|
return self.arg < other.arg
|
||||||
|
return NotImplemented
|
||||||
|
def __hash__(self):
|
||||||
|
return hash(self.arg)
|
||||||
|
|
||||||
|
|
||||||
class MappingTestCase(TestBase):
|
class MappingTestCase(TestBase):
|
||||||
|
|
33
Lib/uuid.py
33
Lib/uuid.py
|
@ -177,9 +177,36 @@ class UUID(object):
|
||||||
int |= version << 76L
|
int |= version << 76L
|
||||||
self.__dict__['int'] = int
|
self.__dict__['int'] = int
|
||||||
|
|
||||||
def __cmp__(self, other):
|
def __eq__(self, other):
|
||||||
if isinstance(other, UUID):
|
if isinstance(other, UUID):
|
||||||
return cmp(self.int, other.int)
|
return self.int == other.int
|
||||||
|
return NotImplemented
|
||||||
|
|
||||||
|
def __ne__(self, other):
|
||||||
|
if isinstance(other, UUID):
|
||||||
|
return self.int != other.int
|
||||||
|
return NotImplemented
|
||||||
|
|
||||||
|
# XXX What's the value of being able to sort UUIDs?
|
||||||
|
|
||||||
|
def __lt__(self, other):
|
||||||
|
if isinstance(other, UUID):
|
||||||
|
return self.int < other.int
|
||||||
|
return NotImplemented
|
||||||
|
|
||||||
|
def __gt__(self, other):
|
||||||
|
if isinstance(other, UUID):
|
||||||
|
return self.int > other.int
|
||||||
|
return NotImplemented
|
||||||
|
|
||||||
|
def __le__(self, other):
|
||||||
|
if isinstance(other, UUID):
|
||||||
|
return self.int <= other.int
|
||||||
|
return NotImplemented
|
||||||
|
|
||||||
|
def __ge__(self, other):
|
||||||
|
if isinstance(other, UUID):
|
||||||
|
return self.int >= other.int
|
||||||
return NotImplemented
|
return NotImplemented
|
||||||
|
|
||||||
def __hash__(self):
|
def __hash__(self):
|
||||||
|
@ -488,7 +515,7 @@ def uuid1(node=None, clock_seq=None):
|
||||||
# 0x01b21dd213814000 is the number of 100-ns intervals between the
|
# 0x01b21dd213814000 is the number of 100-ns intervals between the
|
||||||
# UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00.
|
# UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00.
|
||||||
timestamp = int(nanoseconds/100) + 0x01b21dd213814000L
|
timestamp = int(nanoseconds/100) + 0x01b21dd213814000L
|
||||||
if timestamp <= _last_timestamp:
|
if _last_timestamp is not None and timestamp <= _last_timestamp:
|
||||||
timestamp = _last_timestamp + 1
|
timestamp = _last_timestamp + 1
|
||||||
_last_timestamp = timestamp
|
_last_timestamp = timestamp
|
||||||
if clock_seq is None:
|
if clock_seq is None:
|
||||||
|
|
|
@ -282,56 +282,9 @@ class Fault(Error):
|
||||||
# @param value A boolean value. Any true value is interpreted as True,
|
# @param value A boolean value. Any true value is interpreted as True,
|
||||||
# all other values are interpreted as False.
|
# all other values are interpreted as False.
|
||||||
|
|
||||||
if _bool_is_builtin:
|
boolean = Boolean = bool
|
||||||
boolean = Boolean = bool
|
# to avoid breaking code which references xmlrpclib.{True,False}
|
||||||
# to avoid breaking code which references xmlrpclib.{True,False}
|
True, False = True, False
|
||||||
True, False = True, False
|
|
||||||
else:
|
|
||||||
class Boolean:
|
|
||||||
"""Boolean-value wrapper.
|
|
||||||
|
|
||||||
Use True or False to generate a "boolean" XML-RPC value.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, value = 0):
|
|
||||||
self.value = operator.truth(value)
|
|
||||||
|
|
||||||
def encode(self, out):
|
|
||||||
out.write("<value><boolean>%d</boolean></value>\n" % self.value)
|
|
||||||
|
|
||||||
def __cmp__(self, other):
|
|
||||||
if isinstance(other, Boolean):
|
|
||||||
other = other.value
|
|
||||||
return cmp(self.value, other)
|
|
||||||
|
|
||||||
def __repr__(self):
|
|
||||||
if self.value:
|
|
||||||
return "<Boolean True at %x>" % id(self)
|
|
||||||
else:
|
|
||||||
return "<Boolean False at %x>" % id(self)
|
|
||||||
|
|
||||||
def __int__(self):
|
|
||||||
return self.value
|
|
||||||
|
|
||||||
def __nonzero__(self):
|
|
||||||
return self.value
|
|
||||||
|
|
||||||
True, False = Boolean(1), Boolean(0)
|
|
||||||
|
|
||||||
##
|
|
||||||
# Map true or false value to XML-RPC boolean values.
|
|
||||||
#
|
|
||||||
# @def boolean(value)
|
|
||||||
# @param value A boolean value. Any true value is mapped to True,
|
|
||||||
# all other values are mapped to False.
|
|
||||||
# @return xmlrpclib.True or xmlrpclib.False.
|
|
||||||
# @see Boolean
|
|
||||||
# @see True
|
|
||||||
# @see False
|
|
||||||
|
|
||||||
def boolean(value, _truefalse=(False, True)):
|
|
||||||
"""Convert any Python value to XML-RPC 'boolean'."""
|
|
||||||
return _truefalse[operator.truth(value)]
|
|
||||||
|
|
||||||
##
|
##
|
||||||
# Wrapper for XML-RPC DateTime values. This converts a time value to
|
# Wrapper for XML-RPC DateTime values. This converts a time value to
|
||||||
|
@ -371,10 +324,15 @@ class DateTime:
|
||||||
value = time.strftime("%Y%m%dT%H:%M:%S", value)
|
value = time.strftime("%Y%m%dT%H:%M:%S", value)
|
||||||
self.value = value
|
self.value = value
|
||||||
|
|
||||||
def __cmp__(self, other):
|
def __eq__(self, other):
|
||||||
if isinstance(other, DateTime):
|
if isinstance(other, DateTime):
|
||||||
other = other.value
|
other = other.value
|
||||||
return cmp(self.value, other)
|
return self.value == other
|
||||||
|
|
||||||
|
def __ne__(self, other):
|
||||||
|
if isinstance(other, DateTime):
|
||||||
|
other = other.value
|
||||||
|
return self.value != other
|
||||||
|
|
||||||
##
|
##
|
||||||
# Get date/time value.
|
# Get date/time value.
|
||||||
|
@ -432,10 +390,15 @@ class Binary:
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.data or ""
|
return self.data or ""
|
||||||
|
|
||||||
def __cmp__(self, other):
|
def __eq__(self, other):
|
||||||
if isinstance(other, Binary):
|
if isinstance(other, Binary):
|
||||||
other = other.data
|
other = other.data
|
||||||
return cmp(self.data, other)
|
return self.data == other
|
||||||
|
|
||||||
|
def __ne__(self, other):
|
||||||
|
if isinstance(other, Binary):
|
||||||
|
other = other.data
|
||||||
|
return self.data != other
|
||||||
|
|
||||||
def decode(self, data):
|
def decode(self, data):
|
||||||
self.data = base64.decodestring(data)
|
self.data = base64.decodestring(data)
|
||||||
|
|
|
@ -696,6 +696,8 @@ initthread(void)
|
||||||
/* Initialize types: */
|
/* Initialize types: */
|
||||||
if (PyType_Ready(&localtype) < 0)
|
if (PyType_Ready(&localtype) < 0)
|
||||||
return;
|
return;
|
||||||
|
if (PyType_Ready(&Locktype) < 0)
|
||||||
|
return;
|
||||||
|
|
||||||
/* Create the module and add the functions */
|
/* Create the module and add the functions */
|
||||||
m = Py_InitModule3("thread", thread_methods, thread_doc);
|
m = Py_InitModule3("thread", thread_methods, thread_doc);
|
||||||
|
|
|
@ -252,23 +252,65 @@ buffer_dealloc(PyBufferObject *self)
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
buffer_compare(PyBufferObject *self, PyBufferObject *other)
|
get_bufx(PyObject *obj, void **ptr, Py_ssize_t *size)
|
||||||
|
{
|
||||||
|
PyBufferProcs *bp;
|
||||||
|
|
||||||
|
if (PyBuffer_Check(obj)) {
|
||||||
|
if (!get_buf((PyBufferObject *)obj, ptr, size, ANY_BUFFER)) {
|
||||||
|
PyErr_Clear();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
bp = obj->ob_type->tp_as_buffer;
|
||||||
|
if (bp == NULL ||
|
||||||
|
bp->bf_getreadbuffer == NULL ||
|
||||||
|
bp->bf_getsegcount == NULL)
|
||||||
|
return 0;
|
||||||
|
if ((*bp->bf_getsegcount)(obj, NULL) != 1)
|
||||||
|
return 0;
|
||||||
|
*size = (*bp->bf_getreadbuffer)(obj, 0, ptr);
|
||||||
|
if (*size < 0) {
|
||||||
|
PyErr_Clear();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
buffer_richcompare(PyObject *self, PyObject *other, int op)
|
||||||
{
|
{
|
||||||
void *p1, *p2;
|
void *p1, *p2;
|
||||||
Py_ssize_t len_self, len_other, min_len;
|
Py_ssize_t len1, len2, min_len;
|
||||||
int cmp;
|
int cmp, ok;
|
||||||
|
|
||||||
if (!get_buf(self, &p1, &len_self, ANY_BUFFER))
|
ok = 1;
|
||||||
return -1;
|
if (!get_bufx(self, &p1, &len1))
|
||||||
if (!get_buf(other, &p2, &len_other, ANY_BUFFER))
|
ok = 0;
|
||||||
return -1;
|
if (!get_bufx(other, &p2, &len2))
|
||||||
min_len = (len_self < len_other) ? len_self : len_other;
|
ok = 0;
|
||||||
if (min_len > 0) {
|
if (!ok) {
|
||||||
cmp = memcmp(p1, p2, min_len);
|
/* If we can't get the buffers,
|
||||||
if (cmp != 0)
|
== and != are still defined
|
||||||
return cmp < 0 ? -1 : 1;
|
(and the objects are unequal) */
|
||||||
|
PyObject *result;
|
||||||
|
if (op == Py_EQ)
|
||||||
|
result = Py_False;
|
||||||
|
else if (op == Py_NE)
|
||||||
|
result = Py_True;
|
||||||
|
else
|
||||||
|
result = Py_NotImplemented;
|
||||||
|
Py_INCREF(result);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
return (len_self < len_other) ? -1 : (len_self > len_other) ? 1 : 0;
|
min_len = (len1 < len2) ? len1 : len2;
|
||||||
|
cmp = memcmp(p1, p2, min_len);
|
||||||
|
if (cmp == 0)
|
||||||
|
cmp = (len1 < len2) ? -1 :
|
||||||
|
(len1 > len2) ? 1 : 0;
|
||||||
|
return Py_CmpToRich(op, cmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
|
@ -667,7 +709,7 @@ PyTypeObject PyBuffer_Type = {
|
||||||
0, /* tp_print */
|
0, /* tp_print */
|
||||||
0, /* tp_getattr */
|
0, /* tp_getattr */
|
||||||
0, /* tp_setattr */
|
0, /* tp_setattr */
|
||||||
(cmpfunc)buffer_compare, /* tp_compare */
|
0, /* tp_compare */
|
||||||
(reprfunc)buffer_repr, /* tp_repr */
|
(reprfunc)buffer_repr, /* tp_repr */
|
||||||
0, /* tp_as_number */
|
0, /* tp_as_number */
|
||||||
&buffer_as_sequence, /* tp_as_sequence */
|
&buffer_as_sequence, /* tp_as_sequence */
|
||||||
|
@ -682,7 +724,7 @@ PyTypeObject PyBuffer_Type = {
|
||||||
buffer_doc, /* tp_doc */
|
buffer_doc, /* tp_doc */
|
||||||
0, /* tp_traverse */
|
0, /* tp_traverse */
|
||||||
0, /* tp_clear */
|
0, /* tp_clear */
|
||||||
0, /* tp_richcompare */
|
buffer_richcompare, /* tp_richcompare */
|
||||||
0, /* tp_weaklistoffset */
|
0, /* tp_weaklistoffset */
|
||||||
0, /* tp_iter */
|
0, /* tp_iter */
|
||||||
0, /* tp_iternext */
|
0, /* tp_iternext */
|
||||||
|
|
|
@ -49,18 +49,6 @@ cell_dealloc(PyCellObject *op)
|
||||||
PyObject_GC_Del(op);
|
PyObject_GC_Del(op);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
|
||||||
cell_compare(PyCellObject *a, PyCellObject *b)
|
|
||||||
{
|
|
||||||
if (a->ob_ref == NULL) {
|
|
||||||
if (b->ob_ref == NULL)
|
|
||||||
return 0;
|
|
||||||
return -1;
|
|
||||||
} else if (b->ob_ref == NULL)
|
|
||||||
return 1;
|
|
||||||
return PyObject_Compare(a->ob_ref, b->ob_ref);
|
|
||||||
}
|
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
cell_repr(PyCellObject *op)
|
cell_repr(PyCellObject *op)
|
||||||
{
|
{
|
||||||
|
@ -108,7 +96,7 @@ PyTypeObject PyCell_Type = {
|
||||||
0, /* tp_print */
|
0, /* tp_print */
|
||||||
0, /* tp_getattr */
|
0, /* tp_getattr */
|
||||||
0, /* tp_setattr */
|
0, /* tp_setattr */
|
||||||
(cmpfunc)cell_compare, /* tp_compare */
|
0, /* tp_compare */
|
||||||
(reprfunc)cell_repr, /* tp_repr */
|
(reprfunc)cell_repr, /* tp_repr */
|
||||||
0, /* tp_as_number */
|
0, /* tp_as_number */
|
||||||
0, /* tp_as_sequence */
|
0, /* tp_as_sequence */
|
||||||
|
|
|
@ -80,7 +80,7 @@ PyMethod_New(PyObject *func, PyObject *self, PyObject *klass)
|
||||||
|
|
||||||
#define OFF(x) offsetof(PyMethodObject, x)
|
#define OFF(x) offsetof(PyMethodObject, x)
|
||||||
|
|
||||||
static PyMemberDef instancemethod_memberlist[] = {
|
static PyMemberDef method_memberlist[] = {
|
||||||
{"im_class", T_OBJECT, OFF(im_class), READONLY|RESTRICTED,
|
{"im_class", T_OBJECT, OFF(im_class), READONLY|RESTRICTED,
|
||||||
"the class associated with a method"},
|
"the class associated with a method"},
|
||||||
{"im_func", T_OBJECT, OFF(im_func), READONLY|RESTRICTED,
|
{"im_func", T_OBJECT, OFF(im_func), READONLY|RESTRICTED,
|
||||||
|
@ -96,7 +96,7 @@ static PyMemberDef instancemethod_memberlist[] = {
|
||||||
should only be used for the class, not for instances */
|
should only be used for the class, not for instances */
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
instancemethod_get_doc(PyMethodObject *im, void *context)
|
method_get_doc(PyMethodObject *im, void *context)
|
||||||
{
|
{
|
||||||
static PyObject *docstr;
|
static PyObject *docstr;
|
||||||
if (docstr == NULL) {
|
if (docstr == NULL) {
|
||||||
|
@ -107,13 +107,13 @@ instancemethod_get_doc(PyMethodObject *im, void *context)
|
||||||
return PyObject_GetAttr(im->im_func, docstr);
|
return PyObject_GetAttr(im->im_func, docstr);
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyGetSetDef instancemethod_getset[] = {
|
static PyGetSetDef method_getset[] = {
|
||||||
{"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
|
{"__doc__", (getter)method_get_doc, NULL, NULL},
|
||||||
{0}
|
{0}
|
||||||
};
|
};
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
instancemethod_getattro(PyObject *obj, PyObject *name)
|
method_getattro(PyObject *obj, PyObject *name)
|
||||||
{
|
{
|
||||||
PyMethodObject *im = (PyMethodObject *)obj;
|
PyMethodObject *im = (PyMethodObject *)obj;
|
||||||
PyTypeObject *tp = obj->ob_type;
|
PyTypeObject *tp = obj->ob_type;
|
||||||
|
@ -140,19 +140,19 @@ instancemethod_getattro(PyObject *obj, PyObject *name)
|
||||||
return PyObject_GetAttr(im->im_func, name);
|
return PyObject_GetAttr(im->im_func, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
PyDoc_STRVAR(instancemethod_doc,
|
PyDoc_STRVAR(method_doc,
|
||||||
"instancemethod(function, instance, class)\n\
|
"method(function, instance, class)\n\
|
||||||
\n\
|
\n\
|
||||||
Create an instance method object.");
|
Create an instance method object.");
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
|
method_new(PyTypeObject* type, PyObject* args, PyObject *kw)
|
||||||
{
|
{
|
||||||
PyObject *func;
|
PyObject *func;
|
||||||
PyObject *self;
|
PyObject *self;
|
||||||
PyObject *classObj = NULL;
|
PyObject *classObj = NULL;
|
||||||
|
|
||||||
if (!PyArg_UnpackTuple(args, "instancemethod", 2, 3,
|
if (!PyArg_UnpackTuple(args, "method", 2, 3,
|
||||||
&func, &self, &classObj))
|
&func, &self, &classObj))
|
||||||
return NULL;
|
return NULL;
|
||||||
if (!PyCallable_Check(func)) {
|
if (!PyCallable_Check(func)) {
|
||||||
|
@ -172,7 +172,7 @@ instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
instancemethod_dealloc(register PyMethodObject *im)
|
method_dealloc(register PyMethodObject *im)
|
||||||
{
|
{
|
||||||
_PyObject_GC_UNTRACK(im);
|
_PyObject_GC_UNTRACK(im);
|
||||||
if (im->im_weakreflist != NULL)
|
if (im->im_weakreflist != NULL)
|
||||||
|
@ -184,24 +184,42 @@ instancemethod_dealloc(register PyMethodObject *im)
|
||||||
free_list = im;
|
free_list = im;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static PyObject *
|
||||||
instancemethod_compare(PyMethodObject *a, PyMethodObject *b)
|
method_richcompare(PyObject *self, PyObject *other, int op)
|
||||||
{
|
{
|
||||||
int cmp;
|
PyMethodObject *a, *b;
|
||||||
cmp = PyObject_Compare(a->im_func, b->im_func);
|
PyObject *res;
|
||||||
if (cmp)
|
int eq;
|
||||||
return cmp;
|
|
||||||
|
|
||||||
if (a->im_self == b->im_self)
|
if ((op != Py_EQ && op != Py_NE) ||
|
||||||
return 0;
|
!PyMethod_Check(self) ||
|
||||||
if (a->im_self == NULL || b->im_self == NULL)
|
!PyMethod_Check(other))
|
||||||
return (a->im_self < b->im_self) ? -1 : 1;
|
{
|
||||||
|
Py_INCREF(Py_NotImplemented);
|
||||||
|
return Py_NotImplemented;
|
||||||
|
}
|
||||||
|
a = (PyMethodObject *)self;
|
||||||
|
b = (PyMethodObject *)other;
|
||||||
|
eq = PyObject_RichCompareBool(a->im_func, b->im_func, Py_EQ);
|
||||||
|
if (eq == 1) {
|
||||||
|
if (a->im_self == NULL || b->im_self == NULL)
|
||||||
|
eq = a->im_self == b->im_self;
|
||||||
|
else
|
||||||
|
eq = PyObject_RichCompareBool(a->im_self, b->im_self,
|
||||||
|
Py_EQ);
|
||||||
|
}
|
||||||
|
if (eq < 0)
|
||||||
|
return NULL;
|
||||||
|
if (op == Py_EQ)
|
||||||
|
res = eq ? Py_True : Py_False;
|
||||||
else
|
else
|
||||||
return PyObject_Compare(a->im_self, b->im_self);
|
res = eq ? Py_False : Py_True;
|
||||||
|
Py_INCREF(res);
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
instancemethod_repr(PyMethodObject *a)
|
method_repr(PyMethodObject *a)
|
||||||
{
|
{
|
||||||
PyObject *self = a->im_self;
|
PyObject *self = a->im_self;
|
||||||
PyObject *func = a->im_func;
|
PyObject *func = a->im_func;
|
||||||
|
@ -261,7 +279,7 @@ instancemethod_repr(PyMethodObject *a)
|
||||||
}
|
}
|
||||||
|
|
||||||
static long
|
static long
|
||||||
instancemethod_hash(PyMethodObject *a)
|
method_hash(PyMethodObject *a)
|
||||||
{
|
{
|
||||||
long x, y;
|
long x, y;
|
||||||
if (a->im_self == NULL)
|
if (a->im_self == NULL)
|
||||||
|
@ -280,7 +298,7 @@ instancemethod_hash(PyMethodObject *a)
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
instancemethod_traverse(PyMethodObject *im, visitproc visit, void *arg)
|
method_traverse(PyMethodObject *im, visitproc visit, void *arg)
|
||||||
{
|
{
|
||||||
Py_VISIT(im->im_func);
|
Py_VISIT(im->im_func);
|
||||||
Py_VISIT(im->im_self);
|
Py_VISIT(im->im_self);
|
||||||
|
@ -333,7 +351,7 @@ getinstclassname(PyObject *inst, char *buf, int bufsize)
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
instancemethod_call(PyObject *func, PyObject *arg, PyObject *kw)
|
method_call(PyObject *func, PyObject *arg, PyObject *kw)
|
||||||
{
|
{
|
||||||
PyObject *self = PyMethod_GET_SELF(func);
|
PyObject *self = PyMethod_GET_SELF(func);
|
||||||
PyObject *klass = PyMethod_GET_CLASS(func);
|
PyObject *klass = PyMethod_GET_CLASS(func);
|
||||||
|
@ -392,7 +410,7 @@ instancemethod_call(PyObject *func, PyObject *arg, PyObject *kw)
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
instancemethod_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
|
method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
|
||||||
{
|
{
|
||||||
/* Don't rebind an already bound method, or an unbound method
|
/* Don't rebind an already bound method, or an unbound method
|
||||||
of a class that's not a base class of cls. */
|
of a class that's not a base class of cls. */
|
||||||
|
@ -420,43 +438,43 @@ instancemethod_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
|
||||||
PyTypeObject PyMethod_Type = {
|
PyTypeObject PyMethod_Type = {
|
||||||
PyObject_HEAD_INIT(&PyType_Type)
|
PyObject_HEAD_INIT(&PyType_Type)
|
||||||
0,
|
0,
|
||||||
"instancemethod",
|
"method",
|
||||||
sizeof(PyMethodObject),
|
sizeof(PyMethodObject),
|
||||||
0,
|
0,
|
||||||
(destructor)instancemethod_dealloc, /* tp_dealloc */
|
(destructor)method_dealloc, /* tp_dealloc */
|
||||||
0, /* tp_print */
|
0, /* tp_print */
|
||||||
0, /* tp_getattr */
|
0, /* tp_getattr */
|
||||||
0, /* tp_setattr */
|
0, /* tp_setattr */
|
||||||
(cmpfunc)instancemethod_compare, /* tp_compare */
|
0, /* tp_compare */
|
||||||
(reprfunc)instancemethod_repr, /* tp_repr */
|
(reprfunc)method_repr, /* tp_repr */
|
||||||
0, /* tp_as_number */
|
0, /* tp_as_number */
|
||||||
0, /* tp_as_sequence */
|
0, /* tp_as_sequence */
|
||||||
0, /* tp_as_mapping */
|
0, /* tp_as_mapping */
|
||||||
(hashfunc)instancemethod_hash, /* tp_hash */
|
(hashfunc)method_hash, /* tp_hash */
|
||||||
instancemethod_call, /* tp_call */
|
method_call, /* tp_call */
|
||||||
0, /* tp_str */
|
0, /* tp_str */
|
||||||
instancemethod_getattro, /* tp_getattro */
|
method_getattro, /* tp_getattro */
|
||||||
PyObject_GenericSetAttr, /* tp_setattro */
|
PyObject_GenericSetAttr, /* tp_setattro */
|
||||||
0, /* tp_as_buffer */
|
0, /* tp_as_buffer */
|
||||||
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
|
||||||
instancemethod_doc, /* tp_doc */
|
method_doc, /* tp_doc */
|
||||||
(traverseproc)instancemethod_traverse, /* tp_traverse */
|
(traverseproc)method_traverse, /* tp_traverse */
|
||||||
0, /* tp_clear */
|
0, /* tp_clear */
|
||||||
0, /* tp_richcompare */
|
method_richcompare, /* tp_richcompare */
|
||||||
offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
|
offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
|
||||||
0, /* tp_iter */
|
0, /* tp_iter */
|
||||||
0, /* tp_iternext */
|
0, /* tp_iternext */
|
||||||
0, /* tp_methods */
|
0, /* tp_methods */
|
||||||
instancemethod_memberlist, /* tp_members */
|
method_memberlist, /* tp_members */
|
||||||
instancemethod_getset, /* tp_getset */
|
method_getset, /* tp_getset */
|
||||||
0, /* tp_base */
|
0, /* tp_base */
|
||||||
0, /* tp_dict */
|
0, /* tp_dict */
|
||||||
instancemethod_descr_get, /* tp_descr_get */
|
method_descr_get, /* tp_descr_get */
|
||||||
0, /* tp_descr_set */
|
0, /* tp_descr_set */
|
||||||
0, /* tp_dictoffset */
|
0, /* tp_dictoffset */
|
||||||
0, /* tp_init */
|
0, /* tp_init */
|
||||||
0, /* tp_alloc */
|
0, /* tp_alloc */
|
||||||
instancemethod_new, /* tp_new */
|
method_new, /* tp_new */
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Clear out the free list */
|
/* Clear out the free list */
|
||||||
|
|
|
@ -291,9 +291,15 @@ code_repr(PyCodeObject *co)
|
||||||
return PyString_FromString(buf);
|
return PyString_FromString(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static PyObject *
|
||||||
code_compare(PyCodeObject *co, PyCodeObject *cp)
|
code_richcompare(PyObject *self, PyObject *other, int op)
|
||||||
{
|
{
|
||||||
|
/* Temporarily make this unsupported */
|
||||||
|
_Py_Break();
|
||||||
|
Py_INCREF(Py_NotImplemented);
|
||||||
|
return Py_NotImplemented;
|
||||||
|
|
||||||
|
#if 0
|
||||||
int cmp;
|
int cmp;
|
||||||
cmp = PyObject_Compare(co->co_name, cp->co_name);
|
cmp = PyObject_Compare(co->co_name, cp->co_name);
|
||||||
if (cmp) return cmp;
|
if (cmp) return cmp;
|
||||||
|
@ -325,6 +331,7 @@ code_compare(PyCodeObject *co, PyCodeObject *cp)
|
||||||
return -1;
|
return -1;
|
||||||
else
|
else
|
||||||
return 0;
|
return 0;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
static long
|
static long
|
||||||
|
@ -363,12 +370,12 @@ PyTypeObject PyCode_Type = {
|
||||||
0, /* tp_print */
|
0, /* tp_print */
|
||||||
0, /* tp_getattr */
|
0, /* tp_getattr */
|
||||||
0, /* tp_setattr */
|
0, /* tp_setattr */
|
||||||
(cmpfunc)code_compare, /* tp_compare */
|
0, /* tp_compare */
|
||||||
(reprfunc)code_repr, /* tp_repr */
|
(reprfunc)code_repr, /* tp_repr */
|
||||||
0, /* tp_as_number */
|
0, /* tp_as_number */
|
||||||
0, /* tp_as_sequence */
|
0, /* tp_as_sequence */
|
||||||
0, /* tp_as_mapping */
|
0, /* tp_as_mapping */
|
||||||
(hashfunc)code_hash, /* tp_hash */
|
0, /* tp_hash */
|
||||||
0, /* tp_call */
|
0, /* tp_call */
|
||||||
0, /* tp_str */
|
0, /* tp_str */
|
||||||
PyObject_GenericGetAttr, /* tp_getattro */
|
PyObject_GenericGetAttr, /* tp_getattro */
|
||||||
|
|
|
@ -582,6 +582,36 @@ PyDict_GetItem(PyObject *op, PyObject *key)
|
||||||
return ep->me_value;
|
return ep->me_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
|
||||||
|
This returns NULL *with* an exception set if an exception occurred.
|
||||||
|
It returns NULL *without* an exception set if the key wasn't present.
|
||||||
|
*/
|
||||||
|
PyObject *
|
||||||
|
PyDict_GetItemWithError(PyObject *op, PyObject *key)
|
||||||
|
{
|
||||||
|
long hash;
|
||||||
|
dictobject *mp = (dictobject *)op;
|
||||||
|
dictentry *ep;
|
||||||
|
|
||||||
|
if (!PyDict_Check(op)) {
|
||||||
|
PyErr_BadInternalCall();
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
if (!PyString_CheckExact(key) ||
|
||||||
|
(hash = ((PyStringObject *) key)->ob_shash) == -1)
|
||||||
|
{
|
||||||
|
hash = PyObject_Hash(key);
|
||||||
|
if (hash == -1) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ep = (mp->ma_lookup)(mp, key, hash);
|
||||||
|
if (ep == NULL)
|
||||||
|
return NULL;
|
||||||
|
return ep->me_value;
|
||||||
|
}
|
||||||
|
|
||||||
/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
|
/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
|
||||||
* dictionary if it's merely replacing the value for an existing key.
|
* dictionary if it's merely replacing the value for an existing key.
|
||||||
* This means that it's safe to loop over a dictionary with PyDict_Next()
|
* This means that it's safe to loop over a dictionary with PyDict_Next()
|
||||||
|
@ -1432,136 +1462,6 @@ PyDict_Items(PyObject *mp)
|
||||||
return dict_items((dictobject *)mp);
|
return dict_items((dictobject *)mp);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Subroutine which returns the smallest key in a for which b's value
|
|
||||||
is different or absent. The value is returned too, through the
|
|
||||||
pval argument. Both are NULL if no key in a is found for which b's status
|
|
||||||
differs. The refcounts on (and only on) non-NULL *pval and function return
|
|
||||||
values must be decremented by the caller (characterize() increments them
|
|
||||||
to ensure that mutating comparison and PyDict_GetItem calls can't delete
|
|
||||||
them before the caller is done looking at them). */
|
|
||||||
|
|
||||||
static PyObject *
|
|
||||||
characterize(dictobject *a, dictobject *b, PyObject **pval)
|
|
||||||
{
|
|
||||||
PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */
|
|
||||||
PyObject *aval = NULL; /* a[akey] */
|
|
||||||
Py_ssize_t i;
|
|
||||||
int cmp;
|
|
||||||
|
|
||||||
for (i = 0; i <= a->ma_mask; i++) {
|
|
||||||
PyObject *thiskey, *thisaval, *thisbval;
|
|
||||||
if (a->ma_table[i].me_value == NULL)
|
|
||||||
continue;
|
|
||||||
thiskey = a->ma_table[i].me_key;
|
|
||||||
Py_INCREF(thiskey); /* keep alive across compares */
|
|
||||||
if (akey != NULL) {
|
|
||||||
cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT);
|
|
||||||
if (cmp < 0) {
|
|
||||||
Py_DECREF(thiskey);
|
|
||||||
goto Fail;
|
|
||||||
}
|
|
||||||
if (cmp > 0 ||
|
|
||||||
i > a->ma_mask ||
|
|
||||||
a->ma_table[i].me_value == NULL)
|
|
||||||
{
|
|
||||||
/* Not the *smallest* a key; or maybe it is
|
|
||||||
* but the compare shrunk the dict so we can't
|
|
||||||
* find its associated value anymore; or
|
|
||||||
* maybe it is but the compare deleted the
|
|
||||||
* a[thiskey] entry.
|
|
||||||
*/
|
|
||||||
Py_DECREF(thiskey);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */
|
|
||||||
thisaval = a->ma_table[i].me_value;
|
|
||||||
assert(thisaval);
|
|
||||||
Py_INCREF(thisaval); /* keep alive */
|
|
||||||
thisbval = PyDict_GetItem((PyObject *)b, thiskey);
|
|
||||||
if (thisbval == NULL)
|
|
||||||
cmp = 0;
|
|
||||||
else {
|
|
||||||
/* both dicts have thiskey: same values? */
|
|
||||||
cmp = PyObject_RichCompareBool(
|
|
||||||
thisaval, thisbval, Py_EQ);
|
|
||||||
if (cmp < 0) {
|
|
||||||
Py_DECREF(thiskey);
|
|
||||||
Py_DECREF(thisaval);
|
|
||||||
goto Fail;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (cmp == 0) {
|
|
||||||
/* New winner. */
|
|
||||||
Py_XDECREF(akey);
|
|
||||||
Py_XDECREF(aval);
|
|
||||||
akey = thiskey;
|
|
||||||
aval = thisaval;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
Py_DECREF(thiskey);
|
|
||||||
Py_DECREF(thisaval);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*pval = aval;
|
|
||||||
return akey;
|
|
||||||
|
|
||||||
Fail:
|
|
||||||
Py_XDECREF(akey);
|
|
||||||
Py_XDECREF(aval);
|
|
||||||
*pval = NULL;
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
|
||||||
dict_compare(dictobject *a, dictobject *b)
|
|
||||||
{
|
|
||||||
PyObject *adiff, *bdiff, *aval, *bval;
|
|
||||||
int res;
|
|
||||||
|
|
||||||
/* Compare lengths first */
|
|
||||||
if (a->ma_used < b->ma_used)
|
|
||||||
return -1; /* a is shorter */
|
|
||||||
else if (a->ma_used > b->ma_used)
|
|
||||||
return 1; /* b is shorter */
|
|
||||||
|
|
||||||
/* Same length -- check all keys */
|
|
||||||
bdiff = bval = NULL;
|
|
||||||
adiff = characterize(a, b, &aval);
|
|
||||||
if (adiff == NULL) {
|
|
||||||
assert(!aval);
|
|
||||||
/* Either an error, or a is a subset with the same length so
|
|
||||||
* must be equal.
|
|
||||||
*/
|
|
||||||
res = PyErr_Occurred() ? -1 : 0;
|
|
||||||
goto Finished;
|
|
||||||
}
|
|
||||||
bdiff = characterize(b, a, &bval);
|
|
||||||
if (bdiff == NULL && PyErr_Occurred()) {
|
|
||||||
assert(!bval);
|
|
||||||
res = -1;
|
|
||||||
goto Finished;
|
|
||||||
}
|
|
||||||
res = 0;
|
|
||||||
if (bdiff) {
|
|
||||||
/* bdiff == NULL "should be" impossible now, but perhaps
|
|
||||||
* the last comparison done by the characterize() on a had
|
|
||||||
* the side effect of making the dicts equal!
|
|
||||||
*/
|
|
||||||
res = PyObject_Compare(adiff, bdiff);
|
|
||||||
}
|
|
||||||
if (res == 0 && bval != NULL)
|
|
||||||
res = PyObject_Compare(aval, bval);
|
|
||||||
|
|
||||||
Finished:
|
|
||||||
Py_XDECREF(adiff);
|
|
||||||
Py_XDECREF(bdiff);
|
|
||||||
Py_XDECREF(aval);
|
|
||||||
Py_XDECREF(bval);
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Return 1 if dicts equal, 0 if not, -1 if error.
|
/* Return 1 if dicts equal, 0 if not, -1 if error.
|
||||||
* Gets out as soon as any difference is detected.
|
* Gets out as soon as any difference is detected.
|
||||||
* Uses only Py_EQ comparison.
|
* Uses only Py_EQ comparison.
|
||||||
|
@ -1585,9 +1485,11 @@ dict_equal(dictobject *a, dictobject *b)
|
||||||
/* temporarily bump aval's refcount to ensure it stays
|
/* temporarily bump aval's refcount to ensure it stays
|
||||||
alive until we're done with it */
|
alive until we're done with it */
|
||||||
Py_INCREF(aval);
|
Py_INCREF(aval);
|
||||||
bval = PyDict_GetItem((PyObject *)b, key);
|
bval = PyDict_GetItemWithError((PyObject *)b, key);
|
||||||
if (bval == NULL) {
|
if (bval == NULL) {
|
||||||
Py_DECREF(aval);
|
Py_DECREF(aval);
|
||||||
|
if (PyErr_Occurred())
|
||||||
|
return -1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
|
cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
|
||||||
|
@ -2028,7 +1930,7 @@ PyTypeObject PyDict_Type = {
|
||||||
(printfunc)dict_print, /* tp_print */
|
(printfunc)dict_print, /* tp_print */
|
||||||
0, /* tp_getattr */
|
0, /* tp_getattr */
|
||||||
0, /* tp_setattr */
|
0, /* tp_setattr */
|
||||||
(cmpfunc)dict_compare, /* tp_compare */
|
0, /* tp_compare */
|
||||||
(reprfunc)dict_repr, /* tp_repr */
|
(reprfunc)dict_repr, /* tp_repr */
|
||||||
0, /* tp_as_number */
|
0, /* tp_as_number */
|
||||||
&dict_as_sequence, /* tp_as_sequence */
|
&dict_as_sequence, /* tp_as_sequence */
|
||||||
|
|
|
@ -446,6 +446,17 @@ int_compare(PyIntObject *v, PyIntObject *w)
|
||||||
return (i < j) ? -1 : (i > j) ? 1 : 0;
|
return (i < j) ? -1 : (i > j) ? 1 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
int_richcompare(PyObject *self, PyObject *other, int op)
|
||||||
|
{
|
||||||
|
if (!PyInt_Check(self) || !PyInt_Check(other)) {
|
||||||
|
Py_INCREF(Py_NotImplemented);
|
||||||
|
return Py_NotImplemented;
|
||||||
|
}
|
||||||
|
return Py_CmpToRich(op, int_compare((PyIntObject *)self,
|
||||||
|
(PyIntObject *)other));
|
||||||
|
}
|
||||||
|
|
||||||
static long
|
static long
|
||||||
int_hash(PyIntObject *v)
|
int_hash(PyIntObject *v)
|
||||||
{
|
{
|
||||||
|
@ -1063,7 +1074,7 @@ PyTypeObject PyInt_Type = {
|
||||||
(printfunc)int_print, /* tp_print */
|
(printfunc)int_print, /* tp_print */
|
||||||
0, /* tp_getattr */
|
0, /* tp_getattr */
|
||||||
0, /* tp_setattr */
|
0, /* tp_setattr */
|
||||||
(cmpfunc)int_compare, /* tp_compare */
|
0, /* tp_compare */
|
||||||
(reprfunc)int_repr, /* tp_repr */
|
(reprfunc)int_repr, /* tp_repr */
|
||||||
&int_as_number, /* tp_as_number */
|
&int_as_number, /* tp_as_number */
|
||||||
0, /* tp_as_sequence */
|
0, /* tp_as_sequence */
|
||||||
|
@ -1078,7 +1089,7 @@ PyTypeObject PyInt_Type = {
|
||||||
int_doc, /* tp_doc */
|
int_doc, /* tp_doc */
|
||||||
0, /* tp_traverse */
|
0, /* tp_traverse */
|
||||||
0, /* tp_clear */
|
0, /* tp_clear */
|
||||||
0, /* tp_richcompare */
|
int_richcompare, /* tp_richcompare */
|
||||||
0, /* tp_weaklistoffset */
|
0, /* tp_weaklistoffset */
|
||||||
0, /* tp_iter */
|
0, /* tp_iter */
|
||||||
0, /* tp_iternext */
|
0, /* tp_iternext */
|
||||||
|
|
|
@ -1971,6 +1971,26 @@ build_cmpwrapper(PyObject *cmpfunc)
|
||||||
return (PyObject *)co;
|
return (PyObject *)co;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
is_default_cmp(PyObject *cmpfunc)
|
||||||
|
{
|
||||||
|
PyCFunctionObject *f;
|
||||||
|
if (cmpfunc == NULL || cmpfunc == Py_None)
|
||||||
|
return 1;
|
||||||
|
if (!PyCFunction_Check(cmpfunc))
|
||||||
|
return 0;
|
||||||
|
f = (PyCFunction *)cmpfunc;
|
||||||
|
if (f->m_self != NULL)
|
||||||
|
return 0;
|
||||||
|
if (!PyString_Check(f->m_module))
|
||||||
|
return 0;
|
||||||
|
if (strcmp(PyString_AS_STRING(f->m_module), "__builtin__") != 0)
|
||||||
|
return 0;
|
||||||
|
if (strcmp(f->m_ml->ml_name, "cmp") != 0)
|
||||||
|
return 0;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
/* An adaptive, stable, natural mergesort. See listsort.txt.
|
/* An adaptive, stable, natural mergesort. See listsort.txt.
|
||||||
* Returns Py_None on success, NULL on error. Even in case of error, the
|
* Returns Py_None on success, NULL on error. Even in case of error, the
|
||||||
* list will be some permutation of its input state (nothing is lost or
|
* list will be some permutation of its input state (nothing is lost or
|
||||||
|
@ -2001,7 +2021,7 @@ listsort(PyListObject *self, PyObject *args, PyObject *kwds)
|
||||||
kwlist, &compare, &keyfunc, &reverse))
|
kwlist, &compare, &keyfunc, &reverse))
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (compare == Py_None)
|
if (is_default_cmp(compare))
|
||||||
compare = NULL;
|
compare = NULL;
|
||||||
if (keyfunc == Py_None)
|
if (keyfunc == Py_None)
|
||||||
keyfunc = NULL;
|
keyfunc = NULL;
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
|
|
||||||
|
|
||||||
/* Long (arbitrary precision) integer object implementation */
|
/* Long (arbitrary precision) integer object implementation */
|
||||||
|
|
||||||
/* XXX The functional organization of this file is terrible */
|
/* XXX The functional organization of this file is terrible */
|
||||||
|
@ -1882,6 +1880,14 @@ long_compare(PyLongObject *a, PyLongObject *b)
|
||||||
return sign < 0 ? -1 : sign > 0 ? 1 : 0;
|
return sign < 0 ? -1 : sign > 0 ? 1 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
long_richcompare(PyObject *self, PyObject *other, int op)
|
||||||
|
{
|
||||||
|
PyLongObject *a, *b;
|
||||||
|
CONVERT_BINOP((PyObject *)self, (PyObject *)other, &a, &b);
|
||||||
|
return Py_CmpToRich(op, long_compare(a, b));
|
||||||
|
}
|
||||||
|
|
||||||
static long
|
static long
|
||||||
long_hash(PyLongObject *v)
|
long_hash(PyLongObject *v)
|
||||||
{
|
{
|
||||||
|
@ -3357,7 +3363,7 @@ PyTypeObject PyLong_Type = {
|
||||||
0, /* tp_print */
|
0, /* tp_print */
|
||||||
0, /* tp_getattr */
|
0, /* tp_getattr */
|
||||||
0, /* tp_setattr */
|
0, /* tp_setattr */
|
||||||
(cmpfunc)long_compare, /* tp_compare */
|
0, /* tp_compare */
|
||||||
long_repr, /* tp_repr */
|
long_repr, /* tp_repr */
|
||||||
&long_as_number, /* tp_as_number */
|
&long_as_number, /* tp_as_number */
|
||||||
0, /* tp_as_sequence */
|
0, /* tp_as_sequence */
|
||||||
|
@ -3372,7 +3378,7 @@ PyTypeObject PyLong_Type = {
|
||||||
long_doc, /* tp_doc */
|
long_doc, /* tp_doc */
|
||||||
0, /* tp_traverse */
|
0, /* tp_traverse */
|
||||||
0, /* tp_clear */
|
0, /* tp_clear */
|
||||||
0, /* tp_richcompare */
|
long_richcompare, /* tp_richcompare */
|
||||||
0, /* tp_weaklistoffset */
|
0, /* tp_weaklistoffset */
|
||||||
0, /* tp_iter */
|
0, /* tp_iter */
|
||||||
0, /* tp_iternext */
|
0, /* tp_iternext */
|
||||||
|
|
|
@ -196,17 +196,31 @@ meth_repr(PyCFunctionObject *m)
|
||||||
m->m_self);
|
m->m_self);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static PyObject *
|
||||||
meth_compare(PyCFunctionObject *a, PyCFunctionObject *b)
|
meth_richcompare(PyObject *self, PyObject *other, int op)
|
||||||
{
|
{
|
||||||
if (a->m_self != b->m_self)
|
PyCFunctionObject *a, *b;
|
||||||
return (a->m_self < b->m_self) ? -1 : 1;
|
PyObject *res;
|
||||||
if (a->m_ml->ml_meth == b->m_ml->ml_meth)
|
int eq;
|
||||||
return 0;
|
|
||||||
if (strcmp(a->m_ml->ml_name, b->m_ml->ml_name) < 0)
|
if ((op != Py_EQ && op != Py_NE) ||
|
||||||
return -1;
|
!PyCFunction_Check(self) ||
|
||||||
|
!PyCFunction_Check(other))
|
||||||
|
{
|
||||||
|
Py_INCREF(Py_NotImplemented);
|
||||||
|
return Py_NotImplemented;
|
||||||
|
}
|
||||||
|
a = (PyCFunctionObject *)self;
|
||||||
|
b = (PyCFunctionObject *)other;
|
||||||
|
eq = a->m_self == b->m_self;
|
||||||
|
if (eq)
|
||||||
|
eq = a->m_ml->ml_meth == b->m_ml->ml_meth;
|
||||||
|
if (op == Py_EQ)
|
||||||
|
res = eq ? Py_True : Py_False;
|
||||||
else
|
else
|
||||||
return 1;
|
res = eq ? Py_False : Py_True;
|
||||||
|
Py_INCREF(res);
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
static long
|
static long
|
||||||
|
@ -240,7 +254,7 @@ PyTypeObject PyCFunction_Type = {
|
||||||
0, /* tp_print */
|
0, /* tp_print */
|
||||||
0, /* tp_getattr */
|
0, /* tp_getattr */
|
||||||
0, /* tp_setattr */
|
0, /* tp_setattr */
|
||||||
(cmpfunc)meth_compare, /* tp_compare */
|
0, /* tp_compare */
|
||||||
(reprfunc)meth_repr, /* tp_repr */
|
(reprfunc)meth_repr, /* tp_repr */
|
||||||
0, /* tp_as_number */
|
0, /* tp_as_number */
|
||||||
0, /* tp_as_sequence */
|
0, /* tp_as_sequence */
|
||||||
|
@ -255,7 +269,7 @@ PyTypeObject PyCFunction_Type = {
|
||||||
0, /* tp_doc */
|
0, /* tp_doc */
|
||||||
(traverseproc)meth_traverse, /* tp_traverse */
|
(traverseproc)meth_traverse, /* tp_traverse */
|
||||||
0, /* tp_clear */
|
0, /* tp_clear */
|
||||||
0, /* tp_richcompare */
|
meth_richcompare, /* tp_richcompare */
|
||||||
0, /* tp_weaklistoffset */
|
0, /* tp_weaklistoffset */
|
||||||
0, /* tp_iter */
|
0, /* tp_iter */
|
||||||
0, /* tp_iternext */
|
0, /* tp_iternext */
|
||||||
|
|
566
Objects/object.c
566
Objects/object.c
|
@ -252,14 +252,6 @@ _PyObject_NewVar(PyTypeObject *tp, Py_ssize_t nitems)
|
||||||
return PyObject_INIT_VAR(op, tp, nitems);
|
return PyObject_INIT_VAR(op, tp, nitems);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* for binary compatibility with 2.2 */
|
|
||||||
#undef _PyObject_Del
|
|
||||||
void
|
|
||||||
_PyObject_Del(PyObject *op)
|
|
||||||
{
|
|
||||||
PyObject_FREE(op);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Implementation of PyObject_Print with recursion checking */
|
/* Implementation of PyObject_Print with recursion checking */
|
||||||
static int
|
static int
|
||||||
internal_print(PyObject *op, FILE *fp, int flags, int nesting)
|
internal_print(PyObject *op, FILE *fp, int flags, int nesting)
|
||||||
|
@ -513,431 +505,200 @@ PyObject_Unicode(PyObject *v)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/* Helper to warn about deprecated tp_compare return values. Return:
|
/* The new comparison philosophy is: we completely separate three-way
|
||||||
-2 for an exception;
|
comparison from rich comparison. That is, PyObject_Compare() and
|
||||||
-1 if v < w;
|
PyObject_Cmp() *just* use the tp_compare slot. And PyObject_RichCompare()
|
||||||
0 if v == w;
|
and PyObject_RichCompareBool() *just* use the tp_richcompare slot.
|
||||||
1 if v > w.
|
|
||||||
(This function cannot return 2.)
|
See (*) below for practical amendments.
|
||||||
|
|
||||||
|
IOW, only cmp() uses tp_compare; the comparison operators (==, !=, <=, <,
|
||||||
|
>=, >) only use tp_richcompare. Note that list.sort() only uses <.
|
||||||
|
|
||||||
|
(And yes, eventually we'll rip out cmp() and tp_compare.)
|
||||||
|
|
||||||
|
The calling conventions are different: tp_compare only gets called with two
|
||||||
|
objects of the appropriate type; tp_richcompare gets called with a first
|
||||||
|
argument of the appropriate type and a second object of an arbitrary type.
|
||||||
|
We never do any kind of coercion.
|
||||||
|
|
||||||
|
The return conventions are also different.
|
||||||
|
|
||||||
|
The tp_compare slot should return a C int, as follows:
|
||||||
|
|
||||||
|
-1 if a < b or if an exception occurred
|
||||||
|
0 if a == b
|
||||||
|
+1 if a > b
|
||||||
|
|
||||||
|
No other return values are allowed. PyObject_Compare() has the same
|
||||||
|
calling convention.
|
||||||
|
|
||||||
|
The tp_richcompare slot should return an object, as follows:
|
||||||
|
|
||||||
|
NULL if an exception occurred
|
||||||
|
NotImplemented if the requested comparison is not implemented
|
||||||
|
any other false value if the requested comparison is false
|
||||||
|
any other true value if the requested comparison is true
|
||||||
|
|
||||||
|
The PyObject_RichCompare[Bool]() wrappers raise TypeError when they get
|
||||||
|
NotImplemented.
|
||||||
|
|
||||||
|
(*) Practical amendments:
|
||||||
|
|
||||||
|
- If rich comparison returns NotImplemented, == and != are decided by
|
||||||
|
comparing the object pointer (i.e. falling back to the base object
|
||||||
|
implementation).
|
||||||
|
|
||||||
|
- If three-way comparison is not implemented, it falls back on rich
|
||||||
|
comparison (but not the other way around!).
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/* Forward */
|
||||||
|
static PyObject *do_richcompare(PyObject *v, PyObject *w, int op);
|
||||||
|
|
||||||
|
/* Perform a three-way comparison, raising TypeError if three-way comparison
|
||||||
|
is not supported. */
|
||||||
static int
|
static int
|
||||||
adjust_tp_compare(int c)
|
do_compare(PyObject *v, PyObject *w)
|
||||||
{
|
{
|
||||||
if (PyErr_Occurred()) {
|
cmpfunc f;
|
||||||
if (c != -1 && c != -2) {
|
int ok;
|
||||||
PyObject *t, *v, *tb;
|
|
||||||
PyErr_Fetch(&t, &v, &tb);
|
if (v->ob_type == w->ob_type &&
|
||||||
if (PyErr_Warn(PyExc_RuntimeWarning,
|
(f = v->ob_type->tp_compare) != NULL) {
|
||||||
"tp_compare didn't return -1 or -2 "
|
return (*f)(v, w);
|
||||||
"for exception") < 0) {
|
|
||||||
Py_XDECREF(t);
|
|
||||||
Py_XDECREF(v);
|
|
||||||
Py_XDECREF(tb);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
PyErr_Restore(t, v, tb);
|
|
||||||
}
|
|
||||||
return -2;
|
|
||||||
}
|
|
||||||
else if (c < -1 || c > 1) {
|
|
||||||
if (PyErr_Warn(PyExc_RuntimeWarning,
|
|
||||||
"tp_compare didn't return -1, 0 or 1") < 0)
|
|
||||||
return -2;
|
|
||||||
else
|
|
||||||
return c < -1 ? -1 : 1;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
assert(c >= -1 && c <= 1);
|
|
||||||
return c;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Now try three-way compare before giving up. This is intentionally
|
||||||
|
elaborate; if you have a it will raise TypeError if it detects two
|
||||||
|
objects that aren't ordered with respect to each other. */
|
||||||
|
ok = PyObject_RichCompareBool(v, w, Py_LT);
|
||||||
|
if (ok < 0)
|
||||||
|
return -1; /* Error */
|
||||||
|
if (ok)
|
||||||
|
return -1; /* Less than */
|
||||||
|
ok = PyObject_RichCompareBool(v, w, Py_GT);
|
||||||
|
if (ok < 0)
|
||||||
|
return -1; /* Error */
|
||||||
|
if (ok)
|
||||||
|
return 1; /* Greater than */
|
||||||
|
ok = PyObject_RichCompareBool(v, w, Py_EQ);
|
||||||
|
if (ok < 0)
|
||||||
|
return -1; /* Error */
|
||||||
|
if (ok)
|
||||||
|
return 0; /* Equal */
|
||||||
|
|
||||||
|
/* Give up */
|
||||||
|
PyErr_Format(PyExc_TypeError,
|
||||||
|
"unorderable types: '%.100s' <> '%.100s'",
|
||||||
|
v->ob_type->tp_name,
|
||||||
|
w->ob_type->tp_name);
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Perform a three-way comparison. This wraps do_compare() with a check for
|
||||||
|
NULL arguments and a recursion check. */
|
||||||
|
int
|
||||||
|
PyObject_Compare(PyObject *v, PyObject *w)
|
||||||
|
{
|
||||||
|
int res;
|
||||||
|
|
||||||
/* Macro to get the tp_richcompare field of a type if defined */
|
if (v == NULL || w == NULL) {
|
||||||
#define RICHCOMPARE(t) ((t)->tp_richcompare)
|
if (!PyErr_Occurred())
|
||||||
|
PyErr_BadInternalCall();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (Py_EnterRecursiveCall(" in cmp"))
|
||||||
|
return -1;
|
||||||
|
res = do_compare(v, w);
|
||||||
|
Py_LeaveRecursiveCall();
|
||||||
|
return res < 0 ? -1 : res;
|
||||||
|
}
|
||||||
|
|
||||||
/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
|
/* Map rich comparison operators to their swapped version, e.g. LT <--> GT */
|
||||||
int _Py_SwappedOp[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
|
int _Py_SwappedOp[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
|
||||||
|
|
||||||
/* Try a genuine rich comparison, returning an object. Return:
|
static char *opstrings[] = {">", ">=", "==", "!=", "<", "<="};
|
||||||
NULL for exception;
|
|
||||||
NotImplemented if this particular rich comparison is not implemented or
|
/* Perform a rich comparison, raising TypeError when the requested comparison
|
||||||
undefined;
|
operator is not supported. */
|
||||||
some object not equal to NotImplemented if it is implemented
|
|
||||||
(this latter object may not be a Boolean).
|
|
||||||
*/
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
try_rich_compare(PyObject *v, PyObject *w, int op)
|
do_richcompare(PyObject *v, PyObject *w, int op)
|
||||||
{
|
{
|
||||||
richcmpfunc f;
|
richcmpfunc f;
|
||||||
PyObject *res;
|
PyObject *res;
|
||||||
|
|
||||||
if (v->ob_type != w->ob_type &&
|
if (v->ob_type != w->ob_type &&
|
||||||
PyType_IsSubtype(w->ob_type, v->ob_type) &&
|
PyType_IsSubtype(w->ob_type, v->ob_type) &&
|
||||||
(f = RICHCOMPARE(w->ob_type)) != NULL) {
|
(f = w->ob_type->tp_richcompare) != NULL) {
|
||||||
res = (*f)(w, v, _Py_SwappedOp[op]);
|
res = (*f)(w, v, _Py_SwappedOp[op]);
|
||||||
if (res != Py_NotImplemented)
|
if (res != Py_NotImplemented)
|
||||||
return res;
|
return res;
|
||||||
Py_DECREF(res);
|
Py_DECREF(res);
|
||||||
}
|
}
|
||||||
if ((f = RICHCOMPARE(v->ob_type)) != NULL) {
|
if ((f = v->ob_type->tp_richcompare) != NULL) {
|
||||||
res = (*f)(v, w, op);
|
res = (*f)(v, w, op);
|
||||||
if (res != Py_NotImplemented)
|
if (res != Py_NotImplemented)
|
||||||
return res;
|
return res;
|
||||||
Py_DECREF(res);
|
Py_DECREF(res);
|
||||||
}
|
}
|
||||||
if ((f = RICHCOMPARE(w->ob_type)) != NULL) {
|
if ((f = w->ob_type->tp_richcompare) != NULL) {
|
||||||
return (*f)(w, v, _Py_SwappedOp[op]);
|
res = (*f)(w, v, _Py_SwappedOp[op]);
|
||||||
|
if (res != Py_NotImplemented)
|
||||||
|
return res;
|
||||||
|
Py_DECREF(res);
|
||||||
|
}
|
||||||
|
/* If neither object implements it, provide a sensible default
|
||||||
|
for == and !=, but raise an exception for ordering. */
|
||||||
|
switch (op) {
|
||||||
|
case Py_EQ:
|
||||||
|
res = (v == w) ? Py_True : Py_False;
|
||||||
|
break;
|
||||||
|
case Py_NE:
|
||||||
|
res = (v != w) ? Py_True : Py_False;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
PyErr_Format(PyExc_TypeError,
|
||||||
|
"unorderable types: %.100s() %s %.100s()",
|
||||||
|
v->ob_type->tp_name,
|
||||||
|
opstrings[op],
|
||||||
|
w->ob_type->tp_name);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
res = Py_NotImplemented;
|
|
||||||
Py_INCREF(res);
|
Py_INCREF(res);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Try a genuine rich comparison, returning an int. Return:
|
/* Perform a rich comparison with object result. This wraps do_richcompare()
|
||||||
-1 for exception (including the case where try_rich_compare() returns an
|
with a check for NULL arguments and a recursion check. */
|
||||||
object that's not a Boolean);
|
|
||||||
0 if the outcome is false;
|
|
||||||
1 if the outcome is true;
|
|
||||||
2 if this particular rich comparison is not implemented or undefined.
|
|
||||||
*/
|
|
||||||
static int
|
|
||||||
try_rich_compare_bool(PyObject *v, PyObject *w, int op)
|
|
||||||
{
|
|
||||||
PyObject *res;
|
|
||||||
int ok;
|
|
||||||
|
|
||||||
if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL)
|
|
||||||
return 2; /* Shortcut, avoid INCREF+DECREF */
|
|
||||||
res = try_rich_compare(v, w, op);
|
|
||||||
if (res == NULL)
|
|
||||||
return -1;
|
|
||||||
if (res == Py_NotImplemented) {
|
|
||||||
Py_DECREF(res);
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
ok = PyObject_IsTrue(res);
|
|
||||||
Py_DECREF(res);
|
|
||||||
return ok;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Try rich comparisons to determine a 3-way comparison. Return:
|
|
||||||
-2 for an exception;
|
|
||||||
-1 if v < w;
|
|
||||||
0 if v == w;
|
|
||||||
1 if v > w;
|
|
||||||
2 if this particular rich comparison is not implemented or undefined.
|
|
||||||
*/
|
|
||||||
static int
|
|
||||||
try_rich_to_3way_compare(PyObject *v, PyObject *w)
|
|
||||||
{
|
|
||||||
static struct { int op; int outcome; } tries[3] = {
|
|
||||||
/* Try this operator, and if it is true, use this outcome: */
|
|
||||||
{Py_EQ, 0},
|
|
||||||
{Py_LT, -1},
|
|
||||||
{Py_GT, 1},
|
|
||||||
};
|
|
||||||
int i;
|
|
||||||
|
|
||||||
if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL)
|
|
||||||
return 2; /* Shortcut */
|
|
||||||
|
|
||||||
for (i = 0; i < 3; i++) {
|
|
||||||
switch (try_rich_compare_bool(v, w, tries[i].op)) {
|
|
||||||
case -1:
|
|
||||||
return -2;
|
|
||||||
case 1:
|
|
||||||
return tries[i].outcome;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Try a 3-way comparison, returning an int. Return:
|
|
||||||
-2 for an exception;
|
|
||||||
-1 if v < w;
|
|
||||||
0 if v == w;
|
|
||||||
1 if v > w;
|
|
||||||
2 if this particular 3-way comparison is not implemented or undefined.
|
|
||||||
*/
|
|
||||||
static int
|
|
||||||
try_3way_compare(PyObject *v, PyObject *w)
|
|
||||||
{
|
|
||||||
int c;
|
|
||||||
cmpfunc f;
|
|
||||||
|
|
||||||
/* Comparisons involving instances are given to instance_compare,
|
|
||||||
which has the same return conventions as this function. */
|
|
||||||
|
|
||||||
f = v->ob_type->tp_compare;
|
|
||||||
|
|
||||||
/* If both have the same (non-NULL) tp_compare, use it. */
|
|
||||||
if (f != NULL && f == w->ob_type->tp_compare) {
|
|
||||||
c = (*f)(v, w);
|
|
||||||
return adjust_tp_compare(c);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* If either tp_compare is _PyObject_SlotCompare, that's safe. */
|
|
||||||
if (f == _PyObject_SlotCompare ||
|
|
||||||
w->ob_type->tp_compare == _PyObject_SlotCompare)
|
|
||||||
return _PyObject_SlotCompare(v, w);
|
|
||||||
|
|
||||||
/* If we're here, v and w,
|
|
||||||
a) are not instances;
|
|
||||||
b) have different types or a type without tp_compare; and
|
|
||||||
c) don't have a user-defined tp_compare.
|
|
||||||
tp_compare implementations in C assume that both arguments
|
|
||||||
have their type, so we give up if the coercion fails.
|
|
||||||
*/
|
|
||||||
c = PyNumber_CoerceEx(&v, &w);
|
|
||||||
if (c < 0)
|
|
||||||
return -2;
|
|
||||||
if (c > 0)
|
|
||||||
return 2;
|
|
||||||
f = v->ob_type->tp_compare;
|
|
||||||
if (f != NULL && f == w->ob_type->tp_compare) {
|
|
||||||
c = (*f)(v, w);
|
|
||||||
Py_DECREF(v);
|
|
||||||
Py_DECREF(w);
|
|
||||||
return adjust_tp_compare(c);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* No comparison defined */
|
|
||||||
Py_DECREF(v);
|
|
||||||
Py_DECREF(w);
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Final fallback 3-way comparison, returning an int. Return:
|
|
||||||
-2 if an error occurred;
|
|
||||||
-1 if v < w;
|
|
||||||
0 if v == w;
|
|
||||||
1 if v > w.
|
|
||||||
*/
|
|
||||||
static int
|
|
||||||
default_3way_compare(PyObject *v, PyObject *w)
|
|
||||||
{
|
|
||||||
int c;
|
|
||||||
const char *vname, *wname;
|
|
||||||
|
|
||||||
if (v->ob_type == w->ob_type) {
|
|
||||||
/* When comparing these pointers, they must be cast to
|
|
||||||
* integer types (i.e. Py_uintptr_t, our spelling of C9X's
|
|
||||||
* uintptr_t). ANSI specifies that pointer compares other
|
|
||||||
* than == and != to non-related structures are undefined.
|
|
||||||
*/
|
|
||||||
Py_uintptr_t vv = (Py_uintptr_t)v;
|
|
||||||
Py_uintptr_t ww = (Py_uintptr_t)w;
|
|
||||||
return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* None is smaller than anything */
|
|
||||||
if (v == Py_None)
|
|
||||||
return -1;
|
|
||||||
if (w == Py_None)
|
|
||||||
return 1;
|
|
||||||
|
|
||||||
/* different type: compare type names; numbers are smaller */
|
|
||||||
if (PyNumber_Check(v))
|
|
||||||
vname = "";
|
|
||||||
else
|
|
||||||
vname = v->ob_type->tp_name;
|
|
||||||
if (PyNumber_Check(w))
|
|
||||||
wname = "";
|
|
||||||
else
|
|
||||||
wname = w->ob_type->tp_name;
|
|
||||||
c = strcmp(vname, wname);
|
|
||||||
if (c < 0)
|
|
||||||
return -1;
|
|
||||||
if (c > 0)
|
|
||||||
return 1;
|
|
||||||
/* Same type name, or (more likely) incomparable numeric types */
|
|
||||||
return ((Py_uintptr_t)(v->ob_type) < (
|
|
||||||
Py_uintptr_t)(w->ob_type)) ? -1 : 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Do a 3-way comparison, by hook or by crook. Return:
|
|
||||||
-2 for an exception (but see below);
|
|
||||||
-1 if v < w;
|
|
||||||
0 if v == w;
|
|
||||||
1 if v > w;
|
|
||||||
BUT: if the object implements a tp_compare function, it returns
|
|
||||||
whatever this function returns (whether with an exception or not).
|
|
||||||
*/
|
|
||||||
static int
|
|
||||||
do_cmp(PyObject *v, PyObject *w)
|
|
||||||
{
|
|
||||||
int c;
|
|
||||||
cmpfunc f;
|
|
||||||
|
|
||||||
if (v->ob_type == w->ob_type
|
|
||||||
&& (f = v->ob_type->tp_compare) != NULL) {
|
|
||||||
c = (*f)(v, w);
|
|
||||||
return adjust_tp_compare(c);
|
|
||||||
}
|
|
||||||
/* We only get here if one of the following is true:
|
|
||||||
a) v and w have different types
|
|
||||||
b) v and w have the same type, which doesn't have tp_compare
|
|
||||||
c) v and w are instances, and either __cmp__ is not defined or
|
|
||||||
__cmp__ returns NotImplemented
|
|
||||||
*/
|
|
||||||
c = try_rich_to_3way_compare(v, w);
|
|
||||||
if (c < 2)
|
|
||||||
return c;
|
|
||||||
c = try_3way_compare(v, w);
|
|
||||||
if (c < 2)
|
|
||||||
return c;
|
|
||||||
return default_3way_compare(v, w);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Compare v to w. Return
|
|
||||||
-1 if v < w or exception (PyErr_Occurred() true in latter case).
|
|
||||||
0 if v == w.
|
|
||||||
1 if v > w.
|
|
||||||
XXX The docs (C API manual) say the return value is undefined in case
|
|
||||||
XXX of error.
|
|
||||||
*/
|
|
||||||
int
|
|
||||||
PyObject_Compare(PyObject *v, PyObject *w)
|
|
||||||
{
|
|
||||||
int result;
|
|
||||||
|
|
||||||
if (v == NULL || w == NULL) {
|
|
||||||
PyErr_BadInternalCall();
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
if (v == w)
|
|
||||||
return 0;
|
|
||||||
if (Py_EnterRecursiveCall(" in cmp"))
|
|
||||||
return -1;
|
|
||||||
result = do_cmp(v, w);
|
|
||||||
Py_LeaveRecursiveCall();
|
|
||||||
return result < 0 ? -1 : result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Return (new reference to) Py_True or Py_False. */
|
|
||||||
static PyObject *
|
|
||||||
convert_3way_to_object(int op, int c)
|
|
||||||
{
|
|
||||||
PyObject *result;
|
|
||||||
switch (op) {
|
|
||||||
case Py_LT: c = c < 0; break;
|
|
||||||
case Py_LE: c = c <= 0; break;
|
|
||||||
case Py_EQ: c = c == 0; break;
|
|
||||||
case Py_NE: c = c != 0; break;
|
|
||||||
case Py_GT: c = c > 0; break;
|
|
||||||
case Py_GE: c = c >= 0; break;
|
|
||||||
}
|
|
||||||
result = c ? Py_True : Py_False;
|
|
||||||
Py_INCREF(result);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* We want a rich comparison but don't have one. Try a 3-way cmp instead.
|
|
||||||
Return
|
|
||||||
NULL if error
|
|
||||||
Py_True if v op w
|
|
||||||
Py_False if not (v op w)
|
|
||||||
*/
|
|
||||||
static PyObject *
|
|
||||||
try_3way_to_rich_compare(PyObject *v, PyObject *w, int op)
|
|
||||||
{
|
|
||||||
int c;
|
|
||||||
|
|
||||||
c = try_3way_compare(v, w);
|
|
||||||
if (c >= 2)
|
|
||||||
c = default_3way_compare(v, w);
|
|
||||||
if (c <= -2)
|
|
||||||
return NULL;
|
|
||||||
return convert_3way_to_object(op, c);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Do rich comparison on v and w. Return
|
|
||||||
NULL if error
|
|
||||||
Else a new reference to an object other than Py_NotImplemented, usually(?):
|
|
||||||
Py_True if v op w
|
|
||||||
Py_False if not (v op w)
|
|
||||||
*/
|
|
||||||
static PyObject *
|
|
||||||
do_richcmp(PyObject *v, PyObject *w, int op)
|
|
||||||
{
|
|
||||||
PyObject *res;
|
|
||||||
|
|
||||||
res = try_rich_compare(v, w, op);
|
|
||||||
if (res != Py_NotImplemented)
|
|
||||||
return res;
|
|
||||||
Py_DECREF(res);
|
|
||||||
|
|
||||||
return try_3way_to_rich_compare(v, w, op);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Return:
|
|
||||||
NULL for exception;
|
|
||||||
some object not equal to NotImplemented if it is implemented
|
|
||||||
(this latter object may not be a Boolean).
|
|
||||||
*/
|
|
||||||
PyObject *
|
PyObject *
|
||||||
PyObject_RichCompare(PyObject *v, PyObject *w, int op)
|
PyObject_RichCompare(PyObject *v, PyObject *w, int op)
|
||||||
{
|
{
|
||||||
PyObject *res;
|
PyObject *res;
|
||||||
|
|
||||||
assert(Py_LT <= op && op <= Py_GE);
|
assert(Py_LT <= op && op <= Py_GE);
|
||||||
|
if (v == NULL || w == NULL) {
|
||||||
|
if (!PyErr_Occurred())
|
||||||
|
PyErr_BadInternalCall();
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
if (Py_EnterRecursiveCall(" in cmp"))
|
if (Py_EnterRecursiveCall(" in cmp"))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
res = do_richcompare(v, w, op);
|
||||||
/* If the types are equal, and not old-style instances, try to
|
|
||||||
get out cheap (don't bother with coercions etc.). */
|
|
||||||
if (v->ob_type == w->ob_type) {
|
|
||||||
cmpfunc fcmp;
|
|
||||||
richcmpfunc frich = RICHCOMPARE(v->ob_type);
|
|
||||||
/* If the type has richcmp, try it first. try_rich_compare
|
|
||||||
tries it two-sided, which is not needed since we've a
|
|
||||||
single type only. */
|
|
||||||
if (frich != NULL) {
|
|
||||||
res = (*frich)(v, w, op);
|
|
||||||
if (res != Py_NotImplemented)
|
|
||||||
goto Done;
|
|
||||||
Py_DECREF(res);
|
|
||||||
}
|
|
||||||
/* No richcmp, or this particular richmp not implemented.
|
|
||||||
Try 3-way cmp. */
|
|
||||||
fcmp = v->ob_type->tp_compare;
|
|
||||||
if (fcmp != NULL) {
|
|
||||||
int c = (*fcmp)(v, w);
|
|
||||||
c = adjust_tp_compare(c);
|
|
||||||
if (c == -2) {
|
|
||||||
res = NULL;
|
|
||||||
goto Done;
|
|
||||||
}
|
|
||||||
res = convert_3way_to_object(op, c);
|
|
||||||
goto Done;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Fast path not taken, or couldn't deliver a useful result. */
|
|
||||||
res = do_richcmp(v, w, op);
|
|
||||||
Done:
|
|
||||||
Py_LeaveRecursiveCall();
|
Py_LeaveRecursiveCall();
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Return -1 if error; 1 if v op w; 0 if not (v op w). */
|
/* Perform a rich comparison with integer result. This wraps
|
||||||
|
PyObject_RichCompare(), returning -1 for error, 0 for false, 1 for true. */
|
||||||
int
|
int
|
||||||
PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
|
PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
|
||||||
{
|
{
|
||||||
PyObject *res;
|
PyObject *res;
|
||||||
int ok;
|
int ok;
|
||||||
|
|
||||||
/* Quick result when objects are the same.
|
|
||||||
Guarantees that identity implies equality. */
|
|
||||||
if (v == w) {
|
|
||||||
if (op == Py_EQ)
|
|
||||||
return 1;
|
|
||||||
else if (op == Py_NE)
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
res = PyObject_RichCompare(v, w, op);
|
res = PyObject_RichCompare(v, w, op);
|
||||||
if (res == NULL)
|
if (res == NULL)
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -949,6 +710,44 @@ PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
|
||||||
return ok;
|
return ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Turn the result of a three-way comparison into the result expected by a
|
||||||
|
rich comparison. */
|
||||||
|
PyObject *
|
||||||
|
Py_CmpToRich(int op, int cmp)
|
||||||
|
{
|
||||||
|
PyObject *res;
|
||||||
|
int ok;
|
||||||
|
|
||||||
|
if (PyErr_Occurred())
|
||||||
|
return NULL;
|
||||||
|
switch (op) {
|
||||||
|
case Py_LT:
|
||||||
|
ok = cmp < 0;
|
||||||
|
break;
|
||||||
|
case Py_LE:
|
||||||
|
ok = cmp <= 0;
|
||||||
|
break;
|
||||||
|
case Py_EQ:
|
||||||
|
ok = cmp == 0;
|
||||||
|
break;
|
||||||
|
case Py_NE:
|
||||||
|
ok = cmp != 0;
|
||||||
|
break;
|
||||||
|
case Py_GT:
|
||||||
|
ok = cmp > 0;
|
||||||
|
break;
|
||||||
|
case Py_GE:
|
||||||
|
ok = cmp >= 0;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
PyErr_BadArgument();
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
res = ok ? Py_True : Py_False;
|
||||||
|
Py_INCREF(res);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
/* Set of hash utility functions to help maintaining the invariant that
|
/* Set of hash utility functions to help maintaining the invariant that
|
||||||
if a==b then hash(a)==hash(b)
|
if a==b then hash(a)==hash(b)
|
||||||
|
|
||||||
|
@ -1832,6 +1631,9 @@ _Py_ReadyTypes(void)
|
||||||
|
|
||||||
if (PyType_Ready(&PyNotImplemented_Type) < 0)
|
if (PyType_Ready(&PyNotImplemented_Type) < 0)
|
||||||
Py_FatalError("Can't initialize type(NotImplemented)");
|
Py_FatalError("Can't initialize type(NotImplemented)");
|
||||||
|
|
||||||
|
if (PyType_Ready(&PyCode_Type) < 0)
|
||||||
|
Py_FatalError("Can't initialize 'code'");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -280,25 +280,55 @@ static PyMethodDef slice_methods[] = {
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
static int
|
static PyObject *
|
||||||
slice_compare(PySliceObject *v, PySliceObject *w)
|
slice_richcompare(PyObject *v, PyObject *w, int op)
|
||||||
{
|
{
|
||||||
int result = 0;
|
PyObject *t1;
|
||||||
|
PyObject *t2;
|
||||||
|
PyObject *res;
|
||||||
|
|
||||||
if (v == w)
|
if (v == w) {
|
||||||
return 0;
|
/* XXX Do we really need this shortcut?
|
||||||
|
There's a unit test for it, but is that fair? */
|
||||||
|
switch (op) {
|
||||||
|
case Py_EQ:
|
||||||
|
case Py_LE:
|
||||||
|
case Py_GE:
|
||||||
|
res = Py_True;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
res = Py_False;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
Py_INCREF(res);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
if (PyObject_Cmp(v->start, w->start, &result) < 0)
|
t1 = PyTuple_New(3);
|
||||||
return -2;
|
t2 = PyTuple_New(3);
|
||||||
if (result != 0)
|
if (t1 == NULL || t2 == NULL)
|
||||||
return result;
|
return NULL;
|
||||||
if (PyObject_Cmp(v->stop, w->stop, &result) < 0)
|
|
||||||
return -2;
|
PyTuple_SET_ITEM(t1, 0, ((PySliceObject *)v)->start);
|
||||||
if (result != 0)
|
PyTuple_SET_ITEM(t1, 1, ((PySliceObject *)v)->stop);
|
||||||
return result;
|
PyTuple_SET_ITEM(t1, 2, ((PySliceObject *)v)->step);
|
||||||
if (PyObject_Cmp(v->step, w->step, &result) < 0)
|
PyTuple_SET_ITEM(t2, 0, ((PySliceObject *)w)->start);
|
||||||
return -2;
|
PyTuple_SET_ITEM(t2, 1, ((PySliceObject *)w)->stop);
|
||||||
return result;
|
PyTuple_SET_ITEM(t2, 2, ((PySliceObject *)w)->step);
|
||||||
|
|
||||||
|
res = PyObject_RichCompare(t1, t2, op);
|
||||||
|
|
||||||
|
PyTuple_SET_ITEM(t1, 0, NULL);
|
||||||
|
PyTuple_SET_ITEM(t1, 1, NULL);
|
||||||
|
PyTuple_SET_ITEM(t1, 2, NULL);
|
||||||
|
PyTuple_SET_ITEM(t2, 0, NULL);
|
||||||
|
PyTuple_SET_ITEM(t2, 1, NULL);
|
||||||
|
PyTuple_SET_ITEM(t2, 2, NULL);
|
||||||
|
|
||||||
|
Py_DECREF(t1);
|
||||||
|
Py_DECREF(t2);
|
||||||
|
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
static long
|
static long
|
||||||
|
@ -318,7 +348,7 @@ PyTypeObject PySlice_Type = {
|
||||||
0, /* tp_print */
|
0, /* tp_print */
|
||||||
0, /* tp_getattr */
|
0, /* tp_getattr */
|
||||||
0, /* tp_setattr */
|
0, /* tp_setattr */
|
||||||
(cmpfunc)slice_compare, /* tp_compare */
|
0, /* tp_compare */
|
||||||
(reprfunc)slice_repr, /* tp_repr */
|
(reprfunc)slice_repr, /* tp_repr */
|
||||||
0, /* tp_as_number */
|
0, /* tp_as_number */
|
||||||
0, /* tp_as_sequence */
|
0, /* tp_as_sequence */
|
||||||
|
@ -333,7 +363,7 @@ PyTypeObject PySlice_Type = {
|
||||||
slice_doc, /* tp_doc */
|
slice_doc, /* tp_doc */
|
||||||
0, /* tp_traverse */
|
0, /* tp_traverse */
|
||||||
0, /* tp_clear */
|
0, /* tp_clear */
|
||||||
0, /* tp_richcompare */
|
slice_richcompare, /* tp_richcompare */
|
||||||
0, /* tp_weaklistoffset */
|
0, /* tp_weaklistoffset */
|
||||||
0, /* tp_iter */
|
0, /* tp_iter */
|
||||||
0, /* tp_iternext */
|
0, /* tp_iternext */
|
||||||
|
|
|
@ -361,16 +361,6 @@ static PyGetSetDef type_getsets[] = {
|
||||||
{0}
|
{0}
|
||||||
};
|
};
|
||||||
|
|
||||||
static int
|
|
||||||
type_compare(PyObject *v, PyObject *w)
|
|
||||||
{
|
|
||||||
/* This is called with type objects only. So we
|
|
||||||
can just compare the addresses. */
|
|
||||||
Py_uintptr_t vv = (Py_uintptr_t)v;
|
|
||||||
Py_uintptr_t ww = (Py_uintptr_t)w;
|
|
||||||
return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
type_repr(PyTypeObject *type)
|
type_repr(PyTypeObject *type)
|
||||||
{
|
{
|
||||||
|
@ -2192,12 +2182,12 @@ PyTypeObject PyType_Type = {
|
||||||
0, /* tp_print */
|
0, /* tp_print */
|
||||||
0, /* tp_getattr */
|
0, /* tp_getattr */
|
||||||
0, /* tp_setattr */
|
0, /* tp_setattr */
|
||||||
type_compare, /* tp_compare */
|
0, /* tp_compare */
|
||||||
(reprfunc)type_repr, /* tp_repr */
|
(reprfunc)type_repr, /* tp_repr */
|
||||||
0, /* tp_as_number */
|
0, /* tp_as_number */
|
||||||
0, /* tp_as_sequence */
|
0, /* tp_as_sequence */
|
||||||
0, /* tp_as_mapping */
|
0, /* tp_as_mapping */
|
||||||
(hashfunc)_Py_HashPointer, /* tp_hash */
|
0, /* tp_hash */
|
||||||
(ternaryfunc)type_call, /* tp_call */
|
(ternaryfunc)type_call, /* tp_call */
|
||||||
0, /* tp_str */
|
0, /* tp_str */
|
||||||
(getattrofunc)type_getattro, /* tp_getattro */
|
(getattrofunc)type_getattro, /* tp_getattro */
|
||||||
|
@ -2301,6 +2291,30 @@ object_str(PyObject *self)
|
||||||
return f(self);
|
return f(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
object_richcompare(PyObject *self, PyObject *other, int op)
|
||||||
|
{
|
||||||
|
PyObject *res;
|
||||||
|
|
||||||
|
switch (op) {
|
||||||
|
|
||||||
|
case Py_EQ:
|
||||||
|
res = (self == other) ? Py_True : Py_False;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Py_NE:
|
||||||
|
res = (self != other) ? Py_True : Py_False;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
res = Py_NotImplemented;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
Py_INCREF(res);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
object_get_class(PyObject *self, void *closure)
|
object_get_class(PyObject *self, void *closure)
|
||||||
{
|
{
|
||||||
|
@ -2703,7 +2717,7 @@ PyTypeObject PyBaseObject_Type = {
|
||||||
PyDoc_STR("The most base type"), /* tp_doc */
|
PyDoc_STR("The most base type"), /* tp_doc */
|
||||||
0, /* tp_traverse */
|
0, /* tp_traverse */
|
||||||
0, /* tp_clear */
|
0, /* tp_clear */
|
||||||
0, /* tp_richcompare */
|
object_richcompare, /* tp_richcompare */
|
||||||
0, /* tp_weaklistoffset */
|
0, /* tp_weaklistoffset */
|
||||||
0, /* tp_iter */
|
0, /* tp_iter */
|
||||||
0, /* tp_iternext */
|
0, /* tp_iternext */
|
||||||
|
|
|
@ -184,7 +184,9 @@ weakref_repr(PyWeakReference *self)
|
||||||
static PyObject *
|
static PyObject *
|
||||||
weakref_richcompare(PyWeakReference* self, PyWeakReference* other, int op)
|
weakref_richcompare(PyWeakReference* self, PyWeakReference* other, int op)
|
||||||
{
|
{
|
||||||
if (op != Py_EQ || self->ob_type != other->ob_type) {
|
if ((op != Py_EQ && op != Py_NE) ||
|
||||||
|
!PyWeakref_Check(self) ||
|
||||||
|
!PyWeakref_Check(other)) {
|
||||||
Py_INCREF(Py_NotImplemented);
|
Py_INCREF(Py_NotImplemented);
|
||||||
return Py_NotImplemented;
|
return Py_NotImplemented;
|
||||||
}
|
}
|
||||||
|
@ -458,12 +460,12 @@ proxy_setattr(PyWeakReference *proxy, PyObject *name, PyObject *value)
|
||||||
return PyObject_SetAttr(PyWeakref_GET_OBJECT(proxy), name, value);
|
return PyObject_SetAttr(PyWeakref_GET_OBJECT(proxy), name, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static PyObject *
|
||||||
proxy_compare(PyObject *proxy, PyObject *v)
|
proxy_richcompare(PyObject *proxy, PyObject *v, int op)
|
||||||
{
|
{
|
||||||
UNWRAP_I(proxy);
|
UNWRAP(proxy);
|
||||||
UNWRAP_I(v);
|
UNWRAP(v);
|
||||||
return PyObject_Compare(proxy, v);
|
return PyObject_RichCompare(proxy, v, op);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* number slots */
|
/* number slots */
|
||||||
|
@ -649,7 +651,7 @@ _PyWeakref_ProxyType = {
|
||||||
0, /* tp_print */
|
0, /* tp_print */
|
||||||
0, /* tp_getattr */
|
0, /* tp_getattr */
|
||||||
0, /* tp_setattr */
|
0, /* tp_setattr */
|
||||||
proxy_compare, /* tp_compare */
|
0, /* tp_compare */
|
||||||
(reprfunc)proxy_repr, /* tp_repr */
|
(reprfunc)proxy_repr, /* tp_repr */
|
||||||
&proxy_as_number, /* tp_as_number */
|
&proxy_as_number, /* tp_as_number */
|
||||||
&proxy_as_sequence, /* tp_as_sequence */
|
&proxy_as_sequence, /* tp_as_sequence */
|
||||||
|
@ -664,7 +666,7 @@ _PyWeakref_ProxyType = {
|
||||||
0, /* tp_doc */
|
0, /* tp_doc */
|
||||||
(traverseproc)gc_traverse, /* tp_traverse */
|
(traverseproc)gc_traverse, /* tp_traverse */
|
||||||
(inquiry)gc_clear, /* tp_clear */
|
(inquiry)gc_clear, /* tp_clear */
|
||||||
0, /* tp_richcompare */
|
proxy_richcompare, /* tp_richcompare */
|
||||||
0, /* tp_weaklistoffset */
|
0, /* tp_weaklistoffset */
|
||||||
(getiterfunc)proxy_iter, /* tp_iter */
|
(getiterfunc)proxy_iter, /* tp_iter */
|
||||||
(iternextfunc)proxy_iternext, /* tp_iternext */
|
(iternextfunc)proxy_iternext, /* tp_iternext */
|
||||||
|
@ -683,7 +685,7 @@ _PyWeakref_CallableProxyType = {
|
||||||
0, /* tp_print */
|
0, /* tp_print */
|
||||||
0, /* tp_getattr */
|
0, /* tp_getattr */
|
||||||
0, /* tp_setattr */
|
0, /* tp_setattr */
|
||||||
proxy_compare, /* tp_compare */
|
0, /* tp_compare */
|
||||||
(unaryfunc)proxy_repr, /* tp_repr */
|
(unaryfunc)proxy_repr, /* tp_repr */
|
||||||
&proxy_as_number, /* tp_as_number */
|
&proxy_as_number, /* tp_as_number */
|
||||||
&proxy_as_sequence, /* tp_as_sequence */
|
&proxy_as_sequence, /* tp_as_sequence */
|
||||||
|
@ -698,7 +700,7 @@ _PyWeakref_CallableProxyType = {
|
||||||
0, /* tp_doc */
|
0, /* tp_doc */
|
||||||
(traverseproc)gc_traverse, /* tp_traverse */
|
(traverseproc)gc_traverse, /* tp_traverse */
|
||||||
(inquiry)gc_clear, /* tp_clear */
|
(inquiry)gc_clear, /* tp_clear */
|
||||||
0, /* tp_richcompare */
|
proxy_richcompare, /* tp_richcompare */
|
||||||
0, /* tp_weaklistoffset */
|
0, /* tp_weaklistoffset */
|
||||||
(getiterfunc)proxy_iter, /* tp_iter */
|
(getiterfunc)proxy_iter, /* tp_iter */
|
||||||
(iternextfunc)proxy_iternext, /* tp_iternext */
|
(iternextfunc)proxy_iternext, /* tp_iternext */
|
||||||
|
|
|
@ -1324,9 +1324,10 @@ get_len_of_range_longs(PyObject *lo, PyObject *hi, PyObject *step)
|
||||||
PyObject *tmp1 = NULL, *tmp2 = NULL, *tmp3 = NULL;
|
PyObject *tmp1 = NULL, *tmp2 = NULL, *tmp3 = NULL;
|
||||||
/* holds sub-expression evaluations */
|
/* holds sub-expression evaluations */
|
||||||
|
|
||||||
/* if (lo >= hi), return length of 0. */
|
/* If (lo >= hi), return length of 0 (or error). */
|
||||||
if (PyObject_Compare(lo, hi) >= 0)
|
n = PyObject_RichCompareBool(lo, hi, Py_LT);
|
||||||
return 0;
|
if (n <= 0)
|
||||||
|
return n;
|
||||||
|
|
||||||
if ((one = PyLong_FromLong(1L)) == NULL)
|
if ((one = PyLong_FromLong(1L)) == NULL)
|
||||||
goto Fail;
|
goto Fail;
|
||||||
|
@ -1378,7 +1379,7 @@ handle_range_longs(PyObject *self, PyObject *args)
|
||||||
PyObject *v = NULL;
|
PyObject *v = NULL;
|
||||||
long bign;
|
long bign;
|
||||||
int i, n;
|
int i, n;
|
||||||
int cmp_result;
|
int step_pos;
|
||||||
|
|
||||||
PyObject *zero = PyLong_FromLong(0);
|
PyObject *zero = PyLong_FromLong(0);
|
||||||
|
|
||||||
|
@ -1439,17 +1440,20 @@ handle_range_longs(PyObject *self, PyObject *args)
|
||||||
goto Fail;
|
goto Fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (PyObject_Cmp(istep, zero, &cmp_result) == -1)
|
step_pos = PyObject_RichCompareBool(istep, zero, Py_GT);
|
||||||
|
if (step_pos < 0)
|
||||||
goto Fail;
|
goto Fail;
|
||||||
if (cmp_result == 0) {
|
if (step_pos)
|
||||||
PyErr_SetString(PyExc_ValueError,
|
|
||||||
"range() step argument must not be zero");
|
|
||||||
goto Fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (cmp_result > 0)
|
|
||||||
bign = get_len_of_range_longs(ilow, ihigh, istep);
|
bign = get_len_of_range_longs(ilow, ihigh, istep);
|
||||||
else {
|
else {
|
||||||
|
int step_zero = PyObject_RichCompareBool(istep, zero, Py_EQ);
|
||||||
|
if (step_zero < 0)
|
||||||
|
goto Fail;
|
||||||
|
if (step_zero) {
|
||||||
|
PyErr_SetString(PyExc_ValueError,
|
||||||
|
"range() step argument must not be zero");
|
||||||
|
goto Fail;
|
||||||
|
}
|
||||||
PyObject *neg_istep = PyNumber_Negative(istep);
|
PyObject *neg_istep = PyNumber_Negative(istep);
|
||||||
if (neg_istep == NULL)
|
if (neg_istep == NULL)
|
||||||
goto Fail;
|
goto Fail;
|
||||||
|
|
Loading…
Reference in New Issue