本文整理汇总了Python中cPickle.UnpicklingError方法的典型用法代码示例。如果您正苦于以下问题:Python cPickle.UnpicklingError方法的具体用法?Python cPickle.UnpicklingError怎么用?Python cPickle.UnpicklingError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cPickle
的用法示例。
在下文中一共展示了cPickle.UnpicklingError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import cPickle [as 别名]
# 或者: from cPickle import UnpicklingError [as 别名]
def __init__(self, getter=DEFAULT_GETTER, *args, **kwargs):
super(TokenDictionaryFeature, self).__init__(getter=getter, *args, **kwargs)
self._is_boolean = True
if self._path is not None:
try:
self._value = pickle.load(open(self._path))
except (pickle.UnpicklingError, ImportError, EOFError, IndexError, TypeError):
self._value = compile_token(self._path, "utf-8")
self._entries = None
elif self._entries is not None:
self._value = set()
for entry in self._entries:
entry = entry.strip()
if entry:
self._value.add(entry)
assert self._value is not None
示例2: load
# 需要导入模块: import cPickle [as 别名]
# 或者: from cPickle import UnpicklingError [as 别名]
def load(self):
"""Load the file from disk.
Returns:
bool: True if successfully loaded, False if the file
doesn't exist.
Raises:
UnknownFormat: When the file exists but couldn't be loaded.
"""
if not self._loaded and os.path.exists(self.file_path):
with open(self.file_path, 'rb') as f:
for loader in (pickle.load, json.load):
try:
f.seek(0)
self._store = loader(f)
self._loaded = True
break
except pickle.UnpicklingError:
pass
# If the file exists and wasn't able to be loaded, raise an error.
if not self._loaded:
raise UnknownFormat('Failed to load file')
return self._loaded
示例3: Do_SConsignDir
# 需要导入模块: import cPickle [as 别名]
# 或者: from cPickle import UnpicklingError [as 别名]
def Do_SConsignDir(name):
try:
fp = open(name, 'rb')
except (IOError, OSError) as e:
sys.stderr.write("sconsign: %s\n" % (e))
return
try:
sconsign = SCons.SConsign.Dir(fp)
except KeyboardInterrupt:
raise
except cPickle.UnpicklingError:
sys.stderr.write("sconsign: ignoring invalid .sconsign file `%s'\n" % (name))
return
except Exception as e:
sys.stderr.write("sconsign: ignoring invalid .sconsign file `%s': %s\n" % (name, e))
return
printentries(sconsign.entries, args[0])
##############################################################################
示例4: from_db_fmt
# 需要导入模块: import cPickle [as 别名]
# 或者: from cPickle import UnpicklingError [as 别名]
def from_db_fmt(x):
# recursive descent through lists
if isinstance(x, list):
return [from_db_fmt(v) for v in x]
# recursive descent through dicts
if isinstance(x, dict):
return {k: from_db_fmt(v) for k, v in x.items()}
# further code occasionally serializes `ObjectId`s to json, so stringify them now
if isinstance(x, ObjectId):
return str(x)
if isinstance(x, Binary):
# this might be pickled data; let's attempt to deserialize it
try:
return cPickle.loads(x)
except cPickle.UnpicklingError:
# this wasn't pickled data. just return it.
return x
# not a datatype we need to deserialize! just pass it out
return x
示例5: load_models
# 需要导入模块: import cPickle [as 别名]
# 或者: from cPickle import UnpicklingError [as 别名]
def load_models(models_dir):
"""
Load saved models from disk. This will attempt to unpickle all files in a
directory; any files that give errors on unpickling (such as README.txt) will
be skipped.
Inputs:
- models_dir: String giving the path to a directory containing model files.
Each model file is a pickled dictionary with a 'model' field.
Returns:
A dictionary mapping model file names to models.
"""
models = {}
for model_file in os.listdir(models_dir):
with open(os.path.join(models_dir, model_file), 'rb') as f:
try:
models[model_file] = pickle.load(f)['model']
except pickle.UnpicklingError:
continue
return models
示例6: test_getInvalid
# 需要导入模块: import cPickle [as 别名]
# 或者: from cPickle import UnpicklingError [as 别名]
def test_getInvalid(self):
"""
If the value associated with the property name passed to
L{xattrPropertyStore.get} cannot be interpreted, an error is logged and
L{HTTPError} is raised with the I{INTERNAL SERVER ERROR} response code.
"""
document = self._makeValue()
self._setValue(
document,
"random garbage goes here! \0 that nul is definitely garbage")
property = document.root_element.qname()
error = self.assertRaises(HTTPError, self.propertyStore.get, property)
self.assertEquals(error.response.code, INTERNAL_SERVER_ERROR)
self.assertEquals(
len(self.flushLoggedErrors(UnpicklingError, IndexError)), 1)
示例7: get_vector
# 需要导入模块: import cPickle [as 别名]
# 或者: from cPickle import UnpicklingError [as 别名]
def get_vector(word, model, lowercase=True):
# returns vector of word as 300 dimensions, each containing a 16 bit floating point number, trying first for the lowercase version if that's set to True, and returning None if neither exists
if lowercase:
formatted_word = word.replace(" ", "_").lower()
try:
vector = model[formatted_word]
return np.asarray(vector)
except (EOFError, KeyError, UnpicklingError):
return get_vector(word, model, lowercase=False)
else:
formatted_word = word.replace(" ", "_")
try:
vector = model[formatted_word]
return np.asarray(vector)
except (EOFError, KeyError, UnpicklingError):
return None
示例8: get_index
# 需要导入模块: import cPickle [as 别名]
# 或者: from cPickle import UnpicklingError [as 别名]
def get_index(word, model, lowercase=True):
# returns index of word ranging between 0 and 99,999 (corresponding to the order that words were encountered during word2vec training), trying first for the lowercase version if that's set to True, and returning None if neither exists
if lowercase:
formatted_word = word.replace(" ", "_").lower()
try:
word_index = model.vocab[formatted_word].index
return word_index
except (EOFError, KeyError, UnpicklingError):
return get_index(word, model, lowercase=False)
else:
formatted_word = word.replace(" ", "_")
try:
word_index = model.vocab[formatted_word].index
return word_index
except (EOFError, KeyError, UnpicklingError):
return None
示例9: secure_loads
# 需要导入模块: import cPickle [as 别名]
# 或者: from cPickle import UnpicklingError [as 别名]
def secure_loads(data, encryption_key, hash_key=None, compression_level=None):
if not ':' in data:
return None
if not hash_key:
hash_key = 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
示例10: sloppyload
# 需要导入模块: import cPickle [as 别名]
# 或者: from cPickle import UnpicklingError [as 别名]
def sloppyload( f ):
"""
f - str, file name
-> any, unpickled object
"""
try:
T.flushPrint( "Loading " + str(f) + '\n' )
return T.load( T.absfile( f ) )
except cPickle.UnpicklingError:
print "Trying to load %s in sloppy mode..." % f
return PickleUpgrader(open(T.absfile(f))).load()
#########
## Main
#########
示例11: get
# 需要导入模块: import cPickle [as 别名]
# 或者: from cPickle import UnpicklingError [as 别名]
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)
示例12: get
# 需要导入模块: import cPickle [as 别名]
# 或者: from cPickle import UnpicklingError [as 别名]
def get(self, *args, **kw):
# We do it with *args and **kw so if the default value wasn't
# given nothing is passed to the extension module. That way
# an exception can be raised if set_get_returns_none is turned
# off.
data = self.db.get(*args, **kw)
try:
return cPickle.loads(data)
except (EOFError, TypeError, cPickle.UnpicklingError):
return data # we may be getting the default value, or None,
# so it doesn't need unpickled.
示例13: test_load_negative
# 需要导入模块: import cPickle [as 别名]
# 或者: from cPickle import UnpicklingError [as 别名]
def test_load_negative(self):
if cPickle.__name__ == "cPickle": # pickle vs. cPickle report different exceptions, even on Cpy
filename = os.tempnam()
for temp in ['\x02', "No"]:
self.write_to_file(filename, content=temp)
f = open(filename)
self.assertRaises(cPickle.UnpicklingError, cPickle.load, f)
f.close()
示例14: test_bad_input
# 需要导入模块: import cPickle [as 别名]
# 或者: from cPickle import UnpicklingError [as 别名]
def test_bad_input(self):
# Test issue4298
s = '\x58\0\0\0\x54'
self.assertRaises(EOFError, self.module.loads, s)
# Test issue7455
s = '0'
# XXX Why doesn't pickle raise UnpicklingError?
self.assertRaises((IndexError, cPickle.UnpicklingError),
self.module.loads, s)
示例15: pollmessage
# 需要导入模块: import cPickle [as 别名]
# 或者: from cPickle import UnpicklingError [as 别名]
def pollmessage(self, wait):
packet = self.pollpacket(wait)
if packet is None:
return None
try:
message = pickle.loads(packet)
except pickle.UnpicklingError:
print >>sys.__stderr__, "-----------------------"
print >>sys.__stderr__, "cannot unpickle packet:", repr(packet)
traceback.print_stack(file=sys.__stderr__)
print >>sys.__stderr__, "-----------------------"
raise
return message