python: correction of reference counting logic and addition of None bindings

db4
Björn Lindqvist 2014-10-27 00:23:54 +01:00 committed by Doug Coleman
parent 3feec7b14b
commit 87b0205c87
2 changed files with 25 additions and 15 deletions

View File

@ -1,7 +1,14 @@
USING: alien.c-types alien.data classes.struct kernel python.errors
python.ffi ;
USING: alien.c-types alien.data alien.libraries classes.struct kernel
python.errors python.ffi ;
IN: python.objects
! The None object
: (none) ( -- none )
"_Py_NoneStruct" "python" address-of ;
: <none> ( -- none )
(none) check-borrowed-ref ;
! Objects
: getattr ( obj str -- value )
PyObject_GetAttrString check-new-ref ;
@ -20,7 +27,7 @@ IN: python.objects
PyTuple_New check-new-ref ;
: py-tuple-set-item ( obj pos val -- )
unsteal-ref PyTuple_SetItem check-zero ;
dup unsteal-ref PyTuple_SetItem check-zero ;
: py-tuple-get-item ( obj pos -- val )
PyTuple_GetItem dup Py_IncRef check-new-ref ;
@ -58,7 +65,7 @@ IN: python.objects
PyList_GetItem check-borrowed-ref ;
: py-list-set-item ( obj pos val -- )
unsteal-ref PyList_SetItem check-zero ;
dup unsteal-ref PyList_SetItem check-zero ;
! Functions
: <py-cfunction> ( alien -- cfunction )

View File

@ -61,20 +61,23 @@ SPECIALIZED-ARRAY: void*
DEFER: >py
GENERIC: (>py) ( obj -- obj' )
M: string (>py) utf8>py-unicode ;
M: math:fixnum (>py) PyLong_FromLong ;
M: math:float (>py) PyFloat_FromDouble ;
M: array (>py) [ >py ] map array>py-tuple ;
M: hashtable (>py)
GENERIC: >py ( obj -- obj' )
M: string >py
utf8>py-unicode check-new-ref ;
M: math:fixnum >py
PyLong_FromLong 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
swapd [ >py ] bi@ py-dict-set-item
] with assoc-each ;
M: vector (>py)
M: vector >py
[ >py ] map vector>py-list ;
: >py ( obj -- py-obj )
(>py) &Py_DecRef ;
M: f >py
drop <none> ;
! Data marshalling to Factor
SYMBOL: py-type-dispatch