Rename the extension registry variables to have leading underscores --
this clarifies that they are part of an internal API (albeit shared between pickle.py, copy_reg.py and cPickle.c). I'd like to do the same for copy_reg.dispatch_table, but worry that it might be used by existing code. This risk doesn't exist for the extension registry.
This commit is contained in:
parent
322d553143
commit
d4b920c8bd
|
@ -78,9 +78,9 @@ def _reduce(self):
|
|||
# don't have this restriction.) Codes are positive ints; 0 is
|
||||
# reserved.
|
||||
|
||||
extension_registry = {} # key -> code
|
||||
inverted_registry = {} # code -> key
|
||||
extension_cache = {} # code -> object
|
||||
_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
|
||||
# it's initialized, and won't see a rebinding.
|
||||
|
||||
|
@ -90,32 +90,32 @@ def add_extension(module, name, code):
|
|||
if not 1 <= code < 0x7fffffff:
|
||||
raise ValueError, "code out of range"
|
||||
key = (module, name)
|
||||
if (extension_registry.get(key) == code and
|
||||
inverted_registry.get(code) == key):
|
||||
if (_extension_registry.get(key) == code and
|
||||
_inverted_registry.get(code) == key):
|
||||
return # Redundant registrations are benign
|
||||
if key in extension_registry:
|
||||
if key in _extension_registry:
|
||||
raise ValueError("key %s is already registered with code %s" %
|
||||
(key, extension_registry[key]))
|
||||
if code in inverted_registry:
|
||||
(key, _extension_registry[key]))
|
||||
if code in _inverted_registry:
|
||||
raise ValueError("code %s is already in use for key %s" %
|
||||
(code, inverted_registry[code]))
|
||||
extension_registry[key] = code
|
||||
inverted_registry[code] = key
|
||||
(code, _inverted_registry[code]))
|
||||
_extension_registry[key] = code
|
||||
_inverted_registry[code] = key
|
||||
|
||||
def remove_extension(module, name, code):
|
||||
"""Unregister an extension code. For testing only."""
|
||||
key = (module, name)
|
||||
if (extension_registry.get(key) != code or
|
||||
inverted_registry.get(code) != key):
|
||||
if (_extension_registry.get(key) != code or
|
||||
_inverted_registry.get(code) != key):
|
||||
raise ValueError("key %s is not registered with code %s" %
|
||||
(key, code))
|
||||
del extension_registry[key]
|
||||
del inverted_registry[code]
|
||||
if code in extension_cache:
|
||||
del extension_cache[code]
|
||||
del _extension_registry[key]
|
||||
del _inverted_registry[code]
|
||||
if code in _extension_cache:
|
||||
del _extension_cache[code]
|
||||
|
||||
def clear_extension_cache():
|
||||
extension_cache.clear()
|
||||
_extension_cache.clear()
|
||||
|
||||
# Standard extension code assignments
|
||||
|
||||
|
|
|
@ -28,7 +28,7 @@ __version__ = "$Revision$" # Code version
|
|||
|
||||
from types import *
|
||||
from copy_reg import dispatch_table, _reconstructor
|
||||
from copy_reg import extension_registry, inverted_registry, extension_cache
|
||||
from copy_reg import _extension_registry, _inverted_registry, _extension_cache
|
||||
import marshal
|
||||
import sys
|
||||
import struct
|
||||
|
@ -845,7 +845,7 @@ class Pickler:
|
|||
(obj, module, name))
|
||||
|
||||
if self.proto >= 2:
|
||||
code = extension_registry.get((module, name))
|
||||
code = _extension_registry.get((module, name))
|
||||
if code:
|
||||
assert code > 0
|
||||
if code <= 0xff:
|
||||
|
@ -1238,15 +1238,15 @@ class Unpickler:
|
|||
|
||||
def get_extension(self, code):
|
||||
nil = []
|
||||
obj = extension_cache.get(code, nil)
|
||||
obj = _extension_cache.get(code, nil)
|
||||
if obj is not nil:
|
||||
self.append(obj)
|
||||
return
|
||||
key = inverted_registry.get(code)
|
||||
key = _inverted_registry.get(code)
|
||||
if not key:
|
||||
raise ValueError("unregistered extension code %d" % code)
|
||||
obj = self.find_class(*key)
|
||||
extension_cache[code] = obj
|
||||
_extension_cache[code] = obj
|
||||
self.append(obj)
|
||||
|
||||
def find_class(self, module, name):
|
||||
|
|
|
@ -102,11 +102,11 @@ static PyObject *empty_tuple;
|
|||
static PyObject *dispatch_table;
|
||||
|
||||
/* For EXT[124] opcodes. */
|
||||
/* copy_reg.extension_registry, {(module_name, function_name): code} */
|
||||
/* copy_reg._extension_registry, {(module_name, function_name): code} */
|
||||
static PyObject *extension_registry;
|
||||
/* copy_reg.inverted_registry, {code: (module_name, function_name)} */
|
||||
/* copy_reg._inverted_registry, {code: (module_name, function_name)} */
|
||||
static PyObject *inverted_registry;
|
||||
/* copy_reg.extension_cache, {code: object} */
|
||||
/* copy_reg._extension_cache, {code: object} */
|
||||
static PyObject *extension_cache;
|
||||
|
||||
static PyObject *__class___str, *__getinitargs___str, *__dict___str,
|
||||
|
@ -5092,15 +5092,15 @@ init_stuff(PyObject *module_dict)
|
|||
if (!dispatch_table) return -1;
|
||||
|
||||
extension_registry = PyObject_GetAttrString(copy_reg,
|
||||
"extension_registry");
|
||||
"_extension_registry");
|
||||
if (!extension_registry) return -1;
|
||||
|
||||
inverted_registry = PyObject_GetAttrString(copy_reg,
|
||||
"inverted_registry");
|
||||
"_inverted_registry");
|
||||
if (!inverted_registry) return -1;
|
||||
|
||||
extension_cache = PyObject_GetAttrString(copy_reg,
|
||||
"extension_cache");
|
||||
"_extension_cache");
|
||||
if (!extension_cache) return -1;
|
||||
|
||||
Py_DECREF(copy_reg);
|
||||
|
|
Loading…
Reference in New Issue