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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
"""Marshal module written in Python.
This doesn't marshal code objects, but supports everything else.
Performance or careful error checking is not an issue.
"""
import StringIO
import string
from types import *
try:
import new
except ImportError:
new = None
TYPE_NULL = '0'
TYPE_NONE = 'N'
TYPE_ELLIPSIS = '.'
TYPE_INT = 'i'
TYPE_INT64 = 'I'
TYPE_FLOAT = 'f'
TYPE_COMPLEX = 'x'
TYPE_LONG = 'l'
TYPE_STRING = 's'
TYPE_TUPLE = '('
TYPE_LIST = '['
TYPE_DICT = '{'
TYPE_CODE = 'c'
TYPE_UNKNOWN = '?'
class Marshaller:
dispatch = {}
def __init__(self, f):
self.f = f
def dump(self, x):
self.dispatch[type(x)](self, x)
def w_long64(self, x):
self.w_long(x)
self.w_long(x>>32)
def w_long(self, x):
write = self.f.write
write(chr((x) & 0xff))
write(chr((x>> 8) & 0xff))
write(chr((x>>16) & 0xff))
write(chr((x>>24) & 0xff))
def w_short(self, x):
write = self.f.write
write(chr((x) & 0xff))
write(chr((x>> 8) & 0xff))
def dump_none(self, x):
self.f.write(TYPE_NONE)
dispatch[NoneType] = dump_none
def dump_ellipsis(self, x):
self.f.write(TYPE_ELLIPSIS)
try:
dispatch[EllipsisType] = dump_ellipsis
except NameError:
pass
def dump_int(self, x):
y = x>>31
if y and y != -1:
self.f.write(TYPE_INT64)
self.w_long64(x)
else:
self.f.write(TYPE_INT)
self.w_long(x)
dispatch[IntType] = dump_int
def dump_long(self, x):
self.f.write(TYPE_LONG)
sign = 1
if x < 0:
sign = -1
x = -x
digits = []
while x:
digits.append(x & 0x7FFF)
x = x>>15
self.w_long(len(digits) * sign)
for d in digits:
self.w_short(d)
dispatch[LongType] = dump_long
def dump_float(self, x):
write = self.f.write
write(TYPE_FLOAT)
s = `x`
write(chr(len(s)))
write(s)
dispatch[FloatType] = dump_float
def dump_complex(self, x):
write = self.f.write
write(TYPE_COMPLEX)
s = `x.real`
write(chr(len(s)))
write(s)
s = `x.imag`
write(chr(len(s)))
write(s)
try:
dispatch[ComplexType] = dump_complex
except NameError:
pass
def dump_string(self, x):
self.f.write(TYPE_STRING)
self.w_long(len(x))
self.f.write(x)
dispatch[StringType] = dump_string
def dump_tuple(self, x):
self.f.write(TYPE_TUPLE)
self.w_long(len(x))
for item in x:
self.dump(item)
dispatch[TupleType] = dump_tuple
def dump_list(self, x):
self.f.write(TYPE_LIST)
self.w_long(len(x))
for item in x:
self.dump(item)
dispatch[ListType] = dump_list
def dump_dict(self, x):
self.f.write(TYPE_DICT)
for key, value in x.items():
self.dump(key)
self.dump(value)
self.f.write(TYPE_NULL)
dispatch[DictionaryType] = dump_dict
def dump_code(self, x):
self.f.write(TYPE_CODE)
self.w_short(x.co_argcount)
self.w_short(x.co_nlocals)
self.w_short(x.co_stacksize)
self.w_short(x.co_flags)
self.dump(x.co_code)
self.dump(x.co_consts)
self.dump(x.co_names)
self.dump(x.co_varnames)
self.dump(x.co_filename)
self.dump(x.co_name)
self.w_short(x.co_firstlineno)
self.dump(x.co_lnotab)
try:
dispatch[CodeType] = dump_code
except NameError:
pass
class NULL:
pass
class Unmarshaller:
dispatch = {}
def __init__(self, f):
self.f = f
def load(self):
c = self.f.read(1)
if not c:
raise EOFError
return self.dispatch[c](self)
def r_short(self):
read = self.f.read
lo = ord(read(1))
hi = ord(read(1))
x = lo | (hi<<8)
if x & 0x8000:
x = x - 0x10000
return x
def r_long(self):
read = self.f.read
a = ord(read(1))
b = ord(read(1))
c = ord(read(1))
d = ord(read(1))
x = a | (b<<8) | (c<<16) | (d<<24)
if x & 0x80000000 and x > 0:
x = string.atoi(x - 0x100000000L)
return x
def r_long64(self):
a = self.r_long()
b = self.r_long()
return a | (b<<32)
def load_null(self):
return NULL
dispatch[TYPE_NULL] = load_null
def load_none(self):
return None
dispatch[TYPE_NONE] = load_none
def load_ellipsis(self):
return EllipsisType
dispatch[TYPE_ELLIPSIS] = load_ellipsis
def load_int(self):
return self.r_long()
dispatch[TYPE_INT] = load_int
def load_int64(self):
return self.r_long64()
dispatch[TYPE_INT64] = load_int64
def load_long(self):
size = self.r_long()
sign = 1
if size < 0:
sign = -1
size = -size
x = 0L
for i in range(size):
d = self.r_short()
x = x | (d<<(i*15L))
return x * sign
dispatch[TYPE_LONG] = load_long
def load_float(self):
n = ord(self.f.read(1))
s = self.f.read(n)
return string.atof(s)
dispatch[TYPE_FLOAT] = load_float
def load_complex(self):
n = ord(self.f.read(1))
s = self.f.read(n)
real = float(s)
n = ord(self.f.read(1))
s = self.f.read(n)
imag = float(s)
return complex(real, imag)
dispatch[TYPE_COMPLEX] = load_complex
def load_string(self):
n = self.r_long()
return self.f.read(n)
dispatch[TYPE_STRING] = load_string
def load_tuple(self):
return tuple(self.load_list())
dispatch[TYPE_TUPLE] = load_tuple
def load_list(self):
n = self.r_long()
list = []
for i in range(n):
list.append(self.load())
return list
dispatch[TYPE_LIST] = load_list
def load_dict(self):
d = {}
while 1:
key = self.load()
if key is NULL:
break
value = self.load()
d[key] = value
return d
dispatch[TYPE_DICT] = load_dict
def load_code(self):
argcount = self.r_short()
nlocals = self.r_short()
stacksize = self.r_short()
flags = self.r_short()
code = self.load()
consts = self.load()
names = self.load()
varnames = self.load()
filename = self.load()
name = self.load()
firstlineno = self.r_short()
lnotab = self.load()
if not new:
raise RuntimeError, "can't unmarshal code objects; no 'new' module"
return new.code(argcount, nlocals, stacksize, flags, code, consts,
names, varnames, filename, name, firstlineno, lnotab)
dispatch[TYPE_CODE] = load_code
def dump(x, f):
Marshaller(f).dump(x)
def load(f):
return Unmarshaller(f).load()
def dumps(x):
f = StringIO.StringIO()
dump(x, f)
return f.getvalue()
def loads(s):
f = StringIO.StringIO(s)
return load(f)
|