diff options
author | Josh Blum | 2012-11-03 12:44:38 -0700 |
---|---|---|
committer | Josh Blum | 2012-11-03 12:44:38 -0700 |
commit | dd3382aa8fa8d4b6d1de46621bbec189dd7e8965 (patch) | |
tree | e09edb42891a30d6c7f46127a1db1cfd01380118 | |
parent | ebfbb31868f4d6e0ff31ee97943cb446688809b7 (diff) | |
download | sandhi-dd3382aa8fa8d4b6d1de46621bbec189dd7e8965.tar.gz sandhi-dd3382aa8fa8d4b6d1de46621bbec189dd7e8965.tar.bz2 sandhi-dd3382aa8fa8d4b6d1de46621bbec189dd7e8965.zip |
work on python block class
-rw-r--r-- | python/gras/GRAS_Block.i | 70 | ||||
-rw-r--r-- | python/gras/__init__.py | 2 | ||||
-rw-r--r-- | python/gras/__init__.pyc | bin | 352 -> 0 bytes | |||
-rw-r--r-- | tests/block_test.py | 16 |
4 files changed, 80 insertions, 8 deletions
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 <gras/block.hpp> @@ -31,6 +31,63 @@ %include <gras/block.hpp> %include <PMC/PMC.i> + +//////////////////////////////////////////////////////////////////////// +// 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 Binary files differdeleted file mode 100644 index a639e14..0000000 --- a/python/gras/__init__.pyc +++ /dev/null diff --git a/tests/block_test.py b/tests/block_test.py index 9b53d1f..020e615 100644 --- a/tests/block_test.py +++ b/tests/block_test.py @@ -6,8 +6,20 @@ import numpy class BlockTest(unittest.TestCase): def test_make_block(self): - b = gras.Block('FooBLock') - b.set_output_signature([numpy.int32]) + null_src = gras.Block('NullSource') + null_src.set_output_signature([numpy.int32]) + + null_sink = gras.Block('NullSink') + null_sink.set_input_signature([numpy.int32]) + + tb = gras.TopBlock() + + print 'connect...' + tb.connect(null_src, null_sink) + print 'ok' + + tb.disconnect(null_src, null_sink) + print 'done' if __name__ == '__main__': unittest.main() |