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/faq/extending.rst

    r2 r391  
    33=======================
    44
    5 .. contents::
     5.. only:: html
     6
     7   .. contents::
    68
    79.. highlight:: c
     
    2628C++ objects with constructors are probably not a good idea.
    2729
     30
     31.. _c-wrapper-software:
    2832
    2933Writing C is hard; are there any alternatives?
     
    5256<http://cxx.sourceforge.net/>`_ `Boost
    5357<http://www.boost.org/libs/python/doc/index.html>`_, or `Weave
    54 <http://www.scipy.org/site_content/weave>`_ are also alternatives for wrapping
     58<http://www.scipy.org/Weave>`_ are also alternatives for wrapping
    5559C++ libraries.
    5660
     
    5963-----------------------------------------------------
    6064
    61 The highest-level function to do this is :cfunc:`PyRun_SimpleString` which takes
     65The highest-level function to do this is :c:func:`PyRun_SimpleString` which takes
    6266a single string argument to be executed in the context of the module
    6367``__main__`` and returns 0 for success and -1 when an exception occurred
    6468(including ``SyntaxError``).  If you want more control, use
    65 :cfunc:`PyRun_String`; see the source for :cfunc:`PyRun_SimpleString` in
     69:c:func:`PyRun_String`; see the source for :c:func:`PyRun_SimpleString` in
    6670``Python/pythonrun.c``.
    6771
     
    7074---------------------------------------------------------
    7175
    72 Call the function :cfunc:`PyRun_String` from the previous question with the
    73 start symbol :cdata:`Py_eval_input`; it parses an expression, evaluates it and
     76Call the function :c:func:`PyRun_String` from the previous question with the
     77start symbol :c:data:`Py_eval_input`; it parses an expression, evaluates it and
    7478returns its value.
    7579
     
    7882-----------------------------------------------
    7983
    80 That depends on the object's type.  If it's a tuple, :cfunc:`PyTuple_Size`
    81 returns its length and :cfunc:`PyTuple_GetItem` returns the item at a specified
    82 index.  Lists have similar functions, :cfunc:`PyListSize` and
    83 :cfunc:`PyList_GetItem`.
    84 
    85 For strings, :cfunc:`PyString_Size` returns its length and
    86 :cfunc:`PyString_AsString` a pointer to its value.  Note that Python strings may
    87 contain null bytes so C's :cfunc:`strlen` should not be used.
     84That depends on the object's type.  If it's a tuple, :c:func:`PyTuple_Size`
     85returns its length and :c:func:`PyTuple_GetItem` returns the item at a specified
     86index.  Lists have similar functions, :c:func:`PyListSize` and
     87:c:func:`PyList_GetItem`.
     88
     89For strings, :c:func:`PyString_Size` returns its length and
     90:c:func:`PyString_AsString` a pointer to its value.  Note that Python strings may
     91contain null bytes so C's :c:func:`strlen` should not be used.
    8892
    8993To test the type of an object, first make sure it isn't *NULL*, and then use
    90 :cfunc:`PyString_Check`, :cfunc:`PyTuple_Check`, :cfunc:`PyList_Check`, etc.
     94:c:func:`PyString_Check`, :c:func:`PyTuple_Check`, :c:func:`PyList_Check`, etc.
    9195
    9296There is also a high-level API to Python objects which is provided by the
    9397so-called 'abstract' interface -- read ``Include/abstract.h`` for further
    9498details.  It allows interfacing with any kind of Python sequence using calls
    95 like :cfunc:`PySequence_Length`, :cfunc:`PySequence_GetItem`, etc.)  as well as
     99like :c:func:`PySequence_Length`, :c:func:`PySequence_GetItem`, etc.)  as well as
    96100many other useful protocols.
    97101
     
    102106You can't.  Use ``t = PyTuple_New(n)`` instead, and fill it with objects using
    103107``PyTuple_SetItem(t, i, o)`` -- note that this "eats" a reference count of
    104 ``o``, so you have to :cfunc:`Py_INCREF` it.  Lists have similar functions
     108``o``, so you have to :c:func:`Py_INCREF` it.  Lists have similar functions
    105109``PyList_New(n)`` and ``PyList_SetItem(l, i, o)``.  Note that you *must* set all
    106110the tuple items to some value before you pass the tuple to Python code --
     
    111115----------------------------------------
    112116
    113 The :cfunc:`PyObject_CallMethod` function can be used to call an arbitrary
     117The :c:func:`PyObject_CallMethod` function can be used to call an arbitrary
    114118method of an object.  The parameters are the object, the name of the method to
    115 call, a format string like that used with :cfunc:`Py_BuildValue`, and the
     119call, a format string like that used with :c:func:`Py_BuildValue`, and the
    116120argument values::
    117121
     
    121125
    122126This works for any object that has methods -- whether built-in or user-defined.
    123 You are responsible for eventually :cfunc:`Py_DECREF`\ 'ing the return value.
     127You are responsible for eventually :c:func:`Py_DECREF`\ 'ing the return value.
    124128
    125129To call, e.g., a file object's "seek" method with arguments 10, 0 (assuming the
     
    134138   }
    135139
    136 Note that since :cfunc:`PyObject_CallObject` *always* wants a tuple for the
     140Note that since :c:func:`PyObject_CallObject` *always* wants a tuple for the
    137141argument list, to call a function without arguments, pass "()" for the format,
    138142and to call a function with one argument, surround the argument in parentheses,
     
    185189   attr = PyObject_GetAttrString(module, "<attrname>");
    186190
    187 Calling :cfunc:`PyObject_SetAttrString` to assign to variables in the module
     191Calling :c:func:`PyObject_SetAttrString` to assign to variables in the module
    188192also works.
    189193
     
    198202Python type around a C structure (pointer) type will also work for C++ objects.
    199203
    200 For C++ libraries, you can look at `SIP
    201 <http://www.riverbankcomputing.co.uk/sip/>`_, `CXX
    202 <http://cxx.sourceforge.net/>`_, `Boost
    203 <http://www.boost.org/libs/python/doc/index.html>`_, `Weave
    204 <http://www.scipy.org/site_content/weave>`_ or `SWIG <http://www.swig.org>`_
     204For C++ libraries, see :ref:`c-wrapper-software`.
    205205
    206206
     
    270270behavior sufficiently.  IDLE uses this, for example.
    271271
    272 The easiest way to do it in C is to call :cfunc:`PyRun_InteractiveLoop` (perhaps
     272The easiest way to do it in C is to call :c:func:`PyRun_InteractiveLoop` (perhaps
    273273in a separate thread) and let the Python interpreter handle the input for
    274 you. You can also set the :cfunc:`PyOS_ReadlineFunctionPointer` to point at your
     274you. You can also set the :c:func:`PyOS_ReadlineFunctionPointer` to point at your
    275275custom input function. See ``Modules/readline.c`` and ``Parser/myreadline.c``
    276276for more hints.
     
    278278However sometimes you have to run the embedded Python interpreter in the same
    279279thread as your rest application and you can't allow the
    280 :cfunc:`PyRun_InteractiveLoop` to stop while waiting for user input.  The one
    281 solution then is to call :cfunc:`PyParser_ParseString` and test for ``e.error``
     280:c:func:`PyRun_InteractiveLoop` to stop while waiting for user input.  The one
     281solution then is to call :c:func:`PyParser_ParseString` and test for ``e.error``
    282282equal to ``E_EOF``, which means the input is incomplete).  Here's a sample code
    283283fragment, untested, inspired by code from Alex Farber::
     
    310310
    311311Another solution is trying to compile the received string with
    312 :cfunc:`Py_CompileString`. If it compiles without errors, try to execute the
    313 returned code object by calling :cfunc:`PyEval_EvalCode`. Otherwise save the
     312:c:func:`Py_CompileString`. If it compiles without errors, try to execute the
     313returned code object by calling :c:func:`PyEval_EvalCode`. Otherwise save the
    314314input for later. If the compilation fails, find out if it's an error or just
    315315more input is required - by extracting the message string from the exception
     
    463463Unicode.  This only causes the link failure if the extension uses any of the
    464464``PyUnicode_*()`` functions.  It is also a problem if an extension uses any of
    465 the Unicode-related format specifiers for :cfunc:`Py_BuildValue` (or similar) or
    466 parameter specifications for :cfunc:`PyArg_ParseTuple`.
     465the Unicode-related format specifiers for :c:func:`Py_BuildValue` (or similar) or
     466parameter specifications for :c:func:`PyArg_ParseTuple`.
    467467
    468468You can check the size of the Unicode character a Python interpreter is using by
Note: See TracChangeset for help on using the changeset viewer.