]> gitweb.factorcode.org Git - factor.git/commitdiff
python.objects: fix <py-cfunction> need to malloc-struct PyMethodDef
authorBjörn Lindqvist <bjourne@gmail.com>
Mon, 27 Oct 2014 22:12:47 +0000 (23:12 +0100)
committerDoug Coleman <doug.coleman@gmail.com>
Tue, 28 Oct 2014 02:38:27 +0000 (19:38 -0700)
extra/python/ffi/ffi.factor
extra/python/objects/objects.factor
extra/python/python-tests.factor

index aa9283474f68bf36c074796a110e492db369f9ed..c1b3b50f1db424a96b68e7a7cc4d9aa5053f098a 100644 (file)
@@ -28,7 +28,7 @@ CONSTANT: METH_COEXIST  0x0040
 C-TYPE: PyCFunction
 
 STRUCT: PyMethodDef
-    { ml_name void* }
+    { ml_name c-string }
     { ml_meth PyCFunction* }
     { ml_flags int }
     { ml_doc c-string } ;
index d843fae3d32f2fa10482e613aa4cbd3d7fc5228c..e1c733f8870078b0a7c4f5e7e61a33d3724ed572 100644 (file)
@@ -1,5 +1,5 @@
-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
@@ -68,8 +68,15 @@ IN: python.objects
     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 ;
index 8e8e9f8634bc52fd65eff0502f9ed0a6089341e5..ef2c8a52d5e17d7696c4d5a0d254bcea49e08762 100644 (file)
@@ -1,6 +1,6 @@
 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 -- )
@@ -152,3 +152,19 @@ IN: python
 [ 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