1 <!DOCTYPE doctype PUBLIC "-//w3c//dtd html 4.0 transitional//en">
3 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
4 <meta name="GENERATOR" content="Mozilla/4.51 (Macintosh; I; PPC) [Netscape]"><title>FAQ.html</title></head>
7 <center> <h1> <hr width="100%">Cython FAQ
12 <li> <b><a href="#CallCAPI">How do I call Python/C API routines?</a></b></li>
13 <li> <b><a href="#NullBytes">How do I convert a C string containing null
14 bytes to a Python string?</a></b></li>
15 <li> <b><a href="#NumericAccess">How do I access the data inside a Numeric
16 array object?</a></b></li>
17 <li><b><a href="#Rhubarb">Cython says my extension type object has no attribute
18 '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>
22 <hr width="100%"> <h2> <a name="CallCAPI"></a>How do I call Python/C API routines?</h2>
23 Declare them as C functions inside a <tt>cdef extern from</tt> block.
24 Use the type name <tt>object</tt> for any parameters and return types which
25 are Python object references. Don't use the word <tt>const</tt> anywhere.
26 Here is an example which defines and uses the <tt>PyString_FromStringAndSize</tt> routine:
27 <blockquote><tt>cdef extern from "Python.h":</tt> <br>
28 <tt> object PyString_FromStringAndSize(char *, int)</tt> <p><tt>cdef char buf[42]</tt> <br>
29 <tt>my_string = PyString_FromStringAndSize(buf, 42)</tt></p>
31 <h2> <a name="NullBytes"></a>How do I convert a C string containing null
32 bytes to a Python string?</h2>
33 Put in a declaration for the <tt>PyString_FromStringAndSize</tt> API routine
34 and use that<tt>.</tt> See <a href="#CallCAPI">How do I call Python/C API
35 routines?</a> <h2> <a name="NumericAccess"></a>How do I access the data inside a Numeric
37 Use a <tt>cdef extern from</tt> block to include the Numeric header file
38 and declare the array object as an external extension type. The following
39 code illustrates how to do this:
40 <blockquote><tt>cdef extern from "Numeric/arrayobject.h":</tt> <p><tt> struct PyArray_Descr:</tt> <br>
41 <tt> int type_num, elsize</tt> <br>
42 <tt> char type</tt> </p>
43 <p><tt> ctypedef class Numeric.ArrayType [object PyArrayObject]</tt><tt>:</tt> <br>
44 <tt> cdef char *data</tt> <br>
45 <tt> cdef int nd</tt> <br>
46 <tt> cdef int *dimensions,
48 <tt> cdef object base</tt>
50 <tt> cdef PyArray_Descr *descr</tt> <br>
51 <tt> cdef int flags<br>
54 <p>For more information about external extension types, see the <a href="extension_types.html#ExternalExtTypes">"External Extension Types"</a>
55 section of the <a href="extension_types.html">"Extension Types"</a> documentation
58 <h2><a name="Rhubarb"></a>Cython says my extension type object has no attribute
59 'rhubarb', but I know it does. What gives?</h2>
60 You're probably trying to access it through a reference which Cython thinks
61 is a generic Python object. You need to tell Cython that it's a reference
62 to your extension type by means of a declaration,<br>
64 <blockquote><tt>cdef class Vegetables:</tt><br>
65 <tt> cdef int rhubarb</tt><br>
68 <tt>cdef Vegetables veg</tt><br>
69 <tt>veg.rhubarb = 42</tt><br>
71 Also see the <a href="extension_types.html#ExtTypeAttrs">"Attributes"</a>
72 section of the <a href="extension_types.html">"Extension
73 Types"</a> documentation page.<br>
74 <h2><a name="Quack"></a>Python says my extension type has no method called 'quack', but I know it does. What gives?</h2>
75 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>