summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorJosh Blum2012-11-03 12:44:38 -0700
committerJosh Blum2012-11-03 12:44:38 -0700
commitdd3382aa8fa8d4b6d1de46621bbec189dd7e8965 (patch)
treee09edb42891a30d6c7f46127a1db1cfd01380118 /python
parentebfbb31868f4d6e0ff31ee97943cb446688809b7 (diff)
downloadsandhi-dd3382aa8fa8d4b6d1de46621bbec189dd7e8965.tar.gz
sandhi-dd3382aa8fa8d4b6d1de46621bbec189dd7e8965.tar.bz2
sandhi-dd3382aa8fa8d4b6d1de46621bbec189dd7e8965.zip
work on python block class
Diffstat (limited to 'python')
-rw-r--r--python/gras/GRAS_Block.i70
-rw-r--r--python/gras/__init__.py2
-rw-r--r--python/gras/__init__.pycbin352 -> 0 bytes
3 files changed, 66 insertions, 6 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
deleted file mode 100644
index a639e14..0000000
--- a/python/gras/__init__.pyc
+++ /dev/null
Binary files differ