merged in Vitja's tab removals
[cython.git] / Doc / FAQ.html
1 <!DOCTYPE doctype PUBLIC "-//w3c//dtd html 4.0 transitional//en">
2 <html><head>
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>
5
6 <body>
7       <center>   <h1>    <hr width="100%">Cython FAQ   
8 <hr width="100%"></h1>
9   </center>
10       <h2> Contents</h2>
11       <ul>
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>
19   </li>
20
21      </ul>
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>&nbsp;&nbsp;&nbsp; object PyString_FromStringAndSize(char *, int)</tt>         <p><tt>cdef char buf[42]</tt> <br>
29     <tt>my_string = PyString_FromStringAndSize(buf, 42)</tt></p>
30   </blockquote>
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
36  array object?</h2>
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>&nbsp;&nbsp;&nbsp; struct PyArray_Descr:</tt> <br>
41     <tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int type_num, elsize</tt>    <br>
42     <tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char type</tt> </p>
43          <p><tt>&nbsp;&nbsp;&nbsp; ctypedef class Numeric.ArrayType [object PyArrayObject]</tt><tt>:</tt>    <br>
44     <tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cdef char *data</tt> <br>
45     <tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cdef int nd</tt> <br>
46     <tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cdef int *dimensions,
47 *strides</tt>    <br>
48     <tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cdef object base</tt>
49   <br>
50     <tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cdef PyArray_Descr *descr</tt>    <br>
51     <tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cdef int flags<br>
52     </tt></p>
53 </blockquote>
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
56 page.<br>
57 <tt>    </tt> </p>
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>
63 for example,<br>
64 <blockquote><tt>cdef class Vegetables:</tt><br>
65   <tt>&nbsp; &nbsp; cdef int rhubarb</tt><br>
66   <br>
67   <tt>...</tt><br>
68   <tt>cdef Vegetables veg</tt><br>
69   <tt>veg.rhubarb = 42</tt><br>
70 </blockquote>
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>
76 --- 
77 </body></html>