Skip to content

Commit

Permalink
Tests and Python implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamadden committed Aug 2, 2018
1 parent 77193c4 commit 1e9f7c4
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 5 deletions.
30 changes: 27 additions & 3 deletions persistent/persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
import sys


from zope.interface import implementer

Expand All @@ -20,14 +20,13 @@
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

Expand Down Expand Up @@ -558,6 +557,31 @@ def _p_is_in_cache(self, jar=None):
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
Expand Down
112 changes: 110 additions & 2 deletions persistent/tests/test_persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
##############################################################################

import platform
import re
import sys
import unittest

Expand Down Expand Up @@ -1670,7 +1671,7 @@ class TestPersistent(self._getTargetClass()):
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)
Expand All @@ -1691,11 +1692,118 @@ class TestPersistent(self._getTargetClass()):
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):

Expand Down

0 comments on commit 1e9f7c4

Please sign in to comment.