Commit 79bb7559 authored by Stefan Behnel's avatar Stefan Behnel

clean up Pyrex comparison doc page

parent c200ca4f
.. _pure-mode:
Pure Python Mode Pure Python Mode
================ ================
......
...@@ -43,7 +43,7 @@ generally preferred to use the usual :keyword:`for` ... :keyword:`in` ...@@ -43,7 +43,7 @@ generally preferred to use the usual :keyword:`for` ... :keyword:`in`
.. note:: see :ref:`automatic-range-conversion` .. note:: see :ref:`automatic-range-conversion`
Note that Cython also supports set literals starting from Python 2.3. Note that Cython also supports set literals starting from Python 2.4.
Keyword-only arguments Keyword-only arguments
---------------------- ----------------------
...@@ -68,8 +68,8 @@ takes exactly two positional parameters and has two required keyword parameters. ...@@ -68,8 +68,8 @@ takes exactly two positional parameters and has two required keyword parameters.
Conditional expressions "x if b else y" (Python 2.5) Conditional expressions "x if b else y"
===================================================== =========================================
Conditional expressions as described in Conditional expressions as described in
http://www.python.org/dev/peps/pep-0308/:: http://www.python.org/dev/peps/pep-0308/::
...@@ -156,8 +156,8 @@ Cython. One can declare variables and return values for functions to be of the ...@@ -156,8 +156,8 @@ Cython. One can declare variables and return values for functions to be of the
cdef bint b = x cdef bint b = x
The first conversion would happen via ``x.__int__()`` whereas the second would The first conversion would happen via ``x.__int__()`` whereas the second would
happen via ``x.__nonzero__()``. (Actually, if ``x`` is the python object happen via ``x.__bool__()`` (a.k.a. ``__nonzero__()``), with appropriate
``True`` or ``False`` then no method call is made.) optimisations for known builtin types.
Executable class bodies Executable class bodies
======================= =======================
...@@ -170,7 +170,7 @@ Including a working :func:`classmethod`:: ...@@ -170,7 +170,7 @@ Including a working :func:`classmethod`::
some_method = classmethod(some_method) some_method = classmethod(some_method)
a = 2*3 a = 2*3
print "hi", a print "hi", a
cpdef functions cpdef functions
================= =================
...@@ -181,18 +181,18 @@ essentially think of a :keyword:`cpdef` method as a :keyword:`cdef` method + ...@@ -181,18 +181,18 @@ essentially think of a :keyword:`cpdef` method as a :keyword:`cdef` method +
some extras. (That's how it's implemented at least.) First, it creates a some extras. (That's how it's implemented at least.) First, it creates a
:keyword:`def` method that does nothing but call the underlying :keyword:`def` method that does nothing but call the underlying
:keyword:`cdef` method (and does argument unpacking/coercion if needed). At :keyword:`cdef` method (and does argument unpacking/coercion if needed). At
the top of the :keyword:`cdef` method a little bit of code is added to check the top of the :keyword:`cdef` method a little bit of code is added to see
to see if it's overridden. Specifically, in pseudocode:: if it's overridden, similar to the following pseudocode::
if type(self) has a __dict__: if hasattr(type(self), '__dict__'):
foo = self.getattr('foo') foo = self.foo
if foo is not wrapper_foo: if foo is not wrapper_foo:
return foo(args) return foo(args)
[cdef method body] [cdef method body]
To detect whether or not a type has a dictionary, it just checks the To detect whether or not a type has a dictionary, it just checks the
tp_dictoffset slot, which is ``NULL`` (by default) for extension types, but ``tp_dictoffset`` slot, which is ``NULL`` (by default) for extension types,
non- null for instance classes. If the dictionary exists, it does a single but non- null for instance classes. If the dictionary exists, it does a single
attribute lookup and can tell (by comparing pointers) whether or not the attribute lookup and can tell (by comparing pointers) whether or not the
returned result is actually a new function. If, and only if, it is a new returned result is actually a new function. If, and only if, it is a new
function, then the arguments packed into a tuple and the method called. This function, then the arguments packed into a tuple and the method called. This
...@@ -240,10 +240,10 @@ In Cython ``<type>x`` will try and do a coercion (as would happen on assignment ...@@ -240,10 +240,10 @@ In Cython ``<type>x`` will try and do a coercion (as would happen on assignment
It does not stop one from casting where there is no conversion (though it will It does not stop one from casting where there is no conversion (though it will
emit a warning). If one really wants the address, cast to a ``void *`` first. emit a warning). If one really wants the address, cast to a ``void *`` first.
As in Pyrex ``<MyExtensionType>x`` will cast ``x`` to type :c:type:`MyExtensionType` without any As in Pyrex ``<MyExtensionType>x`` will cast ``x`` to type :c:type:`MyExtensionType`
type checking. Cython supports the syntax ``<MyExtensionType?>`` to do the cast without any type checking. Cython supports the syntax ``<MyExtensionType?>`` to do
with type checking (i.e. it will throw an error if ``x`` is not a (subclass of) the cast with type checking (i.e. it will throw an error if ``x`` is not a
:c:type:`MyExtensionType`. (subclass of) :c:type:`MyExtensionType`.
Optional arguments in cdef/cpdef functions Optional arguments in cdef/cpdef functions
============================================ ============================================
...@@ -329,7 +329,8 @@ Cython emits a (non-spoofable and faster) typecheck whenever ...@@ -329,7 +329,8 @@ Cython emits a (non-spoofable and faster) typecheck whenever
From __future__ directives From __future__ directives
========================== ==========================
Cython supports several from __future__ directives, namely ``unicode_literals`` and ``division``. Cython supports several ``from __future__ import ...`` directives, namely
``absolute_import``, ``unicode_literals``, ``print_function`` and ``division``.
With statements are always enabled. With statements are always enabled.
...@@ -340,7 +341,4 @@ Cython has support for compiling ``.py`` files, and ...@@ -340,7 +341,4 @@ Cython has support for compiling ``.py`` files, and
accepting type annotations using decorators and other accepting type annotations using decorators and other
valid Python syntax. This allows the same source to valid Python syntax. This allows the same source to
be interpreted as straight Python, or compiled for be interpreted as straight Python, or compiled for
optimized results. optimized results. See :ref:`pure-mode` for more details.
See http://wiki.cython.org/pure
for more details.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment