FAQ.html 4.46 KB
Newer Older
William Stein's avatar
William Stein committed
1 2 3 4 5 6
<!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>
mathbunnyru's avatar
mathbunnyru committed
7
      <center>   <h1>    <hr width="100%">Cython FAQ
William Stein's avatar
William Stein committed
8 9 10 11 12 13 14 15 16
<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>
17
  <li><b><a href="#Rhubarb">Cython says my extension type object has no attribute
William Stein's avatar
William Stein committed
18 19 20 21 22 23
'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.
mathbunnyru's avatar
mathbunnyru committed
24 25 26
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:
William Stein's avatar
William Stein committed
27 28 29 30 31 32
<blockquote><tt>cdef extern from "Python.h":</tt> <br>
    <tt>&nbsp;&nbsp;&nbsp; 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>
mathbunnyru's avatar
mathbunnyru committed
33 34
   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
William Stein's avatar
William Stein committed
35 36
 routines?</a>   <h2> <a name="NumericAccess"></a>How do I access the data inside a Numeric
 array object?</h2>
mathbunnyru's avatar
mathbunnyru committed
37 38 39
   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:
William Stein's avatar
William Stein committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
<blockquote><tt>cdef extern from "Numeric/arrayobject.h":</tt>         <p><tt>&nbsp;&nbsp;&nbsp; struct PyArray_Descr:</tt> <br>
    <tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int type_num, elsize</tt>    <br>
    <tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char type</tt> </p>
         <p><tt>&nbsp;&nbsp;&nbsp; ctypedef class Numeric.ArrayType [object PyArrayObject]</tt><tt>:</tt>    <br>
    <tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cdef char *data</tt> <br>
    <tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cdef int nd</tt> <br>
    <tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cdef int *dimensions,
*strides</tt>    <br>
    <tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cdef object base</tt>
  <br>
    <tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cdef PyArray_Descr *descr</tt>    <br>
    <tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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>
58
    <h2><a name="Rhubarb"></a>Cython says my extension type object has no attribute
William Stein's avatar
William Stein committed
59
'rhubarb', but I know it does. What gives?</h2>
60 61
You're probably trying to access it through a reference which Cython thinks
is a generic Python object. You need to tell Cython that it's a reference
William Stein's avatar
William Stein committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75
to your extension type by means of a declaration,<br>
for example,<br>
<blockquote><tt>cdef class Vegetables:</tt><br>
  <tt>&nbsp; &nbsp; 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="extension_types.html#ExtTypeAttrs">"Attributes"</a>
section of the <a href="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>
mathbunnyru's avatar
mathbunnyru committed
76
---
William Stein's avatar
William Stein committed
77
</body></html>