]> gitweb.factorcode.org Git - factor.git/commitdiff
python: some cleanup.
authorJohn Benediktsson <mrjbq7@gmail.com>
Tue, 18 Jan 2022 22:48:04 +0000 (14:48 -0800)
committerJohn Benediktsson <mrjbq7@gmail.com>
Tue, 18 Jan 2022 22:48:04 +0000 (14:48 -0800)
extra/python/python-tests.factor
extra/python/python.factor

index 94b4ce1b81520fb54ca68c71750c502f73868a1d..50c4760f97bebb1138a823fc823f20dc0dec9414 100644 (file)
@@ -186,7 +186,7 @@ IN: python
 
 : py-list-call ( alien-cb -- seq )
     [
-        py-list swap { 1 } >py 2array array>py-tuple f
+        py-list swap 1array array>py-tuple f
         call-object-full
     ] with-callback py> ;
 
@@ -194,7 +194,7 @@ IN: python
     [
         <py-cfunction> py-map swap { 1 2 } >py 2array array>py-tuple f
         call-object-full
-    ] with-callback py-list-call py> ;
+    ] with-callback py-list-call ;
 
 : always-33-fun ( -- alien )
     [ 3drop 33 >py ] PyCallback ;
index 2c49863e575f8bc6a393d2689d2abf17c7468c0a..7445c6c931e5d39704fbec1c9e9701104ccf3f2c 100644 (file)
@@ -30,13 +30,23 @@ SPECIALIZED-ARRAY: void*
     [ length <py-list> dup ] keep
     [ rot py-list-set-item ] with each-index ;
 
+DEFER: >py
+DEFER: py>
+
 : py-tuple>array ( py-tuple -- arr )
-    dup py-tuple-size <iota> [ py-tuple-get-item ] with map ;
+    dup py-tuple-size <iota> [ py-tuple-get-item py> ] with map ;
 
 : py-list>vector ( py-list -- vector )
-    dup py-list-size <iota> [ py-list-get-item ] with V{ } map-as ;
+    dup py-list-size <iota> [ py-list-get-item py> ] with V{ } map-as ;
 
-DEFER: >py
+: py-unicode>string ( py-unicode -- str )
+    PyUnicode_AsUTF8 (check-ref) utf8 decode ;
+
+: py-bytes>byte-array ( py-bytes -- byte-array )
+    PyBytes_AsString (check-ref) >byte-array ;
+
+: py-dict>hashtable ( py-dict -- hashtable )
+    PyDict_Items (check-ref) py> >hashtable ;
 
 GENERIC: >py ( obj -- py-obj )
 
@@ -72,18 +82,16 @@ M: f >py
 ! Data marshalling to Factor
 SYMBOL: py-type-dispatch
 
-DEFER: py>
-
 : init-py-type-dispatch ( -- table )
     H{
         { "NoneType" [ drop f ] }
         { "bool" [ PyObject_IsTrue 1 = ] }
-        { "bytes" [ PyBytes_AsString (check-ref) ] }
-        { "dict" [ PyDict_Items (check-ref) py> >hashtable ] }
+        { "bytes" [ py-bytes>byte-array ] }
+        { "dict" [ py-dict>hashtable ] }
         { "int" [ PyLong_AsLong ] }
-        { "list" [ py-list>vector [ py> ] map ] }
-        { "str" [ PyUnicode_AsUTF8 (check-ref) utf8 decode ] }
-        { "tuple" [ py-tuple>array [ py> ] map ] }
+        { "list" [ py-list>vector ] }
+        { "str" [ py-unicode>string ] }
+        { "tuple" [ py-tuple>array ] }
     } clone ;
 
 py-type-dispatch [ init-py-type-dispatch ] initialize
@@ -91,7 +99,7 @@ py-type-dispatch [ init-py-type-dispatch ] initialize
 ERROR: missing-type type ;
 
 : py> ( py-obj -- obj )
-    dup "__class__" getattr "__name__" getattr PyUnicode_AsUTF8
+    dup "__class__" getattr "__name__" getattr py-unicode>string
     py-type-dispatch get ?at [ call( x -- x ) ] [ missing-type ] if ;
 
 ! Callbacks