]> gitweb.factorcode.org Git - factor.git/commitdiff
python: both METH_VARARGS METH_KEYWORDS needed in ml_flags should fix #1171
authorBjörn Lindqvist <bjourne@gmail.com>
Fri, 31 Oct 2014 17:40:47 +0000 (18:40 +0100)
committerDoug Coleman <doug.coleman@gmail.com>
Fri, 31 Oct 2014 17:48:55 +0000 (10:48 -0700)
extra/python/ffi/ffi.factor
extra/python/objects/objects.factor
extra/python/python-tests.factor

index c1b3b50f1db424a96b68e7a7cc4d9aa5053f098a..271818f8194d34e8acff808f2bffed4c701bf529 100644 (file)
@@ -36,6 +36,7 @@ STRUCT: PyMethodDef
 FUNCTION: PyObject* PyCFunction_NewEx ( PyMethodDef* ml,
                                         PyObject* self,
                                         PyObject* module ) ;
+FUNCTION: int PyCFunction_GetFlags ( PyObject* op ) ;
 
 CALLBACK: PyObject* PyCallback ( PyObject* self,
                                  PyObject* args,
index e1c733f8870078b0a7c4f5e7e61a33d3724ed572..d685bcfb4f7d72928634e21368200158ede68fd4 100644 (file)
@@ -1,5 +1,6 @@
 USING: accessors alien.c-types alien.data alien.libraries classes.struct
-io.encodings.ascii io.encodings.utf8 kernel libc python.errors python.ffi ;
+io.encodings.ascii io.encodings.utf8 kernel libc math python.errors
+python.ffi ;
 IN: python.objects
 
 ! The None object
@@ -73,7 +74,7 @@ IN: python.objects
     swap [ utf8 malloc-string &free >>ml_doc ] when*
     swap ascii malloc-string &free >>ml_name
     swap >>ml_meth
-    METH_VARARGS >>ml_flags ;
+    METH_VARARGS METH_KEYWORDS bitor >>ml_flags ;
 
 : <py-cfunction> ( alien -- cfunction )
     "cfunction" f <PyMethodDef> f f
index ef2c8a52d5e17d7696c4d5a0d254bcea49e08762..d5881c7914846403817797eb02564302b36e4358 100644 (file)
@@ -154,8 +154,14 @@ IN: python
 ] py-test
 
 ! CFunctions
-{ f } [
-    1234 <alien> <py-cfunction> "__doc__" getattr py>
+{ t } [
+    1234 <alien> "foo" f <PyMethodDef>
+    ml_flags>> METH_VARARGS METH_KEYWORDS bitor =
+] unit-test
+
+{ f 3 } [
+    1234 <alien> <py-cfunction>
+    [ "__doc__" getattr py> ] [ PyCFunction_GetFlags ] bi
 ] py-test
 
 { "cfunction" } [
@@ -168,3 +174,23 @@ IN: python
 { 3 } [
     1234 <alien> <py-cfunction> drop always-destructors get length
 ] py-test
+
+! Callbacks
+: py-map ( -- alien )
+    "__builtin__" py-import "map" getattr ;
+
+: py-map-call ( alien-cb -- seq )
+    [
+        <py-cfunction> py-map swap { 1 2 } >py 2array array>py-tuple f
+        call-object-full
+    ] with-callback py> ;
+
+: always-33-fun ( -- alien )
+    [ 3drop 33 >py ] PyCallback ;
+
+{ V{ 33 33 } } [ always-33-fun py-map-call ] py-test
+
+: id-fun ( -- alien )
+    [ drop nip py> first >py ] PyCallback ;
+
+{ V{ 1 2 } } [ id-fun py-map-call ] py-test