IN: python.ffi
<< "python" {
- { unix { "3.0" "2.6" "2.7" } } { windows { "26" "27" "30" } }
+ { linux { "3.0" "2.6" "2.7" } }
+ { windows { "26" "27" "30" } }
} os of [
"python" prepend find-library
] map-find drop cdecl add-library >>
FUNCTION: PyObject* PyImport_ImportModule ( c-string name ) ;
! Sys module
+! Borrowed reference
FUNCTION: PyObject* PySys_GetObject ( c-string name ) ;
! Dicts
+! Borrowed reference
FUNCTION: PyObject* PyDict_GetItemString ( PyObject* d, c-string key ) ;
FUNCTION: PyObject* PyDict_New ( ) ;
FUNCTION: int PyDict_Size ( PyObject* d ) ;
! Tuples
FUNCTION: PyObject* PyTuple_GetItem ( PyObject* t, int pos ) ;
FUNCTION: PyObject* PyTuple_New ( int len ) ;
+! Steals the reference
FUNCTION: int PyTuple_SetItem ( PyObject* t, int pos, PyObject* o ) ;
FUNCTION: int PyTuple_Size ( PyObject* t ) ;
FUNCTION: c-string PyEval_GetFuncName ( PyObject* func ) ;
! Errors
+FUNCTION: void PyErr_Clear ( ) ;
FUNCTION: void PyErr_Print ( ) ;
FUNCTION: void PyErr_Fetch ( PyObject** ptype,
PyObject** pvalue,
accessors arrays assocs
calendar
continuations
+ destructors
fry kernel
math
namespaces
- python python.ffi
+ python python.ffi python.stdlib.sys
sequences
strings tools.test ;
IN: python.tests
+py-initialize
+
: py-test ( result quot -- )
- '[ _ with-py ] unit-test ; inline
+ '[ _ with-destructors ] unit-test ; inline
[ t ] [ Py_GetVersion string? ] unit-test
-[ "os" ] [ "os" PyImport_ImportModule PyModule_GetName ] py-test
+[ "os" ] [ "os" import PyModule_GetName ] py-test
[ t ] [ "os" import "getpid" getattr { } py-call 0 > ] py-test
{ "year" "month" "day" } [ getattr >factor ] with map
first3 0 0 0 instant <timestamp> ;
-! Datetimes
+! ! Datetimes
[ t ] [
[ py-date>factor ] "date" py-type-dispatch get set-at
"datetime" import
! Modules
[ t ] [ "os" import PyModule_GetDict py-dict-size 200 > ] py-test
+
+! Reference counting tests
+[ 2 ] [ 3 <py-tuple> getrefcount >factor ] py-test
+
+[ -2 ] [
+ H{ { "foo" 33 } { "bar" 44 } } >py
+ [ "foo" py-dict-get-item-string getrefcount >factor ]
+ [
+ '[
+ 500 [ _ "foo" py-dict-get-item-string drop ] times
+ ] with-destructors
+ ]
+ [ "foo" py-dict-get-item-string getrefcount >factor ] tri -
+] py-test
alien alien.c-types alien.data
arrays
assocs
- destructors
fry
grouping
hashtables
kernel
- memoize
namespaces
python.ffi
sequences
IN: python
QUALIFIED: math
+! Initialization and finalization
+: py-initialize ( -- )
+ Py_IsInitialized [ Py_Initialize ] unless ;
+
+: py-finalize ( -- )
+ Py_IsInitialized [ Py_Finalize ] when ;
+
! Error handling
ERROR: python-error type message ;
[ get-error throw-error f ] unless* ;
: check-return ( value/f -- value' )
- (check-return) ; ! &Py_DecRef ;
+ (check-return) &Py_DecRef ;
: check-return-code ( return -- )
0 = [ get-error throw-error ] unless ;
: call-object ( obj args -- value )
PyObject_CallObject check-return ;
-! Context
-: with-py ( quot -- )
- '[ Py_Initialize _ call Py_Finalize ] with-destructors ; inline
-
! Types
: <py-tuple> ( length -- tuple )
PyTuple_New check-return ;
: py-tuple-set-item ( obj pos val -- )
- PyTuple_SetItem check-return-code ;
+ dup Py_IncRef PyTuple_SetItem check-return-code ;
: py-tuple-get-item ( obj pos -- val )
PyTuple_GetItem check-return ;
: py-tuple-size ( obj -- len )
PyTuple_Size ;
+: <1py-tuple> ( alien -- tuple )
+ 1 <py-tuple> [ 0 rot py-tuple-set-item ] keep ;
+
! Dicts
: <py-dict> ( -- dict )
PyDict_New check-return ;
PyDict_SetItemString check-return-code ;
: py-dict-get-item-string ( obj key -- val )
- PyDict_GetItemString check-return ;
+ PyDict_GetItemString dup Py_IncRef check-return ;
: py-dict-size ( obj -- len )
PyDict_Size ;
PyList_GetItem check-return ;
! Unicodes
+: py-ucs-size ( -- n )
+ "maxunicode" PySys_GetObject PyInt_AsLong 0xffff = 2 4 ? ;
+
: py-unicode>utf8 ( uni -- str )
- PyUnicodeUCS2_AsUTF8String (check-return)
- PyString_AsString (check-return)
+ py-ucs-size 4 =
+ [ PyUnicodeUCS4_AsUTF8String ]
+ [ PyUnicodeUCS2_AsUTF8String ] if (check-return)
+ PyString_AsString (check-return) ;
-MEMO: py-ucs-size ( -- n )
- "maxunicode" PySys_GetObject check-return PyInt_AsLong 0xffff = 2 4 ? ;
+: utf8>py-unicode ( str -- uni )
+ py-ucs-size 4 =
+ [ PyUnicodeUCS4_FromString ]
+ [ PyUnicodeUCS2_FromString ] if ;
! Data marshalling to Python
GENERIC: (>py) ( obj -- obj' )
-M: string (>py) PyUnicodeUCS2_FromString ;
+M: string (>py) utf8>py-unicode ;
M: math:fixnum (>py) PyLong_FromLong ;
M: math:float (>py) PyFloat_FromDouble ;
swapd [ (>py) ] [ (>py) ] bi* py-dict-set-item
] with assoc-each ;
-! I'll make a fast-path for this
+! ! I'll make a fast-path for this
M: word (>py) name>> (>py) ;
: >py ( obj -- py-obj )
- (>py) ; ! &Py_DecRef ;
+ (>py) &Py_DecRef ;
! Data marshalling to Factor
SYMBOL: py-type-dispatch
{ "NoneType" [ drop f ] }
{ "dict" [ PyDict_Items (check-return) >factor >hashtable ] }
{ "int" [ PyInt_AsLong ] }
-
{ "list" [
dup py-list-size iota [ py-list-get-item >factor ] with map
] }
{ "tuple" [
dup py-tuple-size iota [ py-tuple-get-item >factor ] with map
] }
- { "unicode" [
- PyUnicodeUCS2_AsUTF8String (check-return)
- PyString_AsString (check-return)
- ] }
+ { "unicode" [ py-unicode>utf8 ] }
} clone ;
py-type-dispatch [ init-py-type-dispatch ] initialize