Skip to content

Commit 02e1409

Browse files
committed
Address review feedback
1 parent ac5ab8c commit 02e1409

1 file changed

Lines changed: 53 additions & 29 deletions

File tree

‎Doc/extending/embedding.rst‎

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -414,13 +414,18 @@ the Limited API and must *also* avoid the versioned dependency.
414414
A Python built with :option:`--enable-shared` and without
415415
:option:`--with-pydebug` installs :file:`libpython3.so` next to the versioned
416416
library.
417-
This library defines no Python symbols itself.
418-
It only depends on the versioned library, which the dynamic loader loads
419-
along with it.
420-
Linking an executable with ``-lpython3`` therefore fails with
421-
``undefined reference`` errors, but an application can load
422-
:file:`libpython3.so` with :manpage:`dlopen(3)` and look up each function that
423-
it uses with :manpage:`dlsym(3)`.
417+
When the dynamic loader loads this library, it also loads the versioned
418+
library that it was built with, and symbols looked up through
419+
:file:`libpython3.so` are found in the versioned library.
420+
An application can therefore load :file:`libpython3.so` with
421+
:manpage:`dlopen(3)` and look up each function that it uses with
422+
:manpage:`dlsym(3)`.
423+
424+
This only works at run time.
425+
When linking an executable, the linker does not use the dependencies of a
426+
library to resolve symbols, and :file:`libpython3.so` defines no Python
427+
symbols of its own, so linking with ``-lpython3`` fails with
428+
``undefined reference`` errors.
424429

425430
For example, with Python 3.12 as the oldest supported version::
426431

@@ -432,14 +437,14 @@ For example, with Python 3.12 as the oldest supported version::
432437
/* The API this application uses, declared as in Python.h.
433438
Everything is reached through these pointers. */
434439
static struct {
435-
const unsigned long *Version; /* Py_Version */
436-
const char *(*GetVersion)(void); /* Py_GetVersion */
437-
void (*InitializeEx)(int); /* Py_InitializeEx */
438-
int (*FinalizeEx)(void); /* Py_FinalizeEx */
440+
const unsigned long *Version;
441+
const char *(*GetVersion)(void);
442+
void (*InitializeEx)(int);
443+
int (*FinalizeEx)(void);
439444
} py;
440445

441-
#define LOAD(lib, name) \
442-
((*(void **)&py.name = dlsym(lib, "Py_" #name)) != NULL)
446+
#define LOAD(lib, pointer, name) \
447+
((*(void **)&(pointer) = dlsym(lib, name)) != NULL)
443448

444449
int
445450
main(int argc, char *argv[])
@@ -451,13 +456,17 @@ For example, with Python 3.12 as the oldest supported version::
451456
argv[0]);
452457
return 2;
453458
}
459+
/* RTLD_GLOBAL is required: it lets the extension modules that
460+
Python imports find Python's symbols. */
454461
void *lib = dlopen(argv[1], RTLD_NOW | RTLD_GLOBAL);
455462
if (lib == NULL) {
456463
fprintf(stderr, "Python is not available: %s\n", dlerror());
457464
return 1;
458465
}
459-
if (!LOAD(lib, Version) || !LOAD(lib, GetVersion)
460-
|| !LOAD(lib, InitializeEx) || !LOAD(lib, FinalizeEx))
466+
if (!LOAD(lib, py.Version, "Py_Version")
467+
|| !LOAD(lib, py.GetVersion, "Py_GetVersion")
468+
|| !LOAD(lib, py.InitializeEx, "Py_InitializeEx")
469+
|| !LOAD(lib, py.FinalizeEx, "Py_FinalizeEx"))
461470
{
462471
fprintf(stderr, "Unsupported Python: %s\n", dlerror());
463472
return 1;
@@ -473,38 +482,54 @@ For example, with Python 3.12 as the oldest supported version::
473482
}
474483

475484
Compile the program with Python's headers, but without the output of
476-
``--ldflags``.
485+
``--ldflags``:
477486

478487
.. code-block:: shell-session
479488
480489
$ gcc $(/opt/bin/python3.12-config --includes) -o myapp myapp.c
481-
$ readelf -d myapp | grep NEEDED
482-
0x0000000000000001 (NEEDED) Shared library: [libc.so.6]
483490
$ ./myapp /usr/local/lib/libpython3.so
484491
Loaded Python 3.14.10 (main, Nov 5 2026, 09:47:10) [GCC 14.2.0]
485492
486493
No Python library is passed to the linker, so the executable does not depend
487494
on one.
495+
You can check this with ``readelf``:
496+
497+
.. code-block:: shell-session
498+
499+
$ readelf -d myapp | grep NEEDED
500+
0x0000000000000001 (NEEDED) Shared library: [libc.so.6]
501+
488502
For the same reason, Python functions can only be called through such
489503
pointers.
490504
This includes the functions that macros in :file:`Python.h` expand to:
491505
for example, call :c:func:`Py_DecRef` through a pointer instead of using
492506
:c:func:`Py_DECREF`.
493507

494508
* The path passed to ``dlopen()`` selects the code that runs in your process.
495-
Use an absolute path that comes from a trusted source and that names a file
496-
only trusted users can replace.
497-
* The dynamic loader looks for the versioned library by name in its usual
498-
search path, such as :envvar:`!LD_LIBRARY_PATH` and the system library
499-
directories, and not next to :file:`libpython3.so`.
509+
It should come from a trusted source and name a file that only trusted
510+
users can replace.
511+
* Use an absolute path.
512+
The dynamic loader looks for a name without a slash in its usual search
513+
path, such as :envvar:`!LD_LIBRARY_PATH` and the system library directories.
514+
It looks for the versioned library that :file:`libpython3.so` depends on in
515+
the same way, and not next to :file:`libpython3.so`.
500516
To choose the exact file, pass the absolute path of the versioned library
501517
to ``dlopen()`` instead.
502518
* ``RTLD_GLOBAL`` makes Python's symbols available to the extension modules
503519
that Python imports, which are normally not linked against a Python library.
504520

505-
An existing application that calls the C API directly can instead be linked
506-
as described :ref:`above <compiling>`, and have the dependency recorded in
507-
the executable renamed afterwards, for example with
521+
.. rubric:: Alternative: renaming the dependency after linking
522+
523+
Loading Python at run time, as described above,
524+
is the preferred way to run one binary with several Python versions,
525+
and it does not need anything described below.
526+
527+
The following is a packaging workaround for an existing application that
528+
calls the C API directly and cannot easily be changed.
529+
It is not recommended; use it at your own risk.
530+
531+
Such an application is linked as described :ref:`above <compiling>`.
532+
The dependency recorded in the executable is then renamed, for example with
508533
`patchelf <https://github.com/NixOS/patchelf>`__:
509534

510535
.. code-block:: shell-session
@@ -513,9 +538,8 @@ the executable renamed afterwards, for example with
513538
514539
.. warning::
515540

516-
This is a packaging workaround.
517-
It does not make the application compatible with other Python versions,
518-
and it has additional limitations and caveats:
541+
Renaming the dependency does not make the application compatible with
542+
other Python versions, and it has additional limitations and caveats:
519543

520544
* Renaming the dependency does not check that the application uses only
521545
the Stable ABI.

0 commit comments

Comments
 (0)