diff --git a/extra/python/objects/objects.factor b/extra/python/objects/objects.factor index e605975d0d..d843fae3d3 100644 --- a/extra/python/objects/objects.factor +++ b/extra/python/objects/objects.factor @@ -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) 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 : ( alien -- cfunction ) diff --git a/extra/python/python.factor b/extra/python/python.factor index 18f43ad55d..70a4bc425b 100644 --- a/extra/python/python.factor +++ b/extra/python/python.factor @@ -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 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 ; ! Data marshalling to Factor SYMBOL: py-type-dispatch