Skip to content

Commit

Permalink
Merge d1ed3a4 into 133c163
Browse files Browse the repository at this point in the history
  • Loading branch information
jamadden committed Oct 26, 2018
2 parents 133c163 + d1ed3a4 commit 5e871e0
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 80 deletions.
7 changes: 5 additions & 2 deletions CHANGES.rst
Expand Up @@ -2,10 +2,13 @@
Changes
=========

4.2.1 (unreleased)
5.0.0 (unreleased)
==================

- Nothing changed yet.
- Remove ``__cmp__`` methods. Since the implementation of the rich
comparison methods (``__eq__``, etc) in 4.0a1, the interpreter won't
call ``__cmp__``, even on Python 2. See `issue 10
<https://github.com/zopefoundation/zope.keyreference/issues/10>`_.


4.2.0 (2018-10-26)
Expand Down
6 changes: 0 additions & 6 deletions src/zope/keyreference/interfaces.py
Expand Up @@ -49,12 +49,6 @@ def __hash__():
"""Get a unique identifier of the referenced object.
"""

def __cmp__(ref):
"""Compare the reference to another reference.
BBB for Python 2.
"""

def __eq__(ref):
"KeyReferences must be totally orderable."

Expand Down
14 changes: 2 additions & 12 deletions src/zope/keyreference/persistent.py
Expand Up @@ -21,6 +21,7 @@

import zope.keyreference.interfaces


@zope.interface.implementer(zope.keyreference.interfaces.IKeyReference)
class KeyReferenceToPersistent(object):
"""An IKeyReference for persistent objects which is comparable.
Expand Down Expand Up @@ -95,18 +96,6 @@ def _get_cmp_keys(self, other):

return self.key_type_id, other.key_type_id

# Py3: For Python 2 BBB.
# If we implement all the rich comparison operations, though, this is
# never actually called.
def __cmp__(self, other):
my_keys, other_keys = self._get_cmp_keys(other)
if my_keys == other_keys:
return 0
if my_keys > other_keys:
return 1
return -1


def __eq__(self, other):
a, b = self._get_cmp_keys(other)
return a == b
Expand Down Expand Up @@ -148,6 +137,7 @@ def connectionOfPersistent(ob):
return None
return cur._p_jar


# BBB: If zope.app.keyreference is not installed, we still want
# old key references to be available. So fake a module to make
# them unpickleable.
Expand Down
4 changes: 2 additions & 2 deletions src/zope/keyreference/persistent.txt
Expand Up @@ -130,10 +130,10 @@ We can't get the key reference for an object that hasn't been saved
yet:

>>> KeyReferenceToPersistent(PersistentMapping())
... # doctest: +ELLIPSIS
...
Traceback (most recent call last):
...
NotYet: ...
zope.keyreference.interfaces.NotYet: ...

Note that we get a NotYet error. This indicates that we might be able
to get a key reference later.
Expand Down
12 changes: 1 addition & 11 deletions src/zope/keyreference/testing.py
Expand Up @@ -19,6 +19,7 @@
import zope.component
import zope.keyreference.interfaces


@zope.component.adapter(zope.interface.Interface)
@zope.interface.implementer(zope.keyreference.interfaces.IKeyReference)
class SimpleKeyReference(object):
Expand All @@ -44,17 +45,6 @@ def _get_cmp_keys(self, other):

return self.key_type_id, other.key_type_id

# Py3: For Python 2 BBB.
# If we implement all the rich comparison operations, though, this is
# never actually called.
def __cmp__(self, other):
my_keys, other_keys = self._get_cmp_keys(other)
if my_keys == other_keys:
return 0
if my_keys > other_keys:
return 1
return -1

def __eq__(self, other):
a, b = self._get_cmp_keys(other)
return a == b
Expand Down
86 changes: 40 additions & 46 deletions src/zope/keyreference/tests.py
Expand Up @@ -14,7 +14,6 @@
"""Tests for the unique id utility.
"""
import doctest
import re
import unittest
from zope.testing import renormalizing

Expand All @@ -41,99 +40,94 @@ def __hash__(self):
return hash(self._p_oid)


class TestKeyReferenceToPersistent(unittest.TestCase):
class TestSimpleKeyReference(unittest.TestCase):

def makeOne(self, obj):
from zope.keyreference.persistent import KeyReferenceToPersistent
return KeyReferenceToPersistent(obj)
from zope.keyreference.testing import SimpleKeyReference
return SimpleKeyReference(obj)

def test__cmp__(self):
def test_comparisons(self):

persistent = MockPersistent()

eq1 = self.makeOne(persistent)
eq2 = self.makeOne(persistent)

self.assertEqual(eq1, eq2)
self.assertEqual(0, eq1.__cmp__(eq2))
self.assertEqual(0, eq1.__cmp__(eq1))
self.assertEqual(0, eq2.__cmp__(eq1))
self.assertEqual(eq2, eq1)

# But if they have different key_type_id, they
# only compare based on that.
eq2.key_type_id = 'aaa'
self.assertNotEqual(eq1, eq2)
self.assertNotEqual(eq2, eq1)

persistent_gt = MockPersistent()
persistent_gt._p_oid = 2

gt = self.makeOne(persistent_gt)
__traceback_info__ = hash(gt), hash(eq1), hash(persistent._p_oid)

self.assertGreater(gt, eq1)
self.assertGreaterEqual(gt, eq1)
self.assertNotEqual(gt, eq1)
self.assertEqual(1, gt.__cmp__(eq1))

self.assertLess(eq1, gt)
self.assertLessEqual(eq1, gt)
self.assertEqual(-1, eq1.__cmp__(gt))


def test__call__(self):
persistent = MockPersistent()
obj = self.makeOne(persistent)

self.assertIs(persistent, obj())

class TestSimpleKeyReference(TestKeyReferenceToPersistent):
class TestKeyReferenceToPersistent(TestSimpleKeyReference):

def makeOne(self, obj):
from zope.keyreference.testing import SimpleKeyReference
return SimpleKeyReference(obj)


checker = renormalizing.RENormalizing([
# Python 3 adds module name to exceptions.
(re.compile("zope.keyreference.interfaces.NotYet"),
r"NotYet"),
])

from zope.keyreference.persistent import KeyReferenceToPersistent
return KeyReferenceToPersistent(obj)

def test_multi_databases():
"""
>>> from ZODB.MappingStorage import DB
>>> import transaction
>>> from BTrees.OOBTree import OOBucket

>>> databases = {}
def test_multi_databases(self):
from ZODB.MappingStorage import DB
import transaction
from BTrees.OOBTree import OOBucket

>>> db1 = DB(databases=databases, database_name='1')
>>> db2 = DB(databases=databases, database_name='2')
databases = {}

>>> conn1 = db1.open()
>>> conn1.root()['ob'] = OOBucket()
db1 = DB(databases=databases, database_name='1')
db2 = DB(databases=databases, database_name='2')

>>> conn2 = conn1.get_connection('2')
>>> conn2.root()['ob'] = OOBucket()
conn1 = db1.open()
conn1.root()['ob'] = OOBucket()

>>> conn1.root()['ob']._p_oid == conn2.root()['ob']._p_oid
True
conn2 = conn1.get_connection('2')
self.assertEqual(conn2.db(), db2)
conn2.root()['ob'] = OOBucket()

>>> transaction.commit()
self.assertEqual(conn1.root()['ob']._p_oid,
conn2.root()['ob']._p_oid)

>>> from zope.keyreference.persistent import KeyReferenceToPersistent
transaction.commit()

>>> key1 = KeyReferenceToPersistent(conn1.root()['ob'])
>>> key2 = KeyReferenceToPersistent(conn2.root()['ob'])
key1 = self.makeOne(conn1.root()['ob'])
key2 = self.makeOne(conn2.root()['ob'])

>>> key1 != key2, key2 > key1, hash(key1) != hash(key2)
(True, True, True)
self.assertNotEqual(key1, key2)
self.assertGreater(key2, key1)
self.assertNotEqual(hash(key1), hash(key2))

"""

def test_suite():
doctest_flags = (
doctest.NORMALIZE_WHITESPACE
| doctest.ELLIPSIS
| renormalizing.IGNORE_EXCEPTION_MODULE_IN_PYTHON2
)
return unittest.TestSuite((
doctest.DocFileSuite('persistent.txt', checker=checker),
doctest.DocTestSuite(),
doctest.DocFileSuite(
'persistent.txt',
optionflags=doctest_flags,
checker=renormalizing.RENormalizing(),
),
unittest.defaultTestLoader.loadTestsFromName(__name__),
))
2 changes: 1 addition & 1 deletion version.txt
@@ -1 +1 @@
4.2.1.dev0
5.0.0.dev0

0 comments on commit 5e871e0

Please sign in to comment.