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
30c6cd93
Commit
30c6cd93
authored
Feb 22, 2017
by
Adam Groszer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix ``__setstate__`` interning when ``state`` parameter is not a built-in dict
parent
1c1cebaf
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
56 additions
and
10 deletions
+56
-10
CHANGES.rst
CHANGES.rst
+2
-0
persistent/cPersistence.c
persistent/cPersistence.c
+36
-10
persistent/tests/test_persistence.py
persistent/tests/test_persistence.py
+18
-0
No files found.
CHANGES.rst
View file @
30c6cd93
...
...
@@ -20,6 +20,8 @@
- Add support for Python 3.6.
- Fix ``__setstate__`` interning when ``state`` parameter is not a built-in dict
4.2.2 (2016-11-29)
------------------
...
...
persistent/cPersistence.c
View file @
30c6cd93
...
...
@@ -588,8 +588,10 @@ pickle___setstate__(PyObject *self, PyObject *state)
if
(
state
!=
Py_None
)
{
PyObject
**
dict
;
PyObject
*
items
;
PyObject
*
d_key
,
*
d_value
;
Py_ssize_t
i
;
int
len
;
dict
=
_PyObject_GetDictPtr
(
self
);
...
...
@@ -609,6 +611,8 @@ pickle___setstate__(PyObject *self, PyObject *state)
PyDict_Clear
(
*
dict
);
if
(
PyDict_CheckExact
(
state
))
{
i
=
0
;
while
(
PyDict_Next
(
state
,
&
i
,
&
d_key
,
&
d_value
))
{
/* normally the keys for instance attributes are
...
...
@@ -622,6 +626,28 @@ pickle___setstate__(PyObject *self, PyObject *state)
return
NULL
;
}
}
else
{
/* can happen that not a built-in dict is passed as state
fall back to iterating over items, instead of silently
failing with PyDict_Next */
items
=
PyMapping_Items
(
state
);
len
=
PySequence_Size
(
items
);
for
(
i
=
0
;
i
<
len
;
++
i
)
{
PyObject
*
item
=
PySequence_GetItem
(
items
,
i
);
d_key
=
PyTuple_GetItem
(
item
,
0
);
d_value
=
PyTuple_GetItem
(
item
,
1
);
if
(
NATIVE_CHECK_EXACT
(
d_key
))
{
Py_INCREF
(
d_key
);
INTERN_INPLACE
(
&
d_key
);
Py_DECREF
(
d_key
);
}
if
(
PyObject_SetItem
(
*
dict
,
d_key
,
d_value
)
<
0
)
return
NULL
;
}
}
}
if
(
slots
&&
pickle_setattrs_from_dict
(
self
,
slots
)
<
0
)
return
NULL
;
...
...
persistent/tests/test_persistence.py
View file @
30c6cd93
...
...
@@ -985,6 +985,24 @@ class _Persistent_Base(object):
key2
=
list
(
inst2
.
__dict__
.
keys
())[
0
]
self
.
assertTrue
(
key1
is
key2
)
import
UserDict
inst1
=
Derived
()
inst2
=
Derived
()
key1
=
'key'
key2
=
'ke'
;
key2
+=
'y'
# construct in a way that won't intern the literal
self
.
assertFalse
(
key1
is
key2
)
state1
=
UserDict
.
IterableUserDict
({
key1
:
1
})
state2
=
UserDict
.
IterableUserDict
({
key2
:
2
})
k1
=
list
(
state1
.
keys
())[
0
]
k2
=
list
(
state2
.
keys
())[
0
]
self
.
assertFalse
(
k1
is
k2
)
# verify
inst1
.
__setstate__
(
state1
)
inst2
.
__setstate__
(
state2
)
key1
=
list
(
inst1
.
__dict__
.
keys
())[
0
]
key2
=
list
(
inst2
.
__dict__
.
keys
())[
0
]
self
.
assertTrue
(
key1
is
key2
)
def
test___setstate___doesnt_fail_on_non_string_keys
(
self
):
class
Derived
(
self
.
_getTargetClass
()):
pass
...
...
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