Python copyreg.pickle() Examples
The following are 30
code examples of copyreg.pickle().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
copyreg
, or try the search function
.
![](https://www.programcreek.com/common/static/images/search.png)
Example #1
Source File: cloudpickle.py From FATE with Apache License 2.0 | 6 votes |
def dumps(obj, protocol=None): """Serialize obj as a string of bytes allocated in memory protocol defaults to cloudpickle.DEFAULT_PROTOCOL which is an alias to pickle.HIGHEST_PROTOCOL. This setting favors maximum communication speed between processes running the same Python version. Set protocol=pickle.DEFAULT_PROTOCOL instead if you need to ensure compatibility with older versions of Python. """ file = StringIO() try: cp = CloudPickler(file, protocol=protocol) cp.dump(obj) return file.getvalue() finally: file.close() # including pickles unloading functions in this namespace
Example #2
Source File: pickletester.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def test_badly_quoted_string(self): # Issue #17710 badpickles = [b"S'\n.", b'S"\n.', b'S\' \n.', b'S" \n.', b'S\'"\n.', b'S"\'\n.', b"S' ' \n.", b'S" " \n.', b"S ''\n.", b'S ""\n.', b'S \n.', b'S\n.', b'S.'] for p in badpickles: self.assertRaises(pickle.UnpicklingError, self.loads, p)
Example #3
Source File: pickle_import_export.py From douglas-quaid with GNU General Public License v3.0 | 6 votes |
def patch_Keypoint_pickiling(): # Create the bundling between class and arguements to save for Keypoint class # See : https://stackoverflow.com/questions/50337569/pickle-exception-for-cv2-boost-when-using-multiprocessing/50394788#50394788 def _pickle_keypoint(keypoint): # : cv2.KeyPoint return cv2.KeyPoint, ( keypoint.pt[0], keypoint.pt[1], keypoint.size, keypoint.angle, keypoint.response, keypoint.octave, keypoint.class_id, ) # C++ : KeyPoint (float x, float y, float _size, float _angle=-1, float _response=0, int _octave=0, int _class_id=-1) # Python: cv2.KeyPoint([x, y, _size[, _angle[, _response[, _octave[, _class_id]]]]]) → <KeyPoint object> # Apply the bundling to pickle copyreg.pickle(cv2.KeyPoint().__class__, _pickle_keypoint) # non static, to be sure we patched it before use, only once
Example #4
Source File: styles.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def _pickleFunction(f): """ Reduce, in the sense of L{pickle}'s C{object.__reduce__} special method, a function object into its constituent parts. @param f: The function to reduce. @type f: L{types.FunctionType} @return: a 2-tuple of a reference to L{_unpickleFunction} and a tuple of its arguments, a 1-tuple of the function's fully qualified name. @rtype: 2-tuple of C{callable, native string} """ if f.__name__ == '<lambda>': raise _UniversalPicklingError( "Cannot pickle lambda function: {}".format(f)) return (_unpickleFunction, tuple([".".join([f.__module__, f.__qualname__])]))
Example #5
Source File: cloudpickle.py From FATE with Apache License 2.0 | 6 votes |
def _save_subimports(self, code, top_level_dependencies): """ Ensure de-pickler imports any package child-modules that are needed by the function """ # check if any known dependency is an imported package for x in top_level_dependencies: if isinstance(x, types.ModuleType) and hasattr(x, '__package__') and x.__package__: # check if the package has any currently loaded sub-imports prefix = x.__name__ + '.' for name, module in sys.modules.items(): # Older versions of pytest will add a "None" module to sys.modules. if name is not None and name.startswith(prefix): # check whether the function can address the sub-module tokens = set(name[len(prefix):].split('.')) if not tokens - set(code.co_names): # ensure unpickler executes this import self.save(module) # then discards the reference to it self.write(pickle.POP)
Example #6
Source File: test_persisted.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_nonIdentityHash(self): global ClassWithCustomHash class ClassWithCustomHash(styles.Versioned): def __init__(self, unique, hash): self.unique = unique self.hash = hash def __hash__(self): return self.hash v1 = ClassWithCustomHash('v1', 0) v2 = ClassWithCustomHash('v2', 0) pkl = pickle.dumps((v1, v2)) del v1, v2 ClassWithCustomHash.persistenceVersion = 1 ClassWithCustomHash.upgradeToVersion1 = lambda self: setattr(self, 'upgraded', True) v1, v2 = pickle.loads(pkl) styles.doUpgrade() self.assertEqual(v1.unique, 'v1') self.assertEqual(v2.unique, 'v2') self.assertTrue(v1.upgraded) self.assertTrue(v2.upgraded)
Example #7
Source File: test_persisted.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_upgradeDeserializesObjectsRequiringUpgrade(self): global ToyClassA, ToyClassB class ToyClassA(styles.Versioned): pass class ToyClassB(styles.Versioned): pass x = ToyClassA() y = ToyClassB() pklA, pklB = pickle.dumps(x), pickle.dumps(y) del x, y ToyClassA.persistenceVersion = 1 def upgradeToVersion1(self): self.y = pickle.loads(pklB) styles.doUpgrade() ToyClassA.upgradeToVersion1 = upgradeToVersion1 ToyClassB.persistenceVersion = 1 ToyClassB.upgradeToVersion1 = lambda self: setattr(self, 'upgraded', True) x = pickle.loads(pklA) styles.doUpgrade() self.assertTrue(x.y.upgraded)
Example #8
Source File: cloudpickle.py From FATE with Apache License 2.0 | 6 votes |
def dumps(obj, protocol=None): """Serialize obj as a string of bytes allocated in memory protocol defaults to cloudpickle.DEFAULT_PROTOCOL which is an alias to pickle.HIGHEST_PROTOCOL. This setting favors maximum communication speed between processes running the same Python version. Set protocol=pickle.DEFAULT_PROTOCOL instead if you need to ensure compatibility with older versions of Python. """ file = StringIO() try: cp = CloudPickler(file, protocol=protocol) cp.dump(obj) return file.getvalue() finally: file.close() # including pickles unloading functions in this namespace
Example #9
Source File: cloudpickle.py From FATE with Apache License 2.0 | 6 votes |
def _save_subimports(self, code, top_level_dependencies): """ Ensure de-pickler imports any package child-modules that are needed by the function """ # check if any known dependency is an imported package for x in top_level_dependencies: if isinstance(x, types.ModuleType) and hasattr(x, '__package__') and x.__package__: # check if the package has any currently loaded sub-imports prefix = x.__name__ + '.' for name, module in sys.modules.items(): # Older versions of pytest will add a "None" module to sys.modules. if name is not None and name.startswith(prefix): # check whether the function can address the sub-module tokens = set(name[len(prefix):].split('.')) if not tokens - set(code.co_names): # ensure unpickler executes this import self.save(module) # then discards the reference to it self.write(pickle.POP)
Example #10
Source File: encapsulation.py From scoop with GNU Lesser General Public License v3.0 | 6 votes |
def __init__(self, in_func, name): """Creates a serializable (picklable) object of a function""" self.code = marshal.dumps(in_func.__code__) self.name = name self.defaults = in_func.__defaults__ # Pickle references to functions used in the function used_globals = {} # name: function used_modules = {} # used name: origin module name for key, value in in_func.__globals__.items(): if key in in_func.__code__.co_names: if ismodule(value): used_modules[key] = value.__name__ else: used_globals[key] = value self.globals = pickle.dumps(used_globals, pickle.HIGHEST_PROTOCOL) self.imports = used_modules
Example #11
Source File: encapsulation.py From scoop with GNU Lesser General Public License v3.0 | 6 votes |
def writeFile(self, directory=None): """Writes back the file to a temporary path (optionaly specified)""" if directory: # If a directory was specified full_path = os.path.join(directory, self.filename) with open(full_path, 'wb') as f: f.write(pickle.loads(self.data).read()) return full_path # if no directory was specified, create a temporary file this_file = tempfile.NamedTemporaryFile(delete=False) this_file.write(pickle.loads(self.data).read()) this_file.close() return this_file.name # The following block handles callables pickling and unpickling # TODO: Make a factory to generate unpickling functions
Example #12
Source File: __init__.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def _ufunc_reduce(func): from pickle import whichmodule name = func.__name__ return _ufunc_reconstruct, (whichmodule(func, name), name)
Example #13
Source File: pickletester.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_load_python2_str_as_bytes(self): # From Python 2: pickle.dumps('a\x00\xa0', protocol=0) self.assertEqual(self.loads(b"S'a\\x00\\xa0'\n.", encoding="bytes"), b'a\x00\xa0') # From Python 2: pickle.dumps('a\x00\xa0', protocol=1) self.assertEqual(self.loads(b'U\x03a\x00\xa0.', encoding="bytes"), b'a\x00\xa0') # From Python 2: pickle.dumps('a\x00\xa0', protocol=2) self.assertEqual(self.loads(b'\x80\x02U\x03a\x00\xa0.', encoding="bytes"), b'a\x00\xa0')
Example #14
Source File: __init__.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def _ufunc_reconstruct(module, name): # The `fromlist` kwarg is required to ensure that `mod` points to the # inner-most module rather than the parent package when module name is # nested. This makes it possible to pickle non-toplevel ufuncs such as # scipy.special.expit for instance. mod = __import__(module, fromlist=[name]) return getattr(mod, name)
Example #15
Source File: __init__.py From nbodykit with GNU General Public License v3.0 | 5 votes |
def _setup_for_distributed(): CurrentMPIComm._stack[-1] = MPI.COMM_SELF try: import copyreg except ImportError: # Python 2 import copy_reg as copyreg copyreg.pickle(MPI.Comm, _comm_pickle, _unpickle) copyreg.pickle(MPI.Intracomm, _comm_pickle, _unpickle) set_options(dask_chunk_size=1024 * 1024 * 2)
Example #16
Source File: __init__.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def _ufunc_reduce(func): from pickle import whichmodule name = func.__name__ return _ufunc_reconstruct, (whichmodule(func, name), name)
Example #17
Source File: pickletester.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_negative_32b_binbytes(self): # On 32-bit builds, a BINBYTES of 2**31 or more is refused dumped = b'\x80\x03B\xff\xff\xff\xffxyzq\x00.' with self.assertRaises((pickle.UnpicklingError, OverflowError)): self.loads(dumped)
Example #18
Source File: pickletester.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_negative_32b_binunicode(self): # On 32-bit builds, a BINUNICODE of 2**31 or more is refused dumped = b'\x80\x03X\xff\xff\xff\xffxyzq\x00.' with self.assertRaises((pickle.UnpicklingError, OverflowError)): self.loads(dumped)
Example #19
Source File: pickletester.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_large_32b_binunicode8(self): dumped = b'\x80\x04\x8d\4\0\0\0\1\0\0\0\xe2\x82\xac\x00.' with self.assertRaises((pickle.UnpicklingError, OverflowError)): self.loads(dumped)
Example #20
Source File: cloudpickle.py From FATE with Apache License 2.0 | 5 votes |
def dump(obj, file, protocol=None): """Serialize obj as bytes streamed into file protocol defaults to cloudpickle.DEFAULT_PROTOCOL which is an alias to pickle.HIGHEST_PROTOCOL. This setting favors maximum communication speed between processes running the same Python version. Set protocol=pickle.DEFAULT_PROTOCOL instead if you need to ensure compatibility with older versions of Python. """ CloudPickler(file, protocol=protocol).dump(obj)
Example #21
Source File: cloudpickle.py From FATE with Apache License 2.0 | 5 votes |
def save_file(self, obj): """Save a file""" try: import StringIO as pystringIO # we can't use cStringIO as it lacks the name attribute except ImportError: import io as pystringIO if not hasattr(obj, 'name') or not hasattr(obj, 'mode'): raise pickle.PicklingError("Cannot pickle files that do not map to an actual file") if obj is sys.stdout: return self.save_reduce(getattr, (sys, 'stdout'), obj=obj) if obj is sys.stderr: return self.save_reduce(getattr, (sys, 'stderr'), obj=obj) if obj is sys.stdin: raise pickle.PicklingError("Cannot pickle standard input") if obj.closed: raise pickle.PicklingError("Cannot pickle closed files") if hasattr(obj, 'isatty') and obj.isatty(): raise pickle.PicklingError("Cannot pickle files that map to tty objects") if 'r' not in obj.mode and '+' not in obj.mode: raise pickle.PicklingError("Cannot pickle files that are not opened for reading: %s" % obj.mode) name = obj.name retval = pystringIO.StringIO() try: # Read the whole file curloc = obj.tell() obj.seek(0) contents = obj.read() obj.seek(curloc) except IOError: raise pickle.PicklingError("Cannot pickle file %s as it cannot be read" % name) retval.write(contents) retval.seek(curloc) retval.name = name self.save(retval) self.memoize(obj)
Example #22
Source File: cloudpickle.py From FATE with Apache License 2.0 | 5 votes |
def save_unsupported(self, obj): raise pickle.PicklingError("Cannot pickle objects of type %s" % type(obj))
Example #23
Source File: cloudpickle.py From FATE with Apache License 2.0 | 5 votes |
def dump(self, obj): self.inject_addons() try: return Pickler.dump(self, obj) except RuntimeError as e: if 'recursion' in e.args[0]: msg = """Could not pickle object as excessively deep recursion required.""" raise pickle.PicklingError(msg)
Example #24
Source File: __init__.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def _ufunc_reduce(func): from pickle import whichmodule name = func.__name__ return _ufunc_reconstruct, (whichmodule(func, name), name)
Example #25
Source File: __init__.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def _ufunc_reconstruct(module, name): # The `fromlist` kwarg is required to ensure that `mod` points to the # inner-most module rather than the parent package when module name is # nested. This makes it possible to pickle non-toplevel ufuncs such as # scipy.special.expit for instance. mod = __import__(module, fromlist=[name]) return getattr(mod, name)
Example #26
Source File: test_futurize.py From kgsgo-dataset-preprocessor with Mozilla Public License 2.0 | 5 votes |
def test_renamed_copy_reg_and_cPickle_modules(self): """ Example from docs.python.org/2/library/copy_reg.html """ before = """ import copy_reg import copy import cPickle class C(object): def __init__(self, a): self.a = a def pickle_c(c): print('pickling a C instance...') return C, (c.a,) copy_reg.pickle(C, pickle_c) c = C(1) d = copy.copy(c) p = cPickle.dumps(c) """ after = """ import copyreg import copy import pickle class C(object): def __init__(self, a): self.a = a def pickle_c(c): print('pickling a C instance...') return C, (c.a,) copyreg.pickle(C, pickle_c) c = C(1) d = copy.copy(c) p = pickle.dumps(c) """ self.convert_check(before, after)
Example #27
Source File: test_futurize.py From kgsgo-dataset-preprocessor with Mozilla Public License 2.0 | 5 votes |
def test_renamed_modules(self): before = """ import ConfigParser import copy_reg import cPickle import cStringIO """ after = """ import configparser import copyreg import pickle import io """ self.convert_check(before, after)
Example #28
Source File: pickletester.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_roundtrip_equality(self): expected = self._testdata for proto in protocols: s = self.dumps(expected, proto) got = self.loads(s) self.assert_is_copy(expected, got) # There are gratuitous differences between pickles produced by # pickle and cPickle, largely because cPickle starts PUT indices at # 1 and pickle starts them at 0. See XXX comment in cPickle's put2() -- # there's a comment with an exclamation point there whose meaning # is a mystery. cPickle also suppresses PUT for objects with a refcount # of 1.
Example #29
Source File: pickling_support.py From pywren-ibm-cloud with Apache License 2.0 | 5 votes |
def pickle_exception(obj): # All exceptions, unlike generic Python objects, define __reduce_ex__ # __reduce_ex__(4) should be no different from __reduce_ex__(3). # __reduce_ex__(5) could bring benefits in the unlikely case the exception # directly contains buffers, but PickleBuffer objects will cause a crash when # running on protocol=4, and there's no clean way to figure out the current # protocol from here. Note that any object returned by __reduce_ex__(3) will # still be pickled with protocol 5 if pickle.dump() is running with it. rv = obj.__reduce_ex__(3) if isinstance(rv, str): raise TypeError("str __reduce__ output is not supported") assert isinstance(rv, tuple) and len(rv) >= 2 return (unpickle_exception, rv[:2] + (obj.__cause__, obj.__traceback__)) + rv[2:]
Example #30
Source File: cloudpickle.py From FATE with Apache License 2.0 | 5 votes |
def _get_module_builtins(): return pickle.__builtins__