Example #1
0
 def test17_pickle(self):
     "Testing pickle support."
     g1 = OGRGeometry("LINESTRING(1 1 1,2 2 2,3 3 3)", "WGS84")
     g2 = pickle.loads(pickle.dumps(g1))
     self.assertEqual(g1, g2)
     self.assertEqual(4326, g2.srs.srid)
     self.assertEqual(g1.srs.wkt, g2.srs.wkt)
Example #2
0
 def set(self, key, value, timeout=None, version=None):
     key = self.make_key(key, version=version)
     self.validate_key(key)
     with self._lock.writer():
         try:
             pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL)
             self._set(key, pickled, timeout)
         except pickle.PickleError:
             pass
Example #3
0
 def incr(self, key, delta=1, version=None):
     value = self.get(key, version=version)
     if value is None:
         raise ValueError("Key '%s' not found" % key)
     new_value = value + delta
     key = self.make_key(key, version=version)
     with self._lock.writer():
         try:
             pickled = pickle.dumps(new_value, pickle.HIGHEST_PROTOCOL)
             self._cache[key] = pickled
         except pickle.PickleError:
             pass
     return new_value
Example #4
0
 def add(self, key, value, timeout=None, version=None):
     key = self.make_key(key, version=version)
     self.validate_key(key)
     with self._lock.writer():
         exp = self._expire_info.get(key)
         if exp is None or exp <= time.time():
             try:
                 pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL)
                 self._set(key, pickled, timeout)
                 return True
             except pickle.PickleError:
                 pass
         return False
Example #5
0
    def _base_set(self, mode, key, value, timeout=None):
        if timeout is None:
            timeout = self.default_timeout
        db = router.db_for_write(self.cache_model_class)
        table = connections[db].ops.quote_name(self._table)
        cursor = connections[db].cursor()

        cursor.execute("SELECT COUNT(*) FROM %s" % table)
        num = cursor.fetchone()[0]
        now = timezone.now()
        now = now.replace(microsecond=0)
        if settings.USE_TZ:
            exp = datetime.utcfromtimestamp(time.time() + timeout)
        else:
            exp = datetime.fromtimestamp(time.time() + timeout)
        exp = exp.replace(microsecond=0)
        if num > self._max_entries:
            self._cull(db, cursor, now)
        pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL)
        encoded = base64.b64encode(pickled).strip()
        cursor.execute("SELECT cache_key, expires FROM %s "
                       "WHERE cache_key = %%s" % table, [key])
        try:
            result = cursor.fetchone()
            if result and (mode == 'set' or
                    (mode == 'add' and result[1] < now)):
                cursor.execute("UPDATE %s SET value = %%s, expires = %%s "
                               "WHERE cache_key = %%s" % table,
                               [encoded, connections[db].ops.value_to_db_datetime(exp), key])
            else:
                cursor.execute("INSERT INTO %s (cache_key, value, expires) "
                               "VALUES (%%s, %%s, %%s)" % table,
                               [key, encoded, connections[db].ops.value_to_db_datetime(exp)])
        except DatabaseError:
            # To be threadsafe, updates/inserts are allowed to fail silently
            transaction.rollback_unless_managed(using=db)
            return False
        else:
            transaction.commit_unless_managed(using=db)
            return True
Example #6
0
    def test_pickle(self):
        "Testing pickling and unpickling support."
        # Using both pickle and cPickle -- just 'cause.
        from djangocg.utils.six.moves import cPickle
        import pickle

        # Creating a list of test geometries for pickling,
        # and setting the SRID on some of them.
        def get_geoms(lst, srid=None):
            return [GEOSGeometry(tg.wkt, srid) for tg in lst]
        tgeoms = get_geoms(self.geometries.points)
        tgeoms.extend(get_geoms(self.geometries.multilinestrings, 4326))
        tgeoms.extend(get_geoms(self.geometries.polygons, 3084))
        tgeoms.extend(get_geoms(self.geometries.multipolygons, 900913))

        # The SRID won't be exported in GEOS 3.0 release candidates.
        no_srid = self.null_srid == -1
        for geom in tgeoms:
            s1, s2 = cPickle.dumps(geom), pickle.dumps(geom)
            g1, g2 = cPickle.loads(s1), pickle.loads(s2)
            for tmpg in (g1, g2):
                self.assertEqual(geom, tmpg)
                if not no_srid: self.assertEqual(geom.srid, tmpg.srid)
Example #7
0
 def encode(self, session_dict):
     "Returns the given session dictionary pickled and encoded as a string."
     pickled = pickle.dumps(session_dict, pickle.HIGHEST_PROTOCOL)
     hash = self._hash(pickled)
     return base64.b64encode(hash.encode() + b":" + pickled).decode('ascii')
 def dumps(self, obj):
     return pickle.dumps(obj, pickle.HIGHEST_PROTOCOL)