diff --git a/extra/python/ffi/ffi.factor b/extra/python/ffi/ffi.factor index aa9283474f..c1b3b50f1d 100644 --- a/extra/python/ffi/ffi.factor +++ b/extra/python/ffi/ffi.factor @@ -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 } ; diff --git a/extra/python/objects/objects.factor b/extra/python/objects/objects.factor index d843fae3d3..e1c733f887 100644 --- a/extra/python/objects/objects.factor +++ b/extra/python/objects/objects.factor @@ -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 +: ( 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 ; + : ( alien -- cfunction ) - f swap METH_VARARGS f PyMethodDef f f + "cfunction" f 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 ; diff --git a/extra/python/python-tests.factor b/extra/python/python-tests.factor index 8e8e9f8634..ef2c8a52d5 100644 --- a/extra/python/python-tests.factor +++ b/extra/python/python-tests.factor @@ -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 "__doc__" getattr py> +] py-test + +{ "cfunction" } [ + 1234 + ! Force nursery flush + 10000 [ 1000 0xff drop ] times + "__name__" getattr py> +] py-test + +{ 3 } [ + 1234 drop always-destructors get length +] py-test