def test_pickler(self):
basesize = support.calcobjsize('5P2n3i2n3iP')
p = _pickle.Pickler(io.BytesIO())
self.assertEqual(object.__sizeof__(p), basesize)
MT_size = struct.calcsize('3nP0n')
ME_size = struct.calcsize('Pn0P')
check = self.check_sizeof
check(p, basesize +
MT_size + 8 * ME_size + # Minimal memo table size.
sys.getsizeof(b'x'*4096)) # Minimal write buffer size.
for i in range(6):
p.dump(chr(i))
check(p, basesize +
MT_size + 32 * ME_size + # Size of memo table required to
# save references to 6 objects.
0) # Write buffer is cleared after every dump().
python类Pickler()的实例源码
def test_signature_on_builtin_class(self):
self.assertEqual(str(inspect.signature(_pickle.Pickler)),
'(file, protocol=None, fix_imports=True)')
class P(_pickle.Pickler): pass
class EmptyTrait: pass
class P2(EmptyTrait, P): pass
self.assertEqual(str(inspect.signature(P)),
'(file, protocol=None, fix_imports=True)')
self.assertEqual(str(inspect.signature(P2)),
'(file, protocol=None, fix_imports=True)')
class P3(P2):
def __init__(self, spam):
pass
self.assertEqual(str(inspect.signature(P3)), '(spam)')
class MetaP(type):
def __call__(cls, foo, bar):
pass
class P4(P2, metaclass=MetaP):
pass
self.assertEqual(str(inspect.signature(P4)), '(foo, bar)')
def test_signature_on_builtin_class(self):
self.assertEqual(str(inspect.signature(_pickle.Pickler)),
'(file, protocol=None, fix_imports=True)')
class P(_pickle.Pickler): pass
class EmptyTrait: pass
class P2(EmptyTrait, P): pass
self.assertEqual(str(inspect.signature(P)),
'(file, protocol=None, fix_imports=True)')
self.assertEqual(str(inspect.signature(P2)),
'(file, protocol=None, fix_imports=True)')
class P3(P2):
def __init__(self, spam):
pass
self.assertEqual(str(inspect.signature(P3)), '(spam)')
class MetaP(type):
def __call__(cls, foo, bar):
pass
class P4(P2, metaclass=MetaP):
pass
self.assertEqual(str(inspect.signature(P4)), '(foo, bar)')
def test_unbound_builtin_method(self):
self.assertEqual(self._get_summary_line(_pickle.Pickler.dump),
"dump(self, obj, /)")
# these no longer include "self"
def test_bound_builtin_method(self):
s = StringIO()
p = _pickle.Pickler(s)
self.assertEqual(self._get_summary_line(p.dump),
"dump(obj, /) method of _pickle.Pickler instance")
# this should *never* include self!
def test_getfullargspec_builtin_methods(self):
self.assertFullArgSpecEquals(_pickle.Pickler.dump,
args_e=['self', 'obj'], formatted='(self, obj)')
self.assertFullArgSpecEquals(_pickle.Pickler(io.BytesIO()).dump,
args_e=['self', 'obj'], formatted='(self, obj)')
self.assertFullArgSpecEquals(
os.stat,
args_e=['path'],
kwonlyargs_e=['dir_fd', 'follow_symlinks'],
kwonlydefaults_e={'dir_fd': None, 'follow_symlinks': True},
formatted='(path, *, dir_fd=None, follow_symlinks=True)')
def serial(result, fname = "temp.bin"):
if charade.detect(fname)['encoding'] == 'utf-8':
fname = convert(fname)
root_dir = os.path.dirname(__file__)
fname = root_dir + "\\" +fname
f = open(fname,"wb")
p = cPickle.Pickler(f)
p.clear_memo()
p.fast = True
p.dump(result)
f.close()
def test_unbound_builtin_method(self):
self.assertEqual(self._get_summary_line(_pickle.Pickler.dump),
"dump(self, obj, /)")
# these no longer include "self"
def test_bound_builtin_method(self):
s = StringIO()
p = _pickle.Pickler(s)
self.assertEqual(self._get_summary_line(p.dump),
"dump(obj, /) method of _pickle.Pickler instance")
# this should *never* include self!
def test_getfullargspec_builtin_methods(self):
self.assertFullArgSpecEquals(_pickle.Pickler.dump,
args_e=['self', 'obj'], formatted='(self, obj)')
self.assertFullArgSpecEquals(_pickle.Pickler(io.BytesIO()).dump,
args_e=['self', 'obj'], formatted='(self, obj)')
self.assertFullArgSpecEquals(
os.stat,
args_e=['path'],
kwonlyargs_e=['dir_fd', 'follow_symlinks'],
kwonlydefaults_e={'dir_fd': None, 'follow_symlinks': True},
formatted='(path, *, dir_fd=None, follow_symlinks=True)')