Repository navigation
Conversation
Add a section to the embedding guide that explains, for Linux, that linking with `pythonX.Y-config --ldflags --embed` ties an application to one minor version even when it only uses the Limited API, and how a single binary can instead load `libpython3.so` at run time. Mention renaming the dependency with `patchelf` as a packaging workaround, and list the limitations of both approaches. Related discussion: https://discuss.python.org/t/on-embedders-and-stable-abi/110197
Documentation build overview
16 files changed ·
|
zooba
left a comment
There was a problem hiding this comment.
Great! Well written, easy to follow, it's the kind of addition I've been meaning to make for Windows for years now 😄
Left a few comments to help the flow and clarity, but don't rely on me for strict correctness. I've done all of these things before, but I didn't reference my earlier work to check, and I don't do them often enough on Linux to remember the details.
| This library defines no Python symbols itself. | ||
| It only depends on the versioned library, which the dynamic loader loads | ||
| along with it. |
There was a problem hiding this comment.
This would probably flow better as something like "when the OS loads this library, it implicitly loads and references the symbols from the versioned library it is associated with" (IOW, a positive description of what happens, rather than 1.5 negative descriptions of what doesn't happen)
| Linking an executable with ``-lpython3`` therefore fails with | ||
| ``undefined reference`` errors, but an application can load |
There was a problem hiding this comment.
I assume this is because the linker does not load transitive dependencies to bring their symbols into context, but is it safe to assume that everyone will assume that? Or should we be clearer (I'm thinking something like "when linking executables, the versioned library is not considered, and since -lpython3 doesn't include any symbols of its own, it will fail with undefined reference errors"- adjusted for correctness if I got anything wrong)
There was a problem hiding this comment.
yes, that's accurate
| } py; | ||
|
|
||
| #define LOAD(lib, name) \ | ||
| ((*(void **)&py.name = dlsym(lib, "Py_" #name)) != NULL) |
There was a problem hiding this comment.
For an example, I'd remove the "Py_" and just spell it out each time. Removes an additional concept at a very small cost (and arguably an improvement - it helps search functions find references to the name when the name is complete).
| $ readelf -d myapp | grep NEEDED | ||
| 0x0000000000000001 (NEEDED) Shared library: [libc.so.6] |
There was a problem hiding this comment.
These might be worth a comment (or move it after the note at line 486, or just remove it), as it's just inspecting the result to prove a point, and not actual code that people need to run.
There was a problem hiding this comment.
moving it after 486
| * The dynamic loader looks for the versioned library by name in its usual | ||
| search path, such as :envvar:`!LD_LIBRARY_PATH` and the system library | ||
| directories, and not next to :file:`libpython3.so`. | ||
| To choose the exact file, pass the absolute path of the versioned library | ||
| to ``dlopen()`` instead. |
There was a problem hiding this comment.
The previous point says "use an absolute path" already, but I like it better here. Maybe merge the recommendation into this sentence and remove it from the first point?
| * ``RTLD_GLOBAL`` makes Python's symbols available to the extension modules | ||
| that Python imports, which are normally not linked against a Python library. |
There was a problem hiding this comment.
Ah hah! There is a reason! I still think it's worth a one line comment in the code sample, I'm pretty sure this is esoteric knowledge that isn't clearly documented anywhere.
There was a problem hiding this comment.
Since it's now in the source, does it need to stay in the prose?
There was a problem hiding this comment.
I think it's helpful to have it in both places. I don't want important information to show up only as an inline comment in an example.
There was a problem hiding this comment.
Or if there's somewhere in an "extending" section mentioning that it's an intentional part of how extension modules work, that would also suffice. As long as it's actually documented somewhere.
I don't like treating code samples as specification, so prefer seeing it in the prose as well, but I don't have a real strong opinion about where it should go.
There was a problem hiding this comment.
RTLD_GLOBAL allows extensions to not link to libpython. Not linking is the default on *nix, as that makes the extension compatible with debug builds of Python.
The Defining extension modules docs put build options out of scope. This kind of platform-specific esoteric knowledge was part of distlib, but moved out of core with the removal; bringing it all back as docs wouldn't be a small task.
| * ``RTLD_GLOBAL`` makes Python's symbols available to the extension modules | ||
| that Python imports, which are normally not linked against a Python library. | ||
|
|
||
| An existing application that calls the C API directly can instead be linked |
There was a problem hiding this comment.
This is where the alternative approach starts from, yes? Maybe bringing some of the warning up to here will help emphasize that we support everything above this point, and below this point is "at your own risk"?
At the very least, something that looks like a heading (or is a heading) will help avoid people reading this and thinking they need to keep going below here after they've done all the above.
There was a problem hiding this comment.
yes, I'll add a subtitle "Alternative: renaming the dependency after linking" (looks like a heading, not an actual heading because I didn't want it to show up in the navigation TOC)
not sure how strongly we want to call things here "supported" vs "unsupported", especially if later on we will want to reconsider cpython's stance about Stable ABI for embedders altogether.
I'll push one possible wording change, let me know what you think.
There was a problem hiding this comment.
not sure how strongly we want to call things here "supported" vs "unsupported"
Calling them "unsupported" now is fine 😉
There was a problem hiding this comment.
Calling them "unsupported" now is fine
updated
| * ``RTLD_GLOBAL`` makes Python's symbols available to the extension modules | ||
| that Python imports, which are normally not linked against a Python library. |
There was a problem hiding this comment.
Since it's now in the source, does it need to stay in the prose?
| .. rubric:: Alternative: renaming the dependency after linking | ||
|
|
||
| Loading Python at run time, as described above, | ||
| is the preferred way to run one binary with several Python versions, | ||
| and it does not need anything described below. | ||
|
|
||
| The following is a packaging workaround for an existing application that | ||
| calls the C API directly and cannot easily be changed. | ||
| It is not recommended; use it at your own risk. | ||
|
|
||
| Such an application is linked as described :ref:`above <compiling>`. | ||
| The dependency recorded in the executable is then renamed, for example with | ||
| `patchelf <https://github.com/NixOS/patchelf>`__: | ||
|
|
||
| .. code-block:: shell-session | ||
|
|
||
| $ patchelf --replace-needed libpython3.12.so.1.0 libpython3.so myapp | ||
|
|
||
| .. warning:: | ||
|
|
||
| Renaming the dependency does not make the application compatible with | ||
| other Python versions, and it has additional limitations and caveats: | ||
|
|
||
| * Renaming the dependency does not check that the application uses only | ||
| the Stable ABI. | ||
| Compile it with :c:macro:`Py_LIMITED_API` defined, as described in | ||
| :ref:`abi3-compiling`. | ||
| * If the application refers to a Python data symbol directly, for example | ||
| ``&PyLong_Type``, the linker may have used a copy relocation. | ||
| This reserves space for the object in the executable, using its size in | ||
| the Python version that the application was linked against. | ||
| That size is not part of the Stable ABI and can differ between versions. | ||
| Look for such relocations with ``readelf -rW myapp | grep COPY``, and | ||
| compile the application as position-independent code (``-fPIC``) to | ||
| avoid them. | ||
| * The dynamic loader must find :file:`libpython3.so` in its search path | ||
| when the application starts. | ||
| Otherwise the application does not start at all. |
There was a problem hiding this comment.
This is a “you get to keep the pieces” solution; I'd be OK with leaving it out of the official docs.
There was a problem hiding this comment.
would you also be ok with keeping it? :)
this is the bit that directly addresses gh-104612, and I think it's useful to address it in the docs. it gives us an opportunity to discourage users from reaching to this approach
There was a problem hiding this comment.
If it's kept, I'd prefer to reduce it. Maybe something like “Note that libpython3.so is not meant for embedding and linking to it will not work, as noted [above] (link to A Python built with :option:`--enable-shared`…)”.
I don't think we need to discourage users patching their Python. It should be clear that they're on their own at that point.
There was a problem hiding this comment.
I reduced the section
Add a section to the embedding guide that explains, for Linux, that linking with
pythonX.Y-config --ldflags --embedties an application to one minor version even when it only uses the Limited API, and how a single binary can instead loadlibpython3.soat run time. Mention renaming the dependency withpatchelfas a packaging workaround, and list the limitations of both approaches.Related discussion: https://discuss.python.org/t/on-embedders-and-stable-abi/110197