Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
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
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
1f383d7f
Commit
1f383d7f
authored
Jan 28, 2003
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Some experimental support for generating NEWOBJ with proto=2, and
fixed a bug in load_newobj().
parent
8fcacda2
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
70 additions
and
2 deletions
+70
-2
Lib/pickle.py
Lib/pickle.py
+31
-2
Lib/test/pickletester.py
Lib/test/pickletester.py
+39
-0
No files found.
Lib/pickle.py
View file @
1f383d7f
...
...
@@ -77,11 +77,13 @@ class _Stop(Exception):
def
__init__
(
self
,
value
):
self
.
value
=
value
# Jython has PyStringMap; it's a dict subclass with string keys
try
:
from
org.python.core
import
PyStringMap
except
ImportError
:
PyStringMap
=
None
# UnicodeType may or may not be exported (normally imported from types)
try
:
UnicodeType
except
NameError
:
...
...
@@ -249,7 +251,10 @@ class Pickler:
return
GET
+
`i`
+
'
\
n
'
def
save
(
self
,
obj
):
def
save
(
self
,
obj
,
_builtin_type
=
(
int
,
long
,
float
,
complex
,
str
,
unicode
,
tuple
,
list
,
dict
),
):
# Check for persistent id (defined by a subclass)
pid
=
self
.
persistent_id
(
obj
)
if
pid
:
...
...
@@ -278,6 +283,30 @@ class Pickler:
self
.
save_global
(
obj
)
return
# Check for instance of subclass of common built-in types
# XXX This block is experimental code that will go away!
if
self
.
proto
>=
2
:
if
isinstance
(
obj
,
_builtin_type
):
assert
t
not
in
_builtin_type
# Proper subclass
args
=
()
getnewargs
=
getattr
(
obj
,
"__getnewargs__"
,
None
)
if
getnewargs
:
args
=
getnewargs
()
# This better not reference obj
self
.
save_global
(
t
)
self
.
save
(
args
)
self
.
write
(
NEWOBJ
)
self
.
memoize
(
obj
)
getstate
=
getattr
(
obj
,
"__getstate__"
,
None
)
if
getstate
:
state
=
getstate
()
else
:
state
=
getattr
(
obj
,
"__dict__"
,
None
)
# XXX What about __slots__?
if
state
is
not
None
:
self
.
save
(
state
)
self
.
write
(
BUILD
)
return
# Check copy_reg.dispatch_table
reduce
=
dispatch_table
.
get
(
t
)
if
reduce
:
...
...
@@ -970,7 +999,7 @@ class Unpickler:
args
=
self
.
stack
.
pop
()
cls
=
self
.
stack
[
-
1
]
obj
=
cls
.
__new__
(
cls
,
*
args
)
self
.
stack
[
-
1
:
]
=
obj
self
.
stack
[
-
1
]
=
obj
dispatch
[
NEWOBJ
]
=
load_newobj
def
load_global
(
self
):
...
...
Lib/test/pickletester.py
View file @
1f383d7f
...
...
@@ -300,6 +300,45 @@ class AbstractPickleTests(unittest.TestCase):
y
=
self
.
loads
(
s
)
self
.
assert_
(
x
is
y
,
(
proto
,
x
,
s
,
y
))
def
test_newobj_tuple
(
self
):
x
=
MyTuple
([
1
,
2
,
3
],
foo
=
42
,
bar
=
"hello"
)
s
=
self
.
dumps
(
x
,
2
)
y
=
self
.
loads
(
s
)
self
.
assertEqual
(
tuple
(
x
),
tuple
(
y
))
self
.
assertEqual
(
x
.
__dict__
,
y
.
__dict__
)
def
test_newobj_list
(
self
):
x
=
MyList
([
1
,
2
,
3
],
foo
=
42
,
bar
=
"hello"
)
s
=
self
.
dumps
(
x
,
2
)
y
=
self
.
loads
(
s
)
self
.
assertEqual
(
list
(
x
),
list
(
y
))
self
.
assertEqual
(
x
.
__dict__
,
y
.
__dict__
)
class
MyTuple
(
tuple
):
def
__new__
(
cls
,
*
args
,
**
kwds
):
# Ignore **kwds
return
tuple
.
__new__
(
cls
,
*
args
)
def
__getnewargs__
(
self
):
return
(
tuple
(
self
),)
def
__init__
(
self
,
*
args
,
**
kwds
):
for
k
,
v
in
kwds
.
items
():
setattr
(
self
,
k
,
v
)
class
MyList
(
list
):
def
__new__
(
cls
,
*
args
,
**
kwds
):
# Ignore **kwds
return
list
.
__new__
(
cls
,
*
args
)
def
__init__
(
self
,
*
args
,
**
kwds
):
for
k
,
v
in
kwds
.
items
():
setattr
(
self
,
k
,
v
)
def
__getstate__
(
self
):
return
list
(
self
),
self
.
__dict__
def
__setstate__
(
self
,
arg
):
lst
,
dct
=
arg
for
x
in
lst
:
self
.
append
(
x
)
self
.
__init__
(
**
dct
)
class
AbstractPickleModuleTests
(
unittest
.
TestCase
):
def
test_dump_closed_file
(
self
):
...
...
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