Commit 1e9f7c42 authored by Jason Madden's avatar Jason Madden

Tests and Python implementation.

parent 77193c4a
......@@ -11,7 +11,7 @@
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
import sys
from zope.interface import implementer
......@@ -20,14 +20,13 @@ from persistent.interfaces import GHOST
from persistent.interfaces import UPTODATE
from persistent.interfaces import CHANGED
from persistent.interfaces import STICKY
from persistent.interfaces import OID_TYPE
from persistent.interfaces import SERIAL_TYPE
from persistent.timestamp import TimeStamp
from persistent.timestamp import _ZERO
from persistent._compat import copy_reg
from persistent._compat import intern
from . import ring
_INITIAL_SERIAL = _ZERO
......@@ -558,6 +557,31 @@ class Persistent(object):
if cache is not None:
return cache.get(oid) is self
def __repr__(self):
oid = _OGA(self, '_Persistent__oid')
jar = _OGA(self, '_Persistent__jar')
oid_str = ''
jar_str = ''
if oid is not None:
try:
oid_str = ' oid %r' % (oid,)
except Exception as e:
oid_str = ' oid %r' % (e,)
if jar is not None:
try:
jar_str = ' in %r' % (jar,)
except Exception as e:
jar_str = ' in %r' % (e,)
return '<%s.%s object at 0x%x%s%s>' % (
type(self).__module__, type(self).__name__, id(self),
oid_str, jar_str
)
def _estimated_size_in_24_bits(value):
if value > 1073741696:
return 16777215
......
......@@ -13,6 +13,7 @@
##############################################################################
import platform
import re
import sys
import unittest
......@@ -1670,7 +1671,7 @@ class _Persistent_Base(object):
self.assertEqual(candidate._p_state, UPTODATE)
cache.new_ghost(KEY, candidate)
self.assertTrue(cache.get(KEY) is candidate)
self.assertIs(cache.get(KEY), candidate)
self.assertEqual(candidate._p_oid, KEY)
self.assertEqual(candidate._p_state, GHOST)
self.assertEqual(candidate.set_by_new, 1)
......@@ -1691,11 +1692,118 @@ class _Persistent_Base(object):
self.assertEqual(candidate._p_state, UPTODATE)
cache.new_ghost(KEY, candidate)
self.assertTrue(cache.get(KEY) is candidate)
self.assertIs(cache.get(KEY), candidate)
self.assertEqual(candidate._p_oid, KEY)
self.assertEqual(candidate._p_state, GHOST)
self.assertEqual(candidate.set_by_new, 1)
def _normalize_repr(self, r):
# Pure-python vs C
r = r.replace('persistent.persistence.Persistent', 'persistent.Persistent')
# addresses
r = re.sub(r'0x[0-9a-fA-F]*', '0xdeadbeef', r)
# Python 3.7 removed the trailing , in exception reprs
r = r.replace("',)", "')")
# Python 2 doesn't have a leading b prefix for byte literals
r = r.replace("oid '", "oid b'")
return r
def _normalized_repr(self, o):
return self._normalize_repr(repr(o))
def test_repr_no_oid_no_jar(self):
p = self._makeOne()
result = self._normalized_repr(p)
self.assertEqual(result, '<persistent.Persistent object at 0xdeadbeef>')
def test_repr_no_oid_in_jar(self):
p = self._makeOne()
class Jar(object):
def __repr__(self):
return '<SomeJar>'
p._p_jar = Jar()
result = self._normalized_repr(p)
self.assertEqual(
result,
"<persistent.Persistent object at 0xdeadbeef in <SomeJar>>")
def test_repr_oid_no_jar(self):
p = self._makeOne()
p._p_oid = b'12345678'
result = self._normalized_repr(p)
self.assertEqual(
result,
"<persistent.Persistent object at 0xdeadbeef oid b'12345678'>")
def test_repr_no_oid_repr_jar_raises_exception(self):
p = self._makeOne()
class Jar(object):
def __repr__(self):
raise Exception('jar repr failed')
p._p_jar = Jar()
result = self._normalized_repr(p)
self.assertEqual(
result,
"<persistent.Persistent object at 0xdeadbeef in Exception('jar repr failed')>")
def test_repr_oid_raises_exception_no_jar(self):
p = self._makeOne()
class BadOID(bytes):
def __repr__(self):
raise Exception("oid repr failed")
p._p_oid = BadOID(b'12345678')
result = self._normalized_repr(p)
self.assertEqual(
result,
"<persistent.Persistent object at 0xdeadbeef oid Exception('oid repr failed')>")
def test_repr_oid_and_jar_raise_exception(self):
p = self._makeOne()
class BadOID(bytes):
def __repr__(self):
raise Exception("oid repr failed")
p._p_oid = BadOID(b'12345678')
class Jar(object):
def __repr__(self):
raise Exception('jar repr failed')
p._p_jar = Jar()
result = self._normalized_repr(p)
self.assertEqual(
result,
"<persistent.Persistent object at 0xdeadbeef oid Exception('oid repr failed')"
" in Exception('jar repr failed')>")
def test_repr_oid_and_jar(self):
p = self._makeOne()
p._p_oid = b'12345678'
class Jar(object):
def __repr__(self):
return '<SomeJar>'
p._p_jar = Jar()
result = self._normalized_repr(p)
self.assertEqual(result,
"<persistent.Persistent object at 0xdeadbeef oid b'12345678' in <SomeJar>>")
class PyPersistentTests(unittest.TestCase, _Persistent_Base):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment