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

View File

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