Skip to content

Instantly share code, notes, and snippets.

@nhoad

nhoad/cpycpy.py

Created Feb 3, 2016
Embed
What would you like to do?
CPyCPy
import cffi
ffi = cffi.FFI()
ffi.cdef('int PyDict_DelItemString(void *, const char *key);')
lib = ffi.dlopen('libpython3.so')
d = {'foo': 'bar'}
print('before', d)
d_p = ffi.cast('void*', id(d))
lib.PyDict_DelItemString(d_p, ffi.new('char[]', b'foo'))
print('after', d)
assert 'foo' not in d
nathan@xxxxxx ~ $ python cpycpy.py
before {'foo': 'bar'}
after {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment