-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
snappy_hack.py
43 lines (37 loc) · 1.54 KB
/
snappy_hack.py
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
#!/usr/bin/env python
# coding=utf-8
"""
$ python snappy_hack.py
s <type 'str'> 45 The quick brown fox jumped over the lazy dog.
compressed <type 'str'> 47 -�The quick brown fox jumped over the lazy dog.
buf <type 'buffer'> 47 -�The quick brown fox jumped over the lazy dog.
buf_d <type 'str'> 45 The quick brown fox jumped over the lazy dog.
mem <type 'memoryview'> 47 <memory at 0x7ffa61147478>
Traceback (most recent call last):
File "snappy_hack.py", line 23, in <module>
mem_d = snappy.decompress(mem) # <-- TypeError: argument 1 must be string or read-only buffer, not memoryview
File "/home/travis/virtualenv/python2.7.14/lib/python2.7/site-packages/snappy/snappy.py", line 91, in uncompress
return _uncompress(data)
TypeError: argument 1 must be string or read-only buffer, not memoryview
"""
import snappy
fmt = '{:10} {:19} {} {}'
def out(name, x):
print(fmt.format(name, str(type(x)), len(x), x))
s = 'The quick brown fox jumped over the lazy dog.'
out('s', s)
compressed = snappy.compress(s)
out('compressed', compressed)
try:
buf = buffer(compressed)
out('buf', buf)
buf_d = snappy.decompress(buf)
out('buf_d', buf_d)
except NameError:
print('buffer was removed from Python 3.')
mem = memoryview(compressed)
out('mem', mem)
mem_d = snappy.decompress(mem) # <-- TypeError
# Python 2: TypeError: argument 1 must be string or read-only buffer, not memoryview
# Python 3: TypeError: argument 1 must be read-only bytes-like object, not memoryview
out('mem_d', mem_d)