Python pickle 模块,UnpicklingError() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用pickle.UnpicklingError()

项目:code    作者:ActiveState    | 项目源码 | 文件源码
def get_settings(self):
        # Try opening and loading the settings from file.
        filename = os.path.join(self.__path, self.FILENAME)
        try:
            with open(filename, 'rb') as file:
                settings = pickle.load(file)
            # Test the pickle and check each setting inside it.
            assert isinstance(settings, dict)
            key_list = list(self.DEFAULT)
            for key in settings:
                assert isinstance(key, str)
                assert key in self.DEFAULT
                key_list.remove(key)
            # Add new settings as needed (when new ones are created).
            for key in key_list:
                settings[key] = self.DEFAULT[key]
            # Return old settings, or on error, the default settings.
            return False, settings
        except (IOError, pickle.UnpicklingError, AssertionError):
            return True, self.DEFAULT
项目:spc    作者:whbrewer    | 项目源码 | 文件源码
def secure_loads(data, encryption_key, hash_key=None, compression_level=None):
    if not ':' in data:
        return None
    if not hash_key:
        hash_key = hashlib.sha1(encryption_key).hexdigest()
    signature, encrypted_data = data.split(':', 1)
    actual_signature = hmac.new(hash_key, encrypted_data).hexdigest()
    if not compare(signature, actual_signature):
        return None
    key = pad(encryption_key[:32])
    encrypted_data = base64.urlsafe_b64decode(encrypted_data)
    IV, encrypted_data = encrypted_data[:16], encrypted_data[16:]
    cipher, _ = AES_new(key, IV=IV)
    try:
        data = cipher.decrypt(encrypted_data)
        data = data.rstrip(' ')
        if compression_level:
            data = zlib.decompress(data)
        return pickle.loads(data)
    except (TypeError, pickle.UnpicklingError):
        return None

### compute constant CTOKENS
项目:ultimate-tic-tac-toe    作者:stoimenoff    | 项目源码 | 文件源码
def __connect(self, macroboard):
        self.__socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.__socket.settimeout(DEFAULT_CLIENT_TIMEOUT)
        with self.__socket:
            self.__socket.connect((self.host, self.port))
            self.__socket.sendall(self.__get_data(macroboard))
            data = None
            while not self.__cancelled and not data:
                try:
                    # print('Recieve not cancelled')
                    data = self.__socket.recv(BYTES_LENGTH)
                except socket.timeout:
                    continue
            if self.__cancelled:
                return
            print('Received', repr(data), ' Size:', len(data))
            try:
                unpickled_data = pickle.loads(data)
            except (pickle.UnpicklingError, EOFError):
                raise BadResponseError('Response unpickling failed.')
            if self.__is_not_valid(unpickled_data, macroboard):
                raise BadResponseError('Response object is not valid.')
            self.name, move = pickle.loads(data)
        return move
项目:little-python    作者:JeffyLu    | 项目源码 | 文件源码
def load_data():
    global DATA, encrypt, decrypt
    key = md5(input('security code:'))
    iv = key[8:24]
    encrypt = lambda text : b2a_hex(
        AES.new(key, AES.MODE_CBC, iv).encrypt(padding(text)))
    decrypt = lambda text : AES.new(
        key, AES.MODE_CBC, iv).decrypt(a2b_hex(text)).decode().rstrip('\0')
    if os.path.isfile(FILE_NAME):
        with open(FILE_NAME, 'rb') as f:
            try:
                DATA = pickle.load(f)
            except pickle.UnpicklingError:
                print('file error!')
                return False
            except:
                print('loading error!')
                return False
项目:objEnhancer    作者:BabbageCom    | 项目源码 | 文件源码
def Do_SConsignDir(name):
    try:
        fp = open(name, 'rb')
    except (IOError, OSError), e:
        sys.stderr.write("sconsign: %s\n" % (e))
        return
    try:
        sconsign = SCons.SConsign.Dir(fp)
    except KeyboardInterrupt:
        raise
    except pickle.UnpicklingError:
        sys.stderr.write("sconsign: ignoring invalid .sconsign file `%s'\n" % (name))
        return
    except Exception, e:
        sys.stderr.write("sconsign: ignoring invalid .sconsign file `%s': %s\n" % (name, e))
        return
    printentries(sconsign.entries, args[0])

##############################################################################
项目:ouroboros    作者:pybee    | 项目源码 | 文件源码
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.check_unpickling_error(pickle.UnpicklingError, p)
项目:kbe_server    作者:xiaohaoppy    | 项目源码 | 文件源码
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)
项目:python    作者:hienha    | 项目源码 | 文件源码
def __get__(self, obj, typ=None):
    '__get__() -- retrives attribute from disk'
        self.name = name
        if self.name not in FileDescr.saved:
            raise AttributeError, \
                "%r used before assignment" % self.name

    # open pickle file and load object; choke on failure
        try:
            f = open(self.name, 'r')
            val = pickle.load(f)
            f.close()
            return val
        except (pickle.UnpicklingError, IOError,
                EOFError, AttributeError,
                ImportError, IndexError), e:
            raise AttributeError, \
                "could not read %r: %s" % self.name
项目:Question-Answering-System    作者:AdityaAS    | 项目源码 | 文件源码
def __init__(self, index):
        """Initialize the TokI object from a MongoDB or load from disk."""
        self.index = index
        if pymongo:
            if 'toki' in self.index.mongo_db.collection_names():
                self.mongo_toki = self.index.mongo_db['toki']
                if self.mongo_toki.count() == 0:
                    raise IndexLoadError
            else:
                raise IndexLoadError
        else:
            # Load into memory (not suitable for large corpora!)
            try:
                with open(self.index.base_fname + '.toki', mode='rb') as f:
                    self.toki = pickle.load(f)
                if not self.toki:
                    raise IndexLoadError
            except (IOError, pickle.UnpicklingError):
                raise IndexLoadError
项目:bob    作者:BobBuildTool    | 项目源码 | 文件源码
def __generatePackages(self, nameFormatter, env, cacheKey, sandboxEnabled):
        # use separate caches with and without sandbox
        if sandboxEnabled:
            cacheName = ".bob-packages-sb.pickle"
        else:
            cacheName = ".bob-packages.pickle"

        # try to load the persisted packages
        states = { n:s() for (n,s) in self.__states.items() }
        rootPkg = Package()
        rootPkg.construct("<root>", [], nameFormatter, None, [], [], states,
            {}, {}, None, None, [], {}, -1)
        try:
            with open(cacheName, "rb") as f:
                persistedCacheKey = f.read(len(cacheKey))
                if cacheKey == persistedCacheKey:
                    tmp = PackageUnpickler(f, self.getRecipe, self.__plugins,
                                           nameFormatter).load()
                    return tmp.toStep(nameFormatter, rootPkg).getPackage()
        except (EOFError, OSError, pickle.UnpicklingError):
            pass

        # not cached -> calculate packages
        result = self.__rootRecipe.prepare(nameFormatter, env, sandboxEnabled,
                                           states)[0]

        # save package tree for next invocation
        tmp = CoreStepRef(rootPkg, result.getPackageStep())
        try:
            newCacheName = cacheName + ".new"
            with open(newCacheName, "wb") as f:
                f.write(cacheKey)
                PackagePickler(f, nameFormatter).dump(tmp)
            os.replace(newCacheName, cacheName)
        except OSError as e:
            print("Error saving internal state:", str(e), file=sys.stderr)

        return result
项目:bob    作者:BobBuildTool    | 项目源码 | 文件源码
def persistent_load(self, pid):
        (tag, key) = pid
        if tag == "pathfmt":
            return self.__pathFormatter
        elif tag == "recipe":
            return self.__recipeGetter(key)
        else:
            raise pickle.UnpicklingError("unsupported object")
项目:hostapd-mana    作者:adde88    | 项目源码 | 文件源码
def __init__(self, log, journaledService, path, loadedCallback):
        self.path = path
        if os.path.exists(path):
            try:
                self.lastSync, obj = pickle.load(open(path, "rb"))
            except (IOError, OSError, pickle.UnpicklingError):
                self.lastSync, obj = 0, None
            loadedCallback(obj)
        else:
            self.lastSync = 0
            loadedCallback(None)
        Journal.__init__(self, log, journaledService)
项目:esys-pbi    作者:fsxfreak    | 项目源码 | 文件源码
def load_object(file_path):
    file_path = os.path.expanduser(file_path)
    # reading to string and loads is 2.5x faster that using the file handle and load.
    with open(file_path, 'rb') as fh:
        data = fh.read()
    try:
        return pickle.loads(data, encoding='bytes')
    except pickle.UnpicklingError as e:
        raise ValueError from e
项目:gipc    作者:jgehrcke    | 项目源码 | 文件源码
def get(self, timeout=None):
        """Receive, decode and return data from the pipe. Block
        gevent-cooperatively until data is available or timeout expires. The
        default decoder is ``pickle.loads``.

        :arg timeout: ``None`` (default) or a ``gevent.Timeout``
            instance. The timeout must be started to take effect and is
            canceled when the first byte of a new message arrives (i.e.
            providing a timeout does not guarantee that the method completes
            within the timeout interval).

        :returns: a Python object.

        Raises:
            - :exc:`gevent.Timeout` (if provided)
            - :exc:`GIPCError`
            - :exc:`GIPCClosed`
            - :exc:`pickle.UnpicklingError`

        Recommended usage for silent timeout control::

            with gevent.Timeout(TIME_SECONDS, False) as t:
                reader.get(timeout=t)

        .. warning::

            The timeout control is currently not available on Windows,
            because Windows can't apply select() to pipe handles.
            An ``OSError`` is expected to be raised in case you set a
            timeout.
        """
        self._validate()
        with self._lock:
            if timeout:
                # Wait for ready-to-read event.
                h = gevent.get_hub()
                h.wait(h.loop.io(self._fd, 1))
                timeout.cancel()
            msize, = struct.unpack("!i", self._recv_in_buffer(4).getvalue())
            bindata = self._recv_in_buffer(msize).getvalue()
        return self._decoder(bindata)
项目:fidget    作者:angr    | 项目源码 | 文件源码
def __init__(self, infile, cache=False, cfg_options=None, debugangr=False):
        self.infile = infile
        self.error = False
        self._stack_patch_data = []
        self.stack_increases = {}
        if cfg_options is None:
            cfg_options = {}
        cachename = infile + '.fcfg'

        l.info("Loading %s", infile)
        try:
            if not cache: raise IOError('fuck off')
            fh = open(cachename, 'rb')
            self.project, self.cfg = pickle.load(fh)
            self.cfg.project = self.project
            fh.close()
        except (IOError, OSError, pickle.UnpicklingError):
            if debugangr:
                import ipdb; ipdb.set_trace()
            self.project = Project(infile, load_options={'auto_load_libs': False})
            self.cfg = self.project.analyses.CFGFast(**cfg_options)
            try:
                fh = open(cachename, 'wb')
                pickle.dump((self.project, self.cfg), fh, -1)
                fh.close()
            except (IOError, OSError, pickle.PicklingError):
                l.exception('Error pickling CFG')
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
def check_negative_32b_binXXX(self, dumped):
        if sys.maxsize > 2**32:
            self.skipTest("test is only meaningful on 32-bit builds")
        # XXX Pure Python pickle reads lengths as signed and passes
        # them directly to read() (hence the EOFError)
        with self.assertRaises((pickle.UnpicklingError, EOFError,
                                ValueError, OverflowError)):
            self.loads(dumped)
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
def test_bad_init(self):
        # Test issue3664 (pickle can segfault from a badly initialized Pickler).
        # Override initialization without calling __init__() of the superclass.
        class BadPickler(pickle.Pickler):
            def __init__(self): pass

        class BadUnpickler(pickle.Unpickler):
            def __init__(self): pass

        self.assertRaises(pickle.PicklingError, BadPickler().dump, 0)
        self.assertRaises(pickle.UnpicklingError, BadUnpickler().load)
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
def test_bad_input(self):
        # Test issue4298
        s = bytes([0x58, 0, 0, 0, 0x54])
        self.assertRaises(EOFError, pickle.loads, s)
        # Test issue7455
        s = b'0'
        self.assertRaises((pickle.UnpicklingError, IndexError),
                          pickle.loads, s)
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
def pollmessage(self, wait):
        packet = self.pollpacket(wait)
        if packet is None:
            return None
        try:
            message = pickle.loads(packet)
        except pickle.UnpicklingError:
            print("-----------------------", file=sys.__stderr__)
            print("cannot unpickle packet:", repr(packet), file=sys.__stderr__)
            traceback.print_stack(file=sys.__stderr__)
            print("-----------------------", file=sys.__stderr__)
            raise
        return message
项目:knowledge-management    作者:dgore7    | 项目源码 | 文件源码
def retrieve_groups(self, username):
        connection = self.sock
        connection.send("groups_retrieve".encode())
        result = connection.recv(1024)

        if result != SUCCESS:
            print("failed in retrieve groups1")
            return []

        message = "username:" + username
        message = message.encode()
        connection.send(message)
        result = connection.recv(2)

        if result != SUCCESS:
            print("No groups found")
            return []

        chunks = []
        while True:
            bytes_received = connection.recv(1024)
            if bytes_received == SOCKET_EOF:
                break
            else:
                chunks.append(bytes_received)
        result = b''.join(chunks)
        try:
            groups = pickle.loads(result)
        except UnpicklingError:
            return []
        return groups
项目:vivisect-py3    作者:bat-serjo    | 项目源码 | 文件源码
def vivEventsFromFile(filename):
    with open(filename, "rb") as f:
        vivsig = f.read(8)

        # check for various viv serial formats
        if vivsig != vivsig_cpickle:
            # FIXME legacy file format.... ( eventually remove )
            f.seek(0)

        events = []
        # Incremental changes are saved to the file by appending more pickled
        # lists of exported events
        while True:
            try:
                events.extend(pickle.load(f))
            except EOFError as e:
                break
            except pickle.UnpicklingError as e:
                raise vivisect.InvalidWorkspace(filename, "invalid workspace file")

    # FIXME - diagnostics to hunt msgpack unsave values
    # for event in events:
    # import msgpack
    # try:
    # msgpack.dumps(event)
    # except Exception, e:
    # print('Unsafe Event: %d %r' % event)

    return events
项目:bigchaindb    作者:bigchaindb    | 项目源码 | 文件源码
def unpickle(self, data):
        try:
            return pickle.loads(data)
        except (pickle.UnpicklingError,
                AttributeError, EOFError, TypeError) as exc:
            return {
                'msg': '({}) Log handling error: un-pickling failed!'.format(
                    exc.__class__.__name__),
                'exc_info': exc.args,
                'level': logging.ERROR,
                'func': self.unpickle.__name__,
            }
项目:sslstrip-hsts-openwrt    作者:adde88    | 项目源码 | 文件源码
def __init__(self, log, journaledService, path, loadedCallback):
        self.path = path
        if os.path.exists(path):
            try:
                self.lastSync, obj = pickle.load(open(path, "rb"))
            except (IOError, OSError, pickle.UnpicklingError):
                self.lastSync, obj = 0, None
            loadedCallback(obj)
        else:
            self.lastSync = 0
            loadedCallback(None)
        Journal.__init__(self, log, journaledService)
项目:django-watermark-images    作者:abarto    | 项目源码 | 文件源码
def lsb_decode(image):
    try:
        red, green, blue = image.split()
        watermark = ImageMath.eval("(a&0x1)*0x01", a=red)
        watermark = watermark.convert('L')
        watermark_bytes = bytes(watermark.getdata())
        watermark_bits_array = np.fromiter(watermark_bytes, dtype=np.uint8)
        watermark_bytes_array = np.packbits(watermark_bits_array)
        watermark_bytes = bytes(watermark_bytes_array)
        bytes_io = BytesIO(watermark_bytes)
        return load(bytes_io)
    except UnpicklingError:
        return ''
项目:web_ctp    作者:molebot    | 项目源码 | 文件源码
def test_pop_empty_stack(self):
        # Test issue7455
        s = b'0'
        self.assertRaises((pickle.UnpicklingError, IndexError), self.loads, s)
项目:web_ctp    作者:molebot    | 项目源码 | 文件源码
def check_negative_32b_binXXX(self, dumped):
        if sys.maxsize > 2**32:
            self.skipTest("test is only meaningful on 32-bit builds")
        # XXX Pure Python pickle reads lengths as signed and passes
        # them directly to read() (hence the EOFError)
        with self.assertRaises((pickle.UnpicklingError, EOFError,
                                ValueError, OverflowError)):
            self.loads(dumped)
项目:web_ctp    作者:molebot    | 项目源码 | 文件源码
def test_bad_init(self):
        # Test issue3664 (pickle can segfault from a badly initialized Pickler).
        # Override initialization without calling __init__() of the superclass.
        class BadPickler(pickle.Pickler):
            def __init__(self): pass

        class BadUnpickler(pickle.Unpickler):
            def __init__(self): pass

        self.assertRaises(pickle.PicklingError, BadPickler().dump, 0)
        self.assertRaises(pickle.UnpicklingError, BadUnpickler().load)
项目:ultimate-tic-tac-toe    作者:stoimenoff    | 项目源码 | 文件源码
def listen(self, on_move_request):
        """
        Waits a connection from a remote player via socket connection.
        If the socket connection fails will NOT handle exceptions.

        Raises BadRequestError, if the client request is not valid
        or the client is not the opponent.

        Blocking, can be cancelled from another thread by calling
        stop() on the object.

        When a valid request is made, on_move_request function is called
        with the name of the opponent and the board he sent.
        The function should return a move for the board.
        The return value of the function is not checked! If it is
        NOT a valid move, it WILL BE sent to the client.
        """
        self.__socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.__socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        # self.__socket.settimeout(DEFAULT_SERVER_TIMEOUT)
        with self.__socket:
            self.__socket.bind((self.__host, self.__port))
            self.__socket.listen(1)
            connection, address = self.__socket.accept()
            print('Connected by', address)
            if self.opponent is not None and self.opponent != address[0]:
                raise BadRequestError('Not opponent.')
            with connection:
                data = connection.recv(BYTES_LENGTH)
                try:
                    unpickled_data = pickle.loads(data)
                except (pickle.UnpicklingError, EOFError):
                    raise BadRequestError('Request unpickling failed.')
                if self.__is_not_valid(unpickled_data):
                    raise BadRequestError('Request object is not valid.')
                name, macroboard = unpickled_data
                self.opponent = address[0]
                move = on_move_request(name, macroboard)
                connection.sendall(pickle.dumps((self.name, move)))
项目:ultimate-tic-tac-toe    作者:stoimenoff    | 项目源码 | 文件源码
def loadGame(self):
        filename = QFileDialog().getOpenFileName(self, 'Load game')
        if not filename[0]:
            return
        try:
            with open(filename[0], 'rb') as handle:
                config = pickle.load(handle)
        except (pickle.UnpicklingError, FileNotFoundError, EOFError):
            return
        self.game = SinglePlayerGame()
        try:
            self.game.loadConfiguration(config)
        except ValueError:
            return
        self.showGame()
项目:pefile.pypy    作者:cloudtracer    | 项目源码 | 文件源码
def pop(self, index=-1):
        try:
            return list.pop(self, index)
        except IndexError:
            raise UnpicklingError("unpickling stack underflow")
项目:ouroboros    作者:pybee    | 项目源码 | 文件源码
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.'
        self.check_unpickling_error((pickle.UnpicklingError, OverflowError),
                                    dumped)
项目:ouroboros    作者:pybee    | 项目源码 | 文件源码
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.'
        self.check_unpickling_error((pickle.UnpicklingError, OverflowError),
                                    dumped)
项目:ouroboros    作者:pybee    | 项目源码 | 文件源码
def test_large_32b_binbytes8(self):
        dumped = b'\x80\x04\x8e\4\0\0\0\1\0\0\0\xe2\x82\xac\x00.'
        self.check_unpickling_error((pickle.UnpicklingError, OverflowError),
                                    dumped)
项目:ouroboros    作者:pybee    | 项目源码 | 文件源码
def test_large_32b_binunicode8(self):
        dumped = b'\x80\x04\x8d\4\0\0\0\1\0\0\0\xe2\x82\xac\x00.'
        self.check_unpickling_error((pickle.UnpicklingError, OverflowError),
                                    dumped)
项目:ouroboros    作者:pybee    | 项目源码 | 文件源码
def test_bad_init(self):
        # Test issue3664 (pickle can segfault from a badly initialized Pickler).
        # Override initialization without calling __init__() of the superclass.
        class BadPickler(pickle.Pickler):
            def __init__(self): pass

        class BadUnpickler(pickle.Unpickler):
            def __init__(self): pass

        self.assertRaises(pickle.PicklingError, BadPickler().dump, 0)
        self.assertRaises(pickle.UnpicklingError, BadUnpickler().load)
项目:ouroboros    作者:pybee    | 项目源码 | 文件源码
def pollmessage(self, wait):
        packet = self.pollpacket(wait)
        if packet is None:
            return None
        try:
            message = pickle.loads(packet)
        except pickle.UnpicklingError:
            print("-----------------------", file=sys.__stderr__)
            print("cannot unpickle packet:", repr(packet), file=sys.__stderr__)
            traceback.print_stack(file=sys.__stderr__)
            print("-----------------------", file=sys.__stderr__)
            raise
        return message
项目:PySyncObj    作者:bakwc    | 项目源码 | 文件源码
def _load_binstring(self):
        len, = unpack('<i', self.read(4))
        if len < 0:
            raise pickle.UnpicklingError("BINSTRING pickle has negative byte count")
        data = self.read(len)
        try:
            data = str(data, self.encoding, self.errors)
        except:
            pass
        self.append(data)
项目:Deploy_XXNET_Server    作者:jzp820927    | 项目源码 | 文件源码
def _GetValueAndType(self, key):
    """Fetch value from memcache and detect its type.

    Args:
      key: String

    Returns:
      (value, type), value is a Python object or None if the key was not set in
      the cache, type is a string describing the type of the value.
    """
    try:
      value = memcache.get(key)
    except (pickle.UnpicklingError, AttributeError, EOFError, ImportError,
            IndexError), e:


      msg = 'Failed to retrieve value from cache: %s' % e
      return msg, 'error'

    if value is None:

      return None, self.DEFAULT_TYPESTR_FOR_NEW


    for typeobj, _, typestr in self.TYPES:
      if isinstance(value, typeobj):
        break
    else:

      typestr = 'pickled'
      value = pprint.pformat(value, indent=2)

    return value, typestr
项目:taskloaf    作者:tbenthompson    | 项目源码 | 文件源码
def persistent_load(self, pid):
        type_tag = pid
        if type_tag == marker:
            return self.w
        else:
            raise pickle.UnpicklingError("unsupported persistent object")
项目:kbe_server    作者:xiaohaoppy    | 项目源码 | 文件源码
def test_pop_empty_stack(self):
        # Test issue7455
        s = b'0'
        self.assertRaises((pickle.UnpicklingError, IndexError), self.loads, s)
项目:kbe_server    作者:xiaohaoppy    | 项目源码 | 文件源码
def check_negative_32b_binXXX(self, dumped):
        if sys.maxsize > 2**32:
            self.skipTest("test is only meaningful on 32-bit builds")
        # XXX Pure Python pickle reads lengths as signed and passes
        # them directly to read() (hence the EOFError)
        with self.assertRaises((pickle.UnpicklingError, EOFError,
                                ValueError, OverflowError)):
            self.loads(dumped)
项目:kbe_server    作者:xiaohaoppy    | 项目源码 | 文件源码
def test_bad_init(self):
        # Test issue3664 (pickle can segfault from a badly initialized Pickler).
        # Override initialization without calling __init__() of the superclass.
        class BadPickler(pickle.Pickler):
            def __init__(self): pass

        class BadUnpickler(pickle.Unpickler):
            def __init__(self): pass

        self.assertRaises(pickle.PicklingError, BadPickler().dump, 0)
        self.assertRaises(pickle.UnpicklingError, BadUnpickler().load)
项目:kbe_server    作者:xiaohaoppy    | 项目源码 | 文件源码
def pollmessage(self, wait):
        packet = self.pollpacket(wait)
        if packet is None:
            return None
        try:
            message = pickle.loads(packet)
        except pickle.UnpicklingError:
            print("-----------------------", file=sys.__stderr__)
            print("cannot unpickle packet:", repr(packet), file=sys.__stderr__)
            traceback.print_stack(file=sys.__stderr__)
            print("-----------------------", file=sys.__stderr__)
            raise
        return message
项目:python    作者:hienha    | 项目源码 | 文件源码
def __get__(self, obj, typ=None):
        if self.name not in FileDescr.saved:
            raise AttributeError, \
                "%r used before assignment" % self.name

        try:
            f = open(self.name, 'r')
            val = pickle.load(f)
            f.close()
            return val
        except (pickle.UnpicklingError, IOError,
                EOFError, AttributeError,
                ImportError, IndexError), e:
            raise AttributeError, \
                "could not read %r: %s" % self.name
项目:python    作者:hienha    | 项目源码 | 文件源码
def __get__(self, obj, typ=None):
        if self.name not in FileDescr.saved:
            raise AttributeError, \
                "%r used before assignment" % self.name

        try:
            f = open(self.name, 'r')
            val = pickle.load(f)
            f.close()
            return val
        except (pickle.UnpicklingError, IOError,
                EOFError, AttributeError,
                ImportError, IndexError), e:
            raise AttributeError, \
                "could not read %r: %s" % self.name
项目:python    作者:hienha    | 项目源码 | 文件源码
def __get__(self, obj, typ=None):
        if self.name not in FileDescr.saved:
            raise AttributeError, \
                "%r used before assignment" % self.name

        try:
            f = open(self.name, 'r')
            val = pickle.load(f)
            f.close()
            return val
        except (pickle.UnpicklingError, IOError,
                EOFError, AttributeError,
                ImportError, IndexError), e:
            raise AttributeError, \
                "could not read %r: %s" % self.name
项目:python    作者:hienha    | 项目源码 | 文件源码
def __get__(self, obj, typ=None):
        if self.name not in FileDescr.saved:
            raise AttributeError, \
                "%r used before assignment" % self.name

        try:
            f = open(self.name, 'r')
            val = pickle.load(f)
            f.close()
            return val
        except (pickle.UnpicklingError, IOError,
                EOFError, AttributeError,
                ImportError, IndexError), e:
            raise AttributeError, \
                "could not read %r: %s" % self.name
项目:urh    作者:jopohl    | 项目源码 | 文件源码
def read_device_messages(self):
        while self.is_receiving or self.is_transmitting:
            try:
                message = self.parent_ctrl_conn.recv()
                try:
                    action, return_code = message.split(":")
                    self.log_retcode(int(return_code), action)
                except ValueError:
                    self.device_messages.append("{0}: {1}".format(self.__class__.__name__, message))
            except (EOFError, UnpicklingError, OSError, ConnectionResetError) as e:
                logger.info("Exiting read device message thread due to " + str(e))
                break
        self.is_transmitting = False
        self.is_receiving = False
        logger.debug("Exiting read device errors thread")
项目:Question-Answering-System    作者:AdityaAS    | 项目源码 | 文件源码
def load_tokc(self):
        try:
            with open(self.base_fname + '.tokc', mode='rb') as f:
                self.tokc = pickle.load(f)
            if not self.tokc:
                raise IndexLoadError
        except (IOError, pickle.UnpicklingError):
            raise IndexLoadError
项目:w4py    作者:Cito    | 项目源码 | 文件源码
def findGlobal(self, module, klass):
        """Find class name."""
        if (module, klass) not in self.allowedGlobals():
            raise UnpicklingError("For security reasons, you can\'t unpickle"
                " objects from module %s with type %s." % (module, klass))
        g = {}
        exec 'from %s import %s as theClass' % (module, klass) in g
        return g['theClass']