From dd3382aa8fa8d4b6d1de46621bbec189dd7e8965 Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Sat, 3 Nov 2012 12:44:38 -0700 Subject: work on python block class --- python/gras/GRAS_Block.i | 70 +++++++++++++++++++++++++++++++++++++++++++---- python/gras/__init__.py | 2 +- python/gras/__init__.pyc | Bin 352 -> 0 bytes 3 files changed, 66 insertions(+), 6 deletions(-) delete mode 100644 python/gras/__init__.pyc (limited to 'python') diff --git a/python/gras/GRAS_Block.i b/python/gras/GRAS_Block.i index b6d8fa9..ccd3963 100644 --- a/python/gras/GRAS_Block.i +++ b/python/gras/GRAS_Block.i @@ -18,7 +18,7 @@ %module(directors="1") GRAS_Block -%feature("director"); +%feature("director") BlockPython; %{ #include @@ -31,6 +31,63 @@ %include %include + +//////////////////////////////////////////////////////////////////////// +// Make a special block with safe overloads +//////////////////////////////////////////////////////////////////////// + +%inline %{ + +namespace gras +{ + +struct BlockPython : Block +{ + BlockPython(const std::string &name): + Block(name) + { + //NOP + } + + int work + ( + const InputItems &input_items, + const OutputItems &output_items + ) + { + PyGILState_STATE s; + s = PyGILState_Ensure(); + int ret = 0; + try + { + ret = this->python_work(input_items, output_items); + } + catch(...) + { + PyGILState_Release(s); + throw; + } + PyGILState_Release(s); + return ret; + } + + virtual int python_work + ( + const InputItems &input_items, + const OutputItems &output_items + ) + { + throw std::runtime_error("Error in BlockPython::python_work: SWIG directors issue?"); + } +}; + +} + +%} + +//////////////////////////////////////////////////////////////////////// +// Python overload for adding pythonic interfaces +//////////////////////////////////////////////////////////////////////// %pythoncode %{ import numpy @@ -39,21 +96,24 @@ def sig_to_dtype_sig(sig): if sig is None: sig = () return map(numpy.dtype, sig) -class BlockPython(Block): +class Block(BlockPython): def __init__(self, name='Block', in_sig=None, out_sig=None): - Block.__init__(self, name) + BlockPython.__init__(self, name) self.set_input_signature(in_sig) self.set_output_signature(in_sig) def set_input_signature(self, sig): self.__in_sig = sig_to_dtype_sig(sig) - Block.set_input_signature(self, IOSignature([s.itemsize for s in self.__in_sig])) + BlockPython.set_input_signature(self, IOSignature([s.itemsize for s in self.__in_sig])) def set_output_signature(self, sig): self.__out_sig = sig_to_dtype_sig(sig) - Block.set_output_signature(self, IOSignature([s.itemsize for s in self.__out_sig])) + BlockPython.set_output_signature(self, IOSignature([s.itemsize for s in self.__out_sig])) def input_signature(self): return self.__in_sig def output_signature(self): return self.__out_sig + def python_work(self, input_items, output_items): + print 'python work called' + %} diff --git a/python/gras/__init__.py b/python/gras/__init__.py index 869a361..4d74879 100644 --- a/python/gras/__init__.py +++ b/python/gras/__init__.py @@ -1,3 +1,3 @@ -from GRAS_Block import BlockPython as Block +from GRAS_Block import Block from GRAS_HierBlock import HierBlock, TopBlock from GRAS_ThreadPool import ThreadPoolConfig, ThreadPool diff --git a/python/gras/__init__.pyc b/python/gras/__init__.pyc deleted file mode 100644 index a639e14..0000000 Binary files a/python/gras/__init__.pyc and /dev/null differ -- cgit