我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用pickle.UnpicklingError()。
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
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
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
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
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]) ##############################################################################
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)
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)
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
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
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
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")
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)
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
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)
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')
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)
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)
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)
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
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
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
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__, }
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 ''
def test_pop_empty_stack(self): # Test issue7455 s = b'0' self.assertRaises((pickle.UnpicklingError, IndexError), self.loads, s)
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)))
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()
def pop(self, index=-1): try: return list.pop(self, index) except IndexError: raise UnpicklingError("unpickling stack underflow")
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)
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)
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)
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)
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)
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
def persistent_load(self, pid): type_tag = pid if type_tag == marker: return self.w else: raise pickle.UnpicklingError("unsupported persistent object")
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
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")
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
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']