Last active
August 29, 2015 14:05
-
-
Save flacjacket/3cb0fdec0d6414ef179d to your computer and use it in GitHub Desktop.
wtf cffi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import weakref | |
from xcffib.ffi import bytes_to_cdata, ffi | |
global_weakkeydict = weakref.WeakKeyDictionary() | |
def test_to_cffi(): | |
c_key = ffi.new("struct xcb_extension_t *") | |
c_key.name = name = bytes_to_cdata(b"testing") | |
c_key_ref = weakref.ref(c_key) | |
name_ref = weakref.ref(name) | |
return c_key, c_key_ref, name_ref | |
def test_to_cffi2(): | |
c_key = ffi.new("struct xcb_extension_t *") | |
c_key.name = name = bytes_to_cdata(b"testing2") | |
global_weakkeydict[c_key] = name | |
c_key_ref = weakref.ref(c_key) | |
name_ref = weakref.ref(name) | |
return c_key, c_key_ref, name_ref | |
k, kr, nr = test_to_cffi() | |
k2, kr2, nr2 = test_to_cffi2() | |
import gc | |
gc.collect() | |
assert kr() is not None | |
assert nr() is None # We don't want this to be None, but it is | |
assert ffi.string(k.name) != b'testing' # As a result we lose the name variable | |
assert kr2() is not None | |
assert nr2() is not None # Now the reference sticks around | |
assert ffi.string(k2.name) == b'testing2' # Now the name variable doesn't change |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment