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.

}<br />

return Py::List(py_obj);<br />

static Py::String py_to_string(PyObject* py_obj,char* name)<br />

{<br />

if (!PyString_Check(py_obj))<br />

handle_bad_type(py_obj,"string", name);<br />

return Py::String(py_obj);<br />

}<br />

static Py::Dict py_to_dict(PyObject* py_obj,char* name)<br />

{<br />

if (!py_obj || !PyDict_Check(py_obj))<br />

handle_bad_type(py_obj,"dict", name);<br />

return Py::Dict(py_obj);<br />

}<br />

static Py::Tuple py_to_tuple(PyObject* py_obj,char* name)<br />

{<br />

if (!py_obj || !PyTuple_Check(py_obj))<br />

handle_bad_type(py_obj,"tuple", name);<br />

return Py::Tuple(py_obj);<br />

}<br />

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

SCXX handles reference counts on for strings, lists, tuples, and dictionaries, so clean up code isn’t necessary.<br />

File Conversion<br />

For the following code,<br />

>>> a = open("bob",’w’)<br />

>>> inline("",[’a’])<br />

The argument conversion code is:<br />

/* argument conversion code */<br />

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

FILE* a = py_to_file(py_a,"a");<br />

get_variable() reads the variable a from the local and global namespaces. py_to_file() converts PyObject*<br />

to a FILE* and increments the reference count of the PyObject*:<br />

FILE* py_to_file(PyObject* py_obj, char* name)<br />

{<br />

if (!py_obj || !PyFile_Check(py_obj))<br />

handle_bad_type(py_obj,"file", name);<br />

}<br />

Py_INCREF(py_obj);<br />

return PyFile_AsFile(py_obj);<br />

Because the PyObject* was incremented, the clean up code needs to decrement the counter<br />

/* cleanup code */<br />

Py_XDECREF(py_a);<br />

1.12. Weave 107

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

Saved successfully!

Ooh no, something went wrong!