Pyrex - a Language for Writing Python Extension Modules

Development Version


Brief Description

Pyrex lets you write code that mixes Python and C data types any way you want, and compiles it into a C extension for Python.


Documentation

About Pyrex

Language Overview

Quick Guide to Pyrex

Contributed by Michael JasonSmith.

If the above link doesn't work, there is a copy here.

C++ Wrapping Tutorial

Contributed by David McNab.

Got a question? See the FAQ

Download

Pyrex compiler and documentation

Pyrex-0.9.8.5.tar.gz (242223 bytes, 2008-08-27)

Test suite and testing framework

Pyrex-Tests-0.9.8.5.tar.gz (423396 bytes, 2008-08-27)
Note: Currently the testing framework will only work out of the box on MacOS X or Linux, but should be adaptable to other unices with a little work.

Changes

Mercurial Repository and Updates

Previous Releases

Pyrex requires Python 2.3 or later, both to run the Pyrex compiler and to use the generated extension modules.

News


Version 0.9.8.5

  • Some minor bug fixes and improvements.

Version 0.9.8.4

  • Fixed a bug introduced in the last change to integer indexing.

Version 0.9.8.3

  • Compiling multiple source files at once should work as advertised now.
  • Assignment of a nogil function to a non-nogil function pointer is now allowed.
  • Plus some other minor bug fixes.

Version 0.9.8.2

  • A block of external functions can be declared nogil at once.
  • Some minor nogil-related bugs have been fixed.

Version 0.9.8.1

  • Base classes not needed in forward declarations or implementation parts of extension types.
  • Circular cimports of extension types simplified even further.
  • More thorough checking of operations performed without GIL.
  • Casting from a C pointer type to a Python type no longer generates an incref.

Version 0.9.8

There are new features in the following areas:
  • In-place operators (+= etc.)
  • Python-like package directory structure support
  • Built-in dependency tracking and compilation of changed sources
  • C methods can now be declared nogil
  • Circular cimports are easier to manage
For more information, see the CHANGES file and new sections of the documentation.

Version 0.9.7.2

Fix for the other half of the integer indexing problem.

Version 0.9.7.1

Oops! I goofed with the integer indexing optimisation. This version should fix it.

For-From Loop Undeprecated

I seem to have stirred a bit of a panic with the change to the integer for-loop syntax. I've decided to remove the deprecation warning and continue to support the old syntax for the foreseeable future. If you don't want to wait for the next release, you can apply the following patch to eliminate the warning.


I'll also try not to introduce any more incompatible changes without a very good reason and plenty of warning.

Version 0.9.7

Syntax Change

The integer for-loop syntax has been streamlined. Instead of for i in x < i < y it is now simply
for x < i < y
A deprecation warning will be given for uses of the old syntax, and it may be dropped altogether in a future version.

More efficient code

  • Most builtin constants and types are referenced directly
  • Certain methods of lists and dicts are called directly when their type is statically known
  • PySequence_* calls generated for integer indexes
Plus a handful of bug fixes.

Version 0.9.6.4

  • Several bugs fixed and some minor enhancements.

Version 0.9.6.3

  • C API now uses only a single name in the module namespace, instead of one per function
  • Added a getattr3() builtin (3-argument form)
  • cdef block for grouping multiple C declarations

Version 0.9.6.2

Another couple of minor fixes.

Version 0.9.6.1

Fixes some minor problems in the new release.

Version 0.9.6

New Features: C function cimporting and C API generation, fast builtin function calls, conditional compilation, GIL acquisition and release. See Changes in Pyrex 0.9.6 for more details.

Plus a large number of bug fixes and minor improvements.

Old News


Patches

WARNING: I have not reviewed or tested these patches - use them at your own risk.

Sam Rushing has submitted a patch to Pyrex 0.9.3.1 which adds some conditional compilation facilities.


Old Patches

Lenard Lindstrom contributed some patches to Pyrex 0.9.3.1 to improve the C++ compatibility of the generated code. Since 0.9.4 these should no longer be necessary.



Packages

These packages are maintained by others, so they may not be up to date with the latest version.

RedHat/Fedora

Pyrex-0.9.3.1-1.noarch.rpm
Pyrex-0.9.3.1-1.src.rpm
Contributed by Giovanni Bajo

Fedora Core 1
Fedora Core 2
Contributed by Jan Ondrej

Windows

Pyrex-0.9.3.1.win32.exe
Pyrex-0.9.3.1.zip
Contributed by Giovanni Bajo


Related Tools

pyrexdoc
Generate HTML documentation from a compiled Pyrex module, by David McNab

Pyrex Builder X
A mini-IDE for building Pyrex projects on MacOSX, by Nik Molnar.


Mailing List

You are invited to subscribe to the Pyrex Mailing List for all Pyrex-related discussions.

Thanks are due to Johannes Grødem of Copyleft Software for setting up and hosting this list at the request of Joakim Ziegler.

Discussion of Pyrex is also welcome on the Python newsgroup, comp.lang.python.

IRC Channel

irc.freenode.net #pyrex

I don't normally use IRC myself, but you may find someone there who's able to help you.

Author

Greg Ewing (greg.ewing@canterbury.ac.nz )

Example Pyrex modules


#
#  Calculate prime numbers
#

def primes(int kmax):

  cdef int n, k, i
  cdef int p[1000]
  result = []
  if kmax > 1000:
    kmax = 1000
  k = 0
  n = 2
  while k < kmax:
    i = 0
    while i < k and n % p[i] <> 0:
      i = i + 1
    if i == k:
      p[k] = n
      k = k + 1
      result.append(n)
    n = n + 1
  return result


#
#  Defining an extension type
#

cdef class Spam:

  cdef int amount

  def __new__(self):
    self.amount = 0

  def get_amount(self):
    return self.amount

  def set_amount(self, new_amount):
    self.amount = new_amount

  def describe(self):
    print self.amount, "tons of spam!"