Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
persistent
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
persistent
Commits
199da4f5
Commit
199da4f5
authored
Oct 22, 2018
by
Jason Madden
Committed by
GitHub
Oct 22, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #98 from zopefoundation/issue97
Always format 8-byte oids as ints in hexadecimal.
parents
598f9ab7
db0234c7
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
132 additions
and
30 deletions
+132
-30
CHANGES.rst
CHANGES.rst
+9
-2
persistent/cPersistence.c
persistent/cPersistence.c
+60
-6
persistent/persistence.py
persistent/persistence.py
+13
-3
persistent/tests/test_persistence.py
persistent/tests/test_persistence.py
+50
-19
No files found.
CHANGES.rst
View file @
199da4f5
...
@@ -4,8 +4,15 @@
...
@@ -4,8 +4,15 @@
4.4.3 (unreleased)
4.4.3 (unreleased)
------------------
------------------
- Nothing changed yet.
- Fix the repr of the persistent objects to include the module name
when using the C extension. This matches the pure-Python behaviour
and the behaviour prior to 4.4.0. See `issue 92
<https://github.com/zopefoundation/persistent/issues/92>`_.
- Change the repr of persistent objects to format the OID as in
integer in hexadecimal notation if it is an 8-byte byte string, as
ZODB does. This eliminates some issues in doctests. See `issue 95
<https://github.com/zopefoundation/persistent/pull/95>`_.
4.4.2 (2018-08-28)
4.4.2 (2018-08-28)
------------------
------------------
...
...
persistent/cPersistence.c
View file @
199da4f5
...
@@ -24,6 +24,16 @@ struct ccobject_head_struct
...
@@ -24,6 +24,16 @@ struct ccobject_head_struct
CACHE_HEAD
CACHE_HEAD
};
};
/*
The compiler on Windows used for Python 2.7 doesn't include
stdint.h.
*/
#if !defined(PY3K) && defined(_WIN32)
typedef
unsigned
long
long
uint64_t
;
#else
#include <stdint.h>
#endif
/* These two objects are initialized when the module is loaded */
/* These two objects are initialized when the module is loaded */
static
PyObject
*
TimeStamp
,
*
py_simple_new
;
static
PyObject
*
TimeStamp
,
*
py_simple_new
;
...
@@ -1423,10 +1433,16 @@ Per_repr(cPersistentObject *self)
...
@@ -1423,10 +1433,16 @@ Per_repr(cPersistentObject *self)
PyObject
*
prepr
=
NULL
;
PyObject
*
prepr
=
NULL
;
PyObject
*
prepr_exc_str
=
NULL
;
PyObject
*
prepr_exc_str
=
NULL
;
PyObject
*
module
=
NULL
;
PyObject
*
name
=
NULL
;
PyObject
*
oid_str
=
NULL
;
PyObject
*
oid_str
=
NULL
;
PyObject
*
jar_str
=
NULL
;
PyObject
*
jar_str
=
NULL
;
PyObject
*
result
=
NULL
;
PyObject
*
result
=
NULL
;
unsigned
char
*
oid_bytes
;
char
buf
[
20
];
uint64_t
oid_value
;
prepr
=
PyObject_GetAttrString
((
PyObject
*
)
Py_TYPE
(
self
),
"_p_repr"
);
prepr
=
PyObject_GetAttrString
((
PyObject
*
)
Py_TYPE
(
self
),
"_p_repr"
);
if
(
prepr
)
if
(
prepr
)
{
{
...
@@ -1446,23 +1462,61 @@ Per_repr(cPersistentObject *self)
...
@@ -1446,23 +1462,61 @@ Per_repr(cPersistentObject *self)
prepr_exc_str
=
PyUnicode_FromString
(
""
);
prepr_exc_str
=
PyUnicode_FromString
(
""
);
}
}
if
(
self
->
oid
&&
PyBytes_Check
(
self
->
oid
)
&&
PyBytes_GET_SIZE
(
self
->
oid
)
==
8
)
{
oid_bytes
=
(
unsigned
char
*
)
PyBytes_AS_STRING
(
self
->
oid
);
oid_value
=
((
uint64_t
)
oid_bytes
[
0
]
<<
56
)
|
((
uint64_t
)
oid_bytes
[
1
]
<<
48
)
|
((
uint64_t
)
oid_bytes
[
2
]
<<
40
)
|
((
uint64_t
)
oid_bytes
[
3
]
<<
32
)
|
((
uint64_t
)
oid_bytes
[
4
]
<<
24
)
|
((
uint64_t
)
oid_bytes
[
5
]
<<
16
)
|
((
uint64_t
)
oid_bytes
[
6
]
<<
8
)
|
((
uint64_t
)
oid_bytes
[
7
]);
/*
Python's PyUnicode_FromFormat doesn't understand the ll
length modifier for %x, so to format a 64-bit value we need to
use stdio.
*/
snprintf
(
buf
,
sizeof
(
buf
)
-
1
,
"%llx"
,
oid_value
);
oid_str
=
PyUnicode_FromFormat
(
" oid 0x%s"
,
buf
);
}
if
(
!
oid_str
)
{
oid_str
=
repr_helper
(
self
->
oid
,
" oid %R"
);
oid_str
=
repr_helper
(
self
->
oid
,
" oid %R"
);
if
(
!
oid_str
)
if
(
!
oid_str
)
goto
cleanup
;
goto
cleanup
;
}
jar_str
=
repr_helper
(
self
->
jar
,
" in %R"
);
jar_str
=
repr_helper
(
self
->
jar
,
" in %R"
);
if
(
!
jar_str
)
if
(
!
jar_str
)
goto
cleanup
;
goto
cleanup
;
module
=
PyObject_GetAttrString
((
PyObject
*
)
Py_TYPE
(
self
),
"__module__"
);
name
=
PyObject_GetAttrString
((
PyObject
*
)
Py_TYPE
(
self
),
"__name__"
);
if
(
!
module
||
!
name
)
{
/*
Some error retrieving __module__ or __name__. Ignore it, use the
C data.
*/
PyErr_Clear
();
result
=
PyUnicode_FromFormat
(
"<%s object at %p%S%S%S>"
,
result
=
PyUnicode_FromFormat
(
"<%s object at %p%S%S%S>"
,
Py_TYPE
(
self
)
->
tp_name
,
self
,
Py_TYPE
(
self
)
->
tp_name
,
self
,
oid_str
,
jar_str
,
prepr_exc_str
);
oid_str
,
jar_str
,
prepr_exc_str
);
}
else
{
result
=
PyUnicode_FromFormat
(
"<%S.%S object at %p%S%S%S>"
,
module
,
name
,
self
,
oid_str
,
jar_str
,
prepr_exc_str
);
}
cleanup:
cleanup:
Py_XDECREF
(
prepr
);
Py_XDECREF
(
prepr
);
Py_XDECREF
(
prepr_exc_str
);
Py_XDECREF
(
prepr_exc_str
);
Py_XDECREF
(
oid_str
);
Py_XDECREF
(
oid_str
);
Py_XDECREF
(
jar_str
);
Py_XDECREF
(
jar_str
);
Py_XDECREF
(
name
);
Py_XDECREF
(
module
);
return
result
;
return
result
;
}
}
...
...
persistent/persistence.py
View file @
199da4f5
...
@@ -11,7 +11,7 @@
...
@@ -11,7 +11,7 @@
# FOR A PARTICULAR PURPOSE.
# FOR A PARTICULAR PURPOSE.
#
#
##############################################################################
##############################################################################
import
struct
from
zope.interface
import
implementer
from
zope.interface
import
implementer
...
@@ -52,6 +52,11 @@ SPECIAL_NAMES = ('__class__',
...
@@ -52,6 +52,11 @@ SPECIAL_NAMES = ('__class__',
# check in __getattribute__
# check in __getattribute__
_SPECIAL_NAMES
=
set
(
SPECIAL_NAMES
)
_SPECIAL_NAMES
=
set
(
SPECIAL_NAMES
)
# Represent 8-byte OIDs as hex integer, just like
# ZODB does.
_OID_STRUCT
=
struct
.
Struct
(
'>Q'
)
_OID_UNPACK
=
_OID_STRUCT
.
unpack
@
implementer
(
IPersistent
)
@
implementer
(
IPersistent
)
class
Persistent
(
object
):
class
Persistent
(
object
):
""" Pure Python implmentation of Persistent base class
""" Pure Python implmentation of Persistent base class
...
@@ -574,6 +579,9 @@ class Persistent(object):
...
@@ -574,6 +579,9 @@ class Persistent(object):
if
oid
is
not
None
:
if
oid
is
not
None
:
try
:
try
:
if
isinstance
(
oid
,
bytes
)
and
len
(
oid
)
==
8
:
oid_str
=
' oid 0x%x'
%
(
_OID_UNPACK
(
oid
)[
0
],)
else
:
oid_str
=
' oid %r'
%
(
oid
,)
oid_str
=
' oid %r'
%
(
oid
,)
except
Exception
as
e
:
except
Exception
as
e
:
oid_str
=
' oid %r'
%
(
e
,)
oid_str
=
' oid %r'
%
(
e
,)
...
@@ -585,7 +593,9 @@ class Persistent(object):
...
@@ -585,7 +593,9 @@ class Persistent(object):
jar_str
=
' in %r'
%
(
e
,)
jar_str
=
' in %r'
%
(
e
,)
return
'<%s.%s object at 0x%x%s%s%s>'
%
(
return
'<%s.%s object at 0x%x%s%s%s>'
%
(
type
(
self
).
__module__
,
type
(
self
).
__name__
,
id
(
self
),
# Match the C name for this exact class
type
(
self
).
__module__
if
type
(
self
)
is
not
Persistent
else
'persistent'
,
type
(
self
).
__name__
,
id
(
self
),
oid_str
,
jar_str
,
p_repr_str
oid_str
,
jar_str
,
p_repr_str
)
)
...
...
persistent/tests/test_persistence.py
View file @
199da4f5
...
@@ -1697,16 +1697,16 @@ class _Persistent_Base(object):
...
@@ -1697,16 +1697,16 @@ class _Persistent_Base(object):
self
.
assertEqual
(
candidate
.
_p_state
,
GHOST
)
self
.
assertEqual
(
candidate
.
_p_state
,
GHOST
)
self
.
assertEqual
(
candidate
.
set_by_new
,
1
)
self
.
assertEqual
(
candidate
.
set_by_new
,
1
)
# The number 12345678 as a p64, 8-byte string
_PACKED_OID
=
b'
\
x00
\
x00
\
x00
\
x00
\
x00
\
xbc
aN'
# The number 12345678 printed in hex
_HEX_OID
=
'0xbc614e'
def
_normalize_repr
(
self
,
r
):
def
_normalize_repr
(
self
,
r
):
# Pure-python vs C
r
=
r
.
replace
(
'persistent.persistence.Persistent'
,
'persistent.Persistent'
)
r
=
r
.
replace
(
"persistent.tests.test_persistence."
,
''
)
# addresses
# 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
# Python 3.7 removed the trailing , in exception reprs
r
=
r
.
replace
(
"',)"
,
"')"
)
r
=
r
.
replace
(
"',)"
,
"')"
)
# Python 2 doesn't have a leading b prefix for byte literals
r
=
r
.
replace
(
"oid '"
,
"oid b'"
)
return
r
return
r
def
_normalized_repr
(
self
,
o
):
def
_normalized_repr
(
self
,
o
):
...
@@ -1733,12 +1733,27 @@ class _Persistent_Base(object):
...
@@ -1733,12 +1733,27 @@ class _Persistent_Base(object):
def
test_repr_oid_no_jar
(
self
):
def
test_repr_oid_no_jar
(
self
):
p
=
self
.
_makeOne
()
p
=
self
.
_makeOne
()
p
.
_p_oid
=
b'12345678'
p
.
_p_oid
=
self
.
_PACKED_OID
result
=
self
.
_normalized_repr
(
p
)
result
=
self
.
_normalized_repr
(
p
)
self
.
assertEqual
(
self
.
assertEqual
(
result
,
result
,
"<persistent.Persistent object at 0xdeadbeef oid b'12345678'>"
)
"<persistent.Persistent object at 0xdeadbeef oid "
+
self
.
_HEX_OID
+
">"
)
def
test_64bit_oid
(
self
):
import
struct
p
=
self
.
_makeOne
()
oid_value
=
2
<<
62
self
.
assertEqual
(
oid_value
.
bit_length
(),
64
)
oid
=
struct
.
pack
(
">Q"
,
oid_value
)
self
.
assertEqual
(
oid
,
b'
\
x80
\
x00
\
x00
\
x00
\
x00
\
x00
\
x00
\
x00
'
)
p
.
_p_oid
=
oid
result
=
self
.
_normalized_repr
(
p
)
self
.
assertEqual
(
result
,
'<persistent.Persistent object at 0xdeadbeef oid 0x8000000000000000>'
)
def
test_repr_no_oid_repr_jar_raises_exception
(
self
):
def
test_repr_no_oid_repr_jar_raises_exception
(
self
):
p
=
self
.
_makeOne
()
p
=
self
.
_makeOne
()
...
@@ -1761,7 +1776,17 @@ class _Persistent_Base(object):
...
@@ -1761,7 +1776,17 @@ class _Persistent_Base(object):
class
BadOID
(
bytes
):
class
BadOID
(
bytes
):
def
__repr__
(
self
):
def
__repr__
(
self
):
raise
Exception
(
"oid repr failed"
)
raise
Exception
(
"oid repr failed"
)
p
.
_p_oid
=
BadOID
(
b'12345678'
)
# Our OID is bytes, 8 bytes long. We don't call its repr.
p
.
_p_oid
=
BadOID
(
self
.
_PACKED_OID
)
result
=
self
.
_normalized_repr
(
p
)
self
.
assertEqual
(
result
,
"<persistent.Persistent object at 0xdeadbeef oid "
+
self
.
_HEX_OID
+
">"
)
# Anything other than 8 bytes, though, we do.
p
.
_p_oid
=
BadOID
(
b'1234567'
)
result
=
self
.
_normalized_repr
(
p
)
result
=
self
.
_normalized_repr
(
p
)
self
.
assertEqual
(
self
.
assertEqual
(
...
@@ -1775,7 +1800,7 @@ class _Persistent_Base(object):
...
@@ -1775,7 +1800,7 @@ class _Persistent_Base(object):
class
BadOID
(
bytes
):
class
BadOID
(
bytes
):
def
__repr__
(
self
):
def
__repr__
(
self
):
raise
Exception
(
"oid repr failed"
)
raise
Exception
(
"oid repr failed"
)
p
.
_p_oid
=
BadOID
(
b'1234567
8
'
)
p
.
_p_oid
=
BadOID
(
b'1234567'
)
class
Jar
(
object
):
class
Jar
(
object
):
def
__repr__
(
self
):
def
__repr__
(
self
):
...
@@ -1809,12 +1834,17 @@ class _Persistent_Base(object):
...
@@ -1809,12 +1834,17 @@ class _Persistent_Base(object):
raise
BaseException
(
"oid repr failed"
)
raise
BaseException
(
"oid repr failed"
)
p
.
_p_oid
=
BadOID
(
b'12345678'
)
p
.
_p_oid
=
BadOID
(
b'12345678'
)
# An 8 byte byte string doesn't have repr called.
repr
(
p
)
# Anything other does.
p
.
_p_oid
=
BadOID
(
b'1234567'
)
with
self
.
assertRaisesRegex
(
BaseException
,
'oid repr failed'
):
with
self
.
assertRaisesRegex
(
BaseException
,
'oid repr failed'
):
repr
(
p
)
repr
(
p
)
def
test_repr_oid_and_jar
(
self
):
def
test_repr_oid_and_jar
(
self
):
p
=
self
.
_makeOne
()
p
=
self
.
_makeOne
()
p
.
_p_oid
=
b'12345678'
p
.
_p_oid
=
self
.
_PACKED_OID
class
Jar
(
object
):
class
Jar
(
object
):
def
__repr__
(
self
):
def
__repr__
(
self
):
...
@@ -1825,7 +1855,7 @@ class _Persistent_Base(object):
...
@@ -1825,7 +1855,7 @@ class _Persistent_Base(object):
result
=
self
.
_normalized_repr
(
p
)
result
=
self
.
_normalized_repr
(
p
)
self
.
assertEqual
(
self
.
assertEqual
(
result
,
result
,
"<persistent.Persistent object at 0xdeadbeef oid
b'12345678'
in <SomeJar>>"
)
"<persistent.Persistent object at 0xdeadbeef oid
"
+
self
.
_HEX_OID
+
"
in <SomeJar>>"
)
def
test__p_repr
(
self
):
def
test__p_repr
(
self
):
class
P
(
self
.
_getTargetClass
()):
class
P
(
self
.
_getTargetClass
()):
...
@@ -1842,15 +1872,15 @@ class _Persistent_Base(object):
...
@@ -1842,15 +1872,15 @@ class _Persistent_Base(object):
result
=
self
.
_normalized_repr
(
p
)
result
=
self
.
_normalized_repr
(
p
)
self
.
assertEqual
(
self
.
assertEqual
(
result
,
result
,
"<P object at 0xdeadbeef"
"<
persistent.tests.test_persistence.
P object at 0xdeadbeef"
" _p_repr Exception('_p_repr failed')>"
)
" _p_repr Exception('_p_repr failed')>"
)
p
.
_p_oid
=
b'12345678'
p
.
_p_oid
=
self
.
_PACKED_OID
result
=
self
.
_normalized_repr
(
p
)
result
=
self
.
_normalized_repr
(
p
)
self
.
assertEqual
(
self
.
assertEqual
(
result
,
result
,
"<
P object at 0xdeadbeef oid b'12345678'"
"<
persistent.tests.test_persistence.P object at 0xdeadbeef oid "
+
self
.
_HEX_OID
" _p_repr Exception('_p_repr failed')>"
)
+
" _p_repr Exception('_p_repr failed')>"
)
class
Jar
(
object
):
class
Jar
(
object
):
def
__repr__
(
self
):
def
__repr__
(
self
):
...
@@ -1860,8 +1890,8 @@ class _Persistent_Base(object):
...
@@ -1860,8 +1890,8 @@ class _Persistent_Base(object):
result
=
self
.
_normalized_repr
(
p
)
result
=
self
.
_normalized_repr
(
p
)
self
.
assertEqual
(
self
.
assertEqual
(
result
,
result
,
"<
P object at 0xdeadbeef oid b'12345678'"
"<
persistent.tests.test_persistence.P object at 0xdeadbeef oid "
+
self
.
_HEX_OID
" in <SomeJar> _p_repr Exception('_p_repr failed')>"
)
+
" in <SomeJar> _p_repr Exception('_p_repr failed')>"
)
def
test__p_repr_in_instance_ignored
(
self
):
def
test__p_repr_in_instance_ignored
(
self
):
class
P
(
self
.
_getTargetClass
()):
class
P
(
self
.
_getTargetClass
()):
...
@@ -1869,7 +1899,8 @@ class _Persistent_Base(object):
...
@@ -1869,7 +1899,8 @@ class _Persistent_Base(object):
p
=
P
()
p
=
P
()
p
.
_p_repr
=
lambda
:
"Instance"
p
.
_p_repr
=
lambda
:
"Instance"
result
=
self
.
_normalized_repr
(
p
)
result
=
self
.
_normalized_repr
(
p
)
self
.
assertEqual
(
result
,
'<P object at 0xdeadbeef>'
)
self
.
assertEqual
(
result
,
'<persistent.tests.test_persistence.P object at 0xdeadbeef>'
)
def
test__p_repr_baseexception
(
self
):
def
test__p_repr_baseexception
(
self
):
class
P
(
self
.
_getTargetClass
()):
class
P
(
self
.
_getTargetClass
()):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment