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/c-api/intro.rst

    r2 r391  
    4242
    4343This implies inclusion of the following standard headers: ``<stdio.h>``,
    44 ``<string.h>``, ``<errno.h>``, ``<limits.h>``, and ``<stdlib.h>`` (if
    45 available).
     44``<string.h>``, ``<errno.h>``, ``<limits.h>``, ``<assert.h>`` and ``<stdlib.h>``
     45(if available).
    4646
    4747.. note::
     
    8989
    9090Most Python/C API functions have one or more arguments as well as a return value
    91 of type :ctype:`PyObject\*`.  This type is a pointer to an opaque data type
     91of type :c:type:`PyObject\*`.  This type is a pointer to an opaque data type
    9292representing an arbitrary Python object.  Since all Python object types are
    9393treated the same way by the Python language in most situations (e.g.,
     
    9595should be represented by a single C type.  Almost all Python objects live on the
    9696heap: you never declare an automatic or static variable of type
    97 :ctype:`PyObject`, only pointer variables of type :ctype:`PyObject\*` can  be
     97:c:type:`PyObject`, only pointer variables of type :c:type:`PyObject\*` can  be
    9898declared.  The sole exception are the type objects; since these must never be
    99 deallocated, they are typically static :ctype:`PyTypeObject` objects.
     99deallocated, they are typically static :c:type:`PyTypeObject` objects.
    100100
    101101All Python objects (even Python integers) have a :dfn:`type` and a
     
    128128
    129129Reference counts are always manipulated explicitly.  The normal way is  to use
    130 the macro :cfunc:`Py_INCREF` to increment an object's reference count by one,
    131 and :cfunc:`Py_DECREF` to decrement it by   one.  The :cfunc:`Py_DECREF` macro
     130the macro :c:func:`Py_INCREF` to increment an object's reference count by one,
     131and :c:func:`Py_DECREF` to decrement it by   one.  The :c:func:`Py_DECREF` macro
    132132is considerably more complex than the incref one, since it must check whether
    133133the reference count becomes zero and then cause the object's deallocator to be
     
    160160and possible deallocating it. The real danger is that innocent-looking
    161161operations may invoke arbitrary Python code which could do this; there is a code
    162 path which allows control to flow back to the user from a :cfunc:`Py_DECREF`, so
     162path which allows control to flow back to the user from a :c:func:`Py_DECREF`, so
    163163almost any operation is potentially dangerous.
    164164
     
    166166begins with ``PyObject_``, ``PyNumber_``, ``PySequence_`` or ``PyMapping_``).
    167167These operations always increment the reference count of the object they return.
    168 This leaves the caller with the responsibility to call :cfunc:`Py_DECREF` when
     168This leaves the caller with the responsibility to call :c:func:`Py_DECREF` when
    169169they are done with the result; this soon becomes second nature.
    170170
     
    181181reference is no longer needed.  Ownership can also be transferred, meaning that
    182182the code that receives ownership of the reference then becomes responsible for
    183 eventually decref'ing it by calling :cfunc:`Py_DECREF` or :cfunc:`Py_XDECREF`
     183eventually decref'ing it by calling :c:func:`Py_DECREF` or :c:func:`Py_XDECREF`
    184184when it's no longer needed---or passing on this responsibility (usually to its
    185185caller). When a function passes ownership of a reference on to its caller, the
     
    199199
    200200Few functions steal references; the two notable exceptions are
    201 :cfunc:`PyList_SetItem` and :cfunc:`PyTuple_SetItem`, which  steal a reference
     201:c:func:`PyList_SetItem` and :c:func:`PyTuple_SetItem`, which  steal a reference
    202202to the item (but not to the tuple or list into which the item is put!).  These
    203203functions were designed to steal a reference because of a common idiom for
     
    213213   PyTuple_SetItem(t, 2, PyString_FromString("three"));
    214214
    215 Here, :cfunc:`PyInt_FromLong` returns a new reference which is immediately
    216 stolen by :cfunc:`PyTuple_SetItem`.  When you want to keep using an object
    217 although the reference to it will be stolen, use :cfunc:`Py_INCREF` to grab
     215Here, :c:func:`PyInt_FromLong` returns a new reference which is immediately
     216stolen by :c:func:`PyTuple_SetItem`.  When you want to keep using an object
     217although the reference to it will be stolen, use :c:func:`Py_INCREF` to grab
    218218another reference before calling the reference-stealing function.
    219219
    220 Incidentally, :cfunc:`PyTuple_SetItem` is the *only* way to set tuple items;
    221 :cfunc:`PySequence_SetItem` and :cfunc:`PyObject_SetItem` refuse to do this
     220Incidentally, :c:func:`PyTuple_SetItem` is the *only* way to set tuple items;
     221:c:func:`PySequence_SetItem` and :c:func:`PyObject_SetItem` refuse to do this
    222222since tuples are an immutable data type.  You should only use
    223 :cfunc:`PyTuple_SetItem` for tuples that you are creating yourself.
    224 
    225 Equivalent code for populating a list can be written using :cfunc:`PyList_New`
    226 and :cfunc:`PyList_SetItem`.
     223:c:func:`PyTuple_SetItem` for tuples that you are creating yourself.
     224
     225Equivalent code for populating a list can be written using :c:func:`PyList_New`
     226and :c:func:`PyList_SetItem`.
    227227
    228228However, in practice, you will rarely use these ways of creating and populating
    229 a tuple or list.  There's a generic function, :cfunc:`Py_BuildValue`, that can
     229a tuple or list.  There's a generic function, :c:func:`Py_BuildValue`, that can
    230230create most common objects from C values, directed by a :dfn:`format string`.
    231231For example, the above two blocks of code could be replaced by the following
     
    237237   list = Py_BuildValue("[iis]", 1, 2, "three");
    238238
    239 It is much more common to use :cfunc:`PyObject_SetItem` and friends with items
     239It is much more common to use :c:func:`PyObject_SetItem` and friends with items
    240240whose references you are only borrowing, like arguments that were passed in to
    241241the function you are writing.  In that case, their behaviour regarding reference
     
    256256           if (!index)
    257257               return -1;
    258            if (PyObject_SetItem(target, index, item) < 0)
     258           if (PyObject_SetItem(target, index, item) < 0) {
     259               Py_DECREF(index);
    259260               return -1;
     261       }
    260262           Py_DECREF(index);
    261263       }
     
    271273returned object is created  on the fly, and the reference you get is the only
    272274reference to the  object.  Therefore, the generic functions that return object
    273 references, like :cfunc:`PyObject_GetItem` and  :cfunc:`PySequence_GetItem`,
     275references, like :c:func:`PyObject_GetItem` and  :c:func:`PySequence_GetItem`,
    274276always return a new reference (the caller becomes the owner of the reference).
    275277
     
    277279function depends on which function you call only --- *the plumage* (the type of
    278280the object passed as an argument to the function) *doesn't enter into it!*
    279 Thus, if you  extract an item from a list using :cfunc:`PyList_GetItem`, you
     281Thus, if you  extract an item from a list using :c:func:`PyList_GetItem`, you
    280282don't own the reference --- but if you obtain the same item from the same list
    281 using :cfunc:`PySequence_GetItem` (which happens to take exactly the same
     283using :c:func:`PySequence_GetItem` (which happens to take exactly the same
    282284arguments), you do own a reference to the returned object.
    283285
     
    287289
    288290Here is an example of how you could write a function that computes the sum of
    289 the items in a list of integers; once using  :cfunc:`PyList_GetItem`, and once
    290 using :cfunc:`PySequence_GetItem`. ::
     291the items in a list of integers; once using  :c:func:`PyList_GetItem`, and once
     292using :c:func:`PySequence_GetItem`. ::
    291293
    292294   long
     
    341343
    342344There are few other data types that play a significant role in  the Python/C
    343 API; most are simple C types such as :ctype:`int`,  :ctype:`long`,
    344 :ctype:`double` and :ctype:`char\*`.  A few structure types  are used to
     345API; most are simple C types such as :c:type:`int`,  :c:type:`long`,
     346:c:type:`double` and :c:type:`char\*`.  A few structure types  are used to
    345347describe static tables used to list the functions exported  by a module or the
    346348data attributes of a new object type, and another is used to describe the value
     
    362364.. index:: single: PyErr_Occurred()
    363365
    364 For C programmers, however, error checking always has to be explicit.   All
    365 functions in the Python/C API can raise exceptions, unless an  explicit claim is
    366 made otherwise in a function's documentation.  In  general, when a function
    367 encounters an error, it sets an exception,  discards any object references that
    368 it owns, and returns an  error indicator --- usually *NULL* or ``-1``.  A few
    369 functions  return a Boolean true/false result, with false indicating an error.
    370 Very few functions return no explicit error indicator or have an  ambiguous
    371 return value, and require explicit testing for errors with
    372 :cfunc:`PyErr_Occurred`.
     366For C programmers, however, error checking always has to be explicit.  All
     367functions in the Python/C API can raise exceptions, unless an explicit claim is
     368made otherwise in a function's documentation.  In general, when a function
     369encounters an error, it sets an exception, discards any object references that
     370it owns, and returns an error indicator.  If not documented otherwise, this
     371indicator is either *NULL* or ``-1``, depending on the function's return type.
     372A few functions return a Boolean true/false result, with false indicating an
     373error.  Very few functions return no explicit error indicator or have an
     374ambiguous return value, and require explicit testing for errors with
     375:c:func:`PyErr_Occurred`.  These exceptions are always explicitly documented.
    373376
    374377.. index::
     
    379382using global storage in an unthreaded application).  A  thread can be in one of
    380383two states: an exception has occurred, or not. The function
    381 :cfunc:`PyErr_Occurred` can be used to check for this: it returns a borrowed
     384:c:func:`PyErr_Occurred` can be used to check for this: it returns a borrowed
    382385reference to the exception type object when an exception has occurred, and
    383386*NULL* otherwise.  There are a number of functions to set the exception state:
    384 :cfunc:`PyErr_SetString` is the most common (though not the most general)
    385 function to set the exception state, and :cfunc:`PyErr_Clear` clears the
     387:c:func:`PyErr_SetString` is the most common (though not the most general)
     388function to set the exception state, and :c:func:`PyErr_Clear` clears the
    386389exception state.
    387390
     
    424427
    425428A simple example of detecting exceptions and passing them on is shown in the
    426 :cfunc:`sum_sequence` example above.  It so happens that that example doesn't
     429:c:func:`sum_sequence` example above.  It so happens that this example doesn't
    427430need to clean up any owned references when it detects an error.  The following
    428431example function shows some error cleanup.  First, to remind you why you like
     
    491494
    492495This example represents an endorsed use of the ``goto`` statement  in C!
    493 It illustrates the use of :cfunc:`PyErr_ExceptionMatches` and
    494 :cfunc:`PyErr_Clear` to handle specific exceptions, and the use of
    495 :cfunc:`Py_XDECREF` to dispose of owned references that may be *NULL* (note the
    496 ``'X'`` in the name; :cfunc:`Py_DECREF` would crash when confronted with a
     496It illustrates the use of :c:func:`PyErr_ExceptionMatches` and
     497:c:func:`PyErr_Clear` to handle specific exceptions, and the use of
     498:c:func:`Py_XDECREF` to dispose of owned references that may be *NULL* (note the
     499``'X'`` in the name; :c:func:`Py_DECREF` would crash when confronted with a
    497500*NULL* reference).  It is important that the variables used to hold owned
    498501references are initialized to *NULL* for this to work; likewise, the proposed
     
    520523   single: path (in module sys)
    521524
    522 The basic initialization function is :cfunc:`Py_Initialize`. This initializes
     525The basic initialization function is :c:func:`Py_Initialize`. This initializes
    523526the table of loaded modules, and creates the fundamental modules
    524527:mod:`__builtin__`, :mod:`__main__`, :mod:`sys`, and :mod:`exceptions`.  It also
    525528initializes the module search path (``sys.path``).
    526529
    527 .. index:: single: PySys_SetArgv()
    528 
    529 :cfunc:`Py_Initialize` does not set the "script argument list"  (``sys.argv``).
    530 If this variable is needed by Python code that  will be executed later, it must
    531 be set explicitly with a call to  ``PySys_SetArgv(argc, argv)`` subsequent to
    532 the call to :cfunc:`Py_Initialize`.
     530.. index:: single: PySys_SetArgvEx()
     531
     532:c:func:`Py_Initialize` does not set the "script argument list"  (``sys.argv``).
     533If this variable is needed by Python code that will be executed later, it must
     534be set explicitly with a call to  ``PySys_SetArgvEx(argc, argv, updatepath)``
     535after the call to :c:func:`Py_Initialize`.
    533536
    534537On most systems (in particular, on Unix and Windows, although the details are
    535 slightly different), :cfunc:`Py_Initialize` calculates the module search path
     538slightly different), :c:func:`Py_Initialize` calculates the module search path
    536539based upon its best guess for the location of the standard Python interpreter
    537540executable, assuming that the Python library is found in a fixed location
     
    557560
    558561The embedding application can steer the search by calling
    559 ``Py_SetProgramName(file)`` *before* calling  :cfunc:`Py_Initialize`.  Note that
     562``Py_SetProgramName(file)`` *before* calling  :c:func:`Py_Initialize`.  Note that
    560563:envvar:`PYTHONHOME` still overrides this and :envvar:`PYTHONPATH` is still
    561564inserted in front of the standard path.  An application that requires total
    562 control has to provide its own implementation of :cfunc:`Py_GetPath`,
    563 :cfunc:`Py_GetPrefix`, :cfunc:`Py_GetExecPrefix`, and
    564 :cfunc:`Py_GetProgramFullPath` (all defined in :file:`Modules/getpath.c`).
     565control has to provide its own implementation of :c:func:`Py_GetPath`,
     566:c:func:`Py_GetPrefix`, :c:func:`Py_GetExecPrefix`, and
     567:c:func:`Py_GetProgramFullPath` (all defined in :file:`Modules/getpath.c`).
    565568
    566569.. index:: single: Py_IsInitialized()
     
    568571Sometimes, it is desirable to "uninitialize" Python.  For instance,  the
    569572application may want to start over (make another call to
    570 :cfunc:`Py_Initialize`) or the application is simply done with its  use of
     573:c:func:`Py_Initialize`) or the application is simply done with its  use of
    571574Python and wants to free memory allocated by Python.  This can be accomplished
    572 by calling :cfunc:`Py_Finalize`.  The function :cfunc:`Py_IsInitialized` returns
     575by calling :c:func:`Py_Finalize`.  The function :c:func:`Py_IsInitialized` returns
    573576true if Python is currently in the initialized state.  More information about
    574 these functions is given in a later chapter. Notice that :cfunc:`Py_Finalize`
     577these functions is given in a later chapter. Notice that :c:func:`Py_Finalize`
    575578does *not* free all memory allocated by the Python interpreter, e.g. memory
    576579allocated by extension modules currently cannot be released.
     
    592595frequently-used builds will be described in the remainder of this section.
    593596
    594 Compiling the interpreter with the :cmacro:`Py_DEBUG` macro defined produces
    595 what is generally meant by "a debug build" of Python. :cmacro:`Py_DEBUG` is
    596 enabled in the Unix build by adding :option:`--with-pydebug` to the
    597 :file:`configure` command.  It is also implied by the presence of the
    598 not-Python-specific :cmacro:`_DEBUG` macro.  When :cmacro:`Py_DEBUG` is enabled
     597Compiling the interpreter with the :c:macro:`Py_DEBUG` macro defined produces
     598what is generally meant by "a debug build" of Python. :c:macro:`Py_DEBUG` is
     599enabled in the Unix build by adding ``--with-pydebug`` to the
     600:file:`./configure` command.  It is also implied by the presence of the
     601not-Python-specific :c:macro:`_DEBUG` macro.  When :c:macro:`Py_DEBUG` is enabled
    599602in the Unix build, compiler optimization is disabled.
    600603
     
    625628There may be additional checks not mentioned here.
    626629
    627 Defining :cmacro:`Py_TRACE_REFS` enables reference tracing.  When defined, a
     630Defining :c:macro:`Py_TRACE_REFS` enables reference tracing.  When defined, a
    628631circular doubly linked list of active objects is maintained by adding two extra
    629 fields to every :ctype:`PyObject`.  Total allocations are tracked as well.  Upon
     632fields to every :c:type:`PyObject`.  Total allocations are tracked as well.  Upon
    630633exit, all existing references are printed.  (In interactive mode this happens
    631 after every statement run by the interpreter.)  Implied by :cmacro:`Py_DEBUG`.
     634after every statement run by the interpreter.)  Implied by :c:macro:`Py_DEBUG`.
    632635
    633636Please refer to :file:`Misc/SpecialBuilds.txt` in the Python source distribution
Note: See TracChangeset for help on using the changeset viewer.