diff --git a/extra/python/ffi/ffi.factor b/extra/python/ffi/ffi.factor index dc04558c00..32f7034900 100644 --- a/extra/python/ffi/ffi.factor +++ b/extra/python/ffi/ffi.factor @@ -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 diff --git a/extra/python/python-tests.factor b/extra/python/python-tests.factor index d2cb6927dd..5d2c1a0761 100644 --- a/extra/python/python-tests.factor +++ b/extra/python/python-tests.factor @@ -32,6 +32,9 @@ py-initialize { "year" "month" "day" } [ getattr >factor ] with map first3 0 0 0 instant ; +! 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 diff --git a/extra/python/python.factor b/extra/python/python.factor index e7e819e1ad..379e1952db 100644 --- a/extra/python/python.factor +++ b/extra/python/python.factor @@ -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 +: ( 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 dup ] keep [ rot py-tuple-set-item ] with each-index ; +: vector>py-list ( vec -- py-list ) + [ length 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) 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) ] } diff --git a/extra/python/syntax/syntax-tests.factor b/extra/python/syntax/syntax-tests.factor index f37f405f64..71ce136050 100644 --- a/extra/python/syntax/syntax-tests.factor +++ b/extra/python/syntax/syntax-tests.factor @@ -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