Python pickle 模块,INT 实例源码
我们从Python开源项目中,提取了以下13个代码示例,用于说明如何使用pickle.INT。
def test_singletons(self):
# Map (proto,singleton) to expected opcode.
expected_opcode = {(0, None): pickle.NONE,
(1,
(2,
(3,
(0, True): pickle.INT, True): pickle.NEWTRUE, False): pickle.INT, False): pickle.NEWFALSE,
}
for proto in protocols:
for x in None, False, True:
s = self.dumps(x, proto)
y = self.loads(s)
self.assertTrue(x is y, (proto, x, s, y))
expected = expected_opcode[proto, x]
self.assertEqual(opcode_in_pickle(expected, s), True)
def test_singletons(self):
# Map (proto, y))
expected = expected_opcode[min(proto, 3), x]
self.assertTrue(opcode_in_pickle(expected, s))
def test_simple_newobj(self):
x = SimpleNewObj.__new__(SimpleNewObj, 0xface) # avoid __init__
x.abc = 666
for proto in protocols:
s = self.dumps(x, proto)
if proto < 1:
self.assertIn('\nI64206', s) # INT
else:
self.assertIn('M\xce\xfa', s) # BININT2
self.assertEqual(opcode_in_pickle(pickle.NEWOBJ, proto >= 2)
y = self.loads(s) # will raise TypeError if __init__ called
self.assertEqual(y.abc, 666)
self.assertEqual(x.__dict__, y.__dict__)
def test_complex_newobj(self):
x = ComplexNewObj.__new__(ComplexNewObj, s) # INT
elif proto < 2:
self.assertIn('M\xce\xfa', s) # BININT2
else:
self.assertIn('U\x04FACE', s) # SHORT_BINSTRING
self.assertEqual(opcode_in_pickle(pickle.NEWOBJ, y.__dict__)
def test_simple_newobj(self):
x = SimpleNewObj.__new__(SimpleNewObj, y.__dict__)
def test_complex_newobj(self):
x = ComplexNewObj.__new__(ComplexNewObj, y.__dict__)
def test_frame_readline(self):
pickled = b'\x80\x04\x95\x05\x00\x00\x00\x00\x00\x00\x00I42\n.'
# 0: \x80 PROTO 4
# 2: \x95 FRAME 5
# 11: I INT 42
# 15: . STOP
self.assertEqual(self.loads(pickled), 42)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。