Skip to content

Instantly share code, notes, and snippets.

@jkbbwr
Created March 27, 2014 19:18
Show Gist options
  • Save jkbbwr/9815846 to your computer and use it in GitHub Desktop.
Save jkbbwr/9815846 to your computer and use it in GitHub Desktop.
def tuplecpy(dst, src, begin_offset):
"""
Of course this function should NEVER be used in real code
It will probably result in segfaults/crashes
- copy tuple(src) to dst[begin_offset:] tuple
- remember id(x) -> addressof(x)
"""
OFFSET = ctypes.sizeof(ctypes.c_size_t) * 3
PTR_SIZE = ctypes.sizeof(ctypes.c_size_t)
dst_addr = id(dst) + OFFSET + PTR_SIZE * begin_offset
src_addr = id(src) + OFFSET
ctypes.memmove(dst_addr, src_addr, len(src) * PTR_SIZE)
def x2(x):
return x * 2
print x2(4)
# Orly?
tuplecpy(x2.func_code.co_consts, (None, 10), 0)
print x2(4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment