-USING: alien.c-types alien.data alien.libraries classes.struct kernel
-python.errors python.ffi ;
+USING: accessors alien.c-types alien.data alien.libraries classes.struct
+io.encodings.ascii io.encodings.utf8 kernel libc python.errors python.ffi ;
IN: python.objects
! The None object
dup unsteal-ref PyList_SetItem check-zero ;
! Functions
+: <PyMethodDef> ( alien name doc/f -- cfunction )
+ PyMethodDef malloc-struct &free
+ swap [ utf8 malloc-string &free >>ml_doc ] when*
+ swap ascii malloc-string &free >>ml_name
+ swap >>ml_meth
+ METH_VARARGS >>ml_flags ;
+
: <py-cfunction> ( alien -- cfunction )
- f swap METH_VARARGS f PyMethodDef <struct-boa> f f
+ "cfunction" f <PyMethodDef> f f
! It's not clear from the docs whether &Py_DecRef is right for
! PyCFunction_NewEx, but I'm betting on it.
PyCFunction_NewEx check-new-ref ;
USING: accessors alien arrays assocs calendar continuations destructors
-destructors.private fry kernel math namespaces python python.errors python.ffi
-python.objects sequences strings tools.test ;
+destructors.private fry kernel math memory namespaces python python.errors
+python.ffi python.objects sequences strings tools.test ;
IN: python
: py-test ( result quot -- )
[ t ] [
"os" py-import PyModule_GetDict dup Py_IncRef &Py_DecRef py-dict-size 100 >
] py-test
+
+! CFunctions
+{ f } [
+ 1234 <alien> <py-cfunction> "__doc__" getattr py>
+] py-test
+
+{ "cfunction" } [
+ 1234 <alien> <py-cfunction>
+ ! Force nursery flush
+ 10000 [ 1000 0xff <array> drop ] times
+ "__name__" getattr py>
+] py-test
+
+{ 3 } [
+ 1234 <alien> <py-cfunction> drop always-destructors get length
+] py-test