Skip to content

Commit

Permalink
Always represent byte-based oids as hex.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rotonen committed Oct 4, 2018
1 parent 598f9ab commit 78e0eec
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
4 changes: 3 additions & 1 deletion persistent/cPersistence.c
Expand Up @@ -1423,6 +1423,8 @@ Per_repr(cPersistentObject *self)
PyObject *prepr = NULL;
PyObject *prepr_exc_str = NULL;

PyObject *bytes_hex(PyObject *bytes);

PyObject *oid_str = NULL;
PyObject *jar_str = NULL;
PyObject *result = NULL;
Expand All @@ -1446,7 +1448,7 @@ Per_repr(cPersistentObject *self)
prepr_exc_str = PyUnicode_FromString("");
}

oid_str = repr_helper(self->oid, " oid %R");
oid_str = repr_helper(bytes_hex(self->oid), " oid 0x%s");
if (!oid_str)
goto cleanup;

Expand Down
5 changes: 4 additions & 1 deletion persistent/persistence.py
Expand Up @@ -574,7 +574,10 @@ def __repr__(self):

if oid is not None:
try:
oid_str = ' oid %r' % (oid,)
if isinstance(oid, bytes):
oid_str = ' oid 0x%s' % (oid.encode('hex'), )
else:
oid_str = ' oid %r' % (oid, )
except Exception as e:
oid_str = ' oid %r' % (e,)

Expand Down
33 changes: 24 additions & 9 deletions persistent/tests/test_persistence.py
Expand Up @@ -1702,11 +1702,11 @@ def _normalize_repr(self, r):
r = r.replace('persistent.persistence.Persistent', 'persistent.Persistent')
r = r.replace("persistent.tests.test_persistence.", '')
# addresses
r = re.sub(r'0x[0-9a-fA-F]*', '0xdeadbeef', r)
r = re.sub(r'at 0x[0-9a-fA-F]*', 'at 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'")
# r = r.replace("oid '", "oid b'")
return r

def _normalized_repr(self, o):
Expand Down Expand Up @@ -1738,7 +1738,7 @@ def test_repr_oid_no_jar(self):
result = self._normalized_repr(p)
self.assertEqual(
result,
"<persistent.Persistent object at 0xdeadbeef oid b'12345678'>")
"<persistent.Persistent object at 0xdeadbeef oid 0x3132333435363738>")

def test_repr_no_oid_repr_jar_raises_exception(self):
p = self._makeOne()
Expand All @@ -1758,7 +1758,7 @@ def __repr__(self):
def test_repr_oid_raises_exception_no_jar(self):
p = self._makeOne()

class BadOID(bytes):
class BadOID(int):
def __repr__(self):
raise Exception("oid repr failed")
p._p_oid = BadOID(b'12345678')
Expand All @@ -1772,7 +1772,7 @@ def __repr__(self):
def test_repr_oid_and_jar_raise_exception(self):
p = self._makeOne()

class BadOID(bytes):
class BadOID(int):
def __repr__(self):
raise Exception("oid repr failed")
p._p_oid = BadOID(b'12345678')
Expand Down Expand Up @@ -1804,7 +1804,7 @@ def __repr__(self):
def test_repr_oid_raises_baseexception_no_jar(self):
p = self._makeOne()

class BadOID(bytes):
class BadOID(int):
def __repr__(self):
raise BaseException("oid repr failed")
p._p_oid = BadOID(b'12345678')
Expand All @@ -1825,7 +1825,22 @@ def __repr__(self):
result = self._normalized_repr(p)
self.assertEqual(
result,
"<persistent.Persistent object at 0xdeadbeef oid b'12345678' in <SomeJar>>")
"<persistent.Persistent object at 0xdeadbeef oid 0x3132333435363738 in <SomeJar>>")

def test_repr_oid_and_jar_with_nonbytes_oid(self):
p = self._makeOne()
p._p_oid = 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 12345678 in <SomeJar>>")

def test__p_repr(self):
class P(self._getTargetClass()):
Expand All @@ -1849,7 +1864,7 @@ def _p_repr(self):
result = self._normalized_repr(p)
self.assertEqual(
result,
"<P object at 0xdeadbeef oid b'12345678'"
"<P object at 0xdeadbeef oid 0x3132333435363738"
" _p_repr Exception('_p_repr failed')>")

class Jar(object):
Expand All @@ -1860,7 +1875,7 @@ def __repr__(self):
result = self._normalized_repr(p)
self.assertEqual(
result,
"<P object at 0xdeadbeef oid b'12345678'"
"<P object at 0xdeadbeef oid 0x3132333435363738"
" in <SomeJar> _p_repr Exception('_p_repr failed')>")

def test__p_repr_in_instance_ignored(self):
Expand Down

0 comments on commit 78e0eec

Please sign in to comment.