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
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__))
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))
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))
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
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.')
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
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:])
def __reduce__(self): type_info, args = super(Date, self).__reduce__() assert len(args) > 0 return (datetime.date, (zodbpickle.binary(args[0]), ) + args[1:])
def test_has_no_attrs(self): from zodbpickle import binary b = binary('abc') with self.assertRaises(AttributeError): setattr(b, 'attr', 42)
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