Exemplo n.º 1
0
    def save_global(self, obj, name=None, pack=struct.pack):
        write = self.write
        memo = self.memo

        if name is None:
            name = obj.__name__

        module = getattr(obj, "__module__", None)
        if module is None:
            module = whichmodule(obj, name)

        try:
            __import__(module, level=0)
            mod = sys.modules[module]
            klass = getattr(mod, name)
        except (ImportError, KeyError, AttributeError):
            raise PicklingError("Can't pickle %r: it's not found as %s.%s" %
                                (obj, module, name))
        else:
            if klass is not obj:
                raise PicklingError(
                    "Can't pickle %r: it's not the same object as %s.%s" %
                    (obj, module, name))

        if self.proto >= 2:
            code = _extension_registry.get((module, name))
            if code:
                assert code > 0
                if code <= 0xff:
                    write(EXT1 + bytes([code]))
                elif code <= 0xffff:
                    write(EXT2 + bytes([code & 0xff, code >> 8]))
                else:
                    write(EXT4 + pack("<i", code))
                return
        # Non-ASCII identifiers are supported only with protocols >= 3.
        if self.proto >= 3:
            write(GLOBAL + bytes(module, "utf-8") + b'\n' +
                  bytes(name, "utf-8") + b'\n')
        else:
            if self.fix_imports:
                if (module, name) in _compat_pickle.REVERSE_NAME_MAPPING:
                    module, name = _compat_pickle.REVERSE_NAME_MAPPING[(module,
                                                                        name)]
                if module in _compat_pickle.REVERSE_IMPORT_MAPPING:
                    module = _compat_pickle.REVERSE_IMPORT_MAPPING[module]
            try:
                write(GLOBAL + bytes(module, "ascii") + b'\n' +
                      bytes(name, "ascii") + b'\n')
            except UnicodeEncodeError:
                raise PicklingError(
                    "can't pickle global identifier '%s.%s' using "
                    "pickle protocol %i" % (module, name, self.proto))

        self.memoize(obj)
Exemplo n.º 2
0
    def save_global(self, obj, name=None, pack=struct.pack):
        write = self.write
        memo = self.memo

        if name is None:
            name = obj.__name__

        module = getattr(obj, "__module__", None)
        if module is None:
            module = whichmodule(obj, name)

        try:
            __import__(module, level=0)
            mod = sys.modules[module]
            klass = getattr(mod, name)
        except (ImportError, KeyError, AttributeError):
            raise PicklingError(
                "Can't pickle %r: it's not found as %s.%s" %
                (obj, module, name))
        else:
            if klass is not obj:
                raise PicklingError(
                    "Can't pickle %r: it's not the same object as %s.%s" %
                    (obj, module, name))

        if self.proto >= 2:
            code = _extension_registry.get((module, name))
            if code:
                assert code > 0
                if code <= 0xff:
                    write(EXT1 + bytes([code]))
                elif code <= 0xffff:
                    write(EXT2 + bytes([code&0xff, code>>8]))
                else:
                    write(EXT4 + pack("<i", code))
                return
        # Non-ASCII identifiers are supported only with protocols >= 3.
        if self.proto >= 3:
            write(GLOBAL + bytes(module, "utf-8") + b'\n' +
                  bytes(name, "utf-8") + b'\n')
        else:
            if self.fix_imports:
                if (module, name) in _compat_pickle.REVERSE_NAME_MAPPING:
                    module, name = _compat_pickle.REVERSE_NAME_MAPPING[(module, name)]
                if module in _compat_pickle.REVERSE_IMPORT_MAPPING:
                    module = _compat_pickle.REVERSE_IMPORT_MAPPING[module]
            try:
                write(GLOBAL + bytes(module, "ascii") + b'\n' +
                      bytes(name, "ascii") + b'\n')
            except UnicodeEncodeError:
                raise PicklingError(
                    "can't pickle global identifier '%s.%s' using "
                    "pickle protocol %i" % (module, name, self.proto))

        self.memoize(obj)
Exemplo n.º 3
0
    def save_global(self, obj, name=None, pack=struct.pack):
        write = self.write
        memo = self.memo

        if name is None:
            name = obj.__name__

        module = getattr(obj, "__module__", None)
        if module is None:
            module = whichmodule(obj, name)

        try:
            __import__(module, level=0)
            mod = sys.modules[module]
            klass = getattr(mod, name)
        except (ImportError, KeyError, AttributeError):
            raise PicklingError("Can't pickle %r: it's not found as %s.%s" %
                                (obj, module, name))
        else:
            if klass is not obj:
                raise PicklingError(
                    "Can't pickle %r: it's not the same object as %s.%s" %
                    (obj, module, name))

        if self.proto >= 2:
            code = _extension_registry.get((module, name))
            if code:
                assert code > 0
                if code <= 0xff:
                    write(EXT1 + bytes([code]))
                elif code <= 0xffff:
                    write(EXT2 + bytes([code & 0xff, code >> 8]))
                else:
                    write(EXT4 + pack("<i", code))
                return

        raise PicklingError(
            "Can't pickle %r: %s.%s isn't in the extension registry" %
            (obj, module, name), )
Exemplo n.º 4
0
 def save_global(self, obj, name=None, pack=struct.pack):
     write = self.write
     memo = self.memo
     if name is None:
         name = obj.__name__
     module = getattr(obj, '__module__', None)
     if module is None:
         module = whichmodule(obj, name)
     try:
         __import__(module, level=0)
         mod = sys.modules[module]
         klass = getattr(mod, name)
     except (ImportError, KeyError, AttributeError):
         raise PicklingError("Can't pickle %r: it's not found as %s.%s" % (obj, module, name))
     if klass is not obj:
         raise PicklingError("Can't pickle %r: it's not the same object as %s.%s" % (obj, module, name))
     if self.proto >= 2:
         code = _extension_registry.get((module, name))
         if code:
             if code <= 255:
                 write(EXT1 + bytes([code]))
             elif code <= 65535:
                 write(EXT2 + bytes([code & 255, code >> 8]))
             else:
                 write(EXT4 + pack('<i', code))
             return
     if self.proto >= 3:
         write(GLOBAL + bytes(module, 'utf-8') + b'\n' + bytes(name, 'utf-8') + b'\n')
     else:
         if (module, name) in _compat_pickle.REVERSE_NAME_MAPPING:
             (module, name) = _compat_pickle.REVERSE_NAME_MAPPING[(module, name)]
         if self.fix_imports and module in _compat_pickle.REVERSE_IMPORT_MAPPING:
             module = _compat_pickle.REVERSE_IMPORT_MAPPING[module]
         try:
             write(GLOBAL + bytes(module, 'ascii') + b'\n' + bytes(name, 'ascii') + b'\n')
         except UnicodeEncodeError:
             raise PicklingError("can't pickle global identifier '%s.%s' using pickle protocol %i" % (module, name, self.proto))
     self.memoize(obj)
Exemplo n.º 5
0
    def save_global(self, obj, name=None, pack=struct.pack):        
        write = self.write
        memo = self.memo

        if name is None:
            name = obj.__name__
        
        modname = getattr(obj, "__module__", None)
        if modname is None:
            modname = pickle.whichmodule(obj, name)

        try:
            __import__(modname)
            themodule = sys.modules[modname]
        except (ImportError, KeyError, AttributeError):  #should never occur                                    
            raise pickle.PicklingError(
                "Can't pickle %r: Module %s cannot be found" %
                (obj, modname))

        if modname == '__main__':
            themodule = None                            

        if themodule:
            self.modules.add(themodule)

        sendRef = True
        typ = type(obj)
        #print 'saving', obj, typ
        try:
            try: #Deal with case when getattribute fails with exceptions
                klass = getattr(themodule, name)
            except (AttributeError):                
                if modname == '__builtin__':  #new.* are misrepeported
                    modname = 'new'
                    __import__(modname)
                    themodule = sys.modules[modname]
                    try:
                        klass = getattr(themodule, name)
                    except AttributeError as a:
                        #print themodule, name, obj, type(obj)
                        raise pickle.PicklingError("Can't pickle builtin %s" % obj)
                else:
                    raise
                            
        except (ImportError, KeyError, AttributeError):                
            if typ == type:
                sendRef = False
            else: #we can't deal with this
                raise
        else:
            if klass is not obj and (typ == type):
                sendRef = False
        if not sendRef:
            self.save_class_obj(obj, name, pack)            
            return

        if self.proto >= 2:
            code = _extension_registry.get((modname, name))
            if code:
                assert code > 0
                if code <= 0xff:
                    write(pickle.EXT1 + chr(code))
                elif code <= 0xffff:
                    write("%c%c%c" % (pickle.EXT2, code&0xff, code>>8))
                else:
                    write(pickle.EXT4 + pack("<i", code))
                return
              
        if six.PY3:
            write(pickle.GLOBAL + (modname + '\n' + name + '\n').encode('utf-8'))
        else:
            write(pickle.GLOBAL + modname + '\n' + name + '\n')
        self.memoize(obj)