<!DOCTYPE doctype PUBLIC "-//w3c//dtd html 4.0 transitional//en"> <html><head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <meta name="GENERATOR" content="Mozilla/4.51 (Macintosh; I; PPC) [Netscape]"><title>FAQ.html</title></head> <body> <center> <h1> <hr width="100%">Pyrex FAQ <hr width="100%"></h1> </center> <h2> Contents</h2> <ul> <li> <b><a href="#CallCAPI">How do I call Python/C API routines?</a></b></li> <li> <b><a href="#NullBytes">How do I convert a C string containing null bytes to a Python string?</a></b></li> <li> <b><a href="#NumericAccess">How do I access the data inside a Numeric array object?</a></b></li> <li><b><a href="#Rhubarb">Pyrex says my extension type object has no attribute 'rhubarb', but I know it does. What gives?</a></b></li><li><a style="font-weight: bold;" href="#Quack">Python says my extension type has no method called 'quack', but I know it does. What gives?</a><br> </li> </ul> <hr width="100%"> <h2> <a name="CallCAPI"></a>How do I call Python/C API routines?</h2> Declare them as C functions inside a <tt>cdef extern from</tt> block. Use the type name <tt>object</tt> for any parameters and return types which are Python object references. Don't use the word <tt>const</tt> anywhere. Here is an example which defines and uses the <tt>PyString_FromStringAndSize</tt> routine: <blockquote><tt>cdef extern from "Python.h":</tt> <br> <tt> object PyString_FromStringAndSize(char *, int)</tt> <p><tt>cdef char buf[42]</tt> <br> <tt>my_string = PyString_FromStringAndSize(buf, 42)</tt></p> </blockquote> <h2> <a name="NullBytes"></a>How do I convert a C string containing null bytes to a Python string?</h2> Put in a declaration for the <tt>PyString_FromStringAndSize</tt> API routine and use that<tt>.</tt> See <a href="#CallCAPI">How do I call Python/C API routines?</a> <h2> <a name="NumericAccess"></a>How do I access the data inside a Numeric array object?</h2> Use a <tt>cdef extern from</tt> block to include the Numeric header file and declare the array object as an external extension type. The following code illustrates how to do this: <blockquote><tt>cdef extern from "Numeric/arrayobject.h":</tt> <p><tt> struct PyArray_Descr:</tt> <br> <tt> int type_num, elsize</tt> <br> <tt> char type</tt> </p> <p><tt> ctypedef class Numeric.ArrayType [object PyArrayObject]</tt><tt>:</tt> <br> <tt> cdef char *data</tt> <br> <tt> cdef int nd</tt> <br> <tt> cdef int *dimensions, *strides</tt> <br> <tt> cdef object base</tt> <br> <tt> cdef PyArray_Descr *descr</tt> <br> <tt> cdef int flags<br> </tt></p> </blockquote> <p>For more information about external extension types, see the <a href="extension_types.html#ExternalExtTypes">"External Extension Types"</a> section of the <a href="extension_types.html">"Extension Types"</a> documentation page.<br> <tt> </tt> </p> <h2><a name="Rhubarb"></a>Pyrex says my extension type object has no attribute 'rhubarb', but I know it does. What gives?</h2> You're probably trying to access it through a reference which Pyrex thinks is a generic Python object. You need to tell Pyrex that it's a reference to your extension type by means of a declaration. For example,<br> <blockquote><tt>cdef class Vegetables:</tt><br> <tt> cdef int rhubarb</tt><br> <br> <tt>...</tt><br> <tt>cdef Vegetables veg</tt><br> <tt>veg.rhubarb = 42</tt><br> </blockquote> Also see the <a href="Manual/extension_types.html#ExtTypeAttrs">"Attributes"</a> section of the <a href="Manual/extension_types.html">"Extension Types"</a> documentation page.<br> <h2><a name="Quack"></a>Python says my extension type has no method called 'quack', but I know it does. What gives?</h2> You may have declared the method using <span style="font-family: monospace;">cdef</span> instead of <span style="font-family: monospace;">def</span>. Only functions and methods declared with <span style="font-family: monospace;">def</span> are callable from Python code.<br><br> --- </body></html>