Commit f7fa63dd authored by Alexandre Vassalotti's avatar Alexandre Vassalotti

Rename copy_reg module to copyreg.

Updated documentation.
Merged revisions 63042 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r63042 | alexandre.vassalotti | 2008-05-11 04:25:28 -0400 (Sun, 11 May 2008) | 5 lines

  Added module stub for copy_reg renaming in 3.0.
  Renamed copy_reg to copyreg in the standard library, to avoid
  spurious warnings and ease later merging to py3k branch. Public
  documentation remains intact.
........
parent 7760008c
......@@ -60,7 +60,7 @@ of lists by assigning a slice of the entire list, for example,
Classes can use the same interfaces to control copying that they use to control
pickling. See the description of module :mod:`pickle` for information on these
methods. The :mod:`copy` module does not use the :mod:`copy_reg` registration
methods. The :mod:`copy` module does not use the :mod:`copyreg` registration
module.
.. index::
......
:mod:`copy_reg` --- Register :mod:`pickle` support functions
:mod:`copyreg` --- Register :mod:`pickle` support functions
============================================================
.. module:: copy_reg
.. module:: copyreg
:synopsis: Register pickle support functions.
......@@ -11,7 +11,7 @@
module: cPickle
module: copy
The :mod:`copy_reg` module provides support for the :mod:`pickle` and
The :mod:`copyreg` module provides support for the :mod:`pickle` and
:mod:`cPickle` modules. The :mod:`copy` module is likely to use this in the
future as well. It provides configuration information about object constructors
which are not classes. Such constructors may be factory functions or class
......
......@@ -95,7 +95,7 @@ Sample output (may vary depending on the architecture)::
Loaded modules:
_types:
copy_reg: _inverted_registry,_slotnames,__all__
copyreg: _inverted_registry,_slotnames,__all__
sre_compile: isstring,_sre,_optimize_unicode
_sre:
sre_constants: REPEAT_ONE,makedict,AT_END_LINE
......
......@@ -19,7 +19,7 @@ The list of modules described in this chapter is:
.. toctree::
pickle.rst
copy_reg.rst
copyreg.rst
shelve.rst
marshal.rst
anydbm.rst
......
......@@ -524,7 +524,7 @@ not :meth:`__reduce_ex__`, the :meth:`__reduce_ex__` implementation detects this
and calls :meth:`__reduce__`.
An alternative to implementing a :meth:`__reduce__` method on the object to be
pickled, is to register the callable with the :mod:`copy_reg` module. This
pickled, is to register the callable with the :mod:`copyreg` module. This
module provides a way for programs to register "reduction functions" and
constructors for user-defined types. Reduction functions have the same
semantics and interface as the :meth:`__reduce__` method described above, except
......@@ -775,7 +775,7 @@ the same process or a new process. ::
.. seealso::
Module :mod:`copy_reg`
Module :mod:`copyreg`
Pickle interface constructor registration for extension types.
Module :mod:`shelve`
......
......@@ -49,7 +49,7 @@ __getstate__() and __setstate__(). See the documentation for module
"""
import types
from copy_reg import dispatch_table
from copyreg import dispatch_table
class Error(Exception):
pass
......
......@@ -37,7 +37,7 @@ import pickle
import threading
import Queue
import traceback
import copy_reg
import copyreg
import types
import marshal
......@@ -60,8 +60,8 @@ def pickle_code(co):
# assert isinstance(fn, type.FunctionType)
# return repr(fn)
copy_reg.pickle(types.CodeType, pickle_code, unpickle_code)
# copy_reg.pickle(types.FunctionType, pickle_function, unpickle_function)
copyreg.pickle(types.CodeType, pickle_code, unpickle_code)
# copyreg.pickle(types.FunctionType, pickle_function, unpickle_function)
BUFSIZE = 8*1024
LOCALHOST = '127.0.0.1'
......
......@@ -588,7 +588,7 @@ otherwise return -SIG, where SIG is the signal that killed it. """
__all__.extend(["spawnvp", "spawnvpe", "spawnlp", "spawnlpe",])
import copy_reg as _copy_reg
import copyreg as _copyreg
def _make_stat_result(tup, dict):
return stat_result(tup, dict)
......@@ -598,7 +598,7 @@ def _pickle_stat_result(sr):
return (_make_stat_result, args)
try:
_copy_reg.pickle(stat_result, _pickle_stat_result, _make_stat_result)
_copyreg.pickle(stat_result, _pickle_stat_result, _make_stat_result)
except NameError: # stat_result may not exist
pass
......@@ -610,7 +610,7 @@ def _pickle_statvfs_result(sr):
return (_make_statvfs_result, args)
try:
_copy_reg.pickle(statvfs_result, _pickle_statvfs_result,
_copyreg.pickle(statvfs_result, _pickle_statvfs_result,
_make_statvfs_result)
except NameError: # statvfs_result may not exist
pass
......
"""Create portable serialized representations of Python objects.
See module copy_reg for a mechanism for registering custom picklers.
See module copyreg for a mechanism for registering custom picklers.
See module pickletools source for extensive comments.
Classes:
......@@ -26,8 +26,8 @@ Misc variables:
__version__ = "$Revision$" # Code version
from types import FunctionType, BuiltinFunctionType
from copy_reg import dispatch_table
from copy_reg import _extension_registry, _inverted_registry, _extension_cache
from copyreg import dispatch_table
from copyreg import _extension_registry, _inverted_registry, _extension_cache
import marshal
import sys
import struct
......@@ -299,7 +299,7 @@ class Pickler:
self.save_global(obj)
return
# Check copy_reg.dispatch_table
# Check copyreg.dispatch_table
reduce = dispatch_table.get(t)
if reduce:
rv = reduce(obj)
......
......@@ -142,7 +142,7 @@ this and there isn't a use case that warrants the expense of such an
analysis.
To this end, all tests for __safe_for_unpickling__ or for
copy_reg.safe_constructors are removed from the unpickling code.
copyreg.safe_constructors are removed from the unpickling code.
References to these variables in the descriptions below are to be seen
as describing unpickling in Python 2.2 and before.
"""
......@@ -1559,7 +1559,7 @@ opcodes = [
BUILD opcode to apply __setstate__ to that argument.
If not isinstance(callable, type), REDUCE complains unless the
callable has been registered with the copy_reg module's
callable has been registered with the copyreg module's
safe_constructors dict, or the callable has a magic
'__safe_for_unpickling__' attribute with a true value. I'm not sure
why it does this, but I've sure seen this complaint often enough when
......@@ -2137,58 +2137,58 @@ highest protocol among opcodes = 0
0: ( MARK
1: l LIST (MARK at 0)
2: p PUT 0
5: c GLOBAL 'copy_reg _reconstructor'
30: p PUT 1
33: ( MARK
34: c GLOBAL 'pickletools _Example'
56: p PUT 2
59: c GLOBAL 'builtins object'
76: p PUT 3
79: N NONE
80: t TUPLE (MARK at 33)
81: p PUT 4
84: R REDUCE
85: p PUT 5
88: ( MARK
89: d DICT (MARK at 88)
90: p PUT 6
93: V UNICODE 'value'
100: p PUT 7
103: L LONG 42
107: s SETITEM
108: b BUILD
109: a APPEND
110: g GET 5
113: a APPEND
114: . STOP
5: c GLOBAL 'copyreg _reconstructor'
29: p PUT 1
32: ( MARK
33: c GLOBAL 'pickletools _Example'
55: p PUT 2
58: c GLOBAL 'builtins object'
75: p PUT 3
78: N NONE
79: t TUPLE (MARK at 32)
80: p PUT 4
83: R REDUCE
84: p PUT 5
87: ( MARK
88: d DICT (MARK at 87)
89: p PUT 6
92: V UNICODE 'value'
99: p PUT 7
102: L LONG 42
106: s SETITEM
107: b BUILD
108: a APPEND
109: g GET 5
112: a APPEND
113: . STOP
highest protocol among opcodes = 0
>>> dis(pickle.dumps(x, 1))
0: ] EMPTY_LIST
1: q BINPUT 0
3: ( MARK
4: c GLOBAL 'copy_reg _reconstructor'
29: q BINPUT 1
31: ( MARK
32: c GLOBAL 'pickletools _Example'
54: q BINPUT 2
56: c GLOBAL 'builtins object'
73: q BINPUT 3
75: N NONE
76: t TUPLE (MARK at 31)
77: q BINPUT 4
79: R REDUCE
80: q BINPUT 5
82: } EMPTY_DICT
83: q BINPUT 6
85: X BINUNICODE 'value'
95: q BINPUT 7
97: K BININT1 42
99: s SETITEM
100: b BUILD
101: h BINGET 5
103: e APPENDS (MARK at 3)
104: . STOP
4: c GLOBAL 'copyreg _reconstructor'
28: q BINPUT 1
30: ( MARK
31: c GLOBAL 'pickletools _Example'
53: q BINPUT 2
55: c GLOBAL 'builtins object'
72: q BINPUT 3
74: N NONE
75: t TUPLE (MARK at 30)
76: q BINPUT 4
78: R REDUCE
79: q BINPUT 5
81: } EMPTY_DICT
82: q BINPUT 6
84: X BINUNICODE 'value'
94: q BINPUT 7
96: K BININT1 42
98: s SETITEM
99: b BUILD
100: h BINGET 5
102: e APPENDS (MARK at 3)
103: . STOP
highest protocol among opcodes = 1
Try "the canonical" recursive-object test.
......
......@@ -272,12 +272,12 @@ def _subx(pattern, template):
# register myself for pickling
import copy_reg
import copyreg
def _pickle(p):
return _compile, (p.pattern, p.flags)
copy_reg.pickle(_pattern_type, _pickle, _compile)
copyreg.pickle(_pattern_type, _pickle, _compile)
# --------------------------------------------------------------------
# experimental stuff (see python-dev discussions for details)
......
This diff is collapsed.
......@@ -676,7 +676,7 @@ def cleanup_test_droppings(testname, verbose):
def dash_R(the_module, test, indirect_test, huntrleaks):
# This code is hackish and inelegant, but it seems to do the job.
import copy_reg, _abcoll
import copyreg, _abcoll
if not hasattr(sys, 'gettotalrefcount'):
raise Exception("Tracking reference leaks requires a debug build "
......@@ -684,7 +684,7 @@ def dash_R(the_module, test, indirect_test, huntrleaks):
# Save current values for dash_R_cleanup() to restore.
fs = warnings.filters[:]
ps = copy_reg.dispatch_table.copy()
ps = copyreg.dispatch_table.copy()
pic = sys.path_importer_cache.copy()
abcs = {}
for abc in [getattr(_abcoll, a) for a in _abcoll.__all__]:
......@@ -724,7 +724,7 @@ def dash_R(the_module, test, indirect_test, huntrleaks):
refrep.close()
def dash_R_cleanup(fs, ps, pic, abcs):
import gc, copy_reg
import gc, copyreg
import _strptime, linecache
import urlparse, urllib, urllib2, mimetypes, doctest
import struct, filecmp, _abcoll
......@@ -738,8 +738,8 @@ def dash_R_cleanup(fs, ps, pic, abcs):
# Restore some original values.
warnings.filters[:] = fs
copy_reg.dispatch_table.clear()
copy_reg.dispatch_table.update(ps)
copyreg.dispatch_table.clear()
copyreg.dispatch_table.update(ps)
sys.path_importer_cache.clear()
sys.path_importer_cache.update(pic)
......
......@@ -53,7 +53,7 @@ class AllTest(unittest.TestCase):
self.check_all("commands")
self.check_all("compileall")
self.check_all("copy")
self.check_all("copy_reg")
self.check_all("copyreg")
self.check_all("csv")
self.check_all("dbhash")
self.check_all("decimal")
......
"""Unit tests for the copy module."""
import copy
import copy_reg
import copyreg
import unittest
from test import test_support
......@@ -42,7 +42,7 @@ class TestCopy(unittest.TestCase):
return (C, (obj.foo,))
x = C(42)
self.assertRaises(TypeError, copy.copy, x)
copy_reg.pickle(C, pickle_C, C)
copyreg.pickle(C, pickle_C, C)
y = copy.copy(x)
def test_copy_reduce_ex(self):
......@@ -215,7 +215,7 @@ class TestCopy(unittest.TestCase):
return (C, (obj.foo,))
x = C(42)
self.assertRaises(TypeError, copy.deepcopy, x)
copy_reg.pickle(C, pickle_C, C)
copyreg.pickle(C, pickle_C, C)
y = copy.deepcopy(x)
def test_deepcopy_reduce_ex(self):
......
import copy_reg
import copyreg
import unittest
from test import test_support
......@@ -27,15 +27,15 @@ class WithInherited(WithSingleString):
class CopyRegTestCase(unittest.TestCase):
def test_class(self):
self.assertRaises(TypeError, copy_reg.pickle,
self.assertRaises(TypeError, copyreg.pickle,
C, None, None)
def test_noncallable_reduce(self):
self.assertRaises(TypeError, copy_reg.pickle,
self.assertRaises(TypeError, copyreg.pickle,
type(1), "not a callable")
def test_noncallable_constructor(self):
self.assertRaises(TypeError, copy_reg.pickle,
self.assertRaises(TypeError, copyreg.pickle,
type(1), int, "not a callable")
def test_bool(self):
......@@ -47,42 +47,42 @@ class CopyRegTestCase(unittest.TestCase):
e = ExtensionSaver(code)
try:
# Shouldn't be in registry now.
self.assertRaises(ValueError, copy_reg.remove_extension,
self.assertRaises(ValueError, copyreg.remove_extension,
mod, func, code)
copy_reg.add_extension(mod, func, code)
copyreg.add_extension(mod, func, code)
# Should be in the registry.
self.assert_(copy_reg._extension_registry[mod, func] == code)
self.assert_(copy_reg._inverted_registry[code] == (mod, func))
self.assert_(copyreg._extension_registry[mod, func] == code)
self.assert_(copyreg._inverted_registry[code] == (mod, func))
# Shouldn't be in the cache.
self.assert_(code not in copy_reg._extension_cache)
self.assert_(code not in copyreg._extension_cache)
# Redundant registration should be OK.
copy_reg.add_extension(mod, func, code) # shouldn't blow up
copyreg.add_extension(mod, func, code) # shouldn't blow up
# Conflicting code.
self.assertRaises(ValueError, copy_reg.add_extension,
self.assertRaises(ValueError, copyreg.add_extension,
mod, func, code + 1)
self.assertRaises(ValueError, copy_reg.remove_extension,
self.assertRaises(ValueError, copyreg.remove_extension,
mod, func, code + 1)
# Conflicting module name.
self.assertRaises(ValueError, copy_reg.add_extension,
self.assertRaises(ValueError, copyreg.add_extension,
mod[1:], func, code )
self.assertRaises(ValueError, copy_reg.remove_extension,
self.assertRaises(ValueError, copyreg.remove_extension,
mod[1:], func, code )
# Conflicting function name.
self.assertRaises(ValueError, copy_reg.add_extension,
self.assertRaises(ValueError, copyreg.add_extension,
mod, func[1:], code)
self.assertRaises(ValueError, copy_reg.remove_extension,
self.assertRaises(ValueError, copyreg.remove_extension,
mod, func[1:], code)
# Can't remove one that isn't registered at all.
if code + 1 not in copy_reg._inverted_registry:
self.assertRaises(ValueError, copy_reg.remove_extension,
if code + 1 not in copyreg._inverted_registry:
self.assertRaises(ValueError, copyreg.remove_extension,
mod[1:], func[1:], code + 1)
finally:
e.restore()
# Shouldn't be there anymore.
self.assert_((mod, func) not in copy_reg._extension_registry)
# The code *may* be in copy_reg._extension_registry, though, if
self.assert_((mod, func) not in copyreg._extension_registry)
# The code *may* be in copyreg._extension_registry, though, if
# we happened to pick on a registered code. So don't check for
# that.
......@@ -90,25 +90,25 @@ class CopyRegTestCase(unittest.TestCase):
for code in 1, 0x7fffffff:
e = ExtensionSaver(code)
try:
copy_reg.add_extension(mod, func, code)
copy_reg.remove_extension(mod, func, code)
copyreg.add_extension(mod, func, code)
copyreg.remove_extension(mod, func, code)
finally:
e.restore()
# Ensure invalid codes blow up.
for code in -1, 0, 0x80000000:
self.assertRaises(ValueError, copy_reg.add_extension,
self.assertRaises(ValueError, copyreg.add_extension,
mod, func, code)
def test_slotnames(self):
self.assertEquals(copy_reg._slotnames(WithoutSlots), [])
self.assertEquals(copy_reg._slotnames(WithWeakref), [])
self.assertEquals(copyreg._slotnames(WithoutSlots), [])
self.assertEquals(copyreg._slotnames(WithWeakref), [])
expected = ['_WithPrivate__spam']
self.assertEquals(copy_reg._slotnames(WithPrivate), expected)
self.assertEquals(copy_reg._slotnames(WithSingleString), ['spam'])
self.assertEquals(copyreg._slotnames(WithPrivate), expected)
self.assertEquals(copyreg._slotnames(WithSingleString), ['spam'])
expected = ['eggs', 'spam']
expected.sort()
result = copy_reg._slotnames(WithInherited)
result = copyreg._slotnames(WithInherited)
result.sort()
self.assertEquals(result, expected)
......
......@@ -21,6 +21,8 @@ Extension Modules
Library
-------
- The copy_reg module has been renamed to copyreg.
- The mhlib module has been removed.
- The ihooks module has been removed.
......
......@@ -3091,7 +3091,7 @@ initparser(void)
* If this fails, the import of this module will fail because an
* exception will be raised here; should we clear the exception?
*/
copyreg = PyImport_ImportModuleNoBlock("copy_reg");
copyreg = PyImport_ImportModuleNoBlock("copyreg");
if (copyreg != NULL) {
PyObject *func, *pickler;
......
......@@ -2941,31 +2941,31 @@ static PyGetSetDef object_getsets[] = {
/* Stuff to implement __reduce_ex__ for pickle protocols >= 2.
We fall back to helpers in copy_reg for:
We fall back to helpers in copyreg for:
- pickle protocols < 2
- calculating the list of slot names (done only once per class)
- the __newobj__ function (which is used as a token but never called)
*/
static PyObject *
import_copy_reg(void)
import_copyreg(void)
{
static PyObject *copy_reg_str;
static PyObject *copyreg_str;
if (!copy_reg_str) {
copy_reg_str = PyUnicode_InternFromString("copy_reg");
if (copy_reg_str == NULL)
if (!copyreg_str) {
copyreg_str = PyUnicode_InternFromString("copyreg");
if (copyreg_str == NULL)
return NULL;
}
return PyImport_Import(copy_reg_str);
return PyImport_Import(copyreg_str);
}
static PyObject *
slotnames(PyObject *cls)
{
PyObject *clsdict;
PyObject *copy_reg;
PyObject *copyreg;
PyObject *slotnames;
if (!PyType_Check(cls)) {
......@@ -2980,18 +2980,18 @@ slotnames(PyObject *cls)
return slotnames;
}
copy_reg = import_copy_reg();
if (copy_reg == NULL)
copyreg = import_copyreg();
if (copyreg == NULL)
return NULL;
slotnames = PyObject_CallMethod(copy_reg, "_slotnames", "O", cls);
Py_DECREF(copy_reg);
slotnames = PyObject_CallMethod(copyreg, "_slotnames", "O", cls);
Py_DECREF(copyreg);
if (slotnames != NULL &&
slotnames != Py_None &&
!PyList_Check(slotnames))
{
PyErr_SetString(PyExc_TypeError,
"copy_reg._slotnames didn't return a list or None");
"copyreg._slotnames didn't return a list or None");
Py_DECREF(slotnames);
slotnames = NULL;
}
......@@ -3006,7 +3006,7 @@ reduce_2(PyObject *obj)
PyObject *args = NULL, *args2 = NULL;
PyObject *getstate = NULL, *state = NULL, *names = NULL;
PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL;
PyObject *copy_reg = NULL, *newobj = NULL, *res = NULL;
PyObject *copyreg = NULL, *newobj = NULL, *res = NULL;
Py_ssize_t i, n;
cls = PyObject_GetAttrString(obj, "__class__");
......@@ -3105,10 +3105,10 @@ reduce_2(PyObject *obj)
goto end;
}
copy_reg = import_copy_reg();
if (copy_reg == NULL)
copyreg = import_copyreg();
if (copyreg == NULL)
goto end;
newobj = PyObject_GetAttrString(copy_reg, "__newobj__");
newobj = PyObject_GetAttrString(copyreg, "__newobj__");
if (newobj == NULL)
goto end;
......@@ -3135,7 +3135,7 @@ reduce_2(PyObject *obj)
Py_XDECREF(names);
Py_XDECREF(listitems);
Py_XDECREF(dictitems);
Py_XDECREF(copy_reg);
Py_XDECREF(copyreg);
Py_XDECREF(newobj);
return res;
}
......@@ -3158,17 +3158,17 @@ reduce_2(PyObject *obj)
static PyObject *
_common_reduce(PyObject *self, int proto)
{
PyObject *copy_reg, *res;
PyObject *copyreg, *res;
if (proto >= 2)
return reduce_2(self);
copy_reg = import_copy_reg();
if (!copy_reg)
copyreg = import_copyreg();
if (!copyreg)
return NULL;
res = PyEval_CallMethod(copy_reg, "_reduce_ex", "(Oi)", self, proto);
Py_DECREF(copy_reg);
res = PyEval_CallMethod(copyreg, "_reduce_ex", "(Oi)", self, proto);
Py_DECREF(copyreg);
return res;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment