]> gitweb.factorcode.org Git - factor.git/commitdiff
python: cleanup structure handling
authorJohn Benediktsson <mrjbq7@gmail.com>
Wed, 19 Jan 2022 18:26:13 +0000 (10:26 -0800)
committerJohn Benediktsson <mrjbq7@gmail.com>
Wed, 19 Jan 2022 18:26:13 +0000 (10:26 -0800)
extra/python/python-tests.factor
extra/python/python.factor
extra/python/syntax/syntax-tests.factor
extra/python/syntax/syntax.factor

index 50c4760f97bebb1138a823fc823f20dc0dec9414..d92af316c88993913c09629c310d4693c1d46ba0 100644 (file)
@@ -8,13 +8,13 @@ IN: python
 
 ! None testing
 { t } [
-    "builtins" py-import "None" getattr <none> =
+    "builtins" "None" py-import-from <none> =
 ] py-test
 
 ! Pretty sure the # of None references should stay constant.
 : count-none-refs ( -- n )
     [
-        "sys" py-import "getrefcount" getattr
+        "sys" "getrefcount" py-import-from
         <none> <1py-tuple> call-object py>
     ] with-destructors ;
 
@@ -179,10 +179,10 @@ IN: python
 
 ! Callbacks
 : py-map ( -- alien )
-    "builtins" py-import "map" getattr ;
+    "builtins" "map" py-import-from ;
 
 : py-list ( -- alien )
-    "builtins" py-import "list" getattr ;
+    "builtins" "list" py-import-from ;
 
 : py-list-call ( alien-cb -- seq )
     [
index cb54f180611120390436251fde17e6e691c38a1e..68637c3fcb0a8c012fa9f40f60260988577e3311 100644 (file)
@@ -8,8 +8,6 @@ IN: python
 
 ERROR: python-error type message traceback ;
 
-SPECIALIZED-ARRAY: void*
-
 ! Initialization and finalization
 : py-initialize ( -- )
     Py_IsInitialized [ Py_Initialize ] unless ;
@@ -18,35 +16,45 @@ SPECIALIZED-ARRAY: void*
     Py_IsInitialized [ Py_Finalize ] when ;
 
 ! Importing
-: py-import ( str -- module )
+: py-import ( modulename -- module )
     PyImport_ImportModule check-new-ref ;
 
-! Data marshalling to Python
-: array>py-tuple ( arr -- py-tuple )
-    [ length <py-tuple> dup ] keep
-    [ rot py-tuple-set-item ] with each-index ;
-
-: vector>py-list ( vec -- py-list )
-    [ length <py-list> dup ] keep
-    [ rot py-list-set-item ] with each-index ;
+: py-import-from ( modulename objname -- obj )
+    [ py-import ] [ getattr ] bi* ;
 
 DEFER: >py
 DEFER: py>
 
+! Data marshalling to Python
+: array>py-tuple ( array -- py-tuple )
+    [ length <py-tuple> ] keep
+    [ [ dup ] 2dip py-tuple-set-item ] each-index ;
+
+: vector>py-list ( vector -- py-list )
+    [ length <py-list> ] keep
+    [ [ dup ] 2dip py-list-set-item ] each-index ;
+
+: assoc>py-dict ( assoc -- py-dict )
+    <py-dict> swap [ [ dup ] 2dip py-dict-set-item ] assoc-each ;
+
 : py-tuple>array ( py-tuple -- arr )
-    dup py-tuple-size <iota> [ py-tuple-get-item py> ] with map ;
+    dup py-tuple-size <iota> [ py-tuple-get-item ] with map ;
 
 : py-list>vector ( py-list -- vector )
-    dup py-list-size <iota> [ py-list-get-item py> ] with V{ } map-as ;
+    dup py-list-size <iota> [ py-list-get-item ] with V{ } map-as ;
 
-: py-unicode>string ( py-unicode -- str )
+: py-unicode>string ( py-unicode -- string )
     PyUnicode_AsUTF8 (check-ref) ;
 
 : 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 ;
+    PyDict_Items (check-ref) py-list>vector
+    [ py-tuple>array ] map >hashtable ;
+
+: py-class-name ( py-object -- name )
+    "__class__" getattr "__name__" getattr py-unicode>string ;
 
 GENERIC: >py ( obj -- py-obj )
 
@@ -69,9 +77,7 @@ M: array >py
     [ >py ] map array>py-tuple ;
 
 M: hashtable >py
-    <py-dict> swap dupd [
-        swapd [ >py ] bi@ py-dict-set-item
-    ] with assoc-each ;
+    [ [ >py ] bi@ ] assoc-map assoc>py-dict ;
 
 M: vector >py
     [ >py ] map vector>py-list ;
@@ -87,11 +93,11 @@ SYMBOL: py-type-dispatch
         { "NoneType" [ drop f ] }
         { "bool" [ PyObject_IsTrue 1 = ] }
         { "bytes" [ py-bytes>byte-array ] }
-        { "dict" [ py-dict>hashtable ] }
+        { "dict" [ py-dict>hashtable [ [ py> ] bi@ ] assoc-map ] }
         { "int" [ PyLong_AsLong ] }
-        { "list" [ py-list>vector ] }
+        { "list" [ py-list>vector [ py> ] map ] }
         { "str" [ py-unicode>string ] }
-        { "tuple" [ py-tuple>array ] }
+        { "tuple" [ py-tuple>array [ py> ] map ] }
     } clone ;
 
 py-type-dispatch [ init-py-type-dispatch ] initialize
@@ -99,15 +105,12 @@ py-type-dispatch [ init-py-type-dispatch ] initialize
 ERROR: missing-type type ;
 
 : py> ( py-obj -- obj )
-    dup "__class__" getattr "__name__" getattr py-unicode>string
-    py-type-dispatch get ?at [ call( x -- x ) ] [ missing-type ] if ;
+    dup py-class-name py-type-dispatch get ?at
+    [ call( x -- x ) ] [ missing-type ] if ;
 
 ! Callbacks
 : quot>py-callback ( quot: ( args kw -- ret ) -- alien )
-    '[
-        nipd
-        [ [ py> ] [ { } ] if* ] bi@ @ >py
-    ] PyCallback ; inline
+    '[ nipd [ [ py> ] [ { } ] if* ] bi@ @ >py ] PyCallback ; inline
 
 : with-quot>py-cfunction ( alien quot -- )
     '[ <py-cfunction> @ ] with-callback ; inline
index 4f097c2b1ac12820817ce781e138a042685a6c4b..3c5d70378dd66f12cf75329288a46d041e83148a 100644 (file)
@@ -19,7 +19,7 @@ IN: python.syntax.tests
 [ "hello.doc" ] [ "/some/path/hello.doc" >py basename py> ] py-test
 
 [ { "hello" ".doc" } ] [
-    "hello.doc" >py splitext 2array
+    "hello.doc" >py splitext [ py> ] bi@ 2array
 ] py-test
 
 [ ] [ 0 >py sleep ] py-test
@@ -78,7 +78,7 @@ IN: python.syntax.tests
 ] py-test
 
 [ { "hello" "=" "there" } ] [
-    "hello=there" >py "=" >py partition 3array
+    "hello=there" >py "=" >py partition [ py> ] tri@ 3array
 ] py-test
 
 ! Introspection
@@ -106,12 +106,12 @@ PY-METHODS: code =>
     [
         ArgumentParser dup
         "--foo" >py H{ { "help" "badger" } } >py add_argument
-        format_help
+        format_help py>
     ] with-destructors [ blank? ] s:trim split-words "badger" swap in?
 ] py-test
 
 { t } [
-    [ 987 >py basename ] [ traceback>> ] recover s:length 0 >
+    [ 987 >py basename ] [ ] recover python-error?
 ] py-test
 
 ! Test if exceptions leak references. If so, the test will leak a few
@@ -152,6 +152,6 @@ PY-QUALIFIED-FROM: functools =>
 
 { 48 } [
     reduce-func [
-        { 1 2 8 1 2 34 } >py functools:reduce builtins:list py>
+        { 1 2 8 1 2 34 } >py functools:reduce py>
     ] with-quot>py-cfunction
 ] py-test
index fecc028a3f55c673f50d002030a225e0cd3e3d64..d9490eac6a7090a90a6290251e6faed64d244f92 100644 (file)
@@ -37,12 +37,9 @@ SYMBOL: current-context
     [ [ ":" glue ] [ ":$" glue ] 2bi ] [ nip dup "$" prepend ] if
     [ create-word-in ] bi@ ;
 
-: import-getattr ( module name -- alien )
-    [ py-import ] dip getattr ;
-
 :: add-function ( name effect module prefix? -- )
     module name prefix? make-factor-words :> ( call-word obj-word )
-    obj-word module name '[ _ _ import-getattr ] ( -- o ) define-inline
+    obj-word module name '[ _ _ py-import-from ] ( -- o ) define-inline
     call-word obj-word def>> effect make-function-quot effect define-inline ;
 
 : make-method-quot ( name effect -- quot )