]> gitweb.factorcode.org Git - factor.git/commitdiff
python: setup serialization between vectors and python lists
authorBjörn Lindqvist <bjourne@gmail.com>
Thu, 30 Jan 2014 16:24:58 +0000 (17:24 +0100)
committerJohn Benediktsson <mrjbq7@gmail.com>
Tue, 4 Mar 2014 17:23:04 +0000 (09:23 -0800)
extra/python/ffi/ffi.factor
extra/python/python-tests.factor
extra/python/python.factor
extra/python/syntax/syntax-tests.factor

index dc04558c00ba44b85a0bf79aef4fd58a107a203f..32f7034900a338694da036f91e5ada4656831d3f 100644 (file)
@@ -3,8 +3,8 @@ alien.syntax assocs kernel sequences system ;
 IN: python.ffi
 
 << "python" {
-    { linux { "3.0" "2.6" "2.7" } }
-    { windows { "26" "27" "30" } }
+    { linux { "3.0" "2.7" "2.6" } }
+    { windows { "30" "27" "26" } }
 } os of [
     "python" prepend find-library
 ] map-find drop cdecl add-library >>
@@ -56,7 +56,11 @@ FUNCTION: int PyTuple_Size ( PyObject* t ) ;
 ! Lists
 ! Borrowed reference
 FUNCTION: PyObject* PyList_GetItem ( PyObject* l, int pos ) ;
-FUNCTION: int PyList_Size ( PyObject* t ) ;
+! New reference
+FUNCTION: PyObject* PyList_New ( int len ) ;
+FUNCTION: int PyList_Size ( PyObject* l ) ;
+! Steals the reference
+FUNCTION: int PyList_SetItem ( PyObject* l, int pos, PyObject* o ) ;
 
 
 ! Modules
index d2cb6927dd64efe78c06bcaf06d985c16188a014..5d2c1a07618e6a19c4b94501d330cff336fd7bf6 100644 (file)
@@ -32,6 +32,9 @@ py-initialize
     { "year" "month" "day" } [ getattr >factor ] with map
     first3 0 0 0 instant <timestamp> ;
 
+! Lists
+[ t ] [ V{ 4 8 15 16 23 42 } dup >py >factor = ] py-test
+
 ! ! Datetimes
 [ t ] [
     [ py-date>factor ] "date" py-type-dispatch get set-at
index e7e819e1ad8a9de6b153bfa16b1bd55cb1a7ea18..379e1952db459065592961510fbcc4742067ccd8 100644 (file)
@@ -1,5 +1,5 @@
 USING: accessors alien alien.c-types alien.data arrays assocs fry grouping
-hashtables kernel namespaces python.ffi sequences strings words ;
+hashtables kernel namespaces python.ffi sequences strings vectors words ;
 IN: python
 QUALIFIED: math
 
@@ -76,12 +76,18 @@ ERROR: python-error type message ;
     PyDict_Size ;
 
 ! Lists
+: <py-list> ( length -- list )
+    PyList_New check-return ;
+
 : py-list-size ( list -- len )
     PyList_Size ;
 
 : py-list-get-item ( obj pos -- val )
     PyList_GetItem dup Py_IncRef check-return ;
 
+: py-list-set-item ( obj pos val -- )
+    dup Py_IncRef PyList_SetItem check-return-code ;
+
 ! Unicodes
 : py-ucs-size ( -- n )
     "maxunicode" PySys_GetObject PyInt_AsLong 0xffff = 2 4 ? ;
@@ -102,6 +108,10 @@ ERROR: python-error type message ;
     [ 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-tuple>array ( py-tuple -- arr )
     dup py-tuple-size iota [ py-tuple-get-item ] with map ;
 
@@ -114,6 +124,8 @@ M: hashtable (>py)
     <py-dict> swap dupd [
         swapd [ (>py) ] [ (>py) ] bi* py-dict-set-item
     ] with assoc-each ;
+M: vector (>py)
+    [ (>py) ] map vector>py-list ;
 
 M: word (>py) name>> (>py) ;
 
@@ -132,7 +144,7 @@ DEFER: >factor
         { "dict" [ PyDict_Items (check-return) >factor >hashtable ] }
         { "int" [ PyInt_AsLong ] }
         { "list" [
-            dup py-list-size iota [ py-list-get-item >factor ] with map
+            dup py-list-size iota [ py-list-get-item >factor ] with V{ } map-as
         ] }
         { "long" [ PyLong_AsLong ] }
         { "str" [ PyString_AsString (check-return) ] }
index f37f405f648c71fff49ff5a84581f25f0ddbccd5..71ce136050e7bd0b2e4ea13af85fbf9127302692 100644 (file)
@@ -85,6 +85,7 @@ PY-METHODS: file =>
 ] py-test
 
 PY-METHODS: str =>
+    lower ( self -- self' )
     partition ( self sep -- bef sep aft )
     startswith ( self str -- ? )
     title ( self -- self' )
@@ -117,3 +118,10 @@ PY-METHODS: list =>
     $path "test" >py [ append ] [ drop >factor ] [ remove ] 2tri
     "test" swap in?
 ] py-test
+
+! setattr doesn't affect which objects $words are referencing.
+PY-FROM: sys => platform ( -- x ) ;
+
+[ t ] [
+    $platform "sys" import "platform" "tjaba" >py setattr $platform =
+] py-test