]> gitweb.factorcode.org Git - factor.git/commitdiff
python: correction of reference counting logic and addition of None bindings
authorBjörn Lindqvist <bjourne@gmail.com>
Sun, 26 Oct 2014 23:23:54 +0000 (00:23 +0100)
committerDoug Coleman <doug.coleman@gmail.com>
Mon, 27 Oct 2014 16:52:04 +0000 (09:52 -0700)
extra/python/objects/objects.factor
extra/python/python.factor

index e605975d0d17fd3df8d5974410280853ba13280f..d843fae3d32f2fa10482e613aa4cbd3d7fc5228c 100644 (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 )
index 18f43ad55dd7b9fe62139603715a6046715e28e8..70a4bc425bab0b0e4fe0727fb1ed291b573c6bbd 100644 (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