15.12.2012 Views

scipy tutorial - Baustatik-Info-Server

scipy tutorial - Baustatik-Info-Server

scipy tutorial - Baustatik-Info-Server

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

SciPy Reference Guide, Release 0.8.dev<br />

PILED environment variable (discussed later). The cpp files are generally about 200-400 lines long and include quite<br />

a few functions to support type conversions, etc. However, the actual compiled function is pretty simple. Below is the<br />

familiar printf example:<br />

>>> import weave<br />

>>> a = 1<br />

>>> weave.inline(’printf("%d\\n",a);’,[’a’])<br />

1<br />

And here is the extension function generated by inline:<br />

static PyObject* compiled_func(PyObject*self, PyObject* args)<br />

{<br />

py::object return_val;<br />

int exception_occured = 0;<br />

PyObject *py__locals = NULL;<br />

PyObject *py__globals = NULL;<br />

PyObject *py_a;<br />

py_a = NULL;<br />

}<br />

if(!PyArg_ParseTuple(args,"OO:compiled_func",&py__locals,&py__globals))<br />

return NULL;<br />

try<br />

{<br />

PyObject* raw_locals = py_to_raw_dict(py__locals,"_locals");<br />

PyObject* raw_globals = py_to_raw_dict(py__globals,"_globals");<br />

/* argument conversion code */<br />

py_a = get_variable("a",raw_locals,raw_globals);<br />

int a = convert_to_int(py_a,"a");<br />

/* inline code */<br />

/* NDARRAY API VERSION 90907 */<br />

printf("%d\n",a); /*I would like to fill in changed locals and globals here...*/<br />

}<br />

catch(...)<br />

{<br />

return_val = py::object();<br />

exception_occured = 1;<br />

}<br />

/* cleanup code */<br />

if(!(PyObject*)return_val && !exception_occured)<br />

{<br />

return_val = Py_None;<br />

}<br />

return return_val.disown();<br />

Every inline function takes exactly two arguments – the local and global dictionaries for the current scope. All variable<br />

values are looked up out of these dictionaries. The lookups, along with all inline code execution, are done within<br />

a C++ try block. If the variables aren’t found, or there is an error converting a Python variable to the appropriate<br />

type in C++, an exception is raised. The C++ exception is automatically converted to a Python exception by SCXX<br />

and returned to Python. The py_to_int() function illustrates how the conversions and exception handling works.<br />

py_to_int first checks that the given PyObject* pointer is not NULL and is a Python integer. If all is well, it calls the<br />

Python API to convert the value to an int. Otherwise, it calls handle_bad_type() which gathers information<br />

about what went wrong and then raises a SCXX TypeError which returns to Python as a TypeError.<br />

int py_to_int(PyObject* py_obj,char* name)<br />

{<br />

1.12. Weave 103

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!