]> gitweb.factorcode.org Git - factor.git/commitdiff
python: start updating to python3 API
authorJohn Benediktsson <mrjbq7@gmail.com>
Tue, 18 Jan 2022 18:44:14 +0000 (10:44 -0800)
committerJohn Benediktsson <mrjbq7@gmail.com>
Tue, 18 Jan 2022 18:44:14 +0000 (10:44 -0800)
extra/python/ffi/ffi.factor
extra/python/python.factor

index bbab02e2c7a4766fd04e97793ccfa42516e359ea..010da84fd42ea1d71265e7e3417a8d601e395d0b 100644 (file)
@@ -3,9 +3,8 @@ alien.libraries.finder alien.syntax assocs classes.struct kernel sequences
 system ;
 IN: python.ffi
 
-! << "python" { "3.0" "3" "2.7" "2.6" } ! Python 3 has a different api, enable someday
 << "python"
-{ "python2.7" "python2.6" "python27" "python26" } find-library-from-list
+{ "python3.10" "python3.9" "python3.8" "python3.7" } find-library-from-list
 cdecl add-library >>
 
 ! Functions that return borrowed references needs to be called like this:
@@ -27,6 +26,8 @@ CONSTANT: METH_COEXIST  0x0040
 
 C-TYPE: PyCFunction
 
+TYPEDEF: long Py_ssize_t
+
 STRUCT: PyMethodDef
     { ml_name c-string }
     { ml_meth PyCFunction* }
@@ -115,25 +116,18 @@ FUNCTION: int PyObject_SetAttrString ( PyObject* o,
 FUNCTION: PyObject* PyObject_Str ( PyObject* o )
 FUNCTION: int PyObject_IsTrue ( PyObject* o )
 
+! Bytes
+FUNCTION: c-string PyBytes_AsString ( PyObject* string )
+FUNCTION: PyObject* PyBytes_FromStringAndSize ( c-string v, Py_ssize_t size  )
+
 ! Strings
-FUNCTION: c-string PyString_AsString ( PyObject* string )
-FUNCTION: PyObject* PyString_FromString ( c-string v )
-
-! Unicode
-FUNCTION: PyObject* PyUnicode_DecodeUTF8 ( c-string s,
-                                           int size,
-                                           void* errors )
-FUNCTION: PyObject* PyUnicodeUCS4_FromString ( c-string s )
-FUNCTION: PyObject* PyUnicodeUCS2_FromString ( c-string s )
-FUNCTION: PyObject* PyUnicodeUCS2_AsUTF8String ( PyObject* unicode )
-FUNCTION: PyObject* PyUnicodeUCS4_AsUTF8String ( PyObject* unicode )
+FUNCTION: c-string PyUnicode_AsUTF8 ( PyObject* unicode )
+FUNCTION: PyObject* PyUnicode_FromStringAndSize ( c-string v, Py_ssize_t size  )
 
 ! Ints
-FUNCTION: long PyInt_AsLong ( PyObject* io )
-
-! Longs
+FUNCTION: long PyLong_AsLong ( PyObject* io )
 FUNCTION: PyObject* PyLong_FromLong ( long v )
-FUNCTION: long PyLong_AsLong ( PyObject* o )
+FUNCTION: PyObject* PyLong_FromString ( c-string str, char** pend, int base )
 
 ! Floats
 FUNCTION: PyObject* PyFloat_FromDouble ( double d )
index 73950c89331925cf5b51c52bb265ae5427af8b15..611a8283ac29b4977b897d942e69a93932173652 100644 (file)
@@ -1,10 +1,10 @@
-USING: alien alien.c-types alien.data alien.libraries
-arrays assocs command-line fry
-hashtables init io.encodings.utf8 kernel namespaces
-python.errors python.ffi python.objects sequences
+USING: alien alien.c-types alien.data alien.libraries arrays
+assocs byte-arrays command-line hashtables init
+io.encodings.string io.encodings.utf8 kernel math math.parser
+namespaces python.errors python.ffi python.objects sequences
 specialized-arrays strings vectors ;
+
 IN: python
-QUALIFIED: math
 
 ERROR: python-error type message traceback ;
 
@@ -30,21 +30,6 @@ SPECIALIZED-ARRAY: void*
 : py-import ( str -- module )
     PyImport_ImportModule check-new-ref ;
 
-! Unicodes
-: py-ucs-size ( -- n )
-    "maxunicode" PySys_GetObject PyInt_AsLong 0xffff = 2 4 ? ;
-
-: py-unicode>utf8 ( uni -- str )
-    py-ucs-size 4 =
-    [ PyUnicodeUCS4_AsUTF8String ]
-    [ PyUnicodeUCS2_AsUTF8String ] if (check-ref)
-    PyString_AsString (check-ref) ;
-
-: utf8>py-unicode ( str -- uni )
-    py-ucs-size 4 =
-    [ PyUnicodeUCS4_FromString ]
-    [ PyUnicodeUCS2_FromString ] if ;
-
 ! Data marshalling to Python
 : array>py-tuple ( arr -- py-tuple )
     [ length <py-tuple> dup ] keep
@@ -63,20 +48,33 @@ SPECIALIZED-ARRAY: void*
 DEFER: >py
 
 GENERIC: >py ( obj -- py-obj )
+
+M: byte-array >py
+    dup length PyBytes_FromStringAndSize check-new-ref ;
+
 M: string >py
-    utf8>py-unicode check-new-ref ;
+    utf8 encode dup length PyUnicode_FromStringAndSize check-new-ref ;
+
 M: math:fixnum >py
     PyLong_FromLong check-new-ref ;
+
+M: math:bignum >py
+    number>string f 10 PyLong_FromString check-new-ref ;
+
 M: math:float >py
     PyFloat_FromDouble check-new-ref ;
+
 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 ;
+
 M: vector >py
     [ >py ] map vector>py-list ;
+
 M: f >py
     drop <none> ;
 
@@ -89,13 +87,12 @@ DEFER: py>
     H{
         { "NoneType" [ drop f ] }
         { "bool" [ PyObject_IsTrue 1 = ] }
+        { "bytes" [ PyBytes_AsString (check-ref) ] }
         { "dict" [ PyDict_Items (check-ref) py> >hashtable ] }
-        { "int" [ PyInt_AsLong ] }
+        { "int" [ PyLong_AsLong ] }
         { "list" [ py-list>vector [ py> ] map ] }
-        { "long" [ PyLong_AsLong ] }
-        { "str" [ PyString_AsString (check-ref) ] }
+        { "str" [ PyUnicode_AsUTF8 (check-ref) utf8 decode ] }
         { "tuple" [ py-tuple>array [ py> ] map ] }
-        { "unicode" [ py-unicode>utf8 ] }
     } clone ;
 
 py-type-dispatch [ init-py-type-dispatch ] initialize
@@ -103,7 +100,7 @@ py-type-dispatch [ init-py-type-dispatch ] initialize
 ERROR: missing-type type ;
 
 : py> ( py-obj -- obj )
-    dup "__class__" getattr "__name__" getattr PyString_AsString
+    dup "__class__" getattr "__name__" getattr PyUnicode_AsUTF8
     py-type-dispatch get ?at [ call( x -- x ) ] [ missing-type ] if ;
 
 ! Callbacks