Getting rid of cPickle. Mmm, feels good!

This commit is contained in:
Guido van Rossum 2007-07-20 00:22:32 +00:00
parent be6fe5476c
commit 99603b0c1e
22 changed files with 38 additions and 5846 deletions

View File

@ -149,11 +149,10 @@ the value to a string, when the values are set dictionary-style.
SerialCookie
The SerialCookie expects that all values should be serialized using
cPickle (or pickle, if cPickle isn't available). As a result of
serializing, SerialCookie can save almost any Python object to a
value, and recover the exact same object when the cookie has been
returned. (SerialCookie can yield some strange-looking cookie
values, however.)
pickle. As a result of serializing, SerialCookie can save almost any
Python object to a value, and recover the exact same object when the
cookie has been returned. (SerialCookie can yield some
strange-looking cookie values, however.)
>>> C = Cookie.SerialCookie()
>>> C["number"] = 7
@ -162,7 +161,7 @@ values, however.)
7
>>> C["string"].value
'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."'
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.
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,
when the load() method parses out values, it attempts to de-serialize
the value. If it fails, then it fallsback to treating the value
@ -212,10 +211,7 @@ Finis.
#
import string
try:
from cPickle import dumps, loads
except ImportError:
from pickle import dumps, loads
from pickle import dumps, loads
import re, warnings
@ -660,7 +656,7 @@ class SimpleCookie(BaseCookie):
class SerialCookie(BaseCookie):
"""SerialCookie
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
representations. IF AN INCOMING VALUE IS NOT IN A VALID PICKLE
FORMAT, THEN AN EXCEPTION WILL BE RAISED.
@ -687,7 +683,7 @@ class SmartCookie(BaseCookie):
"""SmartCookie
SmartCookie supports arbitrary objects as cookie values. If the
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.
Note: Large cookie values add overhead because they must be

View File

@ -22,7 +22,7 @@ import sys
import copy
import xdrlib
import random
import cPickle as pickle
import pickle
try:
# For Pythons w/distutils pybsddb

View File

@ -21,11 +21,7 @@
# $Id$
import sys, os, re
try:
import cPickle
pickle = cPickle
except ImportError:
import pickle
import pickle
import tempfile
import unittest

View File

@ -1,10 +1,6 @@
import sys, os
import pickle
try:
import cPickle
except ImportError:
cPickle = None
import unittest
import glob
@ -62,10 +58,6 @@ class pickleTestCase(unittest.TestCase):
def test01_pickle_DBError(self):
self._base_test_pickle_DBError(pickle=pickle)
if cPickle:
def test02_cPickle_DBError(self):
self._base_test_pickle_DBError(pickle=cPickle)
#----------------------------------------------------------------------
def test_suite():

View File

@ -58,7 +58,7 @@ def NamedTuple(typename, s):
if __name__ == '__main__':
# verify that instances are pickable
from cPickle import loads, dumps
from pickle import loads, dumps
Point = NamedTuple('Point', 'x y')
p = Point(x=10, y=20)
assert p == loads(dumps(p))

View File

@ -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
C, not for instances of user-defined classes.
@ -146,7 +146,7 @@ def _slotnames(cls):
_extension_registry = {} # key -> code
_inverted_registry = {} # code -> key
_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.
def add_extension(module, name, code):

View File

@ -33,7 +33,7 @@ import socket
import select
import SocketServer
import struct
import cPickle as pickle
import pickle
import threading
import Queue
import traceback

View File

@ -28,10 +28,7 @@ To use, simply 'import logging' and log away!
"""
import sys, logging, socket, os, struct, time, glob
try:
import cPickle as pickle
except ImportError:
import pickle
import pickle
from stat import ST_DEV, ST_INO
try:

View File

@ -1,6 +1,5 @@
"""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 pickletools source for extensive comments.
@ -48,8 +47,7 @@ compatible_formats = ["1.0", # Original protocol 0
"2.0", # Protocol 2
] # Old format versions we can read
# Keep in synch with cPickle. This is the highest protocol number we
# know how to read.
# This is the highest protocol number we know how to read.
HIGHEST_PROTOCOL = 2
# The protocol we write by default. May be less than HIGHEST_PROTOCOL.
@ -591,8 +589,6 @@ class Pickler:
dispatch[list] = save_list
# Keep in synch with cPickle's BATCHSIZE. Nothing will break if it gets
# out of synch, though.
_BATCHSIZE = 1000
def _batch_appends(self, items):
@ -1090,7 +1086,12 @@ class Unpickler:
stack = self.stack
args = stack.pop()
func = stack[-1]
try:
value = func(*args)
except:
print(sys.exc_info())
print(func, args)
raise
stack[-1] = value
dispatch[REDUCE[0]] = load_reduce

View File

@ -597,7 +597,7 @@ float8 = ArgumentDescriptor(
doc="""An 8-byte binary representation of a float, big-endian.
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
strongly related to the IEEE-754 double format, and, in normal
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__
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
only matters whether it exists (XXX this is a bug; cPickle
requires the attribute to be true). If __safe_for_unpickling__
doesn't exist, UnpicklingError is raised.
only matters whether it exists (XXX this is a bug). If
__safe_for_unpickling__ doesn't exist, UnpicklingError is raised.
Else (the class object does have a __safe_for_unpickling__ attr),
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
gathered into an argument tuple, and then the logic seems identical,
except that no __safe_for_unpickling__ check is done (XXX this is
a bug; cPickle does test __safe_for_unpickling__). See INST for
the gory details.
a bug). See INST for the gory details.
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

View File

@ -1,9 +1,5 @@
import unittest
import pickle
try:
import cPickle
except ImportError:
cPickle = None
import pickletools
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
# for proto in protocols:
# kind of outer loop.
if cPickle is not None:
assert pickle.HIGHEST_PROTOCOL == cPickle.HIGHEST_PROTOCOL == 2
protocols = range(pickle.HIGHEST_PROTOCOL + 1)

View File

@ -268,34 +268,6 @@ class BoolTest(unittest.TestCase):
self.assertIs(pickle.loads(pickle.dumps(True, True)), True)
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):
# Test for specific backwards-compatible pickle values
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(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):
# Verify that TypeError occurs when bad things are returned
# from __bool__(). This isn't really a bool test, but

View File

@ -4,7 +4,6 @@ import os
import re
import sys
import pickle
import cPickle
import tempfile
import unittest
import test.test_support
@ -641,11 +640,10 @@ class BytesTest(unittest.TestCase):
self.assertEqual(b.rpartition(b'i'), (b'mississipp', b'i', b''))
def test_pickling(self):
for pm in pickle, cPickle:
for proto in range(pm.HIGHEST_PROTOCOL):
for proto in range(pickle.HIGHEST_PROTOCOL):
for b in b"", b"a", b"abc", b"\xffab\x80", b"\0\0\377\0\0":
ps = pm.dumps(b, proto)
q = pm.loads(ps)
ps = pickle.dumps(b, proto)
q = pickle.loads(ps)
self.assertEqual(b, q)
def test_strip(self):

View File

@ -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()

View File

@ -2807,10 +2807,6 @@ def pickles():
if verbose:
print("Testing pickling and copying new-style classes and objects...")
import pickle
try:
import cPickle
except ImportError:
cPickle = None
def sorteditems(d):
return sorted(d.items())
@ -2863,9 +2859,7 @@ def pickles():
class C4(C4classic, object): # mixed inheritance
pass
for p in pickle, cPickle:
if p is None:
continue # cPickle not found -- skip it
for p in [pickle]:
for bin in 0, 1:
if verbose:
print(p.__name__, ["text", "binary"][bin])
@ -2925,7 +2919,7 @@ def pickles():
def pickleslots():
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
# (when using protocols 0 or 1)
global B, C, D, E
@ -2942,24 +2936,12 @@ def pickleslots():
pass
else:
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:
pickle.dumps(C(), 0)
except TypeError:
pass
else:
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__
class C(base):
__slots__ = ['a']
@ -2984,20 +2966,14 @@ def pickleslots():
x = C()
y = pickle.loads(pickle.dumps(x))
vereq(hasattr(y, 'a'), 0)
y = cPickle.loads(cPickle.dumps(x))
vereq(hasattr(y, 'a'), 0)
x.a = 42
y = pickle.loads(pickle.dumps(x))
vereq(y.a, 42)
y = cPickle.loads(cPickle.dumps(x))
vereq(y.a, 42)
x = D()
x.a = 42
x.b = 100
y = pickle.loads(pickle.dumps(x))
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
class E(C):
__slots__ = ['b']
@ -3007,9 +2983,6 @@ def pickleslots():
y = pickle.loads(pickle.dumps(x))
vereq(y.a, x.a)
vereq(y.b, x.b)
y = cPickle.loads(cPickle.dumps(x))
vereq(y.a, x.a)
vereq(y.b, x.b)
def copies():
if verbose: print("Testing copy.copy() and copy.deepcopy()...")

View File

@ -4,10 +4,6 @@ import os
import sys
import unittest
import pickle
try:
import cPickle
except ImportError:
cPickle = None
from test.test_support import (TESTFN, unlink, run_unittest,
guard_warnings_filter)
@ -299,9 +295,7 @@ class ExceptionTests(unittest.TestCase):
value, expected[checkArgName]))
# test for pickling support
for p in pickle, cPickle:
if p is None:
continue # cPickle not found -- skip it
for p in [pickle]:
for protocol in range(p.HIGHEST_PROTOCOL + 1):
s = p.dumps(e, protocol)
new = p.loads(s)

View File

@ -412,12 +412,6 @@ class ReTests(unittest.TestCase):
def test_pickling(self):
import 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
import warnings
with guard_warnings_filter():

View File

@ -2,7 +2,7 @@
import unittest
from test import test_support
from cPickle import loads, dumps
from pickle import loads, dumps
import sys

View File

@ -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()

View File

@ -58,11 +58,7 @@ import tokenize
import types
import gc
try:
import cPickle
pickle = cPickle
except ImportError:
import pickle
import pickle
def usage(outfile):
outfile.write("""Usage: %s [OPTIONS] <file> [ARGS]

File diff suppressed because it is too large Load Diff

View File

@ -460,9 +460,8 @@ class PyBuildExt(build_ext):
# Fred Drake's interface to the Python parser
exts.append( Extension('parser', ['parsermodule.c']) )
# cStringIO and cPickle
# cStringIO
exts.append( Extension('cStringIO', ['cStringIO.c']) )
exts.append( Extension('cPickle', ['cPickle.c']) )
# Memory-mapped files (also works on Win32).
if platform not in ['atheos', 'mac']: