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 >>
! 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
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
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 ? ;
[ 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 ;
<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) ;
{ "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) ] }
] py-test
PY-METHODS: str =>
+ lower ( self -- self' )
partition ( self sep -- bef sep aft )
startswith ( self str -- ? )
title ( self -- self' )
$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