Picklingerror: Cannot Pickle Files That Are Not Opened for Reading
Python pickle.PicklingError() Examples
The following are xxx code examples for showing how to utilize pickle.PicklingError() . These examples are extracted from open source projects. You tin vote up the ones you lot similar or vote down the ones you don't like, and get to the original project or source file by following the links in a higher place each example.
Y'all may check out the related API usage on the sidebar.
You may likewise desire to check out all available functions/classes of the module pickle , or try the search function .
Instance 1
def __getstate__(self): d = dict(cocky.__dict__) def get_pickleable_dict(cacheDict): pickleableCache = dict() for g, v in cacheDict.items(): try: _pickle.dumps(v) pickleableCache[k] = five except TypeError every bit east: if isinstance(five, dict): self.unpickleable.add(str(one thousand[0]) + str(blazon(v)) + str(e) + str(list(five.keys()))) else: cocky.unpickleable.add(str(k[0]) + str(type(v)) + str(eastward)) # + str(list(five.__dict__.keys()))) except _pickle.PicklingError every bit e: cocky.unpickleable.add(str(k[0]) + str(type(v)) + str(due east)) return pickleableCache d['cache'] = get_pickleable_dict(self.cache) d['outargs'] = get_pickleable_dict(self.outargs) return d
Example 2
def put(self, o): """Encode object ``o`` and write it to the piping. Block gevent-cooperatively until all information is written. The default encoder is ``pickle.dumps``. :arg o: a Python object that is encodable with the encoder of choice. Raises: - :exc:`GIPCError` - :exc:`GIPCClosed` - :exc:`pickle.PicklingError` """ self._validate() with self._lock: bindata = self._encoder(o) self._write(struct.pack("!i", len(bindata)) + bindata)
Case three
def lint(argv: Listing[str]) -> None: """ Invoke Pylint with our preferred options """ print('>>>> Running pylint') args = ['--rcfile=.pylintrc', # Load rcfile beginning. '--ignored-modules=alembic,MySQLdb,flask_sqlalchemy,distutils.dist', # override ignored-modules (codacy hack) '--load-plugins', 'pylint_quotes,pylint_monolith', # Plugins '-f', 'parseable', # Auto-readable output. '-j', str(configuration.get_int('pylint_threads')), # Use four cores for speed. ] args.extend(argv or find_files(file_extension='py')) # pylint: disable=import-outside-toplevel import pylint.lint attempt: linter = pylint.lint.Run(args, exit=False) except PicklingError: print('Error while running pylint with multiprocessing') configuration.write('pylint_threads', 1) lint(argv) return if linter.linter.msg_status: raise TestFailedException(linter.linter.msg_status)
Instance four
def _save_reader_to_cache(reader, cache_file_path): """Salvage a reader object to a pickle file. Args: reader: The Reader instance to exist persisted. cache_file_path: A Path example giving the path to the pickle file location. """ cache_path = cache_file_path.parent os.makedirs(str(cache_path), exist_ok=True) attempt: with cache_file_path.open up('wb') as cache_file: attempt: pickle.dump(reader, cache_file) except (AttributeError, pickle.PicklingError, TypeError) as pickling_error: log.warn("Could non pickle {} because {}".format(reader, pickling_error)) laissez passer except OSError as os_error: log.warn("Could not cache {} because {}".format(reader, os_error))
Case 5
def test_reduce_bad_iterator(cocky): # Issue4176: crash when fourth and 5th items of __reduce__() # are not iterators class C(object): def __reduce__(self): # 4th particular is non an iterator return list, (), None, [], None class D(object): def __reduce__(self): # 5th item is not an iterator return dict, (), None, None, [] # Protocol 0 in Python implementation is less strict and also accepts # iterables. for proto in protocols: try: self.dumps(C(), proto) except (AttributeError, pickle.PicklingError, cPickle.PicklingError): laissez passer try: self.dumps(D(), proto) except (AttributeError, pickle.PicklingError, cPickle.PicklingError): pass
Example vi
def get_pickling_errors(self, obj, seen=None): if seen == None: seen = [] endeavour: land = obj.__getstate__() except AttributeError: return if land == None: return if isinstance(country, tuple): if not isinstance(state[0], dict): state = state[ane] else: land = state[0].update(land[1]) result = {} for i in state: endeavour: pickle.dumps(state[i], protocol=ii) except pickle.PicklingError: if not land[i] in seen: seen.append(state[i]) result[i] = self.get_pickling_errors(state[i], seen) return result
Example vii
def save_global(cocky, obj, proper noun=None, pack=struct.pack): # Nosotros take to override this method in order to bargain with objects # divers interactively in IPython that are not injected in # __main__ kwargs = dict(name=name, pack=pack) if sys.version_info >= (three, 4): del kwargs['pack'] try: Pickler.save_global(cocky, obj, **kwargs) except pickle.PicklingError: Pickler.save_global(self, obj, **kwargs) module = getattr(obj, "__module__", None) if module == '__main__': my_name = name if my_name is None: my_name = obj.__name__ mod = sys.modules[module] if not hasattr(mod, my_name): # IPython doesn't inject the variables define # interactively in __main__ setattr(modern, my_name, obj)
Case viii
def save_global(self, obj, proper name=None, pack=struct.pack): # We have to override this method in order to deal with objects # defined interactively in IPython that are not injected in # __main__ kwargs = dict(name=proper name, pack=pack) if sys.version_info >= (3, 4): del kwargs['pack'] try: Pickler.save_global(self, obj, **kwargs) except pickle.PicklingError: Pickler.save_global(self, obj, **kwargs) module = getattr(obj, "__module__", None) if module == '__main__': my_name = name if my_name is None: my_name = obj.__name__ mod = sys.modules[module] if not hasattr(mod, my_name): # IPython doesn't inject the variables define # interactively in __main__ setattr(modernistic, my_name, obj)
Example 9
def sendFuture(self, hereafter): """Send a Futurity to be executed remotely.""" future = re-create.copy(hereafter) future.greenlet = None future.children = {} try: if shared.getConst(hash(futurity.callable), timeout=0): # Enforce name reference passing if already shared future.callable = SharedElementEncapsulation(hash(future.callable)) self.socket.send_multipart([ Task, pickle.dumps(future.id, pickle.HIGHEST_PROTOCOL), pickle.dumps(future, pickle.HIGHEST_PROTOCOL), ]) except (pickle.PicklingError, TypeError) every bit eastward: # If element not picklable, pickle its proper name # TODO: use its fully qualified proper noun scoop.logger.warn("Pickling Error: {0}".format(e)) future.callable = hash(time to come.callable) self.socket.send_multipart([ TASK, pickle.dumps(time to come.id, pickle.HIGHEST_PROTOCOL), pickle.dumps(future, pickle.HIGHEST_PROTOCOL), ])
Example 10
def sendFuture(self, future): """Transport a Future to be executed remotely.""" try: if shared.getConst(hash(time to come.callable), timeout=0): # Enforce name reference passing if already shared futurity.callable = SharedElementEncapsulation(hash(hereafter.callable)) self.socket.send_multipart([b"TASK", pickle.dumps(futurity, pickle.HIGHEST_PROTOCOL)]) except pickle.PicklingError equally e: # If element not picklable, pickle its name # TODO: apply its fully qualified name scoop.logger.warn("Pickling Mistake: {0}".format(eastward)) previousCallable = future.callable futurity.callable = hash(time to come.callable) cocky.socket.send_multipart([b"Task", pickle.dumps(future, pickle.HIGHEST_PROTOCOL)]) future.callable = previousCallable
Case eleven
def tryPickleOnAllContents3(obj, ignore=None, path=None, verbose=False): """ Definitely find pickle errors Notes ----- In this form, this only finds 1 pickle error and then crashes. If you want to make it work like the other testPickle functions and handle errors, yous could. But usually yous just have to find one unpickleable SOB. """ with tempfile.TemporaryFile() as output: endeavor: MyPickler(output).dump(obj) except (pickle.PicklingError, TypeError): pass
Instance 12
def pickleRoundTrip(self, obj, name, dumper, loader, proto): save_m = sys.modules[name] endeavour: sys.modules[proper noun] = dumper temp = pickle.dumps(obj, proto) sys.modules[proper noun] = loader result = pickle.loads(temp) except pickle.PicklingError as pe: # pyET must be second, because pyET may be (equal to) ET. human = dict([(ET, "cET"), (pyET, "pyET")]) raise back up.TestFailed("Failed to round-trip %r from %r to %r" % (obj, human.get(dumper, dumper), homo.get(loader, loader))) from pe finally: sys.modules[proper name] = save_m return result
Case 13
def pickle_function(func): mod_name = getattr(func, "__module__", None) qname = getattr(func, "__qualname__", None) self_ = getattr(func, "__self__", None) effort: test = unpickle_function(mod_name, qname, self_) except pickle.UnpicklingError: test = None if test is non func: heighten pickle.PicklingError( "Can't pickle {}: information technology's not the aforementioned object as {}".format(func, exam) ) return unpickle_function, (mod_name, qname, self_)
Example 14
def get_pickling_errors(obj, seen=None): if seen == None: seen = [] endeavor: state = obj.__getstate__() except AttributeError: return if state == None: render if isinstance(state, tuple): if not isinstance(state[0], dict): land = state[1] else: country = country[0].update(state[1]) result = {} for i in land: endeavor: pickle.dumps(country[i], protocol=2) except pickle.PicklingError: if not country[i] in seen: seen.append(state[i]) issue[i] = get_pickling_errors(land[i], seen) render effect
Instance xv
def get_pickling_errors(obj, seen=None): if seen == None: seen = [] endeavor: state = obj.__getstate__() except AttributeError: return if state == None: return if isinstance(country, tuple): if not isinstance(country[0], dict): state = state[1] else: state = country[0].crud_instances(country[i]) result = {} for i in state: try: pickle.dumps(country[i], protocol=two) except pickle.PicklingError: if not land[i] in seen: seen.append(country[i]) result[i] = get_pickling_errors(state[i], seen) return result
Case 16
def pickleRoundTrip(self, obj, name, dumper, loader, proto): save_m = sys.modules[proper name] endeavor: sys.modules[proper name] = dumper temp = pickle.dumps(obj, proto) sys.modules[name] = loader result = pickle.loads(temp) except pickle.PicklingError equally pe: # pyET must be second, considering pyET may be (equal to) ET. homo = dict([(ET, "cET"), (pyET, "pyET")]) heighten support.TestFailed("Failed to round-trip %r from %r to %r" % (obj, human.get(dumper, dumper), human being.get(loader, loader))) from pe finally: sys.modules[name] = save_m render effect
Example 17
def _read(etree): states = [CRFInfo.State._read(et) for et in etree.findall('states/country')] weight_groups = [CRFInfo.WeightGroup._read(et) for et in etree.findall('weightGroups/weightGroup')] fd = etree.find('featureDetector') feature_detector = fd.become('proper noun') if fd.find('pickle') is not None: try: feature_detector = pickle.loads(fd.notice('pickle').text) except pickle.PicklingError, e: pass # unable to unpickle it. return CRFInfo(states, float(etree.find('gaussianVariance').text), etree.discover('defaultLabel').text, int(etree.discover('maxIterations').text), etree.find('transductionType').text, weight_groups, bool(etree.find('addStartState').text), bool(etree.find('addEndState').text), etree.notice('modelFile').text, feature_detector)
Example 18
def dump(self, obj): cocky.inject_addons() effort: render Pickler.dump(self, obj) except RuntimeError as e: if 'recursion' in due east.args[0]: msg = """Could not pickle object as excessively deep recursion required.""" raise pickle.PicklingError(msg) else: raise
Instance 19
def save_file(self, obj): """Salve a file""" try: import StringIO as pystringIO # nosotros can't utilise cStringIO as it lacks the name aspect except ImportError: import io as pystringIO if not hasattr(obj, 'name') or not hasattr(obj, 'mode'): heighten pickle.PicklingError("Cannot pickle files that practise 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: heighten 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 '+' non in obj.mode: raise pickle.PicklingError("Cannot pickle files that are not opened for reading: %s" % obj.mode) proper noun = 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 %south as information technology cannot be read" % name) retval.write(contents) retval.seek(curloc) retval.name = name cocky.save(retval) self.memoize(obj)
Example twenty
def test_class_nested_enum_and_pickle_protocol_four(self): # would normally but have this directly in the class namespace form NestedEnum(Enum): twigs = 'common' shiny = 'rare' self.__class__.NestedEnum = NestedEnum cocky.NestedEnum.__qualname__ = '%s.NestedEnum' % self.__class__.__name__ test_pickle_exception( self.assertRaises, PicklingError, self.NestedEnum.twigs, protocol=(0, 3)) test_pickle_dump_load(self.assertTrue, self.NestedEnum.twigs, protocol=(4, HIGHEST_PROTOCOL))
Case 21
def test_exploding_pickle(self): BadPickle = Enum('BadPickle', 'dill sweet bread-northward-butter') enum._make_class_unpicklable(BadPickle) globals()['BadPickle'] = BadPickle test_pickle_exception(self.assertRaises, TypeError, BadPickle.dill) test_pickle_exception(self.assertRaises, PicklingError, BadPickle)
Example 22
def set_to_cache(self): """ Add widget object to Django'south cache. You lot may need to overwrite this method, to pickle all information that is required to serve your JSON response view. """ try: cache.set(self._get_cache_key(), { 'widget': self, 'url': self.get_url(), }) except (PicklingError, cPicklingError, AttributeError): msg = "Y'all need to overwrite \"set_to_cache\" or ensure that %s is serialisable." enhance NotImplementedError(msg % self.__class__.__name__)
Instance 23
def set_to_cache(self): """ Add widget object to Django's cache. You may need to overwrite this method, to pickle all data that is required to serve your JSON response view. """ effort: cache.set(self._get_cache_key(), { 'widget': cocky, 'url': self.get_url(), }) except (PicklingError, cPicklingError, AttributeError): msg = "Yous demand to overwrite \"set_to_cache\" or ensure that %s is serialisable." enhance NotImplementedError(msg % self.__class__.__name__)
Case 24
def set_to_cache(cocky): """ Add widget object to Django's cache. You may need to overwrite this method, to pickle all information that is required to serve your JSON response view. """ try: cache.set(self._get_cache_key(), { 'widget': self, 'url': cocky.get_url(), }) except (PicklingError, cPicklingError, AttributeError): msg = "You lot demand to overwrite \"set_to_cache\" or ensure that %south is serialisable." raise NotImplementedError(msg % cocky.__class__.__name__)
Case 25
def _object_contents(obj): """Render the signature contents of whatever Python object. Nosotros have to handle the case where object contains a code object since it can be pickled straight. """ try: # Test if obj is a method. return _function_contents(obj.__func__) except AttributeError: try: # Examination if obj is a callable object. render _function_contents(obj.__call__.__func__) except AttributeError: try: # Test if obj is a code object. return _code_contents(obj) except AttributeError: try: # Test if obj is a function object. return _function_contents(obj) except AttributeError as ae: # Should be a pickle-able Python object. try: return _object_instance_content(obj) # pickling an Activeness instance or object doesn't yield a stable # content as case property may be dumped in different orders # return pickle.dumps(obj, ACTION_SIGNATURE_PICKLE_PROTOCOL) except (pickle.PicklingError, TypeError, AttributeError) as ex: # This is weird, simply it seems that nested classes # are unpickable. The Python docs say information technology should # e'er be a PicklingError, but some Python # versions seem to render TypeError. But practise # the best nosotros can. return bytearray(repr(obj), 'utf-8')
Example 26
def test_compresspickle(self): for proto in range(pickle.HIGHEST_PROTOCOL + one): with self.assertRaises((TypeError, pickle.PicklingError)): pickle.dumps(zlib.compressobj(zlib.Z_BEST_COMPRESSION), proto)
Instance 27
def test_decompresspickle(self): for proto in range(pickle.HIGHEST_PROTOCOL + 1): with self.assertRaises((TypeError, pickle.PicklingError)): pickle.dumps(zlib.decompressobj(), proto) # Memory use of the following functions takes into account overallocation
Example 28
def test_pickle(cocky): a = ET.Element('a') it = a.iter() for proto in range(pickle.HIGHEST_PROTOCOL + 1): with cocky.assertRaises((TypeError, pickle.PicklingError)): pickle.dumps(it, proto)
Example 29
def test_pickle(self): d = {1: ten, "a": "ABC"} for proto in range(pickle.HIGHEST_PROTOCOL + 1): self.assertRaises((TypeError, pickle.PicklingError), pickle.dumps, d.viewkeys(), proto) cocky.assertRaises((TypeError, pickle.PicklingError), pickle.dumps, d.viewvalues(), proto) self.assertRaises((TypeError, pickle.PicklingError), pickle.dumps, d.viewitems(), proto)
Example xxx
def _object_contents(obj): """Return the signature contents of any Python object. We take to handle the example where object contains a code object since it can be pickled directly. """ endeavour: # Test if obj is a method. return _function_contents(obj.im_func) except AttributeError: endeavor: # Test if obj is a callable object. return _function_contents(obj.__call__.im_func) except AttributeError: try: # Test if obj is a lawmaking object. return _code_contents(obj) except AttributeError: attempt: # Test if obj is a role object. render _function_contents(obj) except AttributeError: # Should be a pickable Python object. try: render pickle.dumps(obj) except (pickle.PicklingError, TypeError): # This is weird, but it seems that nested classes # are unpickable. The Python docs say it should # e'er be a PicklingError, but some Python # versions seem to return TypeError. Just do # the best we tin can. return str(obj)
Source: https://www.programcreek.com/python/example/2422/pickle.PicklingError
0 Response to "Picklingerror: Cannot Pickle Files That Are Not Opened for Reading"
Отправить комментарий