From c4fa206b9579bb739a1cf9ceb7980b47a9b9c5e2 Mon Sep 17 00:00:00 2001 From: William Stein Date: Sat, 28 Jul 2007 13:25:22 -0700 Subject: Initial version --- Doc/About.html | 1 + Doc/FAQ.html | 77 ++++ Doc/extension_types.html | 444 ++++++++++++++++++++++ Doc/index.html | 1 + Doc/overview.html | 960 +++++++++++++++++++++++++++++++++++++++++++++++ Doc/primes.c | 1 + Doc/sharing.html | 201 ++++++++++ Doc/special_methods.html | 598 +++++++++++++++++++++++++++++ 8 files changed, 2283 insertions(+) create mode 100644 Doc/About.html create mode 100644 Doc/FAQ.html create mode 100644 Doc/extension_types.html create mode 100644 Doc/index.html create mode 100644 Doc/overview.html create mode 100644 Doc/primes.c create mode 100644 Doc/sharing.html create mode 100644 Doc/special_methods.html (limited to 'Doc') diff --git a/Doc/About.html b/Doc/About.html new file mode 100644 index 000000000..057d05a37 --- /dev/null +++ b/Doc/About.html @@ -0,0 +1 @@ + About Pyrex


Pyrex

A language for writing Python extension modules

What is Pyrex all about?

Pyrex is a language specially designed for writing Python extension modules. It's designed to bridge the gap between the nice, high-level, easy-to-use world of Python and the messy, low-level world of C.

You may be wondering why anyone would want a special language for this. Python is really easy to extend using C or C++, isn't it? Why not just write your extension modules in one of those languages?

Well, if you've ever written an extension module for Python, you'll know that things are not as easy as all that. First of all, there is a fair bit of boilerplate code to write before you can even get off the ground. Then you're faced with the problem of converting between Python and C data types. For the basic types such as numbers and strings this is not too bad, but anything more elaborate and you're into picking Python objects apart using the Python/C API calls, which requires you to be meticulous about maintaining reference counts, checking for errors at every step and cleaning up properly if anything goes wrong. Any mistakes and you have a nasty crash that's very difficult to debug.

Various tools have been developed to ease some of the burdens of producing extension code, of which perhaps SWIG is the best known. SWIG takes a definition file consisting of a mixture of C code and specialised declarations, and produces an extension module. It writes all the boilerplate for you, and in many cases you can use it without knowing about the Python/C API. But you need to use API calls if any substantial restructuring of the data is required between Python and C.

What's more, SWIG gives you no help at all if you want to create a new built-in Python type. It will generate pure-Python classes which wrap (in a slightly unsafe manner) pointers to C data structures, but creation of true extension types is outside its scope.

Another notable attempt at making it easier to extend Python is PyInline , inspired by a similar facility for Perl. PyInline lets you embed pieces of C code in the midst of a Python file, and automatically extracts them and compiles them into an extension. But it only converts the basic types automatically, and as with SWIG,  it doesn't address the creation of new Python types.

Pyrex aims to go far beyond what any of these previous tools provides. Pyrex deals with the basic types just as easily as SWIG, but it also lets you write code to convert between arbitrary Python data structures and arbitrary C data structures, in a simple and natural way, without knowing anything about the Python/C API. That's right -- nothing at all! Nor do you have to worry about reference counting or error checking -- it's all taken care of automatically, behind the scenes, just as it is in interpreted Python code. And what's more, Pyrex lets you define new built-in Python types just as easily as you can define new classes in Python.

Sound too good to be true? Read on and find out how it's done.

The Basics of Pyrex

The fundamental nature of Pyrex can be summed up as follows: Pyrex is Python with C data types.

Pyrex is Python: Almost any piece of Python code is also valid Pyrex code. (There are a few limitations, but this approximation will serve for now.) The Pyrex compiler will convert it into C code which makes equivalent calls to the Python/C API. In this respect, Pyrex is similar to the former Python2C project (to which I would supply a reference except that it no longer seems to exist).

...with C data types. But Pyrex is much more than that, because parameters and variables can be declared to have C data types. Code which manipulates Python values and C values can be freely intermixed, with conversions occurring automatically wherever possible. Reference count maintenance and error checking of Python operations is also automatic, and the full power of Python's exception handling facilities, including the try-except and try-finally statements, is available to you -- even in the midst of manipulating C data.

Here's a small example showing some of what can be done. It's a routine for finding prime numbers. You tell it how many primes you want, and it returns them as a Python list.

primes.pyx
 1  def primes(int kmax):
 2      cdef int n, k, i
 3      cdef int p[1000]
 4      result = []
 5      if kmax > 1000:
 6          kmax = 1000
 7      k = 0
 8      n = 2
 9      while k < kmax:
10          i = 0
11          while i < k and n % p[i] <> 0:
12              i = i + 1
13          if i == k:
14             p[k] = n
15             k = k + 1
16             result.append(n)
17          n = n + 1
18      return result
You'll see that it starts out just like a normal Python function definition, except that the parameter kmax is declared to be of type int . This means that the object passed will be converted to a C integer (or a TypeError will be raised if it can't be).

Lines 2 and 3 use the cdef statement to define some local C variables. Line 4 creates a Python list which will be used to return the result. You'll notice that this is done exactly the same way it would be in Python. Because the variable result hasn't been given a type, it is assumed to hold a Python object.

Lines 7-9 set up for a loop which will test candidate numbers for primeness until the required number of primes has been found. Lines 11-12, which try dividing a candidate by all the primes found so far, are of particular interest. Because no Python objects are referred to, the loop is translated entirely into C code, and thus runs very fast.

When a prime is found, lines 14-15 add it to the p array for fast access by the testing loop, and line 16 adds it to the result list. Again, you'll notice that line 16 looks very much like a Python statement, and in fact it is, with the twist that the C parameter n is automatically converted to a Python object before being passed to the append method. Finally, at line 18, a normal Python return statement returns the result list.

Compiling primes.pyx with the Pyrex compiler produces an extension module which we can try out in the interactive interpreter as follows:

>>> import primes
>>> primes.primes(10)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
>>>
See, it works! And if you're curious about how much work Pyrex has saved you, take a look at the C code generated for this module .

Language Details

For more about the Pyrex language, see the Language Overview .

Future Plans

Pyrex is not finished. Substantial tasks remaining include: \ No newline at end of file diff --git a/Doc/FAQ.html b/Doc/FAQ.html new file mode 100644 index 000000000..ad09c262c --- /dev/null +++ b/Doc/FAQ.html @@ -0,0 +1,77 @@ + + + + FAQ.html + + +


Pyrex FAQ +

+
+

Contents

+ +

How do I call Python/C API routines?

+ Declare them as C functions inside a cdef extern from block. +Use the type name object for any parameters and return types which +are Python object references. Don't use the word const anywhere. +Here is an example which defines and uses the PyString_FromStringAndSize routine: +
cdef extern from "Python.h":
+     object PyString_FromStringAndSize(char *, int)

cdef char buf[42]
+ my_string = PyString_FromStringAndSize(buf, 42)

+
+

How do I convert a C string containing null +bytes to a Python string?

+ Put in a declaration for the PyString_FromStringAndSize API routine + and use that. See How do I call Python/C API + routines?

How do I access the data inside a Numeric + array object?

+ Use a cdef extern from 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: +
cdef extern from "Numeric/arrayobject.h":

    struct PyArray_Descr:
+         int type_num, elsize
+         char type

+

    ctypedef class Numeric.ArrayType [object PyArrayObject]:
+         cdef char *data
+         cdef int nd
+         cdef int *dimensions, +*strides
+         cdef object base +
+         cdef PyArray_Descr *descr
+         cdef int flags
+

+
+

For more information about external extension types, see the "External Extension Types" +section of the "Extension Types" documentation +page.
+

+

Pyrex says my extension type object has no attribute +'rhubarb', but I know it does. What gives?

+You're probably trying to access it through a reference which Pyrex thinks +is a generic Python object. You need to tell Pyrex that it's a reference +to your extension type by means of a declaration,
+for example,
+
cdef class Vegetables:
+     cdef int rhubarb
+
+ ...
+ cdef Vegetables veg
+ veg.rhubarb = 42
+
+Also see the "Attributes" +section of the "Extension +Types" documentation page.
+

Python says my extension type has no method called 'quack', but I know it does. What gives?

+You may have declared the method using cdef instead of def. Only functions and methods declared with def are callable from Python code.
+--- + \ No newline at end of file diff --git a/Doc/extension_types.html b/Doc/extension_types.html new file mode 100644 index 000000000..cda6377e0 --- /dev/null +++ b/Doc/extension_types.html @@ -0,0 +1,444 @@ + + + + Extension Types + +


Extension Types +

+

Contents

+ +

Introduction

+ As well as creating normal user-defined classes with the Python class +statement, Pyrex also lets you create new built-in Python types, known as +extension types. You define an extension type using the cdef class statement. Here's an example: +
cdef class Shrubbery:

    cdef int width, height

+

    def __init__(self, w, h):
+         self.width = w
+         self.height = h

+

    def describe(self):
+         print "This shrubbery is", +self.width, \
+             +"by", self.height, "cubits."

+
+ As you can see, a Pyrex extension type definition looks a lot like a Python + class definition. Within it, you use the def statement to define +methods that can be called from Python code. You can even define many of +the special methods such as __init__ as you would in Python. +

The main difference is that you can use the cdef statement to define +attributes. The attributes may be Python objects (either generic or of a particular +extension type), or they may be of any C data type. So you can use extension +types to wrap arbitrary C data structures and provide a Python-like interface +to them.

+

Attributes

+ Attributes of an extension type are stored directly in the object's C struct. + The set of attributes is fixed at compile time; you can't add attributes +to an extension type instance at run time simply by assigning to them, as +you could with a Python class instance. (You can subclass the extension type +in Python and add attributes to instances of the subclass, however.) +

There are two ways that attributes of an extension type can be accessed: + by Python attribute lookup, or by direct access to the C struct from Pyrex + code. Python code is only able to access attributes of an extension type +by the first method, but Pyrex code can use either method.

+

By default, extension type attributes are only accessible by direct access, +not Python access, which means that they are not accessible from Python code. +To make them accessible from Python code, you need to declare them as public or readonly. For example,

+
cdef class Shrubbery:
+     cdef public int width, height
+     cdef readonly float depth
+ makes the width and height attributes readable and writable + from Python code, and the depth attribute readable but not writable. + +

Note that you can only expose simple C types, such as ints, floats and + strings, for Python access. You can also expose Python-valued attributes, + although read-write exposure is only possible for generic Python attributes + (of type object). If the attribute is declared to be of an extension + type, it must be exposed readonly.

+

Note also that the public and readonly options apply + only to Python access, not direct access. All the attributes of an +extension type are always readable and writable by direct access.

+

Howerver, for direct access to be possible, the Pyrex compiler must know +that you have an instance of that type, and not just a generic Python object. +It knows this already in the case of the "self" parameter of the methods of +that type, but in other cases you will have to tell it by means of a declaration. +For example,

+
cdef widen_shrubbery(Shrubbery sh, extra_width):
+     sh.width = sh.width + extra_width
+ If you attempt to access an extension type attribute through a generic +object reference, Pyrex will use a Python attribute lookup. If the attribute +is exposed for Python access (using public or readonly) +then this will work, but it will be much slower than direct access. +

Extension types and None

+ When you declare a parameter or C variable as being of an extension type, + Pyrex will allow it to take on the value None as well as values of its declared +type. This is analogous to the way a C pointer can take on the value NULL, +and you need to exercise the same caution because of it. There is no problem +as long as you are performing Python operations on it, because full dynamic +type checking will be applied. However, when you access C attributes of an +extension type (as in the widen_shrubbery function above), it's up +to you to make sure the reference you're using is not None -- in the interests +of efficiency, Pyrex does not check this. +

You need to be particularly careful when exposing Python functions which + take extension types as arguments. If we wanted to make widen_shrubbery +a Python function, for example, if we simply wrote

+
def widen_shrubbery(Shrubbery sh, extra_width): # This is
+     sh.width = sh.width + extra_width           +# dangerous!
+ then users of our module could crash it by passing None for the sh +parameter. +

One way to fix this would be

+
def widen_shrubbery(Shrubbery sh, extra_width):
+     if sh is None:
+         raise TypeError
+     sh.width = sh.width + extra_width
+ but since this is anticipated to be such a frequent requirement, Pyrex +provides a more convenient way. Parameters of a Python function declared +as an extension type can have a not None clause: +
def widen_shrubbery(Shrubbery sh not None, extra_width): +
+     sh.width = sh.width + extra_width
+ Now the function will automatically check that sh is not None +along with checking that it has the right type. +

Note, however that the not None clause can only be used + in Python functions (defined with def) and not C functions (defined + with cdef). If you need to check whether a parameter to a C function + is None, you will need to do it yourself.

+

Some more things to note:

+ + +

Special methods

+ Although the principles are similar, there are substantial differences +between many of the __xxx__ special methods of extension types and their +Python counterparts. There is a separate page devoted to this subject, and you should read it carefully before attempting +to use any special methods in your extension types. +

Properties

+ There is a special syntax for defining properties in an extension + class: +
cdef class Spam:

    property cheese:

+

        "A doc string can go +here."

+

        def __get__(self): +
+             +# This is called when the property is read.
+             +...

+

        def __set__(self, value): +
+             +# This is called when the property is written.
+             +...

+

        def __del__(self): +
+             +# This is called when the property is deleted.
+  

+
+ The __get__, __set__ and __del__ methods are +all optional; if they are omitted, an exception will be raised when the corresponding +operation is attempted. +

Here's a complete example. It defines a property which adds to a list +each time it is written to, returns the list when it is read, and empties +the list when it is deleted.
+  

+
+ + + + + + + + + + + + + + + +
cheesy.pyxTest input
cdef class CheeseShop: +

  cdef object cheeses

+

  def __new__(self):
+     self.cheeses = []

+

  property cheese:

+

    def __get__(self):
+       return "We don't have: %s" % self.cheeses +

+

    def __set__(self, value):
+       self.cheeses.append(value) +

+

    def __del__(self):
+       del self.cheeses[:]

+
from cheesy import CheeseShop +

shop = CheeseShop()
+ print shop.cheese

+

shop.cheese = "camembert"
+ print shop.cheese

+

shop.cheese = "cheddar"
+ print shop.cheese

+

del shop.cheese
+ print shop.cheese

+
Test output
We don't have: []
+ We don't have: ['camembert']
+ We don't have: ['camembert', 'cheddar']
+ We don't have: []
+
+

Subclassing

+ An extension type may inherit from a built-in type or another extension +type: +
cdef class Parrot:
+     ...

cdef class Norwegian(Parrot):
+     ...

+
+


+ A complete definition of the base type must be available to Pyrex, so if +the base type is a built-in type, it must have been previously declared as +an extern extension type. If the base type is defined in another Pyrex +module, it must either be declared as an extern extension type or imported +using the cimport statement.

+

An extension type can only have one base class (no multiple inheritance). +

+

Pyrex extension types can also be subclassed in Python. A Python class + can inherit from multiple extension types provided that the usual Python +rules for multiple inheritance are followed (i.e. the C layouts of all the +base classes must be compatible).
+

+

C methods

+ Extension types can have C methods as well as Python methods. Like C functions, +C methods are declared using cdef instead of def. C methods +are "virtual", and may be overridden in derived extension types.
+
+ + + + + + + + + + +
pets.pyx
+
Output
+
cdef class Parrot:
+
+   cdef void describe(self):
+     print "This parrot is resting."
+
+ cdef class Norwegian(Parrot):
+
+   cdef void describe(self):
+    Parrot.describe(self)
+     print "Lovely plumage!"
+
+
+ cdef Parrot p1, p2
+ p1 = Parrot()
+ p2 = Norwegian()
+print "p1:"
+ p1.describe()
+print "p2:"
+ p2.describe()

+
p1:
+This parrot is resting.
+p2:
+
This parrot is resting.
+
Lovely plumage!
+
+
+ The above example also illustrates that a C method can call an inherited +C method using the usual Python technique, i.e.
+
Parrot.describe(self)
+
+

Forward-declaring extension types

+ Extension types can be forward-declared, like struct and union types. This + will be necessary if you have two extension types that need to refer to +each other, e.g. +
cdef class Shrubbery # forward declaration

cdef class Shrubber:
+     cdef Shrubbery work_in_progress

+

cdef class Shrubbery:
+     cdef Shrubber creator

+
+ If you are forward-declaring an exension type that has a base class, you +must specify the base class in both the forward declaration and its subsequent +definition, for example,
+
cdef class A(B)
+
+...
+
+cdef class A(B):
+    # attributes and methods

+
+

Making extension types weak-referenceable

By +default, extension types do not support having weak references made to +them. You can enable weak referencing by declaring a C attribute of +type object called __weakref__. For example,
+
+
cdef class ExplodingAnimal:
+    """This animal will self-destruct when it is
+       no longer strongly referenced."""
+   
+    cdef object __weakref__
+
+
+

Public and external extension types

+ + Extension types can be declared extern or public. An extern extension type declaration makes +an extension type defined in external C code available to a Pyrex module. +A public extension type declaration makes an extension type defined in a Pyrex module available to external C +code. +

External extension types

+ An extern extension type allows you to gain access to the internals + of Python objects defined in the Python core or in a non-Pyrex extension +module. +
NOTE: In Pyrex versions before 0.8, extern extension + types were also used to reference extension types defined in another Pyrex + module. While you can still do that, Pyrex 0.8 and later provides a better + mechanism for this. See Sharing C Declarations Between + Pyrex Modules.
+ Here is an example which will let you get at the C-level members of the +built-in complex object. +
cdef extern from "complexobject.h":

    struct Py_complex:
+         double real
+         double imag

+

    ctypedef class __builtin__.complex [object PyComplexObject]: +
+         cdef Py_complex cval +

+

# A function which uses the above type
+ def spam(complex c):
+     print "Real:", c.cval.real
+     print "Imag:", c.cval.imag

+
+ Some important things to note are: +
    +
  1. In this example, ctypedef class has been used. This is because, + in the Python header files, the PyComplexObject struct is declared + with
    +
    +
    ctypedef struct {
    +     ...
    + } PyComplexObject;
    +
    +
    +
  2. As well as the name of the extension type, the module in which +its type object can be found is also specified. See the implicit importing section below. 
    +
    +
  3. +
  4. When declaring an external extension type, you don't declare +any methods. Declaration of methods is not required in order to call them, +because the calls are Python method calls. Also, as with structs and unions, +if your extension class declaration is inside a cdef extern from block, + you only need to declare those C members which you wish to access.
  5. +
+

Implicit importing

+
Backwards Incompatibility Note: +You will have to update any pre-0.8 Pyrex modules you have which use extern +extension types. I apologise for this, but for complicated reasons it proved + to be too difficult to continue supporting the old way of doing these while + introducing the new features that I wanted.
+ Pyrex 0.8 and later requires you to include a module name in an extern +extension class declaration, for example, +
cdef extern class MyModule.Spam:
+     ...
+ The type object will be implicitly imported from the specified module and + bound to the corresponding name in this module. In other words, in this +example an implicit +
    +
    from MyModule import Spam
    +
+ statement will be executed at module load time. +

The module name can be a dotted name to refer to a module inside a package + hierarchy, for example,

+
cdef extern class My.Nested.Package.Spam:
+     ...
+ You can also specify an alternative name under which to import the type +using an as clause, for example, +
    + cdef extern class My.Nested.Package.Spam as Yummy:
    +    ...
+ which corresponds to the implicit import statement +
    +
    from My.Nested.Package import Spam as Yummy
    +
+

Type names vs. constructor names

+ Inside a Pyrex module, the name of an extension type serves two distinct + purposes. When used in an expression, it refers to a module-level global +variable holding the type's constructor (i.e. its type-object). However, +it can also be used as a C type name to declare variables, arguments and +return values of that type. +

When you declare

+
cdef extern class MyModule.Spam:
+     ...
+ the name Spam serves both these roles. There may be other names + by which you can refer to the constructor, but only Spam can be +used as a type name. For example, if you were to explicity import MyModule, + you could use MyModule.Spam() to create a Spam instance, but you + wouldn't be able to use MyModule.Spam as a type name. +

When an as clause is used, the name specified in the as +clause also takes over both roles. So if you declare

+
cdef extern class MyModule.Spam as Yummy:
+     ...
+ then Yummy becomes both the type name and a name for the constructor. + Again, there are other ways that you could get hold of the constructor, +but only Yummy is usable as a type name. +

Public extension types

+ An extension type can be declared public, in which case a .h +file is generated containing declarations for its object struct and type +object. By including the .h file in external C code that you write, +that code can access the attributes of the extension type. +

Name specification clause

+ The part of the class declaration in square brackets is a special feature + only available for extern or public extension types. The full +form of this clause is +
[object object_struct_name, type type_object_name ]
+ where object_struct_name is the name to assume for the type's C +struct, and type_object_name is the name to assume for the type's +statically declared type object. (The object and type clauses can be written +in either order.) +

If the extension type declaration is inside a cdef extern from +block, the object clause is required, because Pyrex must be able to +generate code that is compatible with the declarations in the header file. +Otherwise, for extern extension types, the object clause is +optional.

+

For public extension types, the object and type clauses +are both required, because Pyrex must be able to generate code that is compatible +with external C code.

+

+

+ Back to the Language Overview
+  
+
+ \ No newline at end of file diff --git a/Doc/index.html b/Doc/index.html new file mode 100644 index 000000000..6a262e3bc --- /dev/null +++ b/Doc/index.html @@ -0,0 +1 @@ + Pyrex - Front Page  
Pyrex A smooth blend of the finest Python 
with the unsurpassed power 
of raw C.
Welcome to Pyrex, a language for writing Python extension modules. Pyrex makes creating an extension module is almost as easy as creating a Python module! To find out more, consult one of the edifying documents below.

Documentation

About Pyrex

Read this to find out what Pyrex is all about and what it can do for you.

Language Overview

A description of all the features of the Pyrex language. This is the closest thing to a reference manual in existence yet.

FAQ

Want to know how to do something in Pyrex? Check here first.

Other Resources

Michael's Quick Guide to Pyrex

This tutorial-style presentation will take you through the steps of creating some Pyrex modules to wrap existing C libraries. Contributed by Michael JasonSmith.

Mail to the Author

If you have a question that's not answered by anything here, you're not sure about something, or you have a bug to report or a suggestion to make, or anything at all to say about Pyrex, feel free to email me: greg@cosc.canterbury.ac.nz
\ No newline at end of file diff --git a/Doc/overview.html b/Doc/overview.html new file mode 100644 index 000000000..d50eeb6e3 --- /dev/null +++ b/Doc/overview.html @@ -0,0 +1,960 @@ + + + + + + + + Pyrex Language Overview + + + +


Overview of the Pyrex Language 

+ + This document informally describes the extensions to the Python language + made by Pyrex. Some day there will be a reference manual covering everything + in more detail.
+ +   +

Contents

+ + + + + +


Basics +

+ + This section describes the basic features of the Pyrex language. The facilities + covered in this section allow you to create Python-callable functions that + manipulate C data structures and convert between Python and C data types. + Later sections will cover facilities for wrapping external C code, creating new Python types and cooperation between Pyrex modules. +

Python functions vs. C functions

+ + There are two kinds of function definition in Pyrex: +

Python functions are defined using the def statement, as + in Python. They take Python objects as parameters and return Python objects. +

+ + +

C functions are defined using the new cdef statement. They + take either Python objects or C values as parameters, and can return either + Python objects or C values.

+ + +

Within a Pyrex module, Python functions and C functions can call each other +freely, but only Python functions can be called from outside the module by +interpreted Python code. So, any functions that you want to "export" from + your Pyrex module must be declared as Python functions using def.

+ + +

Parameters of either type of function can be declared to have C data types, + using normal C declaration syntax. For example,

+ + +
def spam(int i, char *s):
    ...
+
cdef int eggs(unsigned long l, float f):
    ...
+
+ + When a parameter of a Python function is declared to have a C data type, + it is passed in as a Python object and automatically converted to a C value, + if possible. Automatic conversion is currently only possible for numeric +types and string types; attempting to use any other type for the parameter +of a Python function will result in a compile-time error. +

C functions, on the other hand, can have parameters of any type, since + they're passed in directly using a normal C function call.

+ + +

Python objects as parameters and return values

+ + If no type is specified for a parameter or return value, it is assumed + to be a Python object. (Note that this is different from the C convention, + where it would default to int.) For example, the following defines + a C function that takes two Python objects as parameters and returns a Python + object: +
cdef spamobjs(x, y):
    ...
+
+ + Reference counting for these objects is performed automatically according + to the standard Python/C API rules (i.e. borrowed references are taken as + parameters and a new reference is returned). +

The name object can also be used to explicitly declare something + as a Python object. This can be useful if the name being declared would otherwise +be taken as the name of a type, for example,

+ + +
cdef ftang(object int):
    ...
+
+ + declares a parameter called int which is a Python object. You +can also use object as the explicit return type of a function, e.g. + +
cdef object ftang(object int):
    ...
+
+ + In the interests of clarity, it is probably a good idea to always be explicit + about object parameters in C functions. +

C variable and type definitions

+ + The cdef statement is also used to declare C variables, either +local or module-level: +
cdef int i, j, k
cdef float f, g[42], *h
+
+ + and C struct, union or enum types: +
cdef struct Grail:
    int age
    float volume
+
cdef union Food:
    char *spam
    float *eggs
+
cdef enum CheeseType:
    cheddar, edam, 
    camembert
+
cdef enum CheeseState:
    hard = 1
    soft = 2
    runny = 3
+
+ + There is currently no special syntax for defining a constant, but you +can use an anonymous enum declaration for this purpose, for example, +
cdef enum:
+     tons_of_spam = 3
+ + Note that the words struct, union and enum are used only when defining a type, not when referring to it. For example, to declare a variable pointing + to a Grail you would write +
cdef Grail *gp
+
+ + and not +
cdef struct Grail *gp # WRONG
+
+ + There is also a ctypedef statement for giving names to types, e.g. + +
ctypedef unsigned long ULong
+
ctypedef int *IntPtr
+ +

Automatic type conversions

+ +In most situations, automatic conversions will be performed for the +basic numeric and string types when a Python object is used in a +context requiring a C value, or vice versa. The following table +summarises the conversion possibilities.
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
C types
+
From Python types
+
To Python types
+
[unsigned] char
+[unsigned] short
+ int, long
int, long
+
int
+
unsigned int
+unsigned long
+ [unsigned] long long
+ +
int, long
+
+ +
long
+
+ +
float, double, long double
+
int, long, float
+
float
+
char *
+
str
+
str
+
+ +
+ +

Caveats when using a Python string in a C context

+ +You need to be careful when using a Python string in a context expecting a char *. +In this situation, a pointer to the contents of the Python string is +used, which is only valid as long as the Python string exists. So you +need to make sure that a reference to the original Python string is +held for as long as the C string is needed. If you can't guarantee that +the Python string will live long enough, you will need to copy the C +string.
+ +
+ +Pyrex detects and prevents some mistakes of this kind. For instance, if you attempt something like
+ +
cdef char *s
s = pystring1 + pystring2
+ +then Pyrex will produce the error message "Obtaining char * from temporary Python value". +The reason is that concatenating the two Python strings produces a new +Python string object that is referenced only by a temporary internal +variable that Pyrex generates. As soon as the statement has finished, +the temporary variable will be decrefed and the Python string +deallocated, leaving s dangling. Since this code could not possibly work, Pyrex refuses to compile it.
+ +
+ +The solution is to assign the result of the concatenation to a Python variable, and then obtain the char * from that, i.e.
+ +
cdef char *s
p = pystring1 + pystring2
s = p
+ +It is then your responsibility to hold the reference p for as long as necessary.
+ +
+ +Keep in mind that the rules used to detect such errors are only +heuristics. Sometimes Pyrex will complain unnecessarily, and sometimes +it will fail to detect a problem that exists. Ultimately, you need to +understand the issue and be careful what you do.
+ +
+
+ + + +

Scope rules

+ + Pyrex determines whether a variable belongs to a local scope, the module + scope, or the built-in scope completely statically. As with Python, + assigning to a variable which is not otherwise declared implicitly declares + it to be a Python variable residing in the scope where it is assigned. Unlike + Python, however, a name which is referred to but not declared or assigned + is assumed to reside in the builtin scope, not the module scope. +Names added to the module dictionary at run time will not shadow such names. + +

You can use a global statement at the module level to explicitly + declare a name to be a module-level name when there would otherwise not be +any indication of this, for example,

+ + +
global __name__
+ print __name__
+ + Without the global statement, the above would print the name of +the builtins module.
+ +
+ + Note: A consequence of these rules is that the module-level scope behaves + the same way as a Python local scope if you refer to a variable before assigning + to it. In particular, tricks such as the following will not work +in Pyrex:
+ + +
try:
  x = True
except NameError:
  True = 1
+
+ + because, due to the assignment, the True will always be looked up in the + module-level scope. You would have to do something like this instead:
+ + +
import __builtin__
try:
True = __builtin__.True
except AttributeError:
True = 1
+
+ + +
+

Statements and expressions

+ + Control structures and expressions follow Python syntax for the most part. + When applied to Python objects, they have the same semantics as in Python + (unless otherwise noted). Most of the Python operators can also be applied + to C values, with the obvious semantics. +

If Python objects and C values are mixed in an expression, conversions + are performed automatically between Python objects and C numeric or string + types.

+ + +

Reference counts are maintained automatically for all Python objects, and +all Python operations are automatically checked for errors, with appropriate + action taken.

+ + +

Differences between C and Pyrex +expressions

+There +are some differences in syntax and semantics between C expressions and +Pyrex expressions, particularly in the area of C constructs which have +no direct equivalent in Python.
+ + + + +

Integer for-loops

+ + You should be aware that a for-loop such as +
for i in range(n):
+     ...
+ + won't be very fast, even if i and n are declared as +C integers, because range is a Python function. For iterating over +ranges of integers, Pyrex has another form of for-loop: +
for i from 0 <= i < n:
+     ...
+ + If the loop variable and the lower and upper bounds are all C integers, +this form of loop will be much faster, because Pyrex will translate it into +pure C code. +

Some things to note about the for-from loop:

+ + + + + Like other Python looping statements, break and continue may be used in the body, and the loop may have an else clause. + +


+ + +

Error return values

+ + If you don't do anything special, a function declared with cdef that does not return a Python object has no way of reporting Python exceptions + to its caller. If an exception is detected in such a function, a warning +message is printed and the exception is ignored. +

If you want a C function that does not return a Python object to be able + to propagate exceptions to its caller, you need to declare an exception + value for it. Here is an example:

+ + +
cdef int spam() except -1:
+     ...
+ + With this declaration, whenever an exception occurs inside spam, + it will immediately return with the value -1. Furthermore, whenever + a call to spam returns -1, an exception will be assumed + to have occurred and will be propagated. +

When you declare an exception value for a function, you should never explicitly + return that value. If all possible return values are legal and you can't +reserve one entirely for signalling errors, you can use an alternative form +of exception value declaration:

+ + +
cdef int spam() except? -1:
+     ...
+ + The "?" indicates that the value -1 only indicates a possible error. In this case, Pyrex generates a call to PyErr_Occurredif the +exception value is returned, to make sure it really is an error. +

There is also a third form of exception value declaration:

+ + +
cdef int spam() except *:
+     ...
+ + This form causes Pyrex to generate a call to PyErr_Occurred after + every call to spam, regardless of what value it returns. If you have + a function returning void that needs to propagate errors, you will + have to use this form, since there isn't any return value to test. +

Some things to note:

+ + + + + +

Checking return values of non-Pyrex + functions

+ + It's important to understand that the except clause does not cause an error to be raised when the specified value is returned. For +example, you can't write something like +
cdef extern FILE *fopen(char *filename, char *mode) except NULL # WRONG!
+
+ + and expect an exception to be automatically raised if a call to fopen +returns NULL. The except clause doesn't work that way; its only purpose +is for propagating exceptions that have already been raised, either +by a Pyrex function or a C function that calls Python/C API routines. To +get an exception from a non-Python-aware function such as fopen, you will +have to check the return value and raise it yourself, for example, +
cdef FILE *p
p = fopen("spam.txt", "r")
if p == NULL:
    raise SpamError("Couldn't open the spam file")
+
+ + +


+ + +

The include statement

+ + For convenience, a large Pyrex module can be split up into a number of +files which are put together using the include statement, for example + +
include "spamstuff.pxi"
+
+ + The contents of the named file are textually included at that point. The + included file can contain any complete top-level Pyrex statements, including + other include statements. The include statement itself can +only appear at the top level of a file. +

The include statement can also be used in conjunction with public declarations to make C functions and + variables defined in one Pyrex module accessible to another. However, note + that some of these uses have been superseded by the facilities described +in Sharing Declarations Between Pyrex Modules, +and it is expected that use of the include statement for this purpose +will be phased out altogether in future versions.

+ + +


Interfacing with External + C Code +

+ + One of the main uses of Pyrex is wrapping existing libraries of C code. +This is achieved by using external declarations to declare the C functions and variables from the library that you want to + use. +

You can also use public declarations to make + C functions and variables defined in a Pyrex module available to external + C code. The need for this is expected to be less frequent, but you might +want to do it, for example, if you are embedding Python in another application + as a scripting language. Just as a Pyrex module can be used as a bridge to +allow Python code to call C code, it can also be used to allow C code to +call Python code.

+ + +

External declarations

+ + By default, C functions and variables declared at the module level are +local to the module (i.e. they have the C static storage class). They +can also be declared extern to specify that they are defined elsewhere, + for example: +
cdef extern int spam_counter
+
cdef extern void order_spam(int tons)
+
+ + +
+ + +

Referencing C header files

+ + When you use an extern definition on its own as in the examples above, +Pyrex includes a declaration for it in the generated C file. This can cause +problems if the declaration doesn't exactly match the declaration that will +be seen by other C code. If you're wrapping an existing C library, for example, +it's important that the generated C code is compiled with exactly the same +declarations as the rest of the library. +

To achieve this, you can tell Pyrex that the declarations are to be found + in a C header file, like this:

+ + +
cdef extern from "spam.h":
+
    int spam_counter
+
    void order_spam(int tons)
+
+ + The cdef extern from clause does three things: +
    + +
  1. It directs Pyrex to place a #include statement for the named + header file in the generated C code.
    +
  2. +  
  3. It prevents Pyrex from generating any C code for the declarations + found in the associated block.
    +
  4. +  
  5. It treats all declarations within the block as though they +started with cdef extern.
  6. + +
+ + It's important to understand that Pyrex does not itself read the +C header file, so you still need to provide Pyrex versions of any declarations + from it that you use. However, the Pyrex declarations don't always have to +exactly match the C ones, and in some cases they shouldn't or can't. In particular: + +
    + +
  1. Don't use const. Pyrex doesn't know anything about const, +so just leave it out. Most of the time this shouldn't cause any problem, +although on rare occasions you might have to use a cast. 1
    +
  2. +  
  3. Leave out any platform-specific extensions to C declarations + such as __declspec().
    +
  4. +  
  5. If the header file declares a big struct and you only want +to use a few members, you only need to declare the members you're interested +in. Leaving the rest out doesn't do any harm, because the C compiler will +use the full definition from the header file.
    +
    + In some cases, you might not need any of the struct's members, in +which case you can just put pass in the body of the struct declaration, +e.g.
    +
    +     cdef extern from "foo.h":
    +         struct spam:
    +             pass

    +
    +Note that you can only do this inside a cdef extern from block; struct +declarations anywhere else must be non-empty.
    +
    +
  6. +
  7. If the header file uses typedef names such as size_t to refer +to platform-dependent flavours of numeric types, you will need a corresponding + ctypedef statement, but you don't need to match the type exactly, + just use something of the right general kind (int, float, etc). For example,
  8. +
      +
      ctypedef int size_t
      +
    + will work okay whatever the actual size of a size_t is (provided the header + file defines it correctly).
    +  
  9. If the header file uses macros to define constants, translate + them into a dummy enum declaration.
    +
  10. +  
  11. If the header file defines a function using a macro, declare + it as though it were an ordinary function, with appropriate argument and +result types.
  12. + +
+ + A few more tricks and tips: + + + + + + + + + +
cdef extern from *:
+     ...
+
+ + +

Styles of struct, union and enum declaration

+ + There are two main ways that structs, unions and enums can be declared +in C header files: using a tag name, or using a typedef. There are also some + variations based on various combinations of these. +

It's important to make the Pyrex declarations match the style used in the +header file, so that Pyrex can emit the right sort of references to the type +in the code it generates. To make this possible, Pyrex provides two different +syntaxes for declaring a struct, union or enum type. The style introduced +above corresponds to the use of a tag name. To get the other style, you prefix +the declaration with ctypedef, as illustrated below.

+ + +

The following table shows the various possible styles that can be found + in a header file, and the corresponding Pyrex declaration that you should + put in the cdef exern from block. Struct declarations are used as +an example; the same applies equally to union and enum declarations.

+ + +

Note that in all the cases below, you refer to the type in Pyrex code simply +as Foo, not struct Foo. +
+   + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
 C codePossibilities for corresponding +Pyrex codeComments
1struct Foo {
+   ...
+ };
cdef struct Foo:
+   ...
Pyrex will refer to the type as struct Foo in the generated + C code.
2typedef struct {
+   ...
+ } Foo;
ctypedef struct Foo:
+   ...
Pyrex will refer to the type simply as Foo +in the generated C code.
3typedef struct +foo {
+   ...
+ } Foo;
cdef struct foo:
+   ...
+ ctypedef foo Foo #optional
If the C header uses both a tag and a typedef + with different names, you can use either form of declaration in Pyrex + (although if you need to forward reference the type, you'll have to use +the first form).
ctypedef struct Foo:
+   ...
4typedef struct Foo {
+   ...
+ } Foo;
cdef struct Foo:
+   ...
If the header uses the same name for the tag and the typedef, + you won't be able to include a ctypedef for it -- but then, it's not +necessary.
+

+ + +

Accessing Python/C API routines

+ + One particular use of the cdef extern from statement is for gaining + access to routines in the Python/C API. For example, +
cdef extern from "Python.h":
+
    object PyString_FromStringAndSize(char *s, int len)
+
+ + will allow you to create Python strings containing null bytes. +

+ + +
+

Resolving naming conflicts - C name specifications

+ + Each Pyrex module has a single module-level namespace for both Python +and C names. This can be inconvenient if you want to wrap some external +C functions and provide the Python user with Python functions of the same +names. +

Pyrex 0.8 provides a couple of different ways of solving this problem. + The best way, especially if you have many C functions to wrap, is probably + to put the extern C function declarations into a different namespace using + the facilities described in the section on sharing + declarations between Pyrex modules.

+ + +

The other way is to use a c name specification to give different + Pyrex and C names to the C function. Suppose, for example, that you want +to wrap an external function called eject_tomato. If you declare +it as

+ + +
cdef extern void c_eject_tomato "eject_tomato" (float speed)
+
+ + then its name inside the Pyrex module will be c_eject_tomato, +whereas its name in C will be eject_tomato. You can then wrap it +with +
def eject_tomato(speed):
  c_eject_tomato(speed)
+
+ + so that users of your module can refer to it as eject_tomato. + +

Another use for this feature is referring to external names that happen + to be Pyrex keywords. For example, if you want to call an external function + called print, you can rename it to something else in your Pyrex +module.

+ + +

As well as functions, C names can be specified for variables, structs, + unions, enums, struct and union members, and enum values. For example,

+ + +
cdef extern int one "ein", two "zwei"
cdef extern float three "drei"

cdef struct spam "SPAM":
  int i "eye"
+ cdef enum surprise "inquisition":
+   first "alpha"
+   second "beta" = 3
+ + +
+

Public Declarations

+ + You can make C variables and functions defined in a Pyrex module accessible + to external C code (or another Pyrex module) using the public keyword, as follows: +
cdef public int spam # public variable declaration

cdef public void grail(int num_nuns): # public function declaration
+     ...

+
+ + If there are any public declarations in a Pyrex module, a .h file is generated containing equivalent C declarations for inclusion in other + C code. +

Pyrex also generates a .pxi file containing Pyrex versions of the + declarations for inclusion in another Pyrex module using the include statement. If you use this, you + will need to arrange for the module using the declarations to be linked +against the module defining them, and for both modules to be available to +the dynamic linker at run time. I haven't tested this, so I can't say how +well it will work on the various platforms.

+ + +
NOTE: If all you want to export is an extension type, there is + now a better way -- see Sharing Declarations Between + Pyrex Modules.
+ + +


Extension Types +

+ + One of the most powerful features of Pyrex is the ability to easily create + new built-in Python types, called extension types. This is a major + topic in itself, so there is a  separate + page devoted to it. +


Sharing Declarations Between Pyrex Modules +

+ + Pyrex 0.8 introduces a substantial new set of facilities allowing a Pyrex + module to easily import and use C declarations and extension types from another +Pyrex module. You can now create a set of co-operating Pyrex modules just +as easily as you can create a set of co-operating Python modules. There is +a separate page devoted to this topic. +


Limitations +

+ + +

Unsupported Python features

+ + Pyrex is not quite a full superset of Python. The following restrictions + apply: +
  • Function definitions (whether using def or cdef) + cannot be nested within other function definitions.
    +
  • +  
  • Class definitions can only appear at the top level of a module, + not inside a function.
    +
  • +  
  • The import * form of import is not allowed anywhere + (other forms of the import statement are fine, though).
    +
  • +  
  • Generators cannot be defined in Pyrex.
    +
    +
  • +
  • The globals() and locals() functions cannot be +used.
  • +
    + + The above restrictions will most likely remain, since removing them would + be difficult and they're not really needed for Pyrex's intended applications. + +

    There are also some temporary limitations, which may eventually be lifted, including: +

    + + +
  • Class and function definitions cannot be placed inside +control structures.
    +
  • +  
  • In-place arithmetic operators (+=, etc) are not yet supported.
    +
  • +  
  • List comprehensions are not yet supported.
    +
  • +  
  • There is no support for Unicode.
    +
  • +  
  • Special methods of extension types cannot have functioning +docstrings.
    +
    +
  • +
  • The use of string literals as comments is not recommended at present, + because Pyrex doesn't optimize them away, and won't even accept them in +places where executable statements are not allowed.
  • +
    + +

    Semantic differences between Python + and Pyrex

    + + +

    Behaviour of class scopes

    + + In Python, referring to a method of a class inside the class definition, + i.e. while the class is being defined, yields a plain function object, but + in Pyrex it yields an unbound method2. A consequence of this is that the +usual idiom for using the classmethod and staticmethod functions, e.g. +
    class Spam:
    +
      def method(cls):
        ...
    +
      method = classmethod(method)
    +
    + + will not work in Pyrex. This can be worked around by defining the function + outside the class, and then assigning the result of classmethod or + staticmethod inside the class, i.e. +
    def Spam_method(cls):
      ...
    +
    class Spam:
    +
      method = classmethod(Spam_method)
    +
    + + +


    Footnotes

    + + 1. A problem with const could arise if you have +something like +
    cdef extern from "grail.h":
      char *nun
    +
    + + where grail.h actually contains +
    extern const char *nun;
    +
    + + and you do +
    cdef void languissement(char *s):
      #something that doesn't change s
    +
    ...
    +
    languissement(nun)
    +
    + + which will cause the C compiler to complain. You can work around it by +casting away the constness: +
    languissement(<char *>nun)
    +
    + + +
    2. The reason for the different behaviour +of class scopes is that Pyrex-defined Python functions are PyCFunction objects, +not PyFunction objects, and are not recognised by the machinery that creates +a bound or unbound method when a function is extracted from a class. To get +around this, Pyrex wraps each method in an unbound method object itself before +storing it in the class's dictionary.
    + +  
    + +
    + + \ No newline at end of file diff --git a/Doc/primes.c b/Doc/primes.c new file mode 100644 index 000000000..9a88b84cb --- /dev/null +++ b/Doc/primes.c @@ -0,0 +1 @@ +#include "Python.h" static PyObject *__Pyx_UnpackItem(PyObject *, int); static int __Pyx_EndUnpack(PyObject *, int); static int __Pyx_PrintItem(PyObject *); static int __Pyx_PrintNewline(void); static void __Pyx_ReRaise(void); static void __Pyx_RaiseWithTraceback(PyObject *, PyObject *, PyObject *); static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list); static PyObject *__Pyx_GetExcValue(void); static PyObject *__Pyx_GetName(PyObject *dict, char *name); static PyObject *__pyx_m; static PyObject *__pyx_d; static PyObject *__pyx_b; PyObject *__pyx_f_primes(PyObject *__pyx_self, PyObject *__pyx_args); /*proto*/ PyObject *__pyx_f_primes(PyObject *__pyx_self, PyObject *__pyx_args) { int __pyx_v_kmax; int __pyx_v_n; int __pyx_v_k; int __pyx_v_i; int (__pyx_v_p[1000]); PyObject *__pyx_v_result; PyObject *__pyx_r; PyObject *__pyx_1 = 0; int __pyx_2; int __pyx_3; int __pyx_4; PyObject *__pyx_5 = 0; PyObject *__pyx_6 = 0; if (!PyArg_ParseTuple(__pyx_args, "i", &__pyx_v_kmax)) return 0; __pyx_v_result = Py_None; Py_INCREF(__pyx_v_result); /* "ProjectsA:Python:Pyrex:Demos:primes.pyx":2 */ /* "ProjectsA:Python:Pyrex:Demos:primes.pyx":3 */ /* "ProjectsA:Python:Pyrex:Demos:primes.pyx":4 */ __pyx_1 = PyList_New(0); if (!__pyx_1) goto __pyx_L1; Py_DECREF(__pyx_v_result); __pyx_v_result = __pyx_1; __pyx_1 = 0; /* "ProjectsA:Python:Pyrex:Demos:primes.pyx":5 */ __pyx_2 = (__pyx_v_kmax > 1000); if (__pyx_2) { /* "ProjectsA:Python:Pyrex:Demos:primes.pyx":6 */ __pyx_v_kmax = 1000; goto __pyx_L2; } __pyx_L2:; /* "ProjectsA:Python:Pyrex:Demos:primes.pyx":7 */ __pyx_v_k = 0; /* "ProjectsA:Python:Pyrex:Demos:primes.pyx":8 */ __pyx_v_n = 2; /* "ProjectsA:Python:Pyrex:Demos:primes.pyx":9 */ while (1) { __pyx_L3:; __pyx_2 = (__pyx_v_k < __pyx_v_kmax); if (!__pyx_2) break; /* "ProjectsA:Python:Pyrex:Demos:primes.pyx":10 */ __pyx_v_i = 0; /* "ProjectsA:Python:Pyrex:Demos:primes.pyx":11 */ while (1) { __pyx_L5:; if (__pyx_3 = (__pyx_v_i < __pyx_v_k)) { __pyx_3 = ((__pyx_v_n % (__pyx_v_p[__pyx_v_i])) != 0); } if (!__pyx_3) break; /* "ProjectsA:Python:Pyrex:Demos:primes.pyx":12 */ __pyx_v_i = (__pyx_v_i + 1); } __pyx_L6:; /* "ProjectsA:Python:Pyrex:Demos:primes.pyx":13 */ __pyx_4 = (__pyx_v_i == __pyx_v_k); if (__pyx_4) { /* "ProjectsA:Python:Pyrex:Demos:primes.pyx":14 */ (__pyx_v_p[__pyx_v_k]) = __pyx_v_n; /* "ProjectsA:Python:Pyrex:Demos:primes.pyx":15 */ __pyx_v_k = (__pyx_v_k + 1); /* "ProjectsA:Python:Pyrex:Demos:primes.pyx":16 */ __pyx_1 = PyObject_GetAttrString(__pyx_v_result, "append"); if (!__pyx_1) goto __pyx_L1; __pyx_5 = PyInt_FromLong(__pyx_v_n); if (!__pyx_5) goto __pyx_L1; __pyx_6 = PyTuple_New(1); if (!__pyx_6) goto __pyx_L1; PyTuple_SET_ITEM(__pyx_6, 0, __pyx_5); __pyx_5 = 0; __pyx_5 = PyObject_CallObject(__pyx_1, __pyx_6); if (!__pyx_5) goto __pyx_L1; Py_DECREF(__pyx_6); __pyx_6 = 0; Py_DECREF(__pyx_5); __pyx_5 = 0; goto __pyx_L7; } __pyx_L7:; /* "ProjectsA:Python:Pyrex:Demos:primes.pyx":17 */ __pyx_v_n = (__pyx_v_n + 1); } __pyx_L4:; /* "ProjectsA:Python:Pyrex:Demos:primes.pyx":18 */ Py_INCREF(__pyx_v_result); __pyx_r = __pyx_v_result; goto __pyx_L0; __pyx_r = Py_None; Py_INCREF(__pyx_r); goto __pyx_L0; __pyx_L1:; Py_XDECREF(__pyx_1); Py_XDECREF(__pyx_5); Py_XDECREF(__pyx_6); __pyx_r = 0; __pyx_L0:; Py_DECREF(__pyx_v_result); return __pyx_r; } static struct PyMethodDef __pyx_methods[] = { {"primes", (PyCFunction)__pyx_f_primes, METH_VARARGS, 0}, {0, 0, 0, 0} }; void initprimes(void); /*proto*/ void initprimes(void) { __pyx_m = Py_InitModule4("primes", __pyx_methods, 0, 0, PYTHON_API_VERSION); __pyx_d = PyModule_GetDict(__pyx_m); __pyx_b = PyImport_AddModule("__builtin__"); PyDict_SetItemString(__pyx_d, "__builtins__", __pyx_b); } /* Runtime support code */ \ No newline at end of file diff --git a/Doc/sharing.html b/Doc/sharing.html new file mode 100644 index 000000000..a92856e2b --- /dev/null +++ b/Doc/sharing.html @@ -0,0 +1,201 @@ + + + + Sharing Declarations Between Pyrex Modules + + +


    Sharing Declarations Between Pyrex Modules +

    + This section describes a new set of facilities introduced in Pyrex 0.8 +for making C declarations and extension types in one Pyrex module available +for use in another Pyrex module. These facilities are closely modelled on +the Python import mechanism, and can be thought of as a compile-time version +of it. +

    Contents

    + +

    Definition and Implementation files

    + A Pyrex module can be split into two parts: a definition file with + a .pxd suffix, containing C declarations that are to be available + to other Pyrex modules, and an implementation file with a .pyx +suffix, containing everything else. When a module wants to use something +declared in another module's definition file, it imports it using the cimport statement. +

    What a Definition File contains

    + A definition file can contain: + + It cannot currently contain any non-extern C function or variable declarations + (although this may be possible in a future version). +

    It cannot contain the implementations of any C or Python functions, or +any Python class definitions, or any executable statements.

    +
    NOTE: You don't need to (and shouldn't) declare anything in a +declaration file public in order to make it available to other Pyrex +modules; its mere presence in a definition file does that. You only need a +public declaration if you want to make something available to external C code.
    +

    What an Implementation File contains

    + An implementation file can contain any kind of Pyrex statement, although + there are some restrictions on the implementation part of an extension type +if the corresponding definition file also defines that type (see below). + +

    The cimport statement

    + The cimport statement is used in a definition or implementation +file to gain access to names declared in another definition file. Its syntax +exactly parallels that of the normal Python import statement: +
    cimport module [, module...]
    +
    from module cimport name +[as name] [, name [as name] + ...]
    + Here is an example. The file on the left is a definition file which exports + a C data type. The file on the right is an implementation file which imports + and uses it.
    +   + + + + + + + + + +
    dishes.pxdrestaurant.pyx
    cdef enum otherstuff:
    +     sausage, eggs, lettuce

    cdef struct spamdish:
    +     int oz_of_spam
    +     otherstuff filler

    +
    cimport dishes
    + from dishes cimport spamdish

    cdef void prepare(spamdish *d):
    +     d.oz_of_spam = 42
    +     d.filler = dishes.sausage

    +

    def serve():
    +     spamdish d
    +     prepare(&d)
    +     print "%d oz spam, filler no. %d" % \ +
    +          (d->oz_of_spam, + d->otherstuff)

    +
    +

    It is important to understand that the cimport statement can only +be used to import C data types, external C functions and variables, and extension +types. It cannot be used to import any Python objects, and (with one exception) +it doesn't imply any Python import at run time. If you want to refer to any +Python names from a module that you have cimported, you will have to include +a regular import statement for it as well.

    +

    The exception is that when you use cimport to import an extension + type, its type object is imported at run time and made available by the +name under which you imported it. Using cimport to import extension +types is covered in more detail below. +

    +

    Search paths for definition files

    + When you cimport a module called modulename, the Pyrex +compiler searches for a file called modulename.pxd along the search +path for include files, as specified by -I command line options. +

    Also, whenever you compile a file modulename.pyx, the corresponding + definition file modulename.pxd is first searched for along the +same path, and if found, it is processed before processing the .pyx +file.

    +

    Using cimport to resolve naming + conflicts

    + The cimport mechanism provides a clean and simple way to solve the problem + of wrapping external C functions with Python functions of the same name. +All you need to do is put the extern C declarations into a .pxd file for +an imaginary module, and cimport that module. You can then refer to the C +functions by qualifying them with the name of the module. Here's an example: +
    +   + + + + + + + + + +
    c_lunch.pxdlunch.pyx
    cdef extern from "lunch.h": +
    +     void eject_tomato(float)
    cimport c_lunch

    def eject_tomato(float speed):
    +     c_lunch.eject_tomato(speed)

    +
    +

    You don't need any c_lunch.pyx file, because the only things +defined in c_lunch.pxd are extern C entities. There won't be any +actual c_lunch module at run time, but that doesn't matter -- c_lunch +has done its job of providing an additional namespace at compile time.

    +

    Sharing Extension Types

    + An extension type declaration can also be split into two parts, one in +a definition file and the other in the corresponding implementation file. +
    +
    + The definition part of the extension type can only declare C attributes +and C methods, not Python methods, and it must declare all of that +type's C attributes and C methods.
    +
    + The implementation part must implement all of the C methods declared in +the definition part, and may not add any further C attributes. It may also +define Python methods. +

    Here is an example of a module which defines and exports an extension +type, and another module which uses it.
    +   + + + + + + + + + + + + + + + +
    Shrubbing.pxdShrubbing.pyx
    cdef class Shrubbery:
    +     cdef int width
    +     cdef int length
    cdef class Shrubbery:
    +     def __new__(self, int w, int l):
    +         self.width = w +
    +         self.length = l +

    def standard_shrubbery():
    +     return Shrubbery(3, 7)

    +
    Landscaping.pyx
    cimport Shrubbing +
    + import Shrubbing

    cdef Shrubbing.Shrubbery sh
    + sh = Shrubbing.standard_shrubbery()
    + print "Shrubbery size is %d x %d" % (sh.width, sh.height) +
    +  

    +
    +

    +

    Some things to note about this example:

    + +
    Back to the Language Overview +
    +
    + \ No newline at end of file diff --git a/Doc/special_methods.html b/Doc/special_methods.html new file mode 100644 index 000000000..b5ef495ee --- /dev/null +++ b/Doc/special_methods.html @@ -0,0 +1,598 @@ + + + + Special Methods of Extenstion Types + +


    Special Methods of Extension Types +

    + This page describes the special methods currently supported by Pyrex extension + types. A complete list of all the special methods appears in the table at +the bottom. Some of these methods behave differently from their Python counterparts +or have no direct Python counterparts, and require special mention. +

    Note: Everything said on this page applies only to extension +types, defined with the cdef class statement. It doesn't apply  +to classes defined with the Python class statement, where the normal + Python rules apply.

    +

    Declaration

    Special methods of extension types must be declared with def, not cdef.
    +

    Docstrings

    + + Currently, docstrings are not fully supported in special methods of extension + types. You can place a docstring in the source to serve as a comment, but + it won't show up in the corresponding __doc__ attribute at run time. (This + is a Python limitation -- there's nowhere in the PyTypeObject data structure + to put such docstrings.) +

    Initialisation methods: __new__ and __init__

    + There are two methods concerned with initialising the object. +

    The __new__ method is where you should perform basic C-level +initialisation of the object, including allocation of any C data structures +that your object will own. You need to be careful what you do in the __new__ +method, because the object may not yet be a valid Python object when it is +called. Therefore, you must not invoke any Python operations which might touch +the object; in particular, do not try to call any of its methods.

    +

    Unlike the corresponding method in Python, your __new__ method + is not responsible for creating the object. By the time your + __new__ method is called, memory has been allocated for the object +and any C attributes it has have been initialised to 0 or null. (Any Python +attributes have also been initialised to None, but you probably shouldn't +rely on that.) Your __new__ method is guaranteed to be called exactly +once.
    +
    +If your extension type has a base type, the __new__ method of the +base type is automatically called before your __new__ method +is called; you cannot explicitly call the inherited __new__ method. +If you need to pass a modified argument list to the base type, you will have +to do the relevant part of the initialisation in the __init__ method +instead (where the normal rules for calling inherited methods apply).
    +

    +

    Note that the first parameter of the __new__ method is the object +to be initialised, not the class of the object as it is in Python.

    +

    Any initialisation which cannot safely be done in the __new__ +method should be done in the __init__ method. By the time + __init__ is called, the object is a fully valid Python object and +all operations are safe. Under some circumstances it is possible for __init__ +to be called more than once or not to be called at all, so your other methods + should be designed to be robust in such situations.

    +

    Keep in mind that any arguments passed to the constructor will be passed + to the __new__ method as well as the __init__ method. +If you anticipate subclassing your extension type in Python, you may find +it useful to give the __new__ method * and ** arguments so that +it can accept and ignore extra arguments. Otherwise, any Python subclass +which has an __init__ with a different signature will have to override +__new__ as well as __init__, which the writer of a Python +class wouldn't expect to have to do.

    +

    Finalization method: __dealloc__

    + The counterpart to the __new__ method is the __dealloc__ +method, which should perform the inverse of the __new__ method. +Any C data structures that you allocated in your __new__ method +should be freed in your __dealloc__ method. +

    You need to be careful what you do in a __dealloc__ method. By +the time your __dealloc__ method is called, the object may already +have been partially destroyed and may not be in a valid state as far as Python +is concerned, so you should avoid invoking any Python operations which might +touch the object. In particular, don't call any other methods of the object +or do anything which might cause the object to be resurrected. It's best if +you stick to just deallocating C data.

    +

    You don't need to worry about deallocating Python attributes of your object, +because that will be done for you by Pyrex after your __dealloc__ +method returns.
    +
    + Note: There is no __del__ method for extension types. (Earlier +versions of the Pyrex documentation stated that there was, but this turned +out to be incorrect.)
    +

    +

    Arithmetic methods

    + Arithmetic operator methods, such as __add__, behave differently + from their Python counterparts. There are no separate "reversed" versions + of these methods (__radd__, etc.) Instead, if the first operand +cannot perform the operation, the same method of the second operand +is called, with the operands in the same order. +

    This means that you can't rely on the first parameter of these methods + being "self", and you should test the types of both operands before deciding + what to do. If you can't handle the combination of types you've been given, + you should return NotImplemented.

    +

    This also applies to the in-place arithmetic method __ipow__. + It doesn't apply to any of the other in-place methods (__iadd__, + etc.) which always take self as the first argument.

    +

    Rich comparisons

    + There are no separate methods for the individual rich comparison operations + (__eq__, __le__, etc.) Instead there is a single method + __richcmp__ which takes an integer indicating which operation is +to be performed, as follows: + +

    The __next__ method

    + Extension types wishing to implement the iterator interface should define + a method called __next__, not next. The Python + system will automatically supply a next method which calls your +__next__. Do NOT explicitly give your type a next method, +or bad things could happen (see note 3). +

    Special Method Table

    + This table lists all of the special methods together with their parameter + and return types. A parameter named self is of the type the method + belongs to. Other untyped parameters are generic Python objects. +

    You don't have to declare your method as taking these parameter types. + If you declare different types, conversions will be performed as necessary. +
    +   + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameParametersReturn typeDescription
    General
    __new__self, ... Basic initialisation (no direct Python equivalent)
    __init__self, ... Further initialisation
    __dealloc__self Basic deallocation (no direct Python equivalent)
    __cmp__x, yint3-way comparison
    __richcmp__x, y, int opobjectRich comparison (no direct Python equivalent)
    __str__selfobjectstr(self)
    __repr__selfobjectrepr(self)
    __hash__selfintHash function
    __call__self, ...objectself(...)
    __iter__selfobjectReturn iterator for sequence
    __getattr__self, nameobjectGet attribute
    __setattr__self, name, val Set attribute
    __delattr__self, name Delete attribute
    Arithmetic operators
    __add__x, yobjectbinary + operator
    __sub__x, yobjectbinary - operator
    __mul__x, yobject* operator
    __div__x, yobject/  operator for old-style division
    __floordiv__x, yobject//  operator
    __truediv__x, yobject/  operator for new-style division
    __mod__x, yobject% operator
    __divmod__x, yobjectcombined div and mod
    __pow__x, y, zobject** operator or pow(x, y, z)
    __neg__selfobjectunary - operator
    __pos__selfobjectunary + operator
    __abs__selfobjectabsolute value
    __nonzero__selfintconvert to boolean
    __invert__selfobject~ operator
    __lshift__x, yobject<< operator
    __rshift__x, yobject>> operator
    __and__x, yobject& operator
    __or__x, yobject| operator
    __xor__x, yobject^ operator
    Numeric conversions
    __int__selfobjectConvert to integer
    __long__selfobjectConvert to long integer
    __float__selfobjectConvert to float
    __oct__selfobjectConvert to octal
    __hex__selfobjectConvert to hexadecimal
    In-place arithmetic operators
    __iadd__self, xobject+= operator
    __isub__self, xobject-= operator
    __imul__self, xobject*= operator
    __idiv__self, xobject/= operator for old-style division
    __ifloordiv__self, xobject//= operator
    __itruediv__self, xobject/= operator for new-style division
    __imod__self, xobject%= operator
    __ipow__x, y, zobject**= operator
    __ilshift__self, xobject<<= operator
    __irshift__self, xobject>>= operator
    __iand__self, xobject&= operator
    __ior__self, xobject|= operator
    __ixor__self, xobject^= operator
    Sequences and mappings
    __len__selfintlen(self)
    __getitem__self, xobjectself[x]
    __setitem__self, x, y self[x] = y
    __delitem__self, x del self[x]
    __getslice__self, int i, int jobjectself[i:j]
    __setslice__self, int i, int j, x self[i:j] = x
    __delslice__self, int i, int j del self[i:j]
    __contains__self, xintx in self
    Iterators
    __next__selfobjectGet next item (called next in Python)
    Buffer interface  (no Python equivalents + - see note 1)
    __getreadbuffer__self, int i, void **p  
    __getwritebuffer__self, int i, void **p  
    __getsegcount__self, int *p  
    __getcharbuffer__self, int i, char **p  
    Descriptor objects  (no Python equivalents + - see note 2)
    __get__self, instance, classobjectGet value of attribute
    __set__self, instance, value Set value of attribute
    __delete__self, instance Delete attribute
    +

    +

    Note 1: The buffer interface is intended for use by C code and is not +directly accessible from Python. It is described in the Python/C API Reference +Manual under sections 6.6 +and 10.6. +

    +

    Note 2: Descriptor objects are part of the support mechanism for new-style + Python classes. See the discussion + of descriptors in the Python documentation. See also PEP 252, "Making Types Look +More Like Classes", and PEP 253, "Subtyping Built-In +Types".

    +

    Note 3: If your type defines a __new__ method, any method called + new that you define will be overwritten with the system-supplied + new at module import time.

    +
    +
    + \ No newline at end of file -- cgit v1.2.1