forked from cirosantilli/python-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpickle_cheat.py
More file actions
executable file
·117 lines (88 loc) · 2.57 KB
/
pickle_cheat.py
File metadata and controls
executable file
·117 lines (88 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python
import pickle
if 'slots error':
# Use -1 pickling version.
# http://stackoverflow.com/questions/2204155/why-am-i-getting-an-error-about-my-class-defining-slots-when-trying-to-pickl/2204702#2204702
class C(object):
__slots__ = 'a'
def __init__(self, a):
self.a = a
s = pickle.dumps(C(1), -1)
assert pickle.loads(s).a == 1
if 'instancemethod error':
# http://stackoverflow.com/questions/27318290/why-can-i-pass-an-instance-method-to-multiprocessing-process-but-not-a-multipro
# Basic example
class A(object):
def z(self):
return 1
try:
pickle.dumps(A.z, -1)
except pickle.PicklingError:
pass
else:
raise
# Less obvious via inner member:
class A(object):
def __init__(self):
self.f = self.f
def f():
return 1
try:
pickle.dumps(A(), -1)
except pickle.PicklingError:
pass
else:
raise
# Fine with regular functions:
def f():
return 1
class A(object):
def __init__(self):
self.f = f
s = pickle.dumps(A(), -1)
pickle.loads(s).f() == 1
if '# getstate # setstate':
if 'Minimal example':
"""
- __getstate__ spits a dict
- __setstate__ reads the dict and uses it to setup an object
"""
class C(object):
def __init__(self, i):
self.i = i
def f(self):
return self.i + 1
def __getstate__(self):
return {'a': self.i}
def __setstate__(self, d):
self.i = d['a']
assert pickle.loads(pickle.dumps(C(1), -1)).i == 1
assert pickle.loads(pickle.dumps(C(1), -1)).f() == 2
if 'Mischief example':
x = 0
class C(object):
def __init__(self, i):
self.i = i
def __getstate__(self):
global x
x = 1
self.i *= 2
return self.__dict__
def __setstate__(self, d):
global x
x = 2
self.__dict__ = d
self.i *= 3
c = C(1)
assert c.i == 1
assert x == 0
s = pickle.dumps(C(1))
# __getstate__ was called.
assert x == 1
# But it is not called on the original object, which was not changed.
assert c.i == 1
c2 = pickle.loads(s)
# __setstate__ was called.
assert x == 2
assert c.i == 1
assert c2.i == 6