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

    r2 r391  
    2626So if you are embedding Python, you are providing your own main program.  One of
    2727the things this main program has to do is initialize the Python interpreter.  At
    28 the very least, you have to call the function :cfunc:`Py_Initialize`.  There are
     28the very least, you have to call the function :c:func:`Py_Initialize`.  There are
    2929optional calls to pass command line arguments to Python.  Then later you can
    3030call the interpreter from any part of the application.
    3131
    3232There are several different ways to call the interpreter: you can pass a string
    33 containing Python statements to :cfunc:`PyRun_SimpleString`, or you can pass a
     33containing Python statements to :c:func:`PyRun_SimpleString`, or you can pass a
    3434stdio file pointer and a file name (for identification in error messages only)
    35 to :cfunc:`PyRun_SimpleFile`.  You can also call the lower-level operations
     35to :c:func:`PyRun_SimpleFile`.  You can also call the lower-level operations
    3636described in the previous chapters to construct and use Python objects.
    3737
     
    6262   main(int argc, char *argv[])
    6363   {
     64     Py_SetProgramName(argv[0]);  /* optional but recommended */
    6465     Py_Initialize();
    6566     PyRun_SimpleString("from time import time,ctime\n"
     
    6970   }
    7071
    71 The above code first initializes the Python interpreter with
    72 :cfunc:`Py_Initialize`, followed by the execution of a hard-coded Python script
    73 that print the date and time.  Afterwards, the :cfunc:`Py_Finalize` call shuts
     72The :c:func:`Py_SetProgramName` function should be called before
     73:c:func:`Py_Initialize` to inform the interpreter about paths to Python run-time
     74libraries.  Next, the Python interpreter is initialized with
     75:c:func:`Py_Initialize`, followed by the execution of a hard-coded Python script
     76that prints the date and time.  Afterwards, the :c:func:`Py_Finalize` call shuts
    7477the interpreter down, followed by the end of the program.  In a real program,
    7578you may want to get the Python script from another source, perhaps a text-editor
    7679routine, a file, or a database.  Getting the Python code from a file can better
    77 be done by using the :cfunc:`PyRun_SimpleFile` function, which saves you the
     80be done by using the :c:func:`PyRun_SimpleFile` function, which saves you the
    7881trouble of allocating memory space and loading the file contents.
    7982
     
    138141in ``argv[2]``.  Its integer arguments are the other values of the ``argv``
    139142array.  If you compile and link this program (let's call the finished executable
    140 :program:`call`), and use it to execute a Python script, such as::
     143:program:`call`), and use it to execute a Python script, such as:
     144
     145.. code-block:: python
    141146
    142147   def multiply(a,b):
     
    163168
    164169After initializing the interpreter, the script is loaded using
    165 :cfunc:`PyImport_Import`.  This routine needs a Python string as its argument,
    166 which is constructed using the :cfunc:`PyString_FromString` data conversion
     170:c:func:`PyImport_Import`.  This routine needs a Python string as its argument,
     171which is constructed using the :c:func:`PyString_FromString` data conversion
    167172routine. ::
    168173
     
    176181
    177182Once the script is loaded, the name we're looking for is retrieved using
    178 :cfunc:`PyObject_GetAttrString`.  If the name exists, and the object returned is
     183:c:func:`PyObject_GetAttrString`.  If the name exists, and the object returned is
    179184callable, you can safely assume that it is a function.  The program then
    180185proceeds by constructing a tuple of arguments as normal.  The call to the Python
     
    219224   };
    220225
    221 Insert the above code just above the :cfunc:`main` function. Also, insert the
    222 following two statements directly after :cfunc:`Py_Initialize`::
     226Insert the above code just above the :c:func:`main` function. Also, insert the
     227following two statements directly after :c:func:`Py_Initialize`::
    223228
    224229   numargs = argc;
     
    227232These two lines initialize the ``numargs`` variable, and make the
    228233:func:`emb.numargs` function accessible to the embedded Python interpreter.
    229 With these extensions, the Python script can do things like ::
     234With these extensions, the Python script can do things like
     235
     236.. code-block:: python
    230237
    231238   import emb
     
    252259.. _link-reqs:
    253260
    254 Linking Requirements
    255 ====================
    256 
    257 While the :program:`configure` script shipped with the Python sources will
    258 correctly build Python to export the symbols needed by dynamically linked
    259 extensions, this is not automatically inherited by applications which embed the
    260 Python library statically, at least on Unix.  This is an issue when the
    261 application is linked to the static runtime library (:file:`libpython.a`) and
    262 needs to load dynamic extensions (implemented as :file:`.so` files).
    263 
    264 The problem is that some entry points are defined by the Python runtime solely
    265 for extension modules to use.  If the embedding application does not use any of
    266 these entry points, some linkers will not include those entries in the symbol
    267 table of the finished executable.  Some additional options are needed to inform
    268 the linker not to remove these symbols.
    269 
    270 Determining the right options to use for any given platform can be quite
    271 difficult, but fortunately the Python configuration already has those values.
    272 To retrieve them from an installed Python interpreter, start an interactive
    273 interpreter and have a short session like this::
    274 
    275    >>> import distutils.sysconfig
    276    >>> distutils.sysconfig.get_config_var('LINKFORSHARED')
     261Compiling and Linking under Unix-like systems
     262=============================================
     263
     264It is not necessarily trivial to find the right flags to pass to your
     265compiler (and linker) in order to embed the Python interpreter into your
     266application, particularly because Python needs to load library modules
     267implemented as C dynamic extensions (:file:`.so` files) linked against
     268it.
     269
     270To find out the required compiler and linker flags, you can execute the
     271:file:`python{X.Y}-config` script which is generated as part of the
     272installation process (a :file:`python-config` script may also be
     273available).  This script has several options, of which the following will
     274be directly useful to you:
     275
     276* ``pythonX.Y-config --cflags`` will give you the recommended flags when
     277  compiling::
     278
     279   $ /opt/bin/python2.7-config --cflags
     280   -I/opt/include/python2.7 -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes
     281
     282* ``pythonX.Y-config --ldflags`` will give you the recommended flags when
     283  linking::
     284
     285   $ /opt/bin/python2.7-config --ldflags
     286   -L/opt/lib/python2.7/config -lpthread -ldl -lutil -lm -lpython2.7 -Xlinker -export-dynamic
     287
     288.. note::
     289   To avoid confusion between several Python installations (and especially
     290   between the system Python and your own compiled Python), it is recommended
     291   that you use the absolute path to :file:`python{X.Y}-config`, as in the above
     292   example.
     293
     294If this procedure doesn't work for you (it is not guaranteed to work for
     295all Unix-like platforms; however, we welcome :ref:`bug reports <reporting-bugs>`)
     296you will have to read your system's documentation about dynamic linking and/or
     297examine Python's :file:`Makefile` (use :func:`sysconfig.get_makefile_filename`
     298to find its location) and compilation
     299options.  In this case, the :mod:`sysconfig` module is a useful tool to
     300programmatically extract the configuration values that you will want to
     301combine together.  For example:
     302
     303.. code-block:: python
     304
     305   >>> import sysconfig
     306   >>> sysconfig.get_config_var('LIBS')
     307   '-lpthread -ldl  -lutil'
     308   >>> sysconfig.get_config_var('LINKFORSHARED')
    277309   '-Xlinker -export-dynamic'
    278310
    279 .. index:: module: distutils.sysconfig
    280 
    281 The contents of the string presented will be the options that should be used.
    282 If the string is empty, there's no need to add any additional options.  The
    283 :const:`LINKFORSHARED` definition corresponds to the variable of the same name
    284 in Python's top-level :file:`Makefile`.
    285 
     311
     312.. XXX similar documentation for Windows missing
Note: See TracChangeset for help on using the changeset viewer.