Esempio n. 1
0
def test_analyze__analyze_storage__11(zodb_storage, zodb_root):
    """It ignores binary strings that are marked with `zodbpickle.binary`."""
    zodb_root['obj'] = Example(data=zodbpickle.binary(b'bïnäry'))
    transaction.commit()
    result, errors = analyze_storage(zodb_storage)
    assert {} == result
    assert {} == errors
Esempio n. 2
0
 def test_binbytes(self):
     from zodbpickle import binary
     x = binary(b'\x00ABC\x80' * 100)
     for proto in protocols:
         s = self.dumps(x, proto)
         y = self.loads(s)
         self.assertEqual(x, y)
         self.assertEqual(opcode_in_pickle(pickle.BINBYTES, s),
                          proto >= 3, str(self.__class__))
Esempio n. 3
0
 def test_binbytes(self):
     from zodbpickle import binary
     x = binary(b'\x00ABC\x80' * 100)
     for proto in protocols:
         s = self.dumps(x, proto)
         y = self.loads(s)
         self.assertEqual(x, y)
         self.assertEqual(opcode_in_pickle(pickle.BINBYTES, s), proto >= 3,
                          str(self.__class__))
Esempio n. 4
0
 def test_not_tracked_by_gc(self):
     # PyPy doesn't have gc.is_tracked, but we don't
     # run these tests there.
     import gc
     from zodbpickle import binary
     s = b'abcdef'
     b = binary(s)
     self.assertFalse(gc.is_tracked(s))
     self.assertFalse(gc.is_tracked(b))
Esempio n. 5
0
    def test_same_size(self):
        # PyPy doesn't support sys.getsizeof, but
        # we don't run these tests there.
        import sys
        from zodbpickle import binary

        s = b'abcdef'
        b = binary(s)
        self.assertEqual(sys.getsizeof(b), sys.getsizeof(s))
Esempio n. 6
0
def convert_storage(storage, mapping, verbose=False):
    """Iterate ZODB objects with binary content and apply mapping."""
    result = collections.defaultdict(int)
    errors = collections.defaultdict(int)
    for obj, data, key, value, type_ in find_obj_with_binary_content(
            storage, errors):
        klassname = get_classname(obj)
        dotted_name = get_format_string(obj).format(**locals())
        encoding = mapping.get(dotted_name, None)
        if encoding is None or type_ == 'key':
            continue

        if encoding == 'zodbpickle.binary':
            data[key] = zodbpickle.binary(value)
        else:
            data[key] = value.decode(encoding)
        obj._p_changed = True
        result[dotted_name] += 1

    transaction.commit()
    return result, errors
Esempio n. 7
0
 def __persistent_load(self, reference):
     """Load a persistent reference. The reference might changed
     according a renaming rules. We give back a special object to
     represent that reference, and not the real object designated
     by the reference.
     """
     # This takes care of returning the OID as bytes in order to convert
     # a database to Python 3.
     if isinstance(reference, tuple):
         oid, cls_info = reference
         if isinstance(cls_info, tuple):
             cls_info = self.__update_symb(cls_info)
         return ZODBReference((zodbpickle.binary(oid), cls_info))
     if isinstance(reference, list):
         if len(reference) == 1:
             oid, = reference
             return ZODBReference(['w', (zodbpickle.binary(oid))])
         mode, information = reference
         if mode == 'm':
             database_name, oid, cls_info = information
             if isinstance(cls_info, tuple):
                 cls_info = self.__update_symb(cls_info)
             return ZODBReference(
                 ['m', (database_name, zodbpickle.binary(oid), cls_info)])
         if mode == 'n':
             database_name, oid = information
             return ZODBReference(
                 ['m', (database_name, zodbpickle.binary(oid))])
         if mode == 'w':
             if len(information) == 1:
                 oid, = information
                 return ZODBReference(['w', (zodbpickle.binary(oid))])
             oid, database_name = information
             return ZODBReference(
                 ['w', (zodbpickle.binary(oid), database_name)])
     if isinstance(reference, (str, zodbpickle.binary)):
         oid = reference
         return ZODBReference(zodbpickle.binary(oid))
     raise AssertionError('Unknown reference format.')
Esempio n. 8
0
 def encode(data):
     value = data.get(attribute)
     if value is not None and not isinstance(value, zodbpickle.binary):
         data[attribute] = zodbpickle.binary(value)
         return True
     return False
Esempio n. 9
0
 def __reduce_ex__(self, protocol):
     type_info, args = super(Time, self).__reduce_ex__(protocol)
     assert len(args) > 0
     return (datetime.time, (zodbpickle.binary(args[0]), ) + args[1:])
Esempio n. 10
0
 def __reduce__(self):
     type_info, args = super(Date, self).__reduce__()
     assert len(args) > 0
     return (datetime.date, (zodbpickle.binary(args[0]), ) + args[1:])
Esempio n. 11
0
 def test_has_no_attrs(self):
     from zodbpickle import binary
     b = binary('abc')
     with self.assertRaises(AttributeError):
         setattr(b, 'attr', 42)
Esempio n. 12
0
def safe_binary(value):
    if isinstance(value, bytes):
        return zodbpickle.binary(value)
    if isinstance(value, six.text_type):
        return zodbpickle.binary(value.encode(ENCODING))
    return value