-
Notifications
You must be signed in to change notification settings - Fork 29
/
mutable_str_pack.py
48 lines (35 loc) · 951 Bytes
/
mutable_str_pack.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
44
45
46
47
__all__ = ['MutableString']
import ctypes
from numpy.compat import asbytes
class MutableString:
"""Mutable string.
Attributes
----------
data_ptr : int
Pointer to memory location.
Method
------
__len__ : Length of string.
"""
def __init__(self, s):
"""
Parameters
----------
s : str
String.
"""
self._s = ctypes.create_string_buffer(asbytes(s))
self._n = len(s)
self.data_ptr = ctypes.addressof(self._s)
def __len__(self):
return self._n
def __str__(self):
return self._s.raw.decode('utf-8')
if __name__ == "__main__":
# Basic test
m = MutableString("asdb")
template = 'import base64\ncode=%s\nexec(base64.decodestring(code))'
import base64
with open(__file__, 'r') as f:
content = ''.join(f.readlines())
print(template % base64.b64encode(asbytes(content)))