diff options
Diffstat (limited to 'weave/examples/increment_example.py')
-rw-r--r-- | weave/examples/increment_example.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/weave/examples/increment_example.py b/weave/examples/increment_example.py new file mode 100644 index 000000000..ded586283 --- /dev/null +++ b/weave/examples/increment_example.py @@ -0,0 +1,37 @@ +# examples/increment_example.py + +#from compiler import ext_tools + +# use the following so that development version is used. +import sys +sys.path.insert(0,'..') +import ext_tools + +def build_increment_ext(): + """ Build a simple extension with functions that increment numbers. + The extension will be built in the local directory. + """ + mod = ext_tools.ext_module('increment_ext') + + a = 1 # effectively a type declaration for 'a' in the + # following functions. + + ext_code = "return_val = Py::new_reference_to(Py::Int(a+1));" + func = ext_tools.ext_function('increment',ext_code,['a']) + mod.add_function(func) + + ext_code = "return_val = Py::new_reference_to(Py::Int(a+2));" + func = ext_tools.ext_function('increment_by_2',ext_code,['a']) + mod.add_function(func) + + mod.compile() + +if __name__ == "__main__": + try: + import increment_ext + except ImportError: + build_increment_ext() + import increment_ext + a = 1 + print 'a, a+1:', a, increment_ext.increment(a) + print 'a, a+2:', a, increment_ext.increment_by_2(a) |