Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Doc/howto/cporting.rst

    r2 r391  
    11.. highlightlang:: c
    22
    3 ********************************
    4 Porting Extension Modules to 3.0
    5 ********************************
     3.. _cporting-howto:
     4
     5*************************************
     6Porting Extension Modules to Python 3
     7*************************************
    68
    79:author: Benjamin Peterson
     
    1012.. topic:: Abstract
    1113
    12    Although changing the C-API was not one of Python 3.0's objectives, the many
    13    Python level changes made leaving 2.x's API intact impossible.  In fact, some
    14    changes such as :func:`int` and :func:`long` unification are more obvious on
    15    the C level.  This document endeavors to document incompatibilities and how
    16    they can be worked around.
     14   Although changing the C-API was not one of Python 3's objectives,
     15   the many Python-level changes made leaving Python 2's API intact
     16   impossible.  In fact, some changes such as :func:`int` and
     17   :func:`long` unification are more obvious on the C level.  This
     18   document endeavors to document incompatibilities and how they can
     19   be worked around.
    1720
    1821
     
    2023=======================
    2124
    22 The easiest way to compile only some code for 3.0 is to check if
    23 :cmacro:`PY_MAJOR_VERSION` is greater than or equal to 3. ::
     25The easiest way to compile only some code for Python 3 is to check
     26if :c:macro:`PY_MAJOR_VERSION` is greater than or equal to 3. ::
    2427
    2528   #if PY_MAJOR_VERSION >= 3
     
    3437======================
    3538
    36 Python 3.0 merged together some types with similar functions while cleanly
     39Python 3 merged together some types with similar functions while cleanly
    3740separating others.
    3841
     
    4245
    4346
    44 Python 3.0's :func:`str` (``PyString_*`` functions in C) type is equivalent to
    45 2.x's :func:`unicode` (``PyUnicode_*``).  The old 8-bit string type has become
    46 :func:`bytes`.  Python 2.6 and later provide a compatibility header,
     47Python 3's :func:`str` (``PyString_*`` functions in C) type is equivalent to
     48Python 2's :func:`unicode` (``PyUnicode_*``).  The old 8-bit string type has
     49become :func:`bytes`.  Python 2.6 and later provide a compatibility header,
    4750:file:`bytesobject.h`, mapping ``PyBytes`` names to ``PyString`` ones.  For best
    48 compatibility with 3.0, :ctype:`PyUnicode` should be used for textual data and
    49 :ctype:`PyBytes` for binary data.  It's also important to remember that
    50 :ctype:`PyBytes` and :ctype:`PyUnicode` in 3.0 are not interchangeable like
    51 :ctype:`PyString` and :ctype:`PyString` are in 2.x.  The following example shows
    52 best practices with regards to :ctype:`PyUnicode`, :ctype:`PyString`, and
    53 :ctype:`PyBytes`. ::
     51compatibility with Python 3, :c:type:`PyUnicode` should be used for textual data and
     52:c:type:`PyBytes` for binary data.  It's also important to remember that
     53:c:type:`PyBytes` and :c:type:`PyUnicode` in Python 3 are not interchangeable like
     54:c:type:`PyString` and :c:type:`PyUnicode` are in Python 2.  The following example
     55shows best practices with regards to :c:type:`PyUnicode`, :c:type:`PyString`,
     56and :c:type:`PyBytes`. ::
    5457
    5558   #include "stdlib.h"
     
    9396--------------------
    9497
    95 In Python 3.0, there is only one integer type.  It is called :func:`int` on the
    96 Python level, but actually corresponds to 2.x's :func:`long` type.  In the
    97 C-API, ``PyInt_*`` functions are replaced by their ``PyLong_*`` neighbors.  The
    98 best course of action here is using the ``PyInt_*`` functions aliased to
    99 ``PyLong_*`` found in :file:`intobject.h`.  The abstract ``PyNumber_*`` APIs
    100 can also be used in some cases. ::
    101 
    102    #include "Python.h"
    103    #include "intobject.h"
    104 
    105    static PyObject *
    106    add_ints(PyObject *self, PyObject *args) {
    107        int one, two;
    108        PyObject *result;
    109 
    110        if (!PyArg_ParseTuple(args, "ii:add_ints", &one, &two))
    111            return NULL;
    112 
    113        return PyInt_FromLong(one + two);
    114    }
    115 
     98Python 3 has only one integer type, :func:`int`.  But it actually
     99corresponds to Python 2's :func:`long` type--the :func:`int` type
     100used in Python 2 was removed.  In the C-API, ``PyInt_*`` functions
     101are replaced by their ``PyLong_*`` equivalents.
    116102
    117103
     
    119105===============================
    120106
    121 Python 3.0 has a revamped extension module initialization system.  (See PEP
    122 :pep:`3121`.)  Instead of storing module state in globals, they should be stored
    123 in an interpreter specific structure.  Creating modules that act correctly in
    124 both 2.x and 3.0 is tricky.  The following simple example demonstrates how. ::
     107Python 3 has a revamped extension module initialization system.  (See
     108:pep:`3121`.)  Instead of storing module state in globals, they should
     109be stored in an interpreter specific structure.  Creating modules that
     110act correctly in both Python 2 and Python 3 is tricky.  The following
     111simple example demonstrates how. ::
    125112
    126113   #include "Python.h"
     
    208195
    209196
     197CObject replaced with Capsule
     198=============================
     199
     200The :c:type:`Capsule` object was introduced in Python 3.1 and 2.7 to replace
     201:c:type:`CObject`.  CObjects were useful,
     202but the :c:type:`CObject` API was problematic: it didn't permit distinguishing
     203between valid CObjects, which allowed mismatched CObjects to crash the
     204interpreter, and some of its APIs relied on undefined behavior in C.
     205(For further reading on the rationale behind Capsules, please see :issue:`5630`.)
     206
     207If you're currently using CObjects, and you want to migrate to 3.1 or newer,
     208you'll need to switch to Capsules.
     209:c:type:`CObject` was deprecated in 3.1 and 2.7 and completely removed in
     210Python 3.2.  If you only support 2.7, or 3.1 and above, you
     211can simply switch to :c:type:`Capsule`.  If you need to support Python 3.0,
     212or versions of Python earlier than 2.7,
     213you'll have to support both CObjects and Capsules.
     214(Note that Python 3.0 is no longer supported, and it is not recommended
     215for production use.)
     216
     217The following example header file :file:`capsulethunk.h` may
     218solve the problem for you.  Simply write your code against the
     219:c:type:`Capsule` API and include this header file after
     220:file:`Python.h`.  Your code will automatically use Capsules
     221in versions of Python with Capsules, and switch to CObjects
     222when Capsules are unavailable.
     223
     224:file:`capsulethunk.h` simulates Capsules using CObjects.  However,
     225:c:type:`CObject` provides no place to store the capsule's "name".  As a
     226result the simulated :c:type:`Capsule` objects created by :file:`capsulethunk.h`
     227behave slightly differently from real Capsules.  Specifically:
     228
     229  * The name parameter passed in to :c:func:`PyCapsule_New` is ignored.
     230
     231  * The name parameter passed in to :c:func:`PyCapsule_IsValid` and
     232    :c:func:`PyCapsule_GetPointer` is ignored, and no error checking
     233    of the name is performed.
     234
     235  * :c:func:`PyCapsule_GetName` always returns NULL.
     236
     237  * :c:func:`PyCapsule_SetName` always raises an exception and
     238    returns failure.  (Since there's no way to store a name
     239    in a CObject, noisy failure of :c:func:`PyCapsule_SetName`
     240    was deemed preferable to silent failure here.  If this is
     241    inconvenient, feel free to modify your local
     242    copy as you see fit.)
     243
     244You can find :file:`capsulethunk.h` in the Python source distribution
     245as :source:`Doc/includes/capsulethunk.h`.  We also include it here for
     246your convenience:
     247
     248.. literalinclude:: ../includes/capsulethunk.h
     249
     250
     251
    210252Other options
    211253=============
     
    213255If you are writing a new extension module, you might consider `Cython
    214256<http://www.cython.org>`_.  It translates a Python-like language to C.  The
    215 extension modules it creates are compatible with Python 3.x and 2.x.
    216 
     257extension modules it creates are compatible with Python 3 and Python 2.
     258
Note: See TracChangeset for help on using the changeset viewer.