def __reduce__(self): mod = self.__fn.__module__ name = self.__fn.__name__ try: obj = load_back(mod, name) except (ImportError, KeyError, AttributeError): raise pickle.PicklingError( "Can't pickle as_op(), not found as %s.%s" % (mod, name)) else: if obj is not self: raise pickle.PicklingError( "Can't pickle as_op(), not the object " "at %s.%s" % (mod, name)) return load_back, (mod, name)
def __getstate__(self): """ Return kwarg_dict dict Part of over-riding default pickle/unpickle behavior Raise exception if trying to pickle and active handler """ if self.active: raise pickle.PicklingError("can not pickle active handler") return self.kwarg_dict
def ensure_bytestream(obj): """ Converts an object into a bytestream. If `obj` is file-like, its contents will be read into memory and then wrapped in a bytestream. This has a performance cost, but checking beforehand whether an arbitrary file-like object returns bytes rather than encoded characters is an implementation nightmare. If `obj` is not file-like, it will be serialized and then wrapped in a bytestream. Parameters ---------- obj : file-like or object Object to convert into a bytestream. Returns ------- bytestream : file-like Buffered bytestream of the serialized artifacts. method : {"joblib", "cloudpickle", "pickle", None} Serialization method used to produce the bytestream. Raises ------ pickle.PicklingError If `obj` cannot be serialized. ValueError If `obj` contains no data. """ if hasattr(obj, 'read'): # if `obj` is file-like reset_stream(obj) # reset cursor to beginning in case user forgot contents = obj.read() # read to cast into binary reset_stream(obj) # reset cursor to beginning as a courtesy if not len(contents): raise ValueError("object contains no data") bytestring = six.ensure_binary(contents) bytestream = six.BytesIO(bytestring) bytestream.seek(0) return bytestream, None else: # `obj` is not file-like bytestream = six.BytesIO() try: cloudpickle.dump(obj, bytestream) except pickle.PicklingError: # can't be handled by cloudpickle pass else: bytestream.seek(0) return bytestream, "cloudpickle" try: joblib.dump(obj, bytestream) except ( NameError, # joblib not installed pickle.PicklingError): # can't be handled by joblib pass else: bytestream.seek(0) return bytestream, "joblib" try: pickle.dump(obj, bytestream) except pickle.PicklingError: # can't be handled by pickle six.raise_from( pickle.PicklingError("unable to serialize artifact"), None) else: bytestream.seek(0) return bytestream, "pickle"