Beispiel #1
0
 def __call__(self, val=None):
     val = val if val is not None else {}
     if not isinstance(val, dict):
         raise ValueError('value needs to be a dict')
     ret = {}
     for k, v in val.items():
         ret[self.keyconv(k)] = self.valconv(v)
     return readonlydict(ret)
Beispiel #2
0
    def test_readonly_objects(self, session):
        cc = session.cache
        testval1 = readonlylist(('A', 'B', 'C'))
        cc.put('testcache', 'rolist', testval1)
        cc.flush()
        rol1 = cc.get('testcache', 'rolist')
        rol = cc.get_explicit('testcache', 'rolist', None)
        assert rol[2] is not None
        print(type(rol1), type(testval1))
        assert type(rol1) == type(testval1)
        assert type(rol[2]) == type(testval1)

        testval2 = readonlydict((('A', 'B'), ('C', 'D')))
        cc.put('testcache', 'rodict', testval2)
        cc.flush()
        rod = cc.get_explicit('testcache', 'rodict', None)
        assert rod[2] is not None
        print(type(rod[2]), type(testval2))
        assert type(rod[2]) == type(testval2)

        testval3 = readonlylist((testval1, testval2, 'C'))
        cc.put('testcache', 'rolist2', testval3)
        cc.flush()
        rol = cc.get_explicit('testcache', 'rolist2', None)
        assert rol[2] is not None
        print(type(rol[2]), type(testval3))
        assert type(rol[2]) == type(testval3)
        assert type(rol[2][0]) == type(testval1)
        assert type(rol[2][1]) == type(testval2)

        testval4 = readonlydict((('A', testval1), ('B', testval2), ('C', 'D')))
        cc.put('testcache', 'rodict2', testval4)
        cc.flush()
        rod = cc.get_explicit('testcache', 'rodict2', None)
        assert rod[2] is not None
        print(type(rod[2]), type(testval4))
        assert type(rod[2]) == type(testval4)
        assert type(rod[2]['A']) == type(testval1)
        assert type(rod[2]['B']) == type(testval2)
Beispiel #3
0
 def __call__(self, val=None):
     if val is None:
         return {k: conv() for k, conv in self.convs.items()}
     if not isinstance(val, dict):
         raise ValueError('value needs to be a dict')
     vkeys = set(val)
     msgs = []
     if vkeys - self.keys:
         msgs.append('unknown keys: %s' % (vkeys - self.keys))
     if self.keys - vkeys:
         msgs.append('missing keys: %s' % (self.keys - vkeys))
     if msgs:
         raise ValueError('Key mismatch in dictionary: ' + ', '.join(msgs))
     ret = {}
     for k in self.keys:
         ret[k] = self.convs[k](val[k])
     return readonlydict(ret)
Beispiel #4
0
 def _convert(node):
     if isinstance(node, (Str, Bytes)):
         return node.s
     elif isinstance(node, Num):
         return node.n
     elif isinstance(node, Tuple):
         return tuple(map(_convert, node.elts))
     elif isinstance(node, List):
         return readonlylist(map(_convert, node.elts))
     elif isinstance(node, Dict):
         return readonlydict((_convert(k), _convert(v)) for k, v
                             in zip(node.keys, node.values))
     elif isinstance(node, Set):
         return frozenset(map(_convert, node.elts))
     elif isinstance(node, Name):
         if node.id in _safe_names:
             return _safe_names[node.id]
     elif isinstance(node, NameConstant):
         return node.value
     elif isinstance(node, UnaryOp) and \
             isinstance(node.op, USub) and \
             isinstance(node.operand, Name) and \
             node.operand.id in _safe_names:
         return -_safe_names[node.operand.id]
     elif isinstance(node, UnaryOp) and \
             isinstance(node.op, USub) and \
             isinstance(node.operand, Num):
         return -node.operand.n
     elif isinstance(node, BinOp) and \
             isinstance(node.op, (Add, Sub)) and \
             isinstance(node.right, Num) and \
             isinstance(node.right.n, complex) and \
             isinstance(node.left, Num) and \
             isinstance(node.left.n, number_types):
         left = node.left.n
         right = node.right.n
         if isinstance(node.op, Add):
             return left + right
         else:
             return left - right
     elif isinstance(node, Call) and node.func.id == 'cache_unpickle':
         return pickle.loads(b64decode(ast_eval(node.args[0])))
     raise ValueError('malformed literal string with %s' % node)
Beispiel #5
0
def test_readonly_objects():
    d = readonlydict({'a': 1, 'b': 2})
    assert raises(TypeError, d.update, {})

    # pickle Protocoll 0
    unpickled = pickle.loads(pickle.dumps(d))
    assert isinstance(unpickled, readonlydict)
    assert len(unpickled) == 2
    # pickle Protocoll 2
    unpickled = pickle.loads(pickle.dumps(d, 2))
    assert isinstance(unpickled, readonlydict)
    assert len(unpickled) == 2

    l = readonlylist([1, 2, 3])
    assert raises(TypeError, l.append, 4)

    # pickle Protocoll 0
    unpickled = pickle.loads(pickle.dumps(l))
    assert isinstance(unpickled, readonlylist)
    assert len(unpickled) == 3
    # pickle Protocoll 2
    unpickled = pickle.loads(pickle.dumps(l, 2))
    assert isinstance(unpickled, readonlylist)
    assert len(unpickled) == 3