-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 ;
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 ;
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 )
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