From 0651aff49ae7e066bc301faeecfbd519c4743849 Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Wed, 3 Apr 2013 23:09:49 -0700 Subject: gras: figured out how to do the python reference --- python/gras/GRAS_Block.i | 51 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 5 deletions(-) (limited to 'python/gras') diff --git a/python/gras/GRAS_Block.i b/python/gras/GRAS_Block.i index 82c5d78..e18476a 100644 --- a/python/gras/GRAS_Block.i +++ b/python/gras/GRAS_Block.i @@ -56,15 +56,53 @@ %{ #include #include +#include %} %include %include +%include "GRAS_Utils.i" + +//////////////////////////////////////////////////////////////////////// +// Create a reference holder for python objects +//////////////////////////////////////////////////////////////////////// +%inline %{ + +struct PyObjectRefHolder +{ + PyObjectRefHolder(PyObject *o): + o(o) + { + Py_INCREF(o); + } + ~PyObjectRefHolder(void) + { + PyGILPhondler phil; + Py_DECREF(o); + } + PyObject *o; +}; + +struct WeakElementPyObject : gras::WeakElement +{ + WeakElementPyObject(PyObject *o): + o(o) + { + //NOP + } + boost::shared_ptr lock(void) + { + return boost::make_shared(o); + } + PyObject *o; +}; + +%} + //////////////////////////////////////////////////////////////////////// // Make a special block with safe overloads //////////////////////////////////////////////////////////////////////// -%include "GRAS_Utils.i" %inline %{ namespace gras @@ -84,6 +122,12 @@ struct BlockPython : Block this->reset(); } + void _Py_set_ref(PyObject *o) + { + std::cout << o->ob_refcnt <weak_self.reset(new WeakElementPyObject(o)); + } + void notify_active(void) { PyGILPhondler phil; @@ -195,16 +239,13 @@ def sig_to_dtype_sig(sig): if sig is None: sig = () return map(numpy.dtype, sig) -#FIXME major kludge for ref holding -blocks_ref_container = list() - class Block(BlockPython): def __init__(self, name='Block', in_sig=None, out_sig=None): BlockPython.__init__(self, name) self.set_input_signature(in_sig) self.set_output_signature(out_sig) - blocks_ref_container.append(self) self.__prop_registry = dict(); + self._Py_set_ref(self) def set_input_signature(self, sig): self.__in_sig = sig_to_dtype_sig(sig) -- cgit