Python cPickle.UnpicklingError() Examples

The following are 30 code examples of cPickle.UnpicklingError(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module cPickle , or try the search function .
Example #1
Source File: dictionaryfeatures.py    From SEM with MIT License 7 votes vote down vote up
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 
Example #2
Source File: DatabaseAPI.py    From NEXT with Apache License 2.0 6 votes vote down vote up
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 
Example #3
Source File: redump.py    From biskit with GNU General Public License v3.0 6 votes vote down vote up
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
######### 
Example #4
Source File: utils.py    From termite-visualizations with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 
Example #5
Source File: words2map.py    From words2map with MIT License 6 votes vote down vote up
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 
Example #6
Source File: words2map.py    From words2map with MIT License 6 votes vote down vote up
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 
Example #7
Source File: test_xattrprops.py    From ccs-calendarserver with Apache License 2.0 6 votes vote down vote up
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) 
Example #8
Source File: data_utils.py    From cs231n-practice with MIT License 6 votes vote down vote up
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 
Example #9
Source File: sconsign.py    From pivy with ISC License 6 votes vote down vote up
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])

############################################################################## 
Example #10
Source File: storage.py    From kodiswift with GNU General Public License v3.0 6 votes vote down vote up
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 
Example #11
Source File: base.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
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) 
Example #12
Source File: ringmasters.py    From goreviewpartner with GNU General Public License v3.0 5 votes vote down vote up
def load_status(self):
        """Read the persistent state file and load the state it contains."""
        try:
            status_format_version, status = self._load_status()
            if (status_format_version != self.status_format_version or
                status['comp_vn'] != self.competition.status_format_version):
                raise StandardError
            self.void_game_count = status['void_game_count']
            self.games_in_progress = {}
            self.games_to_replay = {}
            competition_status = status['comp']
        except pickle.UnpicklingError:
            raise RingmasterError("corrupt status file")
        except EnvironmentError, e:
            raise RingmasterError("error loading status file:\n%s" % e) 
Example #13
Source File: bias_classifier.py    From news-audit with GNU General Public License v3.0 5 votes vote down vote up
def load_model(self, model_file=None):
    	""" Load model from pre-trained pickle"""
        if self.debug:
            logging.info("Loading model %s" % model_file)
        try:
            with open(model_file, "rb") as pkl:
                pipeline = pickle.load(pkl)
        except (IOError, pickle.UnpicklingError) as e:
            logging.critical(str(e))
            raise e
        return pipeline 
Example #14
Source File: SensationalismClassifier.py    From news-audit with GNU General Public License v3.0 5 votes vote down vote up
def load_model(self, model_file=None):
    	""" Load model from pre-trained pickle"""
        if self.debug:
            logging.info("Loading model %s" % model_file)
        try:
            with open(model_file, "rb") as pkl:
                pipeline = pickle.load(pkl)
        except (IOError, pickle.UnpicklingError) as e:
            logging.critical(str(e))
            raise e
        return pipeline 
Example #15
Source File: test_cPickle.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
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() 
Example #16
Source File: dbshelve.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
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. 
Example #17
Source File: pickletester.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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) 
Example #18
Source File: dbshelve.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
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. 
Example #19
Source File: rpc.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
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 
Example #20
Source File: dbshelve.py    From BinderFilter with MIT License 5 votes vote down vote up
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. 
Example #21
Source File: base.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
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) 
Example #22
Source File: helper.py    From anna-molly with MIT License 5 votes vote down vote up
def find_class(cls, module, name):
        if module not in cls.PICKLE_SAFE:
            raise pickle.UnpicklingError(
                'Attempting to unpickle unsafe module %s' % module)
        __import__(module)
        mod = sys.modules[module]
        if module not in cls.PICKLE_SAFE[module]:
            raise pickle.UnpicklingError(
                'Attempting to unpickle unsafe class %s' % name)
        return getattr(mod, name) 
Example #23
Source File: pickletester.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
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) 
Example #24
Source File: dictionaryfeatures.py    From SEM with MIT License 5 votes vote down vote up
def __init__(self, *args, **kwargs):
        super(MultiwordDictionaryFeature, self).__init__(*args, **kwargs)
        self._is_sequence = True
        self._entry       = kwargs["entry"]
        self._appendice   = kwargs.get("appendice", "")
        
        if self._path is not None:
            try:
                self._value = pickle.load(open(self._path))
            except (pickle.UnpicklingError, ImportError, EOFError):
                self._value = compile_multiword(self._path, "utf-8")
            self._entries = None
        elif self._entries:
            self._value = Trie()
            for entry in self._entries:
                entry = entry.strip()
                if entry:
                    self._value.add(entry.split())
        else:
            self._value = Trie() 
Example #25
Source File: dbshelve.py    From PokemonGo-DesktopMap with MIT License 5 votes vote down vote up
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. 
Example #26
Source File: pickletester.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
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) 
Example #27
Source File: upgrade.py    From ccs-calendarserver with Apache License 2.0 5 votes vote down vote up
def updateFreeBusySet(value, directory):

    try:
        value = zlib.decompress(value)
    except zlib.error:
        # Legacy data - not zlib compressed
        pass

    try:
        doc = element.WebDAVDocument.fromString(value)
        freeBusySet = doc.root_element
    except ValueError:
        try:
            freeBusySet = unpickle(value)
        except UnpicklingError:
            log.error("Invalid free/busy property value")
            returnValue(None)

    fbset = set()
    didUpdate = False
    for href in freeBusySet.children:
        href = str(href)
        newHref = yield updateFreeBusyHref(href, directory)
        if newHref is None:
            fbset.add(href)
        else:
            didUpdate = True
            if newHref != "":
                fbset.add(newHref)

    if didUpdate:
        property = caldavxml.CalendarFreeBusySet(
            *[element.HRef(fbhref) for fbhref in fbset]
        )
        value = compress(property.toxml())
        returnValue(value)

    returnValue(None)  # no update required 
Example #28
Source File: dbshelve.py    From Computable with MIT License 5 votes vote down vote up
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. 
Example #29
Source File: dbshelve.py    From oss-ftp with MIT License 5 votes vote down vote up
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. 
Example #30
Source File: pickletester.py    From oss-ftp with MIT License 5 votes vote down vote up
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)