Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
e1f0001f
Commit
e1f0001f
authored
Feb 22, 2013
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix and test for #645
Yuck, pickle is weird. Closes #645.
parent
d62e4a7a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
22 additions
and
13 deletions
+22
-13
rest_framework/serializers.py
+11
-8
rest_framework/tests/serializer.py
+11
-5
No files found.
rest_framework/serializers.py
View file @
e1f0001f
...
...
@@ -28,20 +28,23 @@ class DictWithMetadata(dict):
def
__getstate__
(
self
):
"""
Used by pickle (e.g., caching).
Overriden to remove
metadata from the dict, since it shouldn't be pickled
and may in some instances be unpickleable.
Overriden to remove
the metadata from the dict, since it shouldn't be
pickled
and may in some instances be unpickleable.
"""
# return an instance of the first dict in MRO that isn't a DictWithMetadata
for
base
in
self
.
__class__
.
__mro__
:
if
not
issubclass
(
base
,
DictWithMetadata
)
and
issubclass
(
base
,
dict
):
return
base
(
self
)
return
dict
(
self
)
class
SortedDictWithMetadata
(
SortedDict
,
DictWithMetadata
):
class
SortedDictWithMetadata
(
SortedDict
):
"""
A sorted dict-like object, that can have additional properties attached.
"""
pass
def
__getstate__
(
self
):
"""
Used by pickle (e.g., caching).
Overriden to remove the metadata from the dict, since it shouldn't be
pickle and may in some instances be unpickleable.
"""
return
SortedDict
(
self
)
.
__dict__
def
_is_protected_type
(
obj
):
...
...
rest_framework/tests/serializer.py
View file @
e1f0001f
...
...
@@ -951,15 +951,21 @@ class SerializerPickleTests(TestCase):
class
Meta
:
model
=
Person
fields
=
(
'name'
,
'age'
)
pickle
.
dumps
(
InnerPersonSerializer
(
Person
(
name
=
"Noah"
,
age
=
950
))
.
data
)
pickle
.
dumps
(
InnerPersonSerializer
(
Person
(
name
=
"Noah"
,
age
=
950
))
.
data
,
0
)
def
test_getstate_method_should_not_return_none
(
self
):
"""
Regression test for
https://github.com/tomchristie/django-rest-framework/issues/645
Regression test for #645.
"""
d
=
serializers
.
DictWithMetadata
({
1
:
1
})
self
.
assertEqual
(
d
.
__getstate__
(),
serializers
.
SortedDict
({
1
:
1
}))
data
=
serializers
.
DictWithMetadata
({
1
:
1
})
self
.
assertEqual
(
data
.
__getstate__
(),
serializers
.
SortedDict
({
1
:
1
}))
def
test_serializer_data_is_pickleable
(
self
):
"""
Another regression test for #645.
"""
data
=
serializers
.
SortedDictWithMetadata
({
1
:
1
})
repr
(
pickle
.
loads
(
pickle
.
dumps
(
data
,
0
)))
class
DepthTest
(
TestCase
):
...
...
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