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.61 (Macintosh; I; PPC) [Netscape]"><title>Extension Types</title></head>
6 <h1> <hr width="100%">Extension Types
10 <li> <a href="#Introduction">Introduction</a></li>
11 <li> <a href="#ExtTypeAttrs">Attributes</a></li>
12 <li> <a href="#NotNone">Extension types and None</a></li>
13 <li> <a href="special_methods.html">Special methods</a></li>
14 <li> <a href="#Properties">Properties</a> <font style="color: rgb(0, 153, 0);" color="#ed181e">(NEW in
16 <li><a href="#SubclassingExtTypes">Subclassing</a></li>
17 <li> <a href="#CMethods">C Methods</a> <font style="color: rgb(0, 153, 0);" color="#ff0000">(NEW in 0.9)</font><br>
18 <a href="#ForwardDeclaringExtTypes">Forward-declaring extension types</a></li><li><a href="#WeakRefs">Making extension types weak-referenceable</a> <span style="color: rgb(255, 0, 0);">(NEW in 0.9.4)</span><br>
21 <li> <a href="#PublicAndExtern">Public and external extension types</a><font color="#2f8b20"><br>
24 <li> <a href="#ExternalExtTypes">External extension types</a></li>
25 <li> <a href="#ImplicitImport">Implicit importing</a><font color="#2f8b20"><br>
27 <li> <a href="#TypeVsConstructor">Type names vs. constructor names</a></li>
28 <li> <a href="#PublicExtensionTypes">Public extension types</a></li>
29 <li> <a href="#NameSpecClause">Name specification clause</a></li>
32 <h2> <a name="Introduction"></a>Introduction</h2>
33 As well as creating normal user-defined classes with the Python <b>class</b>
34 statement, Cython also lets you create new built-in Python types, known as
35 <i>extension types</i>. You define an extension type using the <b>cdef class</b> statement. Here's an example:
36 <blockquote><tt>cdef class Shrubbery:</tt> <p><tt> cdef int width, height</tt> </p>
37 <p><tt> def __init__(self, w, h):</tt> <br>
38 <tt> self.width = w</tt> <br>
39 <tt> self.height = h</tt> </p>
40 <p><tt> def describe(self):</tt> <br>
41 <tt> print "This shrubbery is",
42 self.width, \</tt> <br>
43 <tt>
44 "by", self.height, "cubits."</tt></p>
46 As you can see, a Cython extension type definition looks a lot like a Python
47 class definition. Within it, you use the <b>def</b> statement to define
48 methods that can be called from Python code. You can even define many of
49 the special methods such as <tt>__init__</tt> as you would in Python.
50 <p>The main difference is that you can use the <b>cdef</b> statement to define
51 attributes. The attributes may be Python objects (either generic or of a particular
52 extension type), or they may be of any C data type. So you can use extension
53 types to wrap arbitrary C data structures and provide a Python-like interface
55 <h2> <a name="ExtTypeAttrs"></a>Attributes</h2>
56 Attributes of an extension type are stored directly in the object's C struct.
57 The set of attributes is fixed at compile time; you can't add attributes
58 to an extension type instance at run time simply by assigning to them, as
59 you could with a Python class instance. (You can subclass the extension type
60 in Python and add attributes to instances of the subclass, however.)
61 <p>There are two ways that attributes of an extension type can be accessed:
62 by Python attribute lookup, or by direct access to the C struct from Cython
63 code. Python code is only able to access attributes of an extension type
64 by the first method, but Cython code can use either method. </p>
65 <p>By default, extension type attributes are only accessible by direct access,
66 not Python access, which means that they are not accessible from Python code.
67 To make them accessible from Python code, you need to declare them as <tt>public</tt> or <tt>readonly</tt>. For example, </p>
68 <blockquote><tt>cdef class Shrubbery:</tt> <br>
69 <tt> cdef public int width, height</tt> <br>
70 <tt> cdef readonly float depth</tt></blockquote>
71 makes the <tt>width</tt> and <tt>height</tt> attributes readable and writable
72 from Python code, and the <tt>depth</tt> attribute readable but not writable.
74 <p>Note that you can only expose simple C types, such as ints, floats and
75 strings, for Python access. You can also expose Python-valued attributes,
76 although read-write exposure is only possible for generic Python attributes
77 (of type <tt>object</tt>). If the attribute is declared to be of an extension
78 type, it must be exposed <tt>readonly</tt>. </p>
79 <p>Note also that the <tt>public</tt> and <tt>readonly</tt> options apply
80 only to <i>Python</i> access, not direct access. All the attributes of an
81 extension type are always readable and writable by direct access. </p>
82 <p>Howerver, for direct access to be possible, the Cython compiler must know
83 that you have an instance of that type, and not just a generic Python object.
84 It knows this already in the case of the "self" parameter of the methods of
85 that type, but in other cases you will have to tell it by means of a declaration.
87 <blockquote><tt>cdef widen_shrubbery(Shrubbery sh, extra_width):</tt> <br>
88 <tt> sh.width = sh.width + extra_width</tt></blockquote>
89 If you attempt to access an extension type attribute through a generic
90 object reference, Cython will use a Python attribute lookup. If the attribute
91 is exposed for Python access (using <tt>public</tt> or <tt>readonly</tt>)
92 then this will work, but it will be much slower than direct access.
93 <h2> <a name="NotNone"></a>Extension types and None</h2>
94 When you declare a parameter or C variable as being of an extension type,
95 Cython will allow it to take on the value None as well as values of its declared
96 type. This is analogous to the way a C pointer can take on the value NULL,
97 and you need to exercise the same caution because of it. There is no problem
98 as long as you are performing Python operations on it, because full dynamic
99 type checking will be applied. However, when you access C attributes of an
100 extension type (as in the <tt>widen_shrubbery</tt> function above), it's up
101 to you to make sure the reference you're using is not None -- in the interests
102 of efficiency, Cython does <i>not</i> check this.
103 <p>You need to be particularly careful when exposing Python functions which
104 take extension types as arguments. If we wanted to make <tt>widen_shrubbery</tt>
105 a Python function, for example, if we simply wrote </p>
106 <blockquote><tt>def widen_shrubbery(Shrubbery sh, extra_width): # <font color="#ed181e">This is</font></tt> <br>
107 <tt> sh.width = sh.width + extra_width
108 # <font color="#ed181e">dangerous!</font></tt></blockquote>
109 then users of our module could crash it by passing None for the <tt>sh</tt>
111 <p>One way to fix this would be </p>
112 <blockquote><tt>def widen_shrubbery(Shrubbery sh, extra_width):</tt> <br>
113 <tt> if sh is None:</tt> <br>
114 <tt> raise TypeError</tt> <br>
115 <tt> sh.width = sh.width + extra_width</tt></blockquote>
116 but since this is anticipated to be such a frequent requirement, Cython
117 provides a more convenient way. Parameters of a Python function declared
118 as an extension type can have a <b><tt>not None</tt></b> clause:
119 <blockquote><tt>def widen_shrubbery(Shrubbery sh not None, extra_width):</tt>
121 <tt> sh.width = sh.width + extra_width</tt></blockquote>
122 Now the function will automatically check that <tt>sh</tt> is not None
123 along with checking that it has the right type.
124 <p>Note, however that the <tt>not None</tt> clause can <i>only</i> be used
125 in Python functions (defined with <tt>def</tt>) and not C functions (defined
126 with <tt>cdef</tt>). If you need to check whether a parameter to a C function
127 is None, you will need to do it yourself. </p>
128 <p>Some more things to note: </p>
130 <li> The <b>self</b> parameter of a method of an extension type is guaranteed
131 never to be None.</li>
134 <li> When comparing a value with None, keep in mind that, if <tt>x</tt> is a Python object, <tt>x is None</tt> and <tt>x is not None</tt> are very
135 efficient because they translate directly to C pointer comparisons, whereas
136 <tt>x == None</tt> and <tt>x != None</tt>, or simply using <tt>x</tt> as a boolean value (as in <tt>if x: ...</tt>) will invoke Python operations
137 and therefore be much slower.</li>
139 <h2> <a name="ExtTypeSpecialMethods"></a>Special methods</h2>
140 Although the principles are similar, there are substantial differences
141 between many of the <span style="font-family: monospace;">__xxx__</span> special methods of extension types and their
142 Python counterparts. There is a <a href="special_methods.html">separate page</a> devoted to this subject, and you should read it carefully before attempting
143 to use any special methods in your extension types.
144 <h2> <a name="Properties"></a>Properties</h2>
145 There is a special syntax for defining <b>properties</b> in an extension
147 <blockquote><tt>cdef class Spam:</tt> <p><tt> property cheese:</tt> </p>
148 <p><tt> "A doc string can go
150 <p><tt> def __get__(self):</tt>
152 <tt>
153 # This is called when the property is read.</tt> <br>
154 <tt>
156 <p><tt> def __set__(self, value):</tt>
158 <tt>
159 # This is called when the property is written.</tt> <br>
160 <tt>
162 <p><tt> def __del__(self):</tt>
164 <tt>
165 # This is called when the property is deleted.</tt> <br>
168 The <tt>__get__</tt>, <tt>__set__</tt> and <tt>__del__</tt> methods are
169 all optional; if they are omitted, an exception will be raised when the corresponding
170 operation is attempted.
171 <p>Here's a complete example. It defines a property which adds to a list
172 each time it is written to, returns the list when it is read, and empties
173 the list when it is deleted. <br>
175 <center> <table align="center" cellpadding="5">
178 <td bgcolor="#ffaf18"><b><tt>cheesy.pyx</tt></b></td>
179 <td bgcolor="#5dbaca"><b>Test input</b></td>
182 <td rowspan="3" bgcolor="#ffaf18" valign="top"><tt>cdef class CheeseShop:</tt>
183 <p><tt> cdef object cheeses</tt> </p>
184 <p><tt> def __new__(self):</tt> <br>
185 <tt> self.cheeses = []</tt> </p>
186 <p><tt> property cheese:</tt> </p>
187 <p><tt> def __get__(self):</tt> <br>
188 <tt> return "We don't have: %s" % self.cheeses</tt>
190 <p><tt> def __set__(self, value):</tt> <br>
191 <tt> self.cheeses.append(value)</tt>
193 <p><tt> def __del__(self):</tt> <br>
194 <tt> del self.cheeses[:]</tt></p>
196 <td bgcolor="#5dbaca" valign="top"><tt>from cheesy import CheeseShop</tt>
197 <p><tt>shop = CheeseShop()</tt> <br>
198 <tt>print shop.cheese</tt> </p>
199 <p><tt>shop.cheese = "camembert"</tt> <br>
200 <tt>print shop.cheese</tt> </p>
201 <p><tt>shop.cheese = "cheddar"</tt> <br>
202 <tt>print shop.cheese</tt> </p>
203 <p><tt>del shop.cheese</tt> <br>
204 <tt>print shop.cheese</tt></p>
208 <td bgcolor="#8cbc1c"><b>Test output</b></td>
211 <td bgcolor="#8cbc1c"><tt>We don't have: []</tt> <br>
212 <tt>We don't have: ['camembert']</tt> <br>
213 <tt>We don't have: ['camembert', 'cheddar']</tt> <br>
214 <tt>We don't have: []</tt></td>
218 <h2> <a name="SubclassingExtTypes"></a>Subclassing</h2>
219 An extension type may inherit from a built-in type or another extension
221 <blockquote><tt>cdef class Parrot:</tt> <br>
222 <tt> ...</tt><tt></tt> <p><tt>cdef class Norwegian(Parrot):</tt> <br>
223 <tt> ...</tt></p>
226 A complete definition of the base type must be available to Cython, so if
227 the base type is a built-in type, it must have been previously declared as
228 an <b>extern</b> extension type. If the base type is defined in another Cython
229 module, it must either be declared as an extern extension type or imported
230 using the <b><a href="sharing.html">cimport</a></b> statement. </p>
231 <p>An extension type can only have one base class (no multiple inheritance).
233 <p>Cython extension types can also be subclassed in Python. A Python class
234 can inherit from multiple extension types provided that the usual Python
235 rules for multiple inheritance are followed (i.e. the C layouts of all the
236 base classes must be compatible).<br>
238 <h2><a name="CMethods"></a>C methods</h2>
239 Extension types can have C methods as well as Python methods. Like C functions,
240 C methods are declared using <tt>cdef</tt> instead of <tt>def</tt>. C methods
241 are "virtual", and may be overridden in derived extension types.<br>
243 <table align="center" cellpadding="5">
246 <td bgcolor="#ffaf18" valign="top" width="50%"><b><tt>pets.pyx</tt></b><br>
248 <td bgcolor="#8cbc1c" valign="top" width="30%"><b>Output</b><br>
252 <td bgcolor="#ffaf18" valign="top" width="50%"><tt>cdef class Parrot:<br>
254 cdef void describe(self):<br>
255 print "This parrot is resting."<br>
257 cdef class Norwegian(Parrot):<br>
259 cdef void describe(self):<br>
260 Parrot.describe(self)<br>
261 print "Lovely plumage!"<br>
264 cdef Parrot p1, p2<br>
270 p2.describe()</tt> <br>
272 <td bgcolor="#8cbc1c" valign="top" width="30%"><tt>p1:<br>
273 This parrot is resting.<br>
275 </tt><tt>This parrot is resting.<br>
276 </tt><tt> Lovely plumage!</tt><br>
281 The above example also illustrates that a C method can call an inherited
282 C method using the usual Python technique, i.e.<br>
283 <blockquote><tt>Parrot.describe(self)</tt><br>
285 <h2><a name="ForwardDeclaringExtTypes"></a>Forward-declaring extension types</h2>
286 Extension types can be forward-declared, like struct and union types. This
287 will be necessary if you have two extension types that need to refer to
289 <blockquote><tt>cdef class Shrubbery # forward declaration</tt> <p><tt>cdef class Shrubber:</tt> <br>
290 <tt> cdef Shrubbery work_in_progress</tt> </p>
291 <p><tt>cdef class Shrubbery:</tt> <br>
292 <tt> cdef Shrubber creator</tt></p>
294 If you are forward-declaring an exension type that has a base class, you
295 must specify the base class in both the forward declaration and its subsequent
296 definition, for example,<br>
297 <blockquote><tt>cdef class A(B)<br>
302 # attributes and methods</tt><br>
304 <h2><a name="WeakRefs"></a>Making extension types weak-referenceable</h2>By
305 default, extension types do not support having weak references made to
306 them. You can enable weak referencing by declaring a C attribute of
307 type <span style="font-family: monospace;">object</span> called <span style="font-family: monospace; font-weight: bold;">__weakref__</span>. For example,<br>
309 <div style="margin-left: 40px;"><span style="font-family: monospace;">cdef class ExplodingAnimal:</span><br style="font-family: monospace;">
310 <span style="font-family: monospace;"> """This animal will self-destruct when it is</span><br>
311 <span style="font-family: monospace;"> no longer strongly referenced."""</span><br>
312 <span style="font-family: monospace;"> </span><br style="font-family: monospace;">
313 <span style="font-family: monospace;"></span><span style="font-family: monospace;"> cdef object __weakref__</span><br>
316 <h2><a name="PublicAndExtern"></a>Public and external extension types</h2>
318 Extension types can be declared <b>extern</b> or <b>public</b>. An <a href="#ExternalExtTypes"><b>extern</b> extension type declaration</a> makes
319 an extension type defined in external C code available to a Cython module.
320 A <a href="#PublicExtensionTypes"><b>public</b> extension type declaration</a> makes an extension type defined in a Cython module available to external C
322 <h3> <a name="ExternalExtTypes"></a>External extension types</h3>
323 An <b>extern</b> extension type allows you to gain access to the internals
324 of Python objects defined in the Python core or in a non-Cython extension
326 <blockquote><b>NOTE:</b> In Cython versions before 0.8, <b>extern</b> extension
327 types were also used to reference extension types defined in another Cython
328 module. While you can still do that, Cython 0.8 and later provides a better
329 mechanism for this. See <a href="sharing.html">Sharing C Declarations Between
330 Cython Modules</a>.</blockquote>
331 Here is an example which will let you get at the C-level members of the
332 built-in <i>complex</i> object.
333 <blockquote><tt>cdef extern from "complexobject.h":</tt> <p><tt> struct Py_complex:</tt> <br>
334 <tt> double real</tt> <br>
335 <tt> double imag</tt> </p>
336 <p><tt> ctypedef class __builtin__.complex [object PyComplexObject]:</tt>
338 <tt> cdef Py_complex cval</tt>
340 <p><tt># A function which uses the above type</tt> <br>
341 <tt>def spam(complex c):</tt> <br>
342 <tt> print "Real:", c.cval.real</tt> <br>
343 <tt> print "Imag:", c.cval.imag</tt></p>
345 Some important things to note are:
347 <li> In this example, <b>ctypedef class</b> has been used. This is because,
348 in the Python header files, the <tt>PyComplexObject</tt> struct is declared
351 <div style="margin-left: 40px;"><tt>ctypedef struct {</tt> <br>
352 <tt> ...</tt> <br>
353 <tt>} PyComplexObject;<br>
356 </li><li>As well as the name of the extension type, the <i>module</i> in which
357 its type object can be found is also specified. See the <a href="#ImplicitImport">implicit importing</a> section below. <br>
360 <li> When declaring an external extension type, you don't declare
361 any methods. Declaration of methods is not required in order to call them,
362 because the calls are Python method calls. Also, as with structs and unions,
363 if your extension class declaration is inside a <i>cdef extern from</i> block,
364 you only need to declare those C members which you wish to access.</li>
366 <h3> <a name="ImplicitImport"></a>Implicit importing</h3>
367 <blockquote><font color="#ef1f1d">Backwards Incompatibility Note</font>:
368 You will have to update any pre-0.8 Cython modules you have which use <b>extern</b>
369 extension types. I apologise for this, but for complicated reasons it proved
370 to be too difficult to continue supporting the old way of doing these while
371 introducing the new features that I wanted.</blockquote>
372 Cython 0.8 and later requires you to include a module name in an extern
373 extension class declaration, for example,
374 <blockquote><tt>cdef extern class MyModule.Spam:</tt> <br>
375 <tt> ...</tt></blockquote>
376 The type object will be implicitly imported from the specified module and
377 bound to the corresponding name in this module. In other words, in this
380 <pre>from <tt>MyModule</tt> import Spam</pre>
382 statement will be executed at module load time.
383 <p>The module name can be a dotted name to refer to a module inside a package
384 hierarchy, for example, </p>
385 <blockquote><tt>cdef extern class My.Nested.Package.Spam:</tt> <br>
386 <tt> ...</tt></blockquote>
387 You can also specify an alternative name under which to import the type
388 using an <b>as</b> clause, for example,
390 <tt>cdef extern class My.Nested.Package.Spam as Yummy:</tt> <br>
391 <tt> ...</tt> </ol>
392 which corresponds to the implicit import statement
394 <pre>from <tt>My.Nested.Package</tt> import <tt>Spam</tt> as <tt>Yummy</tt></pre>
396 <h3> <a name="TypeVsConstructor"></a>Type names vs. constructor names</h3>
397 Inside a Cython module, the name of an extension type serves two distinct
398 purposes. When used in an expression, it refers to a module-level global
399 variable holding the type's constructor (i.e. its type-object). However,
400 it can also be used as a C type name to declare variables, arguments and
401 return values of that type.
402 <p>When you declare </p>
403 <blockquote><tt>cdef extern class MyModule.Spam:</tt> <br>
404 <tt> ...</tt></blockquote>
405 the name <tt>Spam</tt> serves both these roles. There may be other names
406 by which you can refer to the constructor, but only <tt>Spam</tt> can be
407 used as a type name. For example, if you were to explicity <tt>import MyModule</tt>,
408 you could use<tt> MyModule.Spam()</tt> to create a Spam instance, but you
409 wouldn't be able to use <tt>MyModule.Spam</tt> as a type name.
410 <p>When an <b>as</b> clause is used, the name specified in the <b>as</b>
411 clause also takes over both roles. So if you declare </p>
412 <blockquote><tt>cdef extern class MyModule.Spam as Yummy:</tt> <br>
413 <tt> ...</tt></blockquote>
414 then <tt>Yummy</tt> becomes both the type name and a name for the constructor.
415 Again, there are other ways that you could get hold of the constructor,
416 but only <tt>Yummy</tt> is usable as a type name.
417 <h3> <a name="PublicExtensionTypes"></a>Public extension types</h3>
418 An extension type can be declared <b>public</b>, in which case a <b>.h</b>
419 file is generated containing declarations for its object struct and type
420 object. By including the <b>.h</b> file in external C code that you write,
421 that code can access the attributes of the extension type.
422 <h3> <a name="NameSpecClause"></a>Name specification clause</h3>
423 The part of the class declaration in square brackets is a special feature
424 only available for <b>extern</b> or <b>public</b> extension types. The full
425 form of this clause is
426 <blockquote><tt>[object </tt><i>object_struct_name</i><tt>, type </tt><i>type_object_name</i><span style="font-family: monospace;"> ]</span></blockquote>
427 where <i>object_struct_name</i> is the name to assume for the type's C
428 struct, and <i>type_object_name</i> is the name to assume for the type's
429 statically declared type object. (The object and type clauses can be written
431 <p>If the extension type declaration is inside a <b>cdef extern from</b>
432 block, the <b>object</b> clause is required, because Cython must be able to
433 generate code that is compatible with the declarations in the header file.
434 Otherwise, for <b>extern</b> extension types, the <b>object</b> clause is
436 <p>For <b>public</b> extension types, the <b>object</b> and <b>type</b> clauses
437 are both required, because Cython must be able to generate code that is compatible
438 with external C code. </p>
440 <hr width="100%"> <br>
441 Back to the <a href="overview.html">Language Overview</a> <br>