Getting rid of cPickle. Mmm, feels good!
This commit is contained in:
parent
be6fe5476c
commit
99603b0c1e
|
@ -149,11 +149,10 @@ the value to a string, when the values are set dictionary-style.
|
||||||
SerialCookie
|
SerialCookie
|
||||||
|
|
||||||
The SerialCookie expects that all values should be serialized using
|
The SerialCookie expects that all values should be serialized using
|
||||||
cPickle (or pickle, if cPickle isn't available). As a result of
|
pickle. As a result of serializing, SerialCookie can save almost any
|
||||||
serializing, SerialCookie can save almost any Python object to a
|
Python object to a value, and recover the exact same object when the
|
||||||
value, and recover the exact same object when the cookie has been
|
cookie has been returned. (SerialCookie can yield some
|
||||||
returned. (SerialCookie can yield some strange-looking cookie
|
strange-looking cookie values, however.)
|
||||||
values, however.)
|
|
||||||
|
|
||||||
>>> C = Cookie.SerialCookie()
|
>>> C = Cookie.SerialCookie()
|
||||||
>>> C["number"] = 7
|
>>> C["number"] = 7
|
||||||
|
@ -162,7 +161,7 @@ values, however.)
|
||||||
7
|
7
|
||||||
>>> C["string"].value
|
>>> C["string"].value
|
||||||
'seven'
|
'seven'
|
||||||
>>> C.output().replace('p0', 'p1') # Hack for cPickle/pickle differences
|
>>> C.output().replace('p0', 'p1') # Hack for pickling differences
|
||||||
'Set-Cookie: number="I7\\012."\r\nSet-Cookie: string="Vseven\\012p1\\012."'
|
'Set-Cookie: number="I7\\012."\r\nSet-Cookie: string="Vseven\\012p1\\012."'
|
||||||
|
|
||||||
Be warned, however, if SerialCookie cannot de-serialize a value (because
|
Be warned, however, if SerialCookie cannot de-serialize a value (because
|
||||||
|
@ -173,7 +172,7 @@ SmartCookie
|
||||||
|
|
||||||
The SmartCookie combines aspects of each of the other two flavors.
|
The SmartCookie combines aspects of each of the other two flavors.
|
||||||
When setting a value in a dictionary-fashion, the SmartCookie will
|
When setting a value in a dictionary-fashion, the SmartCookie will
|
||||||
serialize (ala cPickle) the value *if and only if* it isn't a
|
serialize (ala pickle) the value *if and only if* it isn't a
|
||||||
Python string. String objects are *not* serialized. Similarly,
|
Python string. String objects are *not* serialized. Similarly,
|
||||||
when the load() method parses out values, it attempts to de-serialize
|
when the load() method parses out values, it attempts to de-serialize
|
||||||
the value. If it fails, then it fallsback to treating the value
|
the value. If it fails, then it fallsback to treating the value
|
||||||
|
@ -212,10 +211,7 @@ Finis.
|
||||||
#
|
#
|
||||||
import string
|
import string
|
||||||
|
|
||||||
try:
|
from pickle import dumps, loads
|
||||||
from cPickle import dumps, loads
|
|
||||||
except ImportError:
|
|
||||||
from pickle import dumps, loads
|
|
||||||
|
|
||||||
import re, warnings
|
import re, warnings
|
||||||
|
|
||||||
|
@ -660,7 +656,7 @@ class SimpleCookie(BaseCookie):
|
||||||
class SerialCookie(BaseCookie):
|
class SerialCookie(BaseCookie):
|
||||||
"""SerialCookie
|
"""SerialCookie
|
||||||
SerialCookie supports arbitrary objects as cookie values. All
|
SerialCookie supports arbitrary objects as cookie values. All
|
||||||
values are serialized (using cPickle) before being sent to the
|
values are serialized (using pickle) before being sent to the
|
||||||
client. All incoming values are assumed to be valid Pickle
|
client. All incoming values are assumed to be valid Pickle
|
||||||
representations. IF AN INCOMING VALUE IS NOT IN A VALID PICKLE
|
representations. IF AN INCOMING VALUE IS NOT IN A VALID PICKLE
|
||||||
FORMAT, THEN AN EXCEPTION WILL BE RAISED.
|
FORMAT, THEN AN EXCEPTION WILL BE RAISED.
|
||||||
|
@ -687,7 +683,7 @@ class SmartCookie(BaseCookie):
|
||||||
"""SmartCookie
|
"""SmartCookie
|
||||||
SmartCookie supports arbitrary objects as cookie values. If the
|
SmartCookie supports arbitrary objects as cookie values. If the
|
||||||
object is a string, then it is quoted. If the object is not a
|
object is a string, then it is quoted. If the object is not a
|
||||||
string, however, then SmartCookie will use cPickle to serialize
|
string, however, then SmartCookie will use pickle to serialize
|
||||||
the object into a string representation.
|
the object into a string representation.
|
||||||
|
|
||||||
Note: Large cookie values add overhead because they must be
|
Note: Large cookie values add overhead because they must be
|
||||||
|
|
|
@ -22,7 +22,7 @@ import sys
|
||||||
import copy
|
import copy
|
||||||
import xdrlib
|
import xdrlib
|
||||||
import random
|
import random
|
||||||
import cPickle as pickle
|
import pickle
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# For Pythons w/distutils pybsddb
|
# For Pythons w/distutils pybsddb
|
||||||
|
|
|
@ -21,11 +21,7 @@
|
||||||
# $Id$
|
# $Id$
|
||||||
|
|
||||||
import sys, os, re
|
import sys, os, re
|
||||||
try:
|
import pickle
|
||||||
import cPickle
|
|
||||||
pickle = cPickle
|
|
||||||
except ImportError:
|
|
||||||
import pickle
|
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
|
|
|
@ -1,10 +1,6 @@
|
||||||
|
|
||||||
import sys, os
|
import sys, os
|
||||||
import pickle
|
import pickle
|
||||||
try:
|
|
||||||
import cPickle
|
|
||||||
except ImportError:
|
|
||||||
cPickle = None
|
|
||||||
import unittest
|
import unittest
|
||||||
import glob
|
import glob
|
||||||
|
|
||||||
|
@ -62,10 +58,6 @@ class pickleTestCase(unittest.TestCase):
|
||||||
def test01_pickle_DBError(self):
|
def test01_pickle_DBError(self):
|
||||||
self._base_test_pickle_DBError(pickle=pickle)
|
self._base_test_pickle_DBError(pickle=pickle)
|
||||||
|
|
||||||
if cPickle:
|
|
||||||
def test02_cPickle_DBError(self):
|
|
||||||
self._base_test_pickle_DBError(pickle=cPickle)
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
|
|
||||||
def test_suite():
|
def test_suite():
|
||||||
|
|
|
@ -58,7 +58,7 @@ def NamedTuple(typename, s):
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# verify that instances are pickable
|
# verify that instances are pickable
|
||||||
from cPickle import loads, dumps
|
from pickle import loads, dumps
|
||||||
Point = NamedTuple('Point', 'x y')
|
Point = NamedTuple('Point', 'x y')
|
||||||
p = Point(x=10, y=20)
|
p = Point(x=10, y=20)
|
||||||
assert p == loads(dumps(p))
|
assert p == loads(dumps(p))
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
"""Helper to provide extensibility for pickle/cPickle.
|
"""Helper to provide extensibility for pickle.
|
||||||
|
|
||||||
This is only useful to add pickle support for extension types defined in
|
This is only useful to add pickle support for extension types defined in
|
||||||
C, not for instances of user-defined classes.
|
C, not for instances of user-defined classes.
|
||||||
|
@ -146,7 +146,7 @@ def _slotnames(cls):
|
||||||
_extension_registry = {} # key -> code
|
_extension_registry = {} # key -> code
|
||||||
_inverted_registry = {} # code -> key
|
_inverted_registry = {} # code -> key
|
||||||
_extension_cache = {} # code -> object
|
_extension_cache = {} # code -> object
|
||||||
# Don't ever rebind those names: cPickle grabs a reference to them when
|
# Don't ever rebind those names: pickling grabs a reference to them when
|
||||||
# it's initialized, and won't see a rebinding.
|
# it's initialized, and won't see a rebinding.
|
||||||
|
|
||||||
def add_extension(module, name, code):
|
def add_extension(module, name, code):
|
||||||
|
|
|
@ -33,7 +33,7 @@ import socket
|
||||||
import select
|
import select
|
||||||
import SocketServer
|
import SocketServer
|
||||||
import struct
|
import struct
|
||||||
import cPickle as pickle
|
import pickle
|
||||||
import threading
|
import threading
|
||||||
import Queue
|
import Queue
|
||||||
import traceback
|
import traceback
|
||||||
|
|
|
@ -28,10 +28,7 @@ To use, simply 'import logging' and log away!
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import sys, logging, socket, os, struct, time, glob
|
import sys, logging, socket, os, struct, time, glob
|
||||||
try:
|
import pickle
|
||||||
import cPickle as pickle
|
|
||||||
except ImportError:
|
|
||||||
import pickle
|
|
||||||
from stat import ST_DEV, ST_INO
|
from stat import ST_DEV, ST_INO
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
"""Create portable serialized representations of Python objects.
|
"""Create portable serialized representations of Python objects.
|
||||||
|
|
||||||
See module cPickle for a (much) faster implementation.
|
|
||||||
See module copy_reg for a mechanism for registering custom picklers.
|
See module copy_reg for a mechanism for registering custom picklers.
|
||||||
See module pickletools source for extensive comments.
|
See module pickletools source for extensive comments.
|
||||||
|
|
||||||
|
@ -48,8 +47,7 @@ compatible_formats = ["1.0", # Original protocol 0
|
||||||
"2.0", # Protocol 2
|
"2.0", # Protocol 2
|
||||||
] # Old format versions we can read
|
] # Old format versions we can read
|
||||||
|
|
||||||
# Keep in synch with cPickle. This is the highest protocol number we
|
# This is the highest protocol number we know how to read.
|
||||||
# know how to read.
|
|
||||||
HIGHEST_PROTOCOL = 2
|
HIGHEST_PROTOCOL = 2
|
||||||
|
|
||||||
# The protocol we write by default. May be less than HIGHEST_PROTOCOL.
|
# The protocol we write by default. May be less than HIGHEST_PROTOCOL.
|
||||||
|
@ -591,8 +589,6 @@ class Pickler:
|
||||||
|
|
||||||
dispatch[list] = save_list
|
dispatch[list] = save_list
|
||||||
|
|
||||||
# Keep in synch with cPickle's BATCHSIZE. Nothing will break if it gets
|
|
||||||
# out of synch, though.
|
|
||||||
_BATCHSIZE = 1000
|
_BATCHSIZE = 1000
|
||||||
|
|
||||||
def _batch_appends(self, items):
|
def _batch_appends(self, items):
|
||||||
|
@ -1090,7 +1086,12 @@ class Unpickler:
|
||||||
stack = self.stack
|
stack = self.stack
|
||||||
args = stack.pop()
|
args = stack.pop()
|
||||||
func = stack[-1]
|
func = stack[-1]
|
||||||
value = func(*args)
|
try:
|
||||||
|
value = func(*args)
|
||||||
|
except:
|
||||||
|
print(sys.exc_info())
|
||||||
|
print(func, args)
|
||||||
|
raise
|
||||||
stack[-1] = value
|
stack[-1] = value
|
||||||
dispatch[REDUCE[0]] = load_reduce
|
dispatch[REDUCE[0]] = load_reduce
|
||||||
|
|
||||||
|
|
|
@ -597,7 +597,7 @@ float8 = ArgumentDescriptor(
|
||||||
doc="""An 8-byte binary representation of a float, big-endian.
|
doc="""An 8-byte binary representation of a float, big-endian.
|
||||||
|
|
||||||
The format is unique to Python, and shared with the struct
|
The format is unique to Python, and shared with the struct
|
||||||
module (format string '>d') "in theory" (the struct and cPickle
|
module (format string '>d') "in theory" (the struct and pickle
|
||||||
implementations don't share the code -- they should). It's
|
implementations don't share the code -- they should). It's
|
||||||
strongly related to the IEEE-754 double format, and, in normal
|
strongly related to the IEEE-754 double format, and, in normal
|
||||||
cases, is in fact identical to the big-endian 754 double format.
|
cases, is in fact identical to the big-endian 754 double format.
|
||||||
|
@ -1587,9 +1587,8 @@ opcodes = [
|
||||||
first insists that the class object have a __safe_for_unpickling__
|
first insists that the class object have a __safe_for_unpickling__
|
||||||
attribute. Unlike as for the __safe_for_unpickling__ check in REDUCE,
|
attribute. Unlike as for the __safe_for_unpickling__ check in REDUCE,
|
||||||
it doesn't matter whether this attribute has a true or false value, it
|
it doesn't matter whether this attribute has a true or false value, it
|
||||||
only matters whether it exists (XXX this is a bug; cPickle
|
only matters whether it exists (XXX this is a bug). If
|
||||||
requires the attribute to be true). If __safe_for_unpickling__
|
__safe_for_unpickling__ doesn't exist, UnpicklingError is raised.
|
||||||
doesn't exist, UnpicklingError is raised.
|
|
||||||
|
|
||||||
Else (the class object does have a __safe_for_unpickling__ attr),
|
Else (the class object does have a __safe_for_unpickling__ attr),
|
||||||
the class object obtained from INST's arguments is applied to the
|
the class object obtained from INST's arguments is applied to the
|
||||||
|
@ -1624,8 +1623,7 @@ opcodes = [
|
||||||
As for INST, the remainder of the stack above the markobject is
|
As for INST, the remainder of the stack above the markobject is
|
||||||
gathered into an argument tuple, and then the logic seems identical,
|
gathered into an argument tuple, and then the logic seems identical,
|
||||||
except that no __safe_for_unpickling__ check is done (XXX this is
|
except that no __safe_for_unpickling__ check is done (XXX this is
|
||||||
a bug; cPickle does test __safe_for_unpickling__). See INST for
|
a bug). See INST for the gory details.
|
||||||
the gory details.
|
|
||||||
|
|
||||||
NOTE: In Python 2.3, INST and OBJ are identical except for how they
|
NOTE: In Python 2.3, INST and OBJ are identical except for how they
|
||||||
get the class object. That was always the intent; the implementations
|
get the class object. That was always the intent; the implementations
|
||||||
|
|
|
@ -1,9 +1,5 @@
|
||||||
import unittest
|
import unittest
|
||||||
import pickle
|
import pickle
|
||||||
try:
|
|
||||||
import cPickle
|
|
||||||
except ImportError:
|
|
||||||
cPickle = None
|
|
||||||
import pickletools
|
import pickletools
|
||||||
import copy_reg
|
import copy_reg
|
||||||
|
|
||||||
|
@ -12,8 +8,6 @@ from test.test_support import TestFailed, TESTFN, run_with_locale
|
||||||
# Tests that try a number of pickle protocols should have a
|
# Tests that try a number of pickle protocols should have a
|
||||||
# for proto in protocols:
|
# for proto in protocols:
|
||||||
# kind of outer loop.
|
# kind of outer loop.
|
||||||
if cPickle is not None:
|
|
||||||
assert pickle.HIGHEST_PROTOCOL == cPickle.HIGHEST_PROTOCOL == 2
|
|
||||||
protocols = range(pickle.HIGHEST_PROTOCOL + 1)
|
protocols = range(pickle.HIGHEST_PROTOCOL + 1)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -268,34 +268,6 @@ class BoolTest(unittest.TestCase):
|
||||||
self.assertIs(pickle.loads(pickle.dumps(True, True)), True)
|
self.assertIs(pickle.loads(pickle.dumps(True, True)), True)
|
||||||
self.assertIs(pickle.loads(pickle.dumps(False, True)), False)
|
self.assertIs(pickle.loads(pickle.dumps(False, True)), False)
|
||||||
|
|
||||||
def test_cpickle(self):
|
|
||||||
try:
|
|
||||||
import cPickle
|
|
||||||
except ImportError:
|
|
||||||
return # Just ignore this if cPickle doesn't exist
|
|
||||||
|
|
||||||
self.assertIs(cPickle.loads(cPickle.dumps(True)), True)
|
|
||||||
self.assertIs(cPickle.loads(cPickle.dumps(False)), False)
|
|
||||||
self.assertIs(cPickle.loads(cPickle.dumps(True, True)), True)
|
|
||||||
self.assertIs(cPickle.loads(cPickle.dumps(False, True)), False)
|
|
||||||
|
|
||||||
def test_mixedpickle(self):
|
|
||||||
import pickle
|
|
||||||
try:
|
|
||||||
import cPickle
|
|
||||||
except ImportError:
|
|
||||||
return # Just ignore this if cPickle doesn't exist
|
|
||||||
|
|
||||||
self.assertIs(pickle.loads(cPickle.dumps(True)), True)
|
|
||||||
self.assertIs(pickle.loads(cPickle.dumps(False)), False)
|
|
||||||
self.assertIs(pickle.loads(cPickle.dumps(True, True)), True)
|
|
||||||
self.assertIs(pickle.loads(cPickle.dumps(False, True)), False)
|
|
||||||
|
|
||||||
self.assertIs(cPickle.loads(pickle.dumps(True)), True)
|
|
||||||
self.assertIs(cPickle.loads(pickle.dumps(False)), False)
|
|
||||||
self.assertIs(cPickle.loads(pickle.dumps(True, True)), True)
|
|
||||||
self.assertIs(cPickle.loads(pickle.dumps(False, True)), False)
|
|
||||||
|
|
||||||
def test_picklevalues(self):
|
def test_picklevalues(self):
|
||||||
# Test for specific backwards-compatible pickle values
|
# Test for specific backwards-compatible pickle values
|
||||||
import pickle
|
import pickle
|
||||||
|
@ -306,19 +278,6 @@ class BoolTest(unittest.TestCase):
|
||||||
self.assertEqual(pickle.dumps(True, protocol=2), b'\x80\x02\x88.')
|
self.assertEqual(pickle.dumps(True, protocol=2), b'\x80\x02\x88.')
|
||||||
self.assertEqual(pickle.dumps(False, protocol=2), b'\x80\x02\x89.')
|
self.assertEqual(pickle.dumps(False, protocol=2), b'\x80\x02\x89.')
|
||||||
|
|
||||||
def test_cpicklevalues(self):
|
|
||||||
# Test for specific backwards-compatible pickle values
|
|
||||||
try:
|
|
||||||
import cPickle
|
|
||||||
except ImportError:
|
|
||||||
return # Just ignore the rest if cPickle doesn't exist
|
|
||||||
self.assertEqual(cPickle.dumps(True, protocol=0), b"I01\n.")
|
|
||||||
self.assertEqual(cPickle.dumps(False, protocol=0), b"I00\n.")
|
|
||||||
self.assertEqual(cPickle.dumps(True, protocol=1), b"I01\n.")
|
|
||||||
self.assertEqual(cPickle.dumps(False, protocol=1), b"I00\n.")
|
|
||||||
self.assertEqual(cPickle.dumps(True, protocol=2), b'\x80\x02\x88.')
|
|
||||||
self.assertEqual(cPickle.dumps(False, protocol=2), b'\x80\x02\x89.')
|
|
||||||
|
|
||||||
def test_convert_to_bool(self):
|
def test_convert_to_bool(self):
|
||||||
# Verify that TypeError occurs when bad things are returned
|
# Verify that TypeError occurs when bad things are returned
|
||||||
# from __bool__(). This isn't really a bool test, but
|
# from __bool__(). This isn't really a bool test, but
|
||||||
|
|
|
@ -4,7 +4,6 @@ import os
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
import pickle
|
import pickle
|
||||||
import cPickle
|
|
||||||
import tempfile
|
import tempfile
|
||||||
import unittest
|
import unittest
|
||||||
import test.test_support
|
import test.test_support
|
||||||
|
@ -641,12 +640,11 @@ class BytesTest(unittest.TestCase):
|
||||||
self.assertEqual(b.rpartition(b'i'), (b'mississipp', b'i', b''))
|
self.assertEqual(b.rpartition(b'i'), (b'mississipp', b'i', b''))
|
||||||
|
|
||||||
def test_pickling(self):
|
def test_pickling(self):
|
||||||
for pm in pickle, cPickle:
|
for proto in range(pickle.HIGHEST_PROTOCOL):
|
||||||
for proto in range(pm.HIGHEST_PROTOCOL):
|
for b in b"", b"a", b"abc", b"\xffab\x80", b"\0\0\377\0\0":
|
||||||
for b in b"", b"a", b"abc", b"\xffab\x80", b"\0\0\377\0\0":
|
ps = pickle.dumps(b, proto)
|
||||||
ps = pm.dumps(b, proto)
|
q = pickle.loads(ps)
|
||||||
q = pm.loads(ps)
|
self.assertEqual(b, q)
|
||||||
self.assertEqual(b, q)
|
|
||||||
|
|
||||||
def test_strip(self):
|
def test_strip(self):
|
||||||
b = b'mississippi'
|
b = b'mississippi'
|
||||||
|
|
|
@ -1,103 +0,0 @@
|
||||||
import cPickle
|
|
||||||
import unittest
|
|
||||||
from cStringIO import StringIO
|
|
||||||
from test.pickletester import AbstractPickleTests, AbstractPickleModuleTests
|
|
||||||
from test import test_support
|
|
||||||
|
|
||||||
class cPickleTests(AbstractPickleTests, AbstractPickleModuleTests):
|
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
self.dumps = cPickle.dumps
|
|
||||||
self.loads = cPickle.loads
|
|
||||||
|
|
||||||
error = cPickle.BadPickleGet
|
|
||||||
module = cPickle
|
|
||||||
|
|
||||||
class cPicklePicklerTests(AbstractPickleTests):
|
|
||||||
|
|
||||||
def dumps(self, arg, proto=0):
|
|
||||||
f = StringIO()
|
|
||||||
p = cPickle.Pickler(f, proto)
|
|
||||||
p.dump(arg)
|
|
||||||
f.seek(0)
|
|
||||||
return f.read()
|
|
||||||
|
|
||||||
def loads(self, buf):
|
|
||||||
f = StringIO(buf)
|
|
||||||
p = cPickle.Unpickler(f)
|
|
||||||
return p.load()
|
|
||||||
|
|
||||||
error = cPickle.BadPickleGet
|
|
||||||
|
|
||||||
class cPickleListPicklerTests(AbstractPickleTests):
|
|
||||||
|
|
||||||
def dumps(self, arg, proto=0):
|
|
||||||
p = cPickle.Pickler(proto)
|
|
||||||
p.dump(arg)
|
|
||||||
return p.getvalue()
|
|
||||||
|
|
||||||
def loads(self, *args):
|
|
||||||
f = StringIO(args[0])
|
|
||||||
p = cPickle.Unpickler(f)
|
|
||||||
return p.load()
|
|
||||||
|
|
||||||
error = cPickle.BadPickleGet
|
|
||||||
|
|
||||||
class cPickleFastPicklerTests(AbstractPickleTests):
|
|
||||||
|
|
||||||
def dumps(self, arg, proto=0):
|
|
||||||
f = StringIO()
|
|
||||||
p = cPickle.Pickler(f, proto)
|
|
||||||
p.fast = 1
|
|
||||||
p.dump(arg)
|
|
||||||
f.seek(0)
|
|
||||||
return f.read()
|
|
||||||
|
|
||||||
def loads(self, *args):
|
|
||||||
f = StringIO(args[0])
|
|
||||||
p = cPickle.Unpickler(f)
|
|
||||||
return p.load()
|
|
||||||
|
|
||||||
error = cPickle.BadPickleGet
|
|
||||||
|
|
||||||
def test_recursive_list(self):
|
|
||||||
self.assertRaises(ValueError,
|
|
||||||
AbstractPickleTests.test_recursive_list,
|
|
||||||
self)
|
|
||||||
|
|
||||||
def test_recursive_inst(self):
|
|
||||||
self.assertRaises(ValueError,
|
|
||||||
AbstractPickleTests.test_recursive_inst,
|
|
||||||
self)
|
|
||||||
|
|
||||||
def test_recursive_dict(self):
|
|
||||||
self.assertRaises(ValueError,
|
|
||||||
AbstractPickleTests.test_recursive_dict,
|
|
||||||
self)
|
|
||||||
|
|
||||||
def test_recursive_multi(self):
|
|
||||||
self.assertRaises(ValueError,
|
|
||||||
AbstractPickleTests.test_recursive_multi,
|
|
||||||
self)
|
|
||||||
|
|
||||||
def test_nonrecursive_deep(self):
|
|
||||||
# If it's not cyclic, it should pickle OK even if the nesting
|
|
||||||
# depth exceeds PY_CPICKLE_FAST_LIMIT. That happens to be
|
|
||||||
# 50 today. Jack Jansen reported stack overflow on Mac OS 9
|
|
||||||
# at 64.
|
|
||||||
a = []
|
|
||||||
for i in range(60):
|
|
||||||
a = [a]
|
|
||||||
b = self.loads(self.dumps(a))
|
|
||||||
self.assertEqual(a, b)
|
|
||||||
|
|
||||||
def test_main():
|
|
||||||
test_support.run_unittest(
|
|
||||||
cPickleTests,
|
|
||||||
cPicklePicklerTests,
|
|
||||||
cPickleListPicklerTests,
|
|
||||||
cPickleFastPicklerTests
|
|
||||||
)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
test_main()
|
|
|
@ -2807,10 +2807,6 @@ def pickles():
|
||||||
if verbose:
|
if verbose:
|
||||||
print("Testing pickling and copying new-style classes and objects...")
|
print("Testing pickling and copying new-style classes and objects...")
|
||||||
import pickle
|
import pickle
|
||||||
try:
|
|
||||||
import cPickle
|
|
||||||
except ImportError:
|
|
||||||
cPickle = None
|
|
||||||
|
|
||||||
def sorteditems(d):
|
def sorteditems(d):
|
||||||
return sorted(d.items())
|
return sorted(d.items())
|
||||||
|
@ -2863,9 +2859,7 @@ def pickles():
|
||||||
class C4(C4classic, object): # mixed inheritance
|
class C4(C4classic, object): # mixed inheritance
|
||||||
pass
|
pass
|
||||||
|
|
||||||
for p in pickle, cPickle:
|
for p in [pickle]:
|
||||||
if p is None:
|
|
||||||
continue # cPickle not found -- skip it
|
|
||||||
for bin in 0, 1:
|
for bin in 0, 1:
|
||||||
if verbose:
|
if verbose:
|
||||||
print(p.__name__, ["text", "binary"][bin])
|
print(p.__name__, ["text", "binary"][bin])
|
||||||
|
@ -2925,7 +2919,7 @@ def pickles():
|
||||||
|
|
||||||
def pickleslots():
|
def pickleslots():
|
||||||
if verbose: print("Testing pickling of classes with __slots__ ...")
|
if verbose: print("Testing pickling of classes with __slots__ ...")
|
||||||
import pickle, pickle as cPickle
|
import pickle
|
||||||
# Pickling of classes with __slots__ but without __getstate__ should fail
|
# Pickling of classes with __slots__ but without __getstate__ should fail
|
||||||
# (when using protocols 0 or 1)
|
# (when using protocols 0 or 1)
|
||||||
global B, C, D, E
|
global B, C, D, E
|
||||||
|
@ -2942,24 +2936,12 @@ def pickleslots():
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
raise TestFailed, "should fail: pickle C instance - %s" % base
|
raise TestFailed, "should fail: pickle C instance - %s" % base
|
||||||
try:
|
|
||||||
cPickle.dumps(C(), 0)
|
|
||||||
except TypeError:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
raise TestFailed, "should fail: cPickle C instance - %s" % base
|
|
||||||
try:
|
try:
|
||||||
pickle.dumps(C(), 0)
|
pickle.dumps(C(), 0)
|
||||||
except TypeError:
|
except TypeError:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
raise TestFailed, "should fail: pickle D instance - %s" % base
|
raise TestFailed, "should fail: pickle D instance - %s" % base
|
||||||
try:
|
|
||||||
cPickle.dumps(D(), 0)
|
|
||||||
except TypeError:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
raise TestFailed, "should fail: cPickle D instance - %s" % base
|
|
||||||
# Give C a nice generic __getstate__ and __setstate__
|
# Give C a nice generic __getstate__ and __setstate__
|
||||||
class C(base):
|
class C(base):
|
||||||
__slots__ = ['a']
|
__slots__ = ['a']
|
||||||
|
@ -2984,20 +2966,14 @@ def pickleslots():
|
||||||
x = C()
|
x = C()
|
||||||
y = pickle.loads(pickle.dumps(x))
|
y = pickle.loads(pickle.dumps(x))
|
||||||
vereq(hasattr(y, 'a'), 0)
|
vereq(hasattr(y, 'a'), 0)
|
||||||
y = cPickle.loads(cPickle.dumps(x))
|
|
||||||
vereq(hasattr(y, 'a'), 0)
|
|
||||||
x.a = 42
|
x.a = 42
|
||||||
y = pickle.loads(pickle.dumps(x))
|
y = pickle.loads(pickle.dumps(x))
|
||||||
vereq(y.a, 42)
|
vereq(y.a, 42)
|
||||||
y = cPickle.loads(cPickle.dumps(x))
|
|
||||||
vereq(y.a, 42)
|
|
||||||
x = D()
|
x = D()
|
||||||
x.a = 42
|
x.a = 42
|
||||||
x.b = 100
|
x.b = 100
|
||||||
y = pickle.loads(pickle.dumps(x))
|
y = pickle.loads(pickle.dumps(x))
|
||||||
vereq(y.a + y.b, 142)
|
vereq(y.a + y.b, 142)
|
||||||
y = cPickle.loads(cPickle.dumps(x))
|
|
||||||
vereq(y.a + y.b, 142)
|
|
||||||
# A subclass that adds a slot should also work
|
# A subclass that adds a slot should also work
|
||||||
class E(C):
|
class E(C):
|
||||||
__slots__ = ['b']
|
__slots__ = ['b']
|
||||||
|
@ -3007,9 +2983,6 @@ def pickleslots():
|
||||||
y = pickle.loads(pickle.dumps(x))
|
y = pickle.loads(pickle.dumps(x))
|
||||||
vereq(y.a, x.a)
|
vereq(y.a, x.a)
|
||||||
vereq(y.b, x.b)
|
vereq(y.b, x.b)
|
||||||
y = cPickle.loads(cPickle.dumps(x))
|
|
||||||
vereq(y.a, x.a)
|
|
||||||
vereq(y.b, x.b)
|
|
||||||
|
|
||||||
def copies():
|
def copies():
|
||||||
if verbose: print("Testing copy.copy() and copy.deepcopy()...")
|
if verbose: print("Testing copy.copy() and copy.deepcopy()...")
|
||||||
|
|
|
@ -4,10 +4,6 @@ import os
|
||||||
import sys
|
import sys
|
||||||
import unittest
|
import unittest
|
||||||
import pickle
|
import pickle
|
||||||
try:
|
|
||||||
import cPickle
|
|
||||||
except ImportError:
|
|
||||||
cPickle = None
|
|
||||||
|
|
||||||
from test.test_support import (TESTFN, unlink, run_unittest,
|
from test.test_support import (TESTFN, unlink, run_unittest,
|
||||||
guard_warnings_filter)
|
guard_warnings_filter)
|
||||||
|
@ -299,9 +295,7 @@ class ExceptionTests(unittest.TestCase):
|
||||||
value, expected[checkArgName]))
|
value, expected[checkArgName]))
|
||||||
|
|
||||||
# test for pickling support
|
# test for pickling support
|
||||||
for p in pickle, cPickle:
|
for p in [pickle]:
|
||||||
if p is None:
|
|
||||||
continue # cPickle not found -- skip it
|
|
||||||
for protocol in range(p.HIGHEST_PROTOCOL + 1):
|
for protocol in range(p.HIGHEST_PROTOCOL + 1):
|
||||||
s = p.dumps(e, protocol)
|
s = p.dumps(e, protocol)
|
||||||
new = p.loads(s)
|
new = p.loads(s)
|
||||||
|
|
|
@ -412,12 +412,6 @@ class ReTests(unittest.TestCase):
|
||||||
def test_pickling(self):
|
def test_pickling(self):
|
||||||
import pickle
|
import pickle
|
||||||
self.pickle_test(pickle)
|
self.pickle_test(pickle)
|
||||||
try:
|
|
||||||
import cPickle
|
|
||||||
except ImportError:
|
|
||||||
pass # cPickle not found -- skip it
|
|
||||||
else:
|
|
||||||
self.pickle_test(cPickle)
|
|
||||||
# old pickles expect the _compile() reconstructor in sre module
|
# old pickles expect the _compile() reconstructor in sre module
|
||||||
import warnings
|
import warnings
|
||||||
with guard_warnings_filter():
|
with guard_warnings_filter():
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
from test import test_support
|
from test import test_support
|
||||||
from cPickle import loads, dumps
|
from pickle import loads, dumps
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
|
@ -1,44 +0,0 @@
|
||||||
# test_pickle dumps and loads pickles via pickle.py.
|
|
||||||
# test_cpickle does the same, but via the cPickle module.
|
|
||||||
# This test covers the other two cases, making pickles with one module and
|
|
||||||
# loading them via the other.
|
|
||||||
|
|
||||||
import pickle
|
|
||||||
import cPickle
|
|
||||||
import unittest
|
|
||||||
|
|
||||||
from test import test_support
|
|
||||||
from test.pickletester import AbstractPickleTests
|
|
||||||
|
|
||||||
class DumpCPickle_LoadPickle(AbstractPickleTests):
|
|
||||||
|
|
||||||
error = KeyError
|
|
||||||
|
|
||||||
def dumps(self, arg, proto=0, fast=0):
|
|
||||||
# Ignore fast
|
|
||||||
return cPickle.dumps(arg, proto)
|
|
||||||
|
|
||||||
def loads(self, buf):
|
|
||||||
# Ignore fast
|
|
||||||
return pickle.loads(buf)
|
|
||||||
|
|
||||||
class DumpPickle_LoadCPickle(AbstractPickleTests):
|
|
||||||
|
|
||||||
error = cPickle.BadPickleGet
|
|
||||||
|
|
||||||
def dumps(self, arg, proto=0, fast=0):
|
|
||||||
# Ignore fast
|
|
||||||
return pickle.dumps(arg, proto)
|
|
||||||
|
|
||||||
def loads(self, buf):
|
|
||||||
# Ignore fast
|
|
||||||
return cPickle.loads(buf)
|
|
||||||
|
|
||||||
def test_main():
|
|
||||||
test_support.run_unittest(
|
|
||||||
DumpCPickle_LoadPickle,
|
|
||||||
DumpPickle_LoadCPickle
|
|
||||||
)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
test_main()
|
|
|
@ -58,11 +58,7 @@ import tokenize
|
||||||
import types
|
import types
|
||||||
import gc
|
import gc
|
||||||
|
|
||||||
try:
|
import pickle
|
||||||
import cPickle
|
|
||||||
pickle = cPickle
|
|
||||||
except ImportError:
|
|
||||||
import pickle
|
|
||||||
|
|
||||||
def usage(outfile):
|
def usage(outfile):
|
||||||
outfile.write("""Usage: %s [OPTIONS] <file> [ARGS]
|
outfile.write("""Usage: %s [OPTIONS] <file> [ARGS]
|
||||||
|
|
5548
Modules/cPickle.c
5548
Modules/cPickle.c
File diff suppressed because it is too large
Load Diff
3
setup.py
3
setup.py
|
@ -460,9 +460,8 @@ class PyBuildExt(build_ext):
|
||||||
# Fred Drake's interface to the Python parser
|
# Fred Drake's interface to the Python parser
|
||||||
exts.append( Extension('parser', ['parsermodule.c']) )
|
exts.append( Extension('parser', ['parsermodule.c']) )
|
||||||
|
|
||||||
# cStringIO and cPickle
|
# cStringIO
|
||||||
exts.append( Extension('cStringIO', ['cStringIO.c']) )
|
exts.append( Extension('cStringIO', ['cStringIO.c']) )
|
||||||
exts.append( Extension('cPickle', ['cPickle.c']) )
|
|
||||||
|
|
||||||
# Memory-mapped files (also works on Win32).
|
# Memory-mapped files (also works on Win32).
|
||||||
if platform not in ['atheos', 'mac']:
|
if platform not in ['atheos', 'mac']:
|
||||||
|
|
Loading…
Reference in New Issue