예제 #1
0
파일: ops.py 프로젝트: shivamvats/Theano
 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)
예제 #2
0
    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
예제 #3
0
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"