File: FAQ.html

package info (click to toggle)
cython 0.9.8-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 6,988 kB
  • ctags: 4,303
  • sloc: python: 16,048; ansic: 3,029; makefile: 71
file content (77 lines) | stat: -rw-r--r-- 4,578 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<!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%">Cython 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">Cython 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>&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>
   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>&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>
    <h2><a name="Rhubarb"></a>Cython 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 Cython thinks
is a generic Python object. You need to tell Cython that it's a reference
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>
--- 
</body></html>