From: William Stein Date: Sat, 28 Jul 2007 20:25:22 +0000 (-0700) Subject: Initial version X-Git-Tag: 0.9.6.14~70 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=c4fa206b9579bb739a1cf9ceb7980b47a9b9c5e2;p=cython.git Initial version --- c4fa206b9579bb739a1cf9ceb7980b47a9b9c5e2 diff --git a/CHANGES.txt b/CHANGES.txt new file mode 100644 index 00000000..7cc0c506 --- /dev/null +++ b/CHANGES.txt @@ -0,0 +1,1179 @@ +0.9.4.1 +------- + +Bug fixes: + + - Fixed indentation problem in Pyrex.Distutils.build_ext. + [Oliver Grisel ] + + +0.9.4 +----- + +Improvements: + + - All use of lvalue casts has been eliminated, for + compatibility with gcc4. + + - PyMODINIT_FUNC now used to declare the module init function. + + - Generated code should be compilable as either C or C++. + When compiling as C++, "extern C" is used where appropriate + to preserve linkage semantics. C++ functions still cannot + be called yet. + + - An extension type can be made weak-referenceable by + giving it a C attribute of type object called __weakref__. + + - Source files opened in universal newlines mode. + + - Support for public extension type C attributes of type + long long and unsigned long long added (but not tested). + [Sam Rushing ] + + - Distutils include directories now passed to Pyrex compiler. + [Konrad Hinsen ] + + - Integer constants with an "L" suffix are now allowed + and are converted to Python long integers. + [Rainer Deyke ] + + - A broken .c file is no longer left behind if there are + compilation errors. + + - Using the result of a Python indexing or attribute access + operation as a char * is no longer considered an error in + most cases, as the former behaviour proved to be more + annoying than helpful. + +Bug fixes: + + - Fixed problems with conversion from Python integers to + C unsigned longs. Now use PyInt_AsUnsignedLongMask and + PyInt_AsUnsignedLongLongMask instead of the PyLong_* + functions (which only work on Python longs). + [Wim Vree ] + + - C unsigned ints now converted to/from Python longs intead + of Python ints to avoid overflow problems. + [Heiko Wundram ] + + - Correct PyArg_ParseTuple format characters now used for + unsigned types. [Jeff Bowden ] + + - Nonzero return value from a base class tp_traverse call + is handled. + + - Taking sizeof an incomplete type caused a crash while + producing an error message. [Drew Perttula ] + + - If a module cimported itself, definitions of global variables + were generated twice. [Parzival Herzog ] + + - Distutils extension updated to handle changed signature of + swig_sources(). [David M. Cooke ] + + - Incorrect C code generated for a raw string containing a double + quote preceded by a backslash. [Thomas Drake ] + + - Declaration of public C function with an exception value written + to generated .pxi file without the except clause. + [Robby Dermody ] + + - __delitem__ method of an extension type with no __setitem__ + did not get called. [Richard Boulton ] + + - A spurious Py_INCREF was generated when a return statement + required a type test. [Jonathan Doda ] + + - Casting a value to a function pointer and then immediately + calling it generated a cast to a function instead of a cast + to a function pointer. [Simon Burton ] + + - Py_TPFLAGS_HAVE_GC was not being set on an extension type that + inherited from an external extension type that used GC but did + not itself have any PyObject* attributes. + [Michael Hordijk ] + + - A return statement inside a for statement leaked a reference + to the loop's iterator. + [Jürgen Kartnaller ] + + - Full module name now appears in __module__ attribute of classes + and extension types, provided a correct dotted name is used + for the .pyx file. [Giovanni Bajo ] + + - Public extension type with no C attributes produced an + invalid .pxi file. [Simon Burton ] + + - Using a dict constructor as the second operand of a boolean + expression crashed the Pyrex compiler. + [Stefan Behnel ] + + - A C declaration list ending with a comma resulted in invalid + C code being generated. [Alex Coventry ] + + - A raw string containing two consecutive backslashes produced + incorrect C code. [Helmut Jarausch ] + + - An error is reported if you attempt to declare a special + method of an extension type using 'cdef' instead of 'def'. + [Sam Rushing ] + +0.9.3 +----- + +Enhancements: + + - Types defined with a ctypedef in a 'cdef extern from' block + are now referred to by the typedef name in generated C code, + so it is no longer necessary to match the type in the C + header file exactly. + + - Conversion to/from unsigned long now done with + PyLong_AsUnsignedLong and PyLong_FromUnsignedLong. + [Dug Song ] + + - A struct, union or enum definition in a 'cdef extern from' + block may now be left empty (using 'pass'). This can be useful + if you need to declare a variable of that type, but don't need + to refer to any of its members. + + - More flexible about ordering of qualifiers such as 'long' and + 'unsigned'. + ["John (J5) Palmieri" ] + + +Bug fixes: + + - Non-interned string literals used in a Python class + definition did not work. + [Atsuo Ishimoto ] + [Andreas Kostyrka ] + + - Return types of the buffer interface functions for extension + types have been corrected. + [Dug Song ] + + - Added 'static' to declarations of string literals. + [Phil Frost ] + + - Float literals are now copied directly to the C code as written, + to avoid problems with loss of precision. + [Mario Pernici ] + + - Inheriting from an extension type with C methods defined in + another Pyrex module did not work. + [Itamar Shtull-Trauring ] + +0.9.2.1 +------- + +Bug fixes: + + - Corrected an import statement setup.py, and made it + check for a unix platform in a more reliable way. + +0.9.2 +----- + +Enhancements: + + - Names of Python global variables and attributes are now + interned, and PyObject_GetAttr/SetAttr are used instead + of PyObject_GetAttrString/SetAttrString. String literals + which resemble Python identifiers are also interned. + + - String literals are now converted to Python objects only + once instead of every time they are used. + + - NUL characters are now allowed in Python string literals. + + - Added some missing error checking code to the beginning + of module init functions. It's unlikely the operations + involved would ever fail, but you never know. + +Bug fixes: + + - Corrected some problems introduced by moving the Plex + package. + +0.9.1.1 +------- + +Bug fixes: + + - Corrected a problem in the setup.py (pyrexc script incorrectly + named). + + - Updated the distutils extension to match changes in the + Pyrex compiler calling interface. + + - Doing 'make clean' in Demos/callback was removing a little too + much (that's why cheesefinder.c kept disappearing). + +0.9.1 +----- + +Enhancements: + + - A C method can now call an inherited C method by the usual + Python technique. + [Jiba ] + + - The __modname__ of a Python class is now set correctly. + [Paul Prescod ] + + - A MANIFEST.in file has been added to the distribution to + facilitate building rpms. + [contributed by Konrad Hinsen ] + +Bug fixes: + + - Conditional code now generated to allow for the renaming of LONG_LONG + to PY_LONG_LONG that occurred between Python 2.2 and 2.3. + + - Header files referenced in cimported modules were not being included. + [Tom Popovich ] + + - References to C functions and variables in a cimported module were + not being recognised if made from within a local scope. + [Tom Popovich ] + + - Spurious declarations in code generated for a "finally" block. + [Brandon Long ] + + - Attempting to return a value from a __contains__ method didn't work. + [Andreas Kostyrka ] + + - Incorrect code generated for an extension type with C methods + inheriting from a base type with no C methods. + [Robin Becker ] + + - Failure to report an error if a C method was defined in the + implementation part of an extension type that was not declared + in the corresponding definition part. Documentation also updated + to explain that this is necessary. + [Jiba ] + + - Made it an error to forward-declare an extension type with + a different base class specification from its subsequent + definition. + [Jiba ] + + - C attributes of an extension type were not being propagated + through more than one level of inheritance. + [Jiba ] + + - If a garbage collection occurred early enough in the __new__ + method of an extension type with Python-valued C attributes, + a crash could occur in its tp_traverse function. + [reported by Jiba ] + [fix suggested by Paul Prescod ] + + - An empty vtable struct is no longer generated for extension + types with no C methods. + [Robin Becker ] + + - Memory was leaked in the sq_item function of an extension + type with a __getitem__ method. + [Atsuo Ishimoto ] + + - Code generated to work around a bug in some versions of Python + 2.2 which fails to initialise the tp_free slot correctly in + some circumstances. + [Matthias Baas ] + + - Compiler crash when defining an extension type with a base + class specified by a dotted name. + [Alain Pointdexter ] + + - Referencing an extension type defined in a cimported module + at run time did not work correctly. + [Alain Pointdexter ] + + - Incorrect object struct code generated for an extension type + whose base class was defined in a .pxd file. + [Alain Pointdexter ] + + - Redeclaring a type that wasn't previously an extension type + as an extension type caused a compiler crash. + [Scott Robinson ] + + - Incorrect code was generated for return statements in a + special method with no return value. + [Gary Bishop ] + + - Single-line def statement did not work. + [Francois Pinard ] + +Modifications: + + - Only the last pathname component of the .pyx file is reported in + backtraces now. + [Bryan Weingarten ] + + - Documentation corrected to remove the erroneous statement that + extension classes can have a __del__ method. + [Bryan Weingarten ] + + - Note added to documentation explaining that it is not possible + for an extension type's __new__ method to explicitly call the + inherited __new__ method. + + - The version of Plex included with Pyrex is now installed + as a subpackage of the Pyrex package, rather than as a + top-level package, so as not to interfere with any other + version of Plex the user may have installed. + +0.9 +--- + +New features: + + - Extension types can have properties. See the new "Properties" + section in the "Extension Types" page. + + - An extension type can inherit from a builtin type or another + extension type. See "Subclassing" in the "Extension Types" page. + + - Extension types can have C methods, which can be overridden + in derived extension types. See "C Methods" in the "Extension Types" + page. + +Enhancements: + + - Conversion is now performed between C long longs and Python + long integers without chopping to the size of a C long. + Also the Python PY_LONG_LONG type is now used for long longs + for greater portability. + +Bug fixes: + + - Names were sometimes being generated that were insufficiently + unique in the presence of cimported declarations. + + - Changed the way the included filename table is declared from + char *[] to char **, to stop MSVC from complaining about it + having an unknown size. + [Alexander A Naanou ] + + - Second argument of assert statement was not being coerced + to a Python value. + [Francois Pinard http://www.iro.umontreal.ca/~pinard] + + - Return statement without value wasn't accepted in some + extension type special methods when it should have been. + [Francois Pinard http://www.iro.umontreal.ca/~pinard] + + - Attempting to call a non-function C value crashed the + compiler. + [John J Lee ] + + - Functions declared as "except *" were not returning exceptions. + [John J Lee ] + + - A syntax warning from Plex about assignment to None has + been eliminated. + [Gordon Williams ] + + - Public function declaration with empty argument list was + producing (void) in .pxi file. + [Michael P. Dubner ] + + - Incorrect error signalling code was being generated in the + __hash__ special method of an extension type. + + +0.8.1 +----- + +Bug fixes: + + - Names of structs, unions and enums in external header + files were getting mangled when they shouldn't have been. + [Norman Shelley ] + + - Modified distutils extension so that it will stop before + compiling the C file if the Pyrex compiler reports errors. + [John J Lee ] + + +0.8 +--- + +New features: + + - INCOMPATIBLE CHANGE: The type object of an external extension + type is now imported at run time using the Python import + mechanism. To make this possible, an 'extern' extension type + declaration must DECLARE THE MODULE from which the extension + type originates. See the new version of the "Extension Types" + documentation for details. + + This change was made to eliminate the need for Pyrex to be + told the C name of the type object, or for the Pyrex module + to be linked against the object code providing the type object. + + You will have to update any existing external extension type + declarations that you are using. I'm sorry about that, but it + was too hard to support both the old and new ways. + + - Compile-time importing: A Pyrex module can now import declarations + from another Pyrex module using the new 'cimport' statement. See + the new section on "Sharing Declarations Between Pyrex Modules" in + the documentation. + +Minor improvements: + + - An error is reported if you declare a struct, union or + extension type using 'cdef' in one place and 'ctypedef' + in another. + + - Struct, union and extension types can only be forward- + declared using 'cdef', not 'ctypedef' (otherwise invalid + C code would be generated). + + - The 'global' statement can be used at the module level to + declare that a name is a module-level name rather than a + builtin. This can be used to access module attributes such + as __name__ that would otherwise be assumed to be builtins. + [Pat Maupin ] + + - The 'assert' statement now accepts a second argument. + [Francois Pinard ] + +Bug fixes: + + - When using Python 2.3, "True" or "False" could sometimes + turn up in generated code instead of "1" or "0". + [Adam Hixson ] + + - Function return value not always converted to or from a + Python object when it should have been. + + - Certain kinds of error in a function call expression + could crash the compiler. + ["Edward C. Jones" ] + + - Fixed memory leak in functions with * or ** args. + [Alexander A Naanou ] + + +0.7.1 +----- + +Bug fixes: + + - Calling a function declared as returning an extension + type could crash the compiler. + + - A function call with type errors in the argument list + could crash the compiler. + + - An 'else' clause on a for-from statement could crash + the compiler. + + - Incorrect casting code was generated when a generic + object argument of a special method was declared as + being of an extension type. + [Phillip J. Eby ] + + - A blank line that couldn't be interpreted wholly as + a valid indentation sequence caused a syntax error. + In particular, a formfeed character on an otherwise + blank line wasn't accepted. + [Francois Pinard ] + + - Parallel assignments were incorrectly optimised. + + - A bare tuple constructor with an extra comma at the + end of a line caused a syntax error. + +0.7 +--- + +New features: + + - Attributes of extension types can be exposed to Python + code, either read/write or read-only. + + - Different internal and external names can be specified + for C entities. + + - None is a compile-time constant, and more efficient code + is generated to reference it. + + - Command line options for specifying directories to + search for include files. + +Enhancements: + + - More efficient code is generated for access to Python + valued C attributes of extension types. + + - Cosmetic code improvement: Less casting back and forth + between extension types and PyObject * when referencing + C members of the object struct. + + - C arguments and variables declared as an extension type + can take the value None. + + - Form feed characters are accepted as whitespace. + + - Function names in tracebacks are qualified with + module name and class name. + +Bug fixes: + + - A sufficiently complex expression in a boolean context + could cause code to be generated twice for the same + subexpression. + + - Incorrect casting code was generated when passing an + extension type to a function expecting a generic Python + object. + + - Executable statements are now disallowed inside a + cdef class block (previously they silently caused + crazy C code to be generated). + + - Tracebacks should now report the correct filename for + functions defined in files included with the 'include' + statement. + + - The documentation incorrectly claimed that an extension + type can't have a __del__ method. In fact, it can, and + it behaves as expected. + + +0.6.1 +----- + +Bug fixes: + + - Fixed broken distutils extension. + + + +0.6 +--- + +New features: + + - Command line options for reporting version number, + requesting a listing file and specifying the name of + the generated C file. + + - An 'include' statement allows inclusion of declarations + from other Pyrex source files. + + - If there are any public declarations, a Pyrex include + file is generated (as well as a .h file) containing + declarations for them. + + - Extension types can be declared public, so their C + attributes are visible to other Pyrex and C code. + + - Try-except statements can now have an 'else' clause. + [Francois Pinard ] + + - Multiple simple statements can be placed on one line + separated by semicolons. + + - A suite consisting of a simple statement list can now + be placed on the same line after the colon in most + cases. + [Francois Pinard ] + + - The automatic coercion of a C string to a C char has + been removed (it proved to be too error-prone). + Instead, there is a new form of literal for C + character constants: c'X' + + - The __get__ special method (used by descriptor objects) + now allows for the possibility of the 2nd or 3rd + arguments being NULL. Also the __set__ method has been + split into two methods, __set__ and __delete__. + [Phillip J. Eby ] + +Bug fixes: + + - Values unpacked into a non-Python destination variable + were not being converted before assignment. + [Gareth Watts ] + + - Hex constants greater than 0x7fffffff caused compiler + to crash. [Gareth Watts ] + + - Type slots are no longer statically initialised with + extern function pointers, to avoid problems with + some compilers. The hack in the distutils extension + to work around this by compiling as C++ has been + disabled. [Phillip J. Eby ] + + - Fixed several more instances of the error-reporting + routine being called with arguments in the wrong + order. Hoping I've *finally* got all of them now... + + - Nested for-from loops used the same control variable. + [Sebastien de Menten ] + + - Fixed some other error message related bugs. + [Francois Pinard ] + + - Assigning to slice didn't work. + [Francois Pinard ] + + - Temp variables were being declared as extension + types and then being assigned PyObject *'s. All + Python temp vars are now declared as PyObject *. + [Francois Pinard ] + +0.5 +--- + +Bug fixes: + + - Algorithm for allocating temp variables redesigned + to fix various errors concerning temp + variable re-use. + [Mark Rowe ] + + - Memory leak occured sometimes when an implicit + type test was applied to the result of an + expression. + [christoph.wiedemann@daimlerchrysler.com] + + - __set__ method of extension types had wrong + signature. + [Josh Littlefield ] + +0.4.6 +----- + +Bug fixes: + + - Indexing multi-dimensional C arrays didn't + work. + [Gary Dietachmayer ] + + +0.4.5 +----- + +New features: + + - There is now a 'public' declaration for + making Pyrex-defined variables and functions + available to external C code. A .h file is + also generated if there are any public + declarations. + +Enhancements: + + - Defining __len__/__getitem__ methods in an + extension class fills sq_length/sq_item slots + as well as mp_length/mp_subscript. + [Matthias Baas ] + + - The Distutils extension now allows .c files + to be incorporated along with .pyx files. + [Modification to Distutils extension contributed + by Darrell Gallion .] + +Bug fixes: + + - Float literals without a decimal point + work again now. + [Mike Rovner ] + [Peter Lepage ] + + - Compiler crashed if exception value didn't + match function return type. + [Michael JasonSmith ] + + - The setup.py file should now install the + Lexicon.pickle file in the right place. + [Patch supplied by David M. Cooke + ] + + - Compiler crashed when compiling a C function that + returned an extension type. + [David M. Cooke + ] + + - Anonymous enum types did not have C code + suppressed inside an extern-from block. + [Matthew Mueller ] + + +0.4.4 +----- + +Enhancements: + + - Tracebacks now extend into Pyrex function + calls and show line numbers in the Pyrex + source file. + + - Syntax for float literals made more lenient + (no longer requires digits both before and + after the point). + [Peter Lepage ] + + - Method calls can be made on string literals + (e.g. ",".join(x)). + [pedro_rodriguez@club-internet.fr] + +Bug fixes: + + - Incorrect refcount code generated when a + Python function needing argument type tests + had local Python variables. + [Matthias Baas ] + + - 'self' parameter of __getitem__ method of + extension type had wrong implicit type. + [Peter Lepage ] + + - Repaired breakage introduced by trying to + allow an empty parameter list to be written + as (void). No longer attempting to allow + this (too hard to parse correctly). + [Peter Lepage ] + + - Found bug in Plex 1.1.2 which was the *real* + cause of the two-newlines-in-a-row problem. + Removed the Opt(Eol)+Str("\n") hacks in + the scanner which were working around this + before. + [pedro_rodriguez@club-internet.fr] + + - __call__ special method of extension types + had wrong signature. + [Peter Lepage ] + + +0.4.3 +----- + +New language features: + + - For-from loop for iterating over integer + ranges, using pure C loop where possible. + +Enhancements: + + - sizeof() can now be applied to types as + well as variables. + + - Improved handling of forward-declared + extension types. + +Bug fixes: + + - Two newlines in a row in a triple quoted + string caused a parse error on some + platforms. + [Matthias Baas ] + + - Fixed problem with break and continue in + the else-clause of a loop. + + +0.4.2 +----- + +New language features: + + - C functions can be declared as having an + exception return value, which is checked + whenever the function is called. If an + exception is detected inside a C function + for which no exception value is declared, + a warning message is printed and the + exception is cleared. + + - Cascaded assignments (i.e. a = b = c + are now supported. + + - Anonymous enum declarations are allowed, + for when you just want to declare constants. + + - The C types "long long" and "long double" + are now understood. Also, "int" is optional + after "short" or "long". + +Enhancements: + + - A * argument in a function call can now be + any sequence, not just a tuple. + + - A C char* or char[] will be turned into + a char by taking its first character if + used in a context where a char is required, + thus allowing a string literal to be used as + a char literal. + + - C string * C int or vice versa is now + interpreted as Python string replication. + + - Function arguments are checked for void or + incomplete type. + +Bug fixes: + + - Non-external extension types show up in the + module dict once more (this got broken in + 0.4.1). + + - A spurious decref has been removed from the + runtime support code for the "import" statement. + Hopefully this will prevent the crashes some + people have been experiencing when importing + builtin modules. + [Mathew Yeates ] + +0.4.1 +----- + +New language features: + + - "ctypedef struct/union/enum/class" statements + added, for use in extern-from blocks when a + header file uses a ctypedef to declare a + tagless struct, union or enum type. + + - "pass" allowed in an extern-from block. + + - "cdef extern from *" for when you don't want + to specify an include file name. + + - Argument names may be omitted in function + signatures when they're not needed. + + - New reserved word NULL for the null C pointer. + +Compiler enhancements: + + - Lexicon is now picked in binary format, so + startup should be much faster on slower + machines. + + - If Pyrex decides to rebuild the lexicon and + then finds that it can't write a pickle file, + it now prints a warning and carries on + instead of crashing. + + - Chat about hash codes and lexicon pickling + now turned off by default except when creating + a new lexicon (which ought never happen now + unless you change the scanner). + +Bug fixes: + + - Modified the runtime support code for "import" + statements, hopefully fixing problem with using + a Pyrex module in conjunction with py2exe. + + - DL_EXPORT now used in both the prototype and + definition of the module init function. + + - Exception state is now saved and restored around + calls to an extension type __dealloc__ method, + to avoid screwing up if the object is deallocated + while an exception is being propagated. + + - Making an attribute reference to a method of + an extension type caused a compiler crash. + + - Doc string in new-style class definition + caused a run-time error. + + - Insufficient parentheses were put around C type + casts. + + - Constructors for extension types are now read-only + C global variables instead of entries in the + module dict. This change was needed to prevent + Numeric from blowing up due to touching its + typeobject before import_numeric() could be called. + +0.4 +--- + +New features: + + - "cdef extern from" statement allows inclusion + of C header files to be specified, solving + a number of problems including: + - Clashes between Pyrex and C declarations, + due to "const" and other reasons + - Windows-specific features required in + function declarations + - Helping deal with types such as "size_t" + - Helping deal with functions defined as + macros + + - Access to internals of pre-existing extension + types is now possible by placing an extension + type declaration inside a "cdef extern from" + block. + +Bug fixes: + + - Error not reported properly when passing + wrong number of args to certain special + methods of extension types. + [Mitch Chapman ] + + - Compile-time crash when defining an extension + type with a __hash__ method. + +Minor enhancements: + + - Hashing of the scanner source file made more + platform-independent, making spurious regeneration + of the pickle less likely. + + +0.3.4 +----- + +Bug fixes: + + - Runtime crash when using * or ** args in + a method of an extension type fixed. + [Matthew Mueller ] + + - Compiler crash when using default argument + values in a method of a Python class. + [Mike Rovner ] + +Enhancements: + + - Type slots filled with functions from outside + the extension module are now initialised dynamically, + which should eliminate at least some of the + "initialiser is not constant" problems experienced + on Windows. + [Marek Baczek ] + + - On Windows, __declspec(dllexport) is now used for + the module init func declaration (or should be -- + I haven't tested this). + [Marek Baczek ] + + - The compiler shouldn't attempt to rewrite the + Lexicon.pickle file unless the source has been + changed (hashing is used now instead of comparing + timestamps). So there should be no problem any more + with installing Pyrex read-only. + [fawcett@uwindsor.ca] + +0.3.3 +----- + +Bug fixes: + +* A void * can be assigned from any other +pointer type. +[piers@cs.su.oz.au] + +* File names in error messages no longer +quoted (this was apparently confusing some +editors). +[donut@azstarnet.com] + +* Reference to a struct member which is an +array is coerced to a pointer. +[donut@azstarnet.com] + +* Default argument values did not work +in methods of an extension type. +[donut@azstarnet.com] + +* Single or double quote characters in a +triple-quoted string didn't work. +[donut@azstarnet.com] + +* Using *args in a function definition +sometimes caused a crash at runtime. +[donut@azstarnet.com] + +* A hack is included which tries to make +functions in Python.h which use 'const' +accessible from Pyrex. But it doesn't +work on all platforms. Thinking about a +better solution. + + +New features: + +* Comment containing Pyrex version number +and date/time at top of generated C file. +[baas@ira.uka.de] + + +0.3.2 +----- + +Bug fixes: + +* The & operator works again. +[matthias.oberlaender@daimlerchrysler.com] +[baas@ira.uka.de] + +* The & operator had incorrect precedence. + +* "SystemError: 'finally' pops bad exception" +under some circumstances when raising an +exception. [baas@ira.uka.de] + +* Calling a Python function sometimes leaked +a reference. + +* Crash under some circumstances when casting +a Python object reference to a C pointer type. +[mpj17@cosc.canterbury.ac.nz] + +* Crash when redeclaring a function. +[baas@ira.uka.de] + +* Crash when using a string constant inside +a Python class definition. +[mike@bindkey.com] + +* 2-element slice indexing expressions. +[mike@bindkey.com] + +* Crash when encountering mixed tabs and +spaces. +[mike@bindkey.com] + +New features: + +* A wider variety of constant expressions is +now accepted for enum values, array +dimensions, etc. +[mike@bindkey.com] + + +0.3.1 +----- + +New features: + +* More special methods for extension types: +__delitem__, __delslice__, __getattr__, +__setattr__, __delattr__ + +* Module-level variable of a Python object type +declared with 'cdef' is private to the module, and +held in a C variable instead of the module dict. + +* External C functions with variable argument lists +can be declared and called. + +* Pyrex-defined Python functions can have default +argument values and * and ** arguments, and can be +called with keyword arguments. + +* Pointer-to-function types can be declared. + +* Pyrex now supports a declaration syntax that +C doesn't! Example: + + cdef (int (*)()) foo() # function returning a function ptr + +* There is now a ctypedef statement. + +* Extension types can now be forward-declared. + +* All permutations of (non-Unicode) string literals +and escape codes should work now. + +* Hex and octal integer literals. + +* Imaginary number literals. + +* Docstrings are now supported. + +Bug fixes: + +* Type tests are performed when using a Python object +in a context requiring a particular extension type. + +* Module-level variable holding the type object +of an extension type had incorrect type. + +0.3 +--- + +New features: + +* Extension types! Yay! + +0.2.2 +----- + +Bug fixes: + +* Fixed error message generation again after a previous +bug was accidentally re-indroduced. + +* Removed the declaration of isspace() from the code +generated for print statement support (it's not needed +and was conflicting with the system-supplied one on +some platforms). + +0.2 +--- + +New features: + +* Executable statements are now allowed at the +top level of a module. + +* Python class definitions are now supported, with +the following limitations: + + - Class definitions are only allowed at the top + level of a module, not inside a control structure + or function or another class definition. + + - Assigning a Pyrex-defined Python function to a + class attribute outside of the class definition + will not create a method (because it's not an + interpreted Python function and therefore + won't trigger the bound-method creation magic). + + - The __metaclass__ mechanism and the creation of + new-style classes is not (yet) supported. + +* Casting between Python and non-Python types is +better supported. + +Bug fixes: + +* Fixed bug preventing for-loops from working. + + +0.1.1 +----- + +* I've discovered a flaw in my algorithm for releasing +temp variables. Fixing this properly will require some +extensive reworking; I've put in a hack in the meantime +which should work at the cost of using more temp variables +than are strictly necessary. + +* Fixed bug preventing access to builtin names from +working. This should also have fixed the import +statement, but I haven't tested it. + +* Fixed some errors in __Pyx_GetExcValue. + +* Fixed bug causing boolean expressions to malfunction +sometimes. diff --git a/COPYING.txt b/COPYING.txt new file mode 100644 index 00000000..f266bada --- /dev/null +++ b/COPYING.txt @@ -0,0 +1,11 @@ +The original Pyrex code as of 2006-04 is licensed under the following +license: "Copyright stuff: Pyrex is free of restrictions. You may use, +redistribute, modify and distribute modified versions." + +------------------ + +Cython, which derives from Pyrex, is licensed under the Python +Software Foundation License. More precisely, all modifications +made to go from Pyrex to Cython are so licensed. + + diff --git a/Demos/Makefile b/Demos/Makefile new file mode 100644 index 00000000..e70687f4 --- /dev/null +++ b/Demos/Makefile @@ -0,0 +1,15 @@ +all: + python Setup.py build_ext --inplace + +test: all + python run_primes.py 20 + python run_numeric_demo.py + python run_spam.py + cd callback; $(MAKE) test + +clean: + @echo Cleaning Demos + @rm -f *.c *.o *.so *~ core + @rm -rf build + @cd callback; $(MAKE) clean + @cd embed; $(MAKE) clean diff --git a/Demos/Makefile.nodistutils b/Demos/Makefile.nodistutils new file mode 100644 index 00000000..d648f849 --- /dev/null +++ b/Demos/Makefile.nodistutils @@ -0,0 +1,21 @@ +PYHOME = $(HOME)/pkg/python/version +PYINCLUDE = \ + -I$(PYHOME)/include/python2.2 \ + -I$(PYHOME)/$(ARCH)/include/python2.2 + +%.c: %.pyx + ../bin/pyrexc $< + +%.o: %.c + gcc -c -fPIC $(PYINCLUDE) $< + +%.so: %.o + gcc -shared $< -lm -o $@ + +all: primes.so spam.so numeric_demo.so + +clean: + @echo Cleaning Demos + @rm -f *.c *.o *.so *~ core core.* + @cd callback; $(MAKE) clean + @cd embed; $(MAKE) clean diff --git a/Demos/Setup.py b/Demos/Setup.py new file mode 100644 index 00000000..658a44c3 --- /dev/null +++ b/Demos/Setup.py @@ -0,0 +1,13 @@ +from distutils.core import setup +from distutils.extension import Extension +from Pyrex.Distutils import build_ext + +setup( + name = 'Demos', + ext_modules=[ + Extension("primes", ["primes.pyx"]), + Extension("spam", ["spam.pyx"]), + Extension("numeric_demo", ["numeric_demo.pyx"]), + ], + cmdclass = {'build_ext': build_ext} +) diff --git a/Demos/callback/Makefile b/Demos/callback/Makefile new file mode 100644 index 00000000..890bc808 --- /dev/null +++ b/Demos/callback/Makefile @@ -0,0 +1,10 @@ +all: + python Setup.py build_ext --inplace + +test: all + python run_cheese.py + +clean: + @echo Cleaning Demos/callback + @rm -f cheese.c *.o *.so *~ core + @rm -rf build diff --git a/Demos/callback/Makefile.nodistutils b/Demos/callback/Makefile.nodistutils new file mode 100644 index 00000000..012c1a86 --- /dev/null +++ b/Demos/callback/Makefile.nodistutils @@ -0,0 +1,19 @@ +PYHOME = $(HOME)/pkg/python/version +PYINCLUDE = \ + -I$(PYHOME)/include/python2.2 \ + -I$(PYHOME)/$(ARCH)/include/python2.2 + +%.c: %.pyx + ../../bin/pyrexc $< + +%.o: %.c + gcc -c -fPIC $(PYINCLUDE) $< + +%.so: %.o + gcc -shared $< -lm -o $@ + +all: cheese.so + +clean: + @echo Cleaning Demos/callback + @rm -f *.c *.o *.so *~ core core.* diff --git a/Demos/callback/README.txt b/Demos/callback/README.txt new file mode 100644 index 00000000..fa3b871c --- /dev/null +++ b/Demos/callback/README.txt @@ -0,0 +1 @@ +This example demonstrates how you can wrap a C API that has a callback interface, so that you can pass Python functions to it as callbacks. The files cheesefinder.h and cheesefinder.c represent the C library to be wrapped. The file cheese.pyx is the Pyrex module which wraps it. The file run_cheese.py demonstrates how to call the wrapper. \ No newline at end of file diff --git a/Demos/callback/Setup.py b/Demos/callback/Setup.py new file mode 100644 index 00000000..5e48206a --- /dev/null +++ b/Demos/callback/Setup.py @@ -0,0 +1,11 @@ +from distutils.core import setup +from distutils.extension import Extension +from Pyrex.Distutils import build_ext + +setup( + name = 'callback', + ext_modules=[ + Extension("cheese", ["cheese.pyx", "cheesefinder.c"]), + ], + cmdclass = {'build_ext': build_ext} +) diff --git a/Demos/callback/cheese.pyx b/Demos/callback/cheese.pyx new file mode 100644 index 00000000..db0fc082 --- /dev/null +++ b/Demos/callback/cheese.pyx @@ -0,0 +1,13 @@ +# +# Pyrex wrapper for the cheesefinder API +# + +cdef extern from "cheesefinder.h": + ctypedef void (*cheesefunc)(char *name, void *user_data) + void find_cheeses(cheesefunc user_func, void *user_data) + +def find(f): + find_cheeses(callback, f) + +cdef void callback(char *name, void *f): + (f)(name) diff --git a/Demos/callback/cheesefinder.c b/Demos/callback/cheesefinder.c new file mode 100644 index 00000000..ab41853b --- /dev/null +++ b/Demos/callback/cheesefinder.c @@ -0,0 +1,21 @@ +/* + * An example of a C API that provides a callback mechanism. + */ + +#include "cheesefinder.h" + +static char *cheeses[] = { + "cheddar", + "camembert", + "that runny one", + 0 +}; + +void find_cheeses(cheesefunc user_func, void *user_data) { + char **p = cheeses; + while (*p) { + user_func(*p, user_data); + ++p; + } +} + diff --git a/Demos/callback/cheesefinder.h b/Demos/callback/cheesefinder.h new file mode 100644 index 00000000..3c31bdc0 --- /dev/null +++ b/Demos/callback/cheesefinder.h @@ -0,0 +1 @@ +typedef void (*cheesefunc)(char *name, void *user_data); void find_cheeses(cheesefunc user_func, void *user_data); \ No newline at end of file diff --git a/Demos/callback/run_cheese.py b/Demos/callback/run_cheese.py new file mode 100644 index 00000000..65fd431b --- /dev/null +++ b/Demos/callback/run_cheese.py @@ -0,0 +1,7 @@ +import cheese + +def report_cheese(name): + print "Found cheese:", name + +cheese.find(report_cheese) + diff --git a/Demos/embed/Makefile b/Demos/embed/Makefile new file mode 100644 index 00000000..c8c7dbeb --- /dev/null +++ b/Demos/embed/Makefile @@ -0,0 +1,30 @@ +PYVERSION = 2.2 +PYHOME = $(HOME)/pkg/python/$(PYVERSION) +PYARCH = $(PYHOME)/$(ARCH) +PYINCLUDE = \ + -I$(PYHOME)/include/python$(PYVERSION) \ + -I$(PYARCH)/include/python$(PYVERSION) +PYLIB = -L$(PYARCH)/lib/python$(PYVERSION)/config \ + -lpython$(PYVERSION) \ + -ldl -lpthread -lutil -lm + +%.c: %.pyx + ../../bin/pyrexc $< + +%.o: %.c + gcc -c -fPIC $(PYINCLUDE) $< + +#%.so: %.o +# gcc -shared $< -lm -o $@ + +all: main + +main: main.o embedded.o + gcc main.o embedded.o $(PYLIB) -o main + +clean: + @echo Cleaning Demos/embed + @rm -f *~ *.o *.so core core.* embedded.h embedded.c main + +embedded.h: embedded.c +main.o: embedded.h diff --git a/Demos/embed/Makefile.msc b/Demos/embed/Makefile.msc new file mode 100644 index 00000000..37114c73 --- /dev/null +++ b/Demos/embed/Makefile.msc @@ -0,0 +1,35 @@ +# Makefile for Microsoft C Compiler, building a DLL +PYVERSION = 2.2 +PYHOME = \Python$(PYVERSION:.=) +PYINCLUDE = -I$(PYHOME)\include +PYLIB = /LIBPATH:$(PYHOME)\libs + +CFLAGS = $(PYINCLUDE) /Ox /W3 /GX -nologo +.SUFFIXES: .exe .dll .obj .c .cpp .pyx + +.pyx.c: + $(PYHOME)\Python.exe ../../pyrexc.py $< + +all: main.exe + +clean: + del /Q/F *.obj embedded.h embedded.c main.exe embedded.dll embedded.lib embedded.exp + +# When linking the DLL we must explicitly list all of the exports +# There doesn't seem to be an easy way to get DL_EXPORT to have the correct definition +# to do the export for us without breaking the importing of symbols from the core +# python library. +embedded.dll: embedded.obj + link /nologo /DLL /INCREMENTAL:NO $(PYLIB) $** /IMPLIB:$*.lib /DEF:<< /OUT:$*.dll +EXPORTS initembedded +EXPORTS spam +<< + +main.exe: main.obj embedded.lib + link /nologo $** $(PYLIB) /OUT:main.exe + +embedded.h: embedded.c +main.obj: embedded.h +embedded.obj: embedded.c + $(CC) /MD $(CFLAGS) -c $** +embedded.lib: embedded.dll diff --git a/Demos/embed/Makefile.msc.static b/Demos/embed/Makefile.msc.static new file mode 100644 index 00000000..14e1080c --- /dev/null +++ b/Demos/embed/Makefile.msc.static @@ -0,0 +1 @@ +# Makefile for Microsoft compiler statically linking PYVERSION = 2.2 PYHOME = \Python$(PYVERSION:.=) PYINCLUDE = -I$(PYHOME)\include PYLIB = /LIBPATH:$(PYHOME)\libs python22.lib CFLAGS = $(PYINCLUDE) /Ox /W3 /GX -nologo .SUFFIXES: .exe .dll .obj .c .cpp .pyx .pyx.c: $(PYHOME)\Python.exe ../../pyrexc.py $< all: main.exe clean: -del /Q/F *.obj embedded.h embedded.c main.exe main.exe: main.obj embedded.obj link /nologo $** $(PYLIB) /OUT:main.exe embedded.h: embedded.c main.obj: embedded.h \ No newline at end of file diff --git a/Demos/embed/Makefile.unix b/Demos/embed/Makefile.unix new file mode 100644 index 00000000..c8c7dbeb --- /dev/null +++ b/Demos/embed/Makefile.unix @@ -0,0 +1,30 @@ +PYVERSION = 2.2 +PYHOME = $(HOME)/pkg/python/$(PYVERSION) +PYARCH = $(PYHOME)/$(ARCH) +PYINCLUDE = \ + -I$(PYHOME)/include/python$(PYVERSION) \ + -I$(PYARCH)/include/python$(PYVERSION) +PYLIB = -L$(PYARCH)/lib/python$(PYVERSION)/config \ + -lpython$(PYVERSION) \ + -ldl -lpthread -lutil -lm + +%.c: %.pyx + ../../bin/pyrexc $< + +%.o: %.c + gcc -c -fPIC $(PYINCLUDE) $< + +#%.so: %.o +# gcc -shared $< -lm -o $@ + +all: main + +main: main.o embedded.o + gcc main.o embedded.o $(PYLIB) -o main + +clean: + @echo Cleaning Demos/embed + @rm -f *~ *.o *.so core core.* embedded.h embedded.c main + +embedded.h: embedded.c +main.o: embedded.h diff --git a/Demos/embed/README b/Demos/embed/README new file mode 100644 index 00000000..55283ac3 --- /dev/null +++ b/Demos/embed/README @@ -0,0 +1 @@ +This example demonstrates how Pyrex-generated code can be called directly from a main program written in C. In this example, the module's initialisation function (called "initembedded", since the module is called "embedded") is called explicitly. This is necessary because the module is not being imported using the normal Python import mechanism. The Windows makefiles were contributed by Duncan Booth . \ No newline at end of file diff --git a/Demos/embed/embedded.pyx b/Demos/embed/embedded.pyx new file mode 100644 index 00000000..90d62f67 --- /dev/null +++ b/Demos/embed/embedded.pyx @@ -0,0 +1,5 @@ +cdef public void spam(): + praise() + +def praise(): + print "Spam, glorious spam!" diff --git a/Demos/embed/main.c b/Demos/embed/main.c new file mode 100644 index 00000000..3e089abc --- /dev/null +++ b/Demos/embed/main.c @@ -0,0 +1,9 @@ +#include "Python.h" +#include "embedded.h" + +int main(int argc, char *argv) { + Py_Initialize(); + initembedded(); + spam(); + Py_Finalize(); +} diff --git a/Demos/numeric_demo.pyx b/Demos/numeric_demo.pyx new file mode 100644 index 00000000..3a20fd90 --- /dev/null +++ b/Demos/numeric_demo.pyx @@ -0,0 +1,39 @@ +# +# This example demonstrates how to access the internals +# of a Numeric array object. +# + +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 + +def print_2d_array(ArrayType a): + print "Type:", chr(a.descr.type) + if chr(a.descr.type) <> "f": + raise TypeError("Float array required") + if a.nd <> 2: + raise ValueError("2 dimensional array required") + cdef int nrows, ncols + cdef float *elems, x + nrows = a.dimensions[0] + ncols = a.dimensions[1] + elems = a.data + hyphen = "-" + divider = ("+" + 10 * hyphen) * ncols + "+" + print divider + for row in range(nrows): + for col in range(ncols): + x = elems[row * ncols + col] + print "| %8f" % x, + print "|" + print divider diff --git a/Demos/primes.pyx b/Demos/primes.pyx new file mode 100644 index 00000000..dfbaae48 --- /dev/null +++ b/Demos/primes.pyx @@ -0,0 +1,18 @@ +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 diff --git a/Demos/pyprimes.py b/Demos/pyprimes.py new file mode 100644 index 00000000..edcce852 --- /dev/null +++ b/Demos/pyprimes.py @@ -0,0 +1,13 @@ +def primes(kmax): + p = [] + k = 0 + n = 2 + while k < kmax: + i = 0 + while i < k and n % p[i] <> 0: + i = i + 1 + if i == k: + p.append(n) + k = k + 1 + n = n + 1 + return p diff --git a/Demos/run_numeric_demo.py b/Demos/run_numeric_demo.py new file mode 100644 index 00000000..803442d5 --- /dev/null +++ b/Demos/run_numeric_demo.py @@ -0,0 +1,5 @@ +import Numeric +import numeric_demo + +a = Numeric.array([[1.0, 3.5, 8.4], [2.3, 6.6, 4.1]], "f") +numeric_demo.print_2d_array(a) diff --git a/Demos/run_primes.py b/Demos/run_primes.py new file mode 100644 index 00000000..487767d4 --- /dev/null +++ b/Demos/run_primes.py @@ -0,0 +1,7 @@ +import sys +from primes import primes +if len(sys.argv) >= 2: + n = int(sys.argv[1]) +else: + n = 1000 +print primes(n) diff --git a/Demos/run_spam.py b/Demos/run_spam.py new file mode 100644 index 00000000..e1c1e155 --- /dev/null +++ b/Demos/run_spam.py @@ -0,0 +1,8 @@ +from spam import Spam + +s = Spam() +print "Created:", s +s.set_amount(42) +print "Amount =", s.get_amount() +s.describe() +s = None diff --git a/Demos/spam.pyx b/Demos/spam.pyx new file mode 100644 index 00000000..d70eccfa --- /dev/null +++ b/Demos/spam.pyx @@ -0,0 +1,22 @@ +# +# Example of an extension type. +# + +cdef class Spam: + + cdef int amount + + def __new__(self): + self.amount = 0 + + def __dealloc__(self): + print self.amount, "tons of spam is history." + + 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!" diff --git a/Doc/About.html b/Doc/About.html new file mode 100644 index 00000000..057d05a3 --- /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:
  • Support for certain Python language features which are planned but not yet implemented. See the Limitations section of the Language Overview for a current list.
  • C++ support. This could be a very big can of worms - careful thought required before going there.
  • Reading C/C++ header files directly would be very nice, but there are some severe problems that I will have to find solutions for first, such as what to do about preprocessor macros. My current thinking is to use a separate tool to convert .h files into Pyrex declarations, possibly with some manual intervention.
\ No newline at end of file diff --git a/Doc/FAQ.html b/Doc/FAQ.html new file mode 100644 index 00000000..ad09c262 --- /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 00000000..cda6377e --- /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:

+
    +
  • The self parameter of a method of an extension type is guaranteed + never to be None.
  • +
+
    +
  • When comparing a value with None, keep in mind that, if x is a Python object, x is None and x is not None are very +efficient because they translate directly to C pointer comparisons, whereas + x == None and x != None, or simply using x as a boolean value (as in if x: ...) will invoke Python operations +and therefore be much slower.
  • +
+

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 00000000..6a262e3b --- /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 00000000..d50eeb6e --- /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.
+ +
    +
  • An integer literal without an L suffix is treated as a C constant, and will be truncated to whatever size your C compiler thinks appropriate. With an L suffix, it will be converted to Python long integer (even if it would be small enough to fit into a C int).
    +
    +
  • +
  • There is no -> operator in Pyrex. Instead of p->x, + use p.x
  • + +  
  • There is no * operator in Pyrex. Instead of + *p, use p[0]
  • + +  
  • There is an & operator, with the same semantics + as in C.
  • + +  
  • The null C pointer is called NULL, not 0 (and + NULL is a reserved word).
  • + +  
  • Character literals are written with a c prefix, for +example:
  • +
      +
      c'X'
      +
    +
  • Type casts are written <type>value , for example:
  • +
      +
      cdef char *p, float *q
      p = <char*>q
      +
    + Warning: Don't attempt to use a typecast to convert between +Python and C data types -- it won't do the right thing. Leave Pyrex to perform +the conversion automatically. +
+ + +

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:

+ + +
    + +
  • The target expression must be a variable name.
  • +
  • The name between the lower and upper bounds must be the same as +the target name.
  • +
  • The direction of iteration is determined by the relations. If they + are both from the set {<, <=} then it is upwards; + if they are both from the set {>, >=} then it is +downwards. (Any other combination is disallowed.)
  • + +
+ + 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:

+ + +
    + +
  • Currently, exception values can only declared for functions returning + an integer, float or pointer type, and the value must be a literal, + not an expression (although it can be negative). The only possible pointer + exception value is NULL. Void functions can only use the except + * form.
  • +
    +  
  • The exception value specification is part of the signature +of the function. If you're passing a pointer to a function as a parameter +or assigning it to a variable, the declared type of the parameter or variable + must have the same exception value specification (or lack thereof). Here +is an example of a pointer-to-function declaration with an exception value:
  • +
      +
      int (*grail)(int, char *) except -1
      +
    +
  • You don't need to (and shouldn't) declare exception values for functions + which return Python objects. Remember that a function with no declared return + type implicitly returns a Python object.
  • + +
+ + +

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: +
    + +
  • If you want to include a C header because it's needed by another +header, but don't want to use any declarations from it, put pass in the extern-from block:
  • + +
+ + +
    + +
      + cdef extern from "spam.h":
      +     pass
    + +
+ + +
    + +
  • If you want to include some external declarations, but don't want +to specify a header file (because it's included by some other header that +you've already included) you can put * in place of the header file +name:
  • + +
+ + +
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 00000000..9a88b84c --- /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 00000000..a92856e2 --- /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: +
      +
    • Any kind of C type declaration.
    • +
    • extern C function or variable declarations.
    • +
    • The definition part of an extension type (see below).
    • +
    + 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:

    +
      +
    • There is a cdef class Shrubbery declaration in both Shrubbing.pxd + and Shrubbing.pyx. When the Shrubbing module is compiled, these two declarations + are combined into one.
    • + +  
    • In Landscaping.pyx, the cimport Shrubbing declaration +allows us to refer to the Shrubbery type as Shrubbing.Shrubbery. +But it doesn't bind the name Shrubbery in Landscaping's module namespace + at run time, so to access Shrubbery.standard_shrubbery we also +need to import Shrubbing.
    • +
    +
    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 00000000..b5ef495e --- /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: +
      +
        +   + + + + + + + + + + + + + + + + + + + + + +
        <
        +
        0
        +
        ==
        +
        2
        +
        >
        +
        4
        <=
        +
        1
        +
        !=
        +
        3
        +
        >=
        +
        5
        +
      +
    +

    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 diff --git a/INSTALL.txt b/INSTALL.txt new file mode 100644 index 00000000..3dccaa67 --- /dev/null +++ b/INSTALL.txt @@ -0,0 +1,22 @@ +Pyrex - Installation Instructions +================================= + +You have two installation options: + +(1) Run the setup.py script in this directory + as follows: + + python setup.py install + + This will install the Pyrex package + into your Python system. + +OR + +(2) If you prefer not to modify your Python + installation, arrange for the directory + containing this file (INSTALL.txt) to be in + your PYTHONPATH. On unix, also put the bin + directory on your PATH. + +See README.txt for pointers to other documentation. diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 00000000..5615d2fb --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,50 @@ +The PYTHON SOFTWARE FOUNDATION LICENSE: + +PSF LICENSE AGREEMENT FOR PYTHON 2.4 +------------------------------------ + +1. This LICENSE AGREEMENT is between the Python Software Foundation +("PSF"), and the Individual or Organization ("Licensee") accessing and +otherwise using Python 2.4 software in source or binary form and its +associated documentation. + +2. Subject to the terms and conditions of this License Agreement, PSF +hereby grants Licensee a nonexclusive, royalty-free, world-wide +license to reproduce, analyze, test, perform and/or display publicly, +prepare derivative works, distribute, and otherwise use Python 2.4 +alone or in any derivative version, provided, however, that PSF's +License Agreement and PSF's notice of copyright, i.e., "Copyright (c) +2001, 2002, 2003, 2004 Python Software Foundation; All Rights Reserved" +are retained in Python 2.4 alone or in any derivative version prepared +by Licensee. + +3. In the event Licensee prepares a derivative work that is based on +or incorporates Python 2.4 or any part thereof, and wants to make +the derivative work available to others as provided herein, then +Licensee hereby agrees to include in any such work a brief summary of +the changes made to Python 2.4. + +4. PSF is making Python 2.4 available to Licensee on an "AS IS" +basis. PSF MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR +IMPLIED. BY WAY OF EXAMPLE, BUT NOT LIMITATION, PSF MAKES NO AND +DISCLAIMS ANY REPRESENTATION OR WARRANTY OF MERCHANTABILITY OR FITNESS +FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF PYTHON 2.4 WILL NOT +INFRINGE ANY THIRD PARTY RIGHTS. + +5. PSF SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF PYTHON +2.4 FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS +A RESULT OF MODIFYING, DISTRIBUTING, OR OTHERWISE USING PYTHON 2.4, +OR ANY DERIVATIVE THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF. + +6. This License Agreement will automatically terminate upon a material +breach of its terms and conditions. + +7. Nothing in this License Agreement shall be deemed to create any +relationship of agency, partnership, or joint venture between PSF and +Licensee. This License Agreement does not grant permission to use PSF +trademarks or trade name in a trademark sense to endorse or promote +products or services of Licensee, or any third party. + +8. By copying, installing or otherwise using Python 2.4, Licensee +agrees to be bound by the terms and conditions of this License +Agreement. diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 00000000..7710f3e0 --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,7 @@ +include MANIFEST.in README.txt INSTALL.txt CHANGES.txt ToDo.txt USAGE.txt +include setup.py +include bin/pyrexc +include pyrexc.py +include Pyrex/Compiler/Lexicon.pickle +include Doc/* +include Demos/* diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..099ccfb8 --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +VERSION = 0.9.4.1 + +version: + @echo "Setting version to $(VERSION)" + @echo "version = '$(VERSION)'" > Pyrex/Compiler/Version.py + +#check_contents: +# @if [ ! -d Pyrex/Distutils ]; then \ +# echo Pyrex/Distutils missing; \ +# exit 1; \ +# fi + +clean: + @echo Cleaning Source + @rm -f *.pyc */*.pyc */*/*.pyc + @rm -f *~ */*~ */*/*~ + @rm -f core */core + @(cd Demos; $(MAKE) clean) diff --git a/README.txt b/README.txt new file mode 100644 index 00000000..4a70f7c2 --- /dev/null +++ b/README.txt @@ -0,0 +1,58 @@ +Welcome to Cython! +================= + +Cython (http://www.cython.org) is based on Pyrex, but +supports more cutting edge functionality and optimizations. + +LICENSE: + +The original Pyrex program was licensed "free of restrictions" (see +below). Cython itself is licensed under the + + PYTHON SOFTWARE FOUNDATION LICENSE + http://www.python.org/psf/license/ + + + + + +-------------------------- + +To see the change history, go to the Pyrex directory and type + + $ hg log + +This requires that you have installed Mercurial. + + -- William Stein (wstein@gmail.com) + +xxxx + + +The following is from Pyrex: +------------------------------------------------------ +This is a development version of Pyrex, a language +for writing Python extension modules. + +For more info, see: + + Doc/About.html for a description of the language + INSTALL.txt for installation instructions + USAGE.txt for usage instructions + Demos for usage examples + +Comments, suggestions, bug reports, etc. are +welcome! + +Copyright stuff: Pyrex is free of restrictions. You +may use, redistribute, modify and distribute modified +versions. + +The latest version of Pyrex can be found here: + +http://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/ + +Greg Ewing, Computer Science Dept, +--------------------------------------+ +University of Canterbury, | A citizen of NewZealandCorp, a | +Christchurch, New Zealand | wholly-owned subsidiary of USA Inc. | +greg@cosc.canterbury.ac.nz +--------------------------------------+ diff --git a/ToDo.txt b/ToDo.txt new file mode 100644 index 00000000..5b5701b9 --- /dev/null +++ b/ToDo.txt @@ -0,0 +1,284 @@ +DONE - Pointer-to-function types. + +DONE - Nested declarators. + +DONE - Varargs C func defs and calls. + +DONE - * and ** args in Python func defs. + +DONE - Default argument values. + +DONE - Tracebacks. + +DONE - Disallow creating char * from Python temporary anywhere + (not just on assignment). + +DONE - Module and function and class doc strings. + +DONE - Predeclare C functions. + +DONE - Constant expressions. + +DONE - Forward C struct declarations. + +DONE - Prefix & operator. + +DONE - Get rid of auto string->char coercion and + add a c'X' syntax for char literals. + +DONE - Cascaded assignments (a = b = c). + +DONE - 'include' statement for including other Pyrex files. + +DONE - Add command line option for specifying name of generated C file. + +DONE - Add 'else' clause to try-except. + +DONE - Allow extension types to be declared "public" so they + can be accessed from another Pyrex module or a C file. + +DONE - Don't try to generate objstruct definition for external + extension type declared without suite (treat as though + declared with empty suite). + +DONE - Implement two-argument form of 'assert' statement. + +Const types. + +Tuple/list construction: Evaluate & store items one at a time? + +Varargs argument traversal. + +Use PyDict_SetItemString to build keyword arg dicts? +(Or wait until names are interned.) + +Intern names. + +print >>file + +abs() and anything similar. + +Semicolon-separated statement lists. + +Optional semicolons after C declarations. + +Multiple C declarations on one line? + +Optimise return without value outside of try-finally. + +exec statement. + +from ... import statement. + +Use iterator protocol for unpacking. + +Save & restore exception being handled on function entry/exit. + +In-place operators (+=, etc). + +Constant declarations. Syntax? + +DONE - Some way for C functions to signal Python errors? + +Check for lack of return with value in non-void C functions? + +Allow 'pass' in struct/union/enum definition. + +Make C structs callable as constructors. + +DONE - Provide way of specifying C names. + +DONE - Public cdefs. + +When calling user __dealloc__ func, save & restore exception. + +DONE - Forward declaration of extension types. + +Complex number parsetuple format? + +DONE - long long type + +DONE - long double type? + +Windows __fooblarg function declaration things. + +Generate type, var and func declarations in the same order that +they appear in the source file. + +Provide a way of declaring a C function as returning a +borrowed Python reference. + +Provide a way of specifying whether a Python object obtained +by casting a pointer should be treated as a new reference +or not. + +Optimize integer for-loops. + +Make sizeof() take types as well as variables. + +Allow "unsigned" to be used alone as a type name. + +Allow duplicate declarations, at least in extern-from. + +Do something about installing proper version of pyrexc +script according to platform in setup.py. + +DONE - Add "-o filename" command line option to unix/dos versions. + +Recognise #line directives? + +Catch floating point exceptions? + +Check that forward-declared non-external extension types +are defined. + +Generate type test when casting from one Python type +to another. + +Generate a Pyrex include file for public declarations +as well as a C one. + +Syntax for defining indefinite-sized int & float types. + +Allow ranges of exception values. + +Support "complex double" and "complex float"? + +Allow module-level Python variables to be declared extern. + +Consider: +>cdef extern from "foo.h": +> int dosomething() except -1 raise MyException + +Properties for Python types. + +DONE - Properties for extension types. + +Find a way to make classmethod and staticmethod work better. + +DONE - Document workarounds for classmethod and staticmethod. + +Statically initialised C arrays & structs. + +Reduce generation of unused vars and unreachable code? + +Support for acquiring and releasing GIL. + +Make docstrings of extension type special methods work. + +Treat result of getting C attribute of extension type as non-ephemeral. + +Make None a reserved identifier. + +Teach it about builtin functions that correspond to +Python/C API calls. + +Teach it about common builtin types. + +Option for generating a main() function? + +DONE - Allow an extension type to inherit from another type. + +Do something about external C functions declared as returning +const * types? + +Use PyString_FromStringAndSize for string literals? + +DONE - C functions as methods of extension types. + +What to do about __name__ etc. attributes of a module (they are +currently assumed to be built-in names). + +Use PyDict_GetItem etc. on module & builtins dicts for speed. + +Intern all string literals used as Python strings? +[Koshy ] + +Make extension types weak-referenceable. +[Matthias Baas ] + +Make 'pass' work in the body of an extern-from struct +or union. + +Disallow a filename which results in an illegal identifier when +used as a module name. + +Use ctypedef names. + +Provide an easy way of exposing a set of enum values as Python names. +[John J Lee ] + +Prevent user from returning a value from special methods that +return an error indicator only. + +Use PyObject_TypeCheck instead of PyObject_IsInstance? + +Allow * in cimport? [John J Lee ] + +FAQ: Q. Pyrex says my extension type object has no attribute 'rhubarb', but + I know it does. + + A. Have you declared the type at the point where you're using it? + +Eliminate lvalue casts! (Illegal in C++, also disallowed by some C compilers) +[Matthias Baas ] + +Make Python class construction work more like it does in Python. + +Give the right module name to Python classes. + +Command line switch for full pathnames in backtraces? + +Use PyString_FromStringAndSize on string literals containing +nulls. + +Peephole optimisation? [Vladislav Bulatov ] + +Avoid PyArg_ParseTuple call when a function takes no positional args. + +Omit incref/decref of arguments that are not assigned to? + +Can a faster way of instantiating extension types be found? + +Disallow declaring a special method of an extension type with +'cdef' instead of 'def'. + +Use PySequence_GetItem instead of PyObject_GetItem when index +is an integer. + +If a __getitem__ method is declared with an int index, use the +sq_item slot instead of the mp_subscript slot. + +Provide some way of controlling the argument list passed to +an extension type's base __new__ method? +[Alain Pointdexter ] + +Rename __new__ in extension types to __alloc__. + +Implement a true __new__ for extension types. + +Way to provide constructors for extension types that are not +available to Python and can accept C types directly? + +Support generators by turning them into extension types? + +List comprehensions. + +Variable declarations inside inner code blocks? + +Initial values when declaring variables? + +Do something about __stdcall. + +Support class methods in extension types using METH_CLASS flag. + +Disallow defaulting types to 'object' in C declarations? + +C globals with static initialisers. + +Find a way of providing C-only initialisers for extension types. + +Metaclasses for extension types? + +Make extension types use Py_TPFLAGS_HEAPTYPE so their __module__ +will get set dynamically? diff --git a/Tools/pyrex-mode.el b/Tools/pyrex-mode.el new file mode 100644 index 00000000..1493a9d8 --- /dev/null +++ b/Tools/pyrex-mode.el @@ -0,0 +1 @@ +;;;; `Pyrex' mode. (add-to-list 'auto-mode-alist '("\\.pyx\\'" . pyrex-mode)) (define-derived-mode pyrex-mode python-mode "Pyrex" (font-lock-add-keywords nil `((,(concat "\\<\\(NULL" "\\|c\\(def\\|har\\|typedef\\)" "\\|e\\(num\\|xtern\\)" "\\|float" "\\|in\\(clude\\|t\\)" "\\|object\\|public\\|struct\\|type\\|union\\|void" "\\)\\>") 1 font-lock-keyword-face t)))) \ No newline at end of file diff --git a/Tools/pyrex.st b/Tools/pyrex.st new file mode 100644 index 00000000..d4fe4ecf --- /dev/null +++ b/Tools/pyrex.st @@ -0,0 +1 @@ +/** * Name: pyrex * Description: Pyrex - a Language for Writing Python Extension Modules * Author: Markku Rossi */ state pyrex extends python { /* Additional keywords. (build-re '( NULL as cdef char ctypedef double enum extern float include int long private public short signed sizeof struct union unsigned void )) */ /\b(NULL|as|c(def|har|typedef)|double|e(num|xtern)|float|in(clude|t)\ |long|p(rivate|ublic)|s(hort|i(gned|zeof)|truct)|un(ion|signed)|void)\b/ { keyword_face(true); language_print($0); keyword_face(false); } } /* Local variables: mode: c End: */ \ No newline at end of file diff --git a/USAGE.txt b/USAGE.txt new file mode 100644 index 00000000..3128cd01 --- /dev/null +++ b/USAGE.txt @@ -0,0 +1,69 @@ +Pyrex - Usage Instructions +========================== + +Building Pyrex extensions using distutils +----------------------------------------- + +Pyrex comes with an experimental distutils extension for compiling +Pyrex modules, contributed by Graham Fawcett of the University of +Windsor (fawcett@uwindsor.ca). + +The Demos directory contains a setup.py file demonstrating its use. To +compile the demos: + +(1) cd Demos + +(2) python setup.py build_ext --inplace + + or + + python setup.py build --build-lib=. + +(You may get a screed of warnings from the C compiler, but you can +ignore these -- as long as there are no actual errors, things are +probably okay.) + +Try out the extensions with: + + python run_primes.py + python run_spam.py + python run_numeric_demo.py + + +Building Pyrex extensions by hand +--------------------------------- + +You can also invoke the Pyrex compiler on its own to translate a .pyx +file to a .c file. On Unix, + + pyrexc filename.pyx + +On other platforms, + + python pyrexc.py filename.pyx + +It's then up to you to compile and link the .c file using whatever +procedure is appropriate for your platform. The file +Makefile.nodistutils in the Demos directory shows how to do this for +one particular Unix system. + + +Command line options +-------------------- + +The pyrexc command supports the following options: + + Short Long Argument Description + ----------------------------------------------------------------------------- + -v --version Display version number of pyrex compiler + -l --create-listing Write error messages to a .lis file + -I --include-dir Search for include files in named + directory (may be repeated) + -o --output-file Specify name of generated C file (only + one source file allowed if this is used) + +Anything else is taken as the name of a Pyrex source file and compiled +to a C source file. Multiple Pyrex source files can be specified +(unless -o is used), in which case each source file is treated as the +source of a distinct extension module and compiled separately to +produce its own C file. diff --git a/bin/cython b/bin/cython new file mode 100644 index 00000000..b604d4d0 --- /dev/null +++ b/bin/cython @@ -0,0 +1,8 @@ +#!/usr/bin/env python + +# +# Pyrex -- Main Program, Unix +# + +from Pyrex.Compiler.Main import main +main(command_line = 1) diff --git a/bin/update_references b/bin/update_references new file mode 100644 index 00000000..70277781 --- /dev/null +++ b/bin/update_references @@ -0,0 +1,49 @@ +#!/usr/bin/env python +# +# Go through the Tests directory and its subdirectories +# copying the latest versions of the test outputs into +# the Reference directories. +# + +import os, sys + +ignore_names = [".DS_Store", "Icon\r"] + +def copy_file(from_path, to_path): + # We copy the contents from one file to the other + # so as to preserve metadata on the Mac. + #print from_path, "-->", to_path + f = open(from_path) + g = open(to_path, "w+") + g.write(f.read()) + f.close() + g.close() + +def update_references(out_dir, ref_dir): + for name in os.listdir(ref_dir): + if name not in ignore_names: + out_file = os.path.join(out_dir, name) + ref_file = os.path.join(ref_dir, name) + if os.path.isfile(out_file): + print "Updating", name + copy_file(out_file, ref_file) + +def update_references_in_dir(dir): + print "Updating references in", dir + for name in os.listdir(dir): + if name <> "Reference" and not name.startswith("("): + item_path = os.path.join(dir, name) + if os.path.isdir(item_path): + update_references_in_dir(item_path) + ref_dir = os.path.join(dir, "Reference") + if os.path.isdir(ref_dir): + update_references(dir, ref_dir) + +def main(): + bin_dir = os.path.dirname(sys.argv[0]) + source_dir = os.path.dirname(bin_dir) + tests_dir = os.path.join(source_dir, "Tests") + update_references_in_dir(tests_dir) + +if __name__ == "__main__": + main() diff --git a/cython.py b/cython.py new file mode 100644 index 00000000..f32cf153 --- /dev/null +++ b/cython.py @@ -0,0 +1,6 @@ +# +# Pyrex -- Main Program, generic +# + +from Pyrex.Compiler.Main import main +main(command_line = 1) diff --git a/setup.py b/setup.py new file mode 100644 index 00000000..cd1945a1 --- /dev/null +++ b/setup.py @@ -0,0 +1,31 @@ +from distutils.core import setup +from distutils.sysconfig import get_python_lib +import os +from Cython.Compiler.Version import version + +compiler_dir = os.path.join(get_python_lib(prefix=''), 'Cython/Compiler') + +if os.name == "posix": + scripts = ["bin/cython"] +else: + scripts = ["cython.py"] + +setup( + name = 'Cython', + version = version, + url = 'http://www.cython.org', + author = 'Greg Ewing, William Stein, Robert Bradshaw, Stefan Behnel, et al.', + author_email = 'wstein@gmail.com', + scripts = scripts, + packages=[ + 'Cython', + 'Cython.Compiler', + 'Cython.Distutils', + 'Cython.Mac', + 'Cython.Plex' + ], + data_files=[ + (compiler_dir, ['Cython/Compiler/Lexicon.pickle']) + ] + ) +