Repository navigation
Description
Bug report
When playing with python3 stable ABI by following code:
#define Py_LIMITED_API
#include <Python.h>
#include <stdio.h>
int main(int argc, char** argv) {
char* ver = Py_GetVersion();
printf("Py version: %s", ver);
return 0;
}
Linking with libpython3.11.so which works:
~/tmp/py_stable_abi via C v13.1.1-gcc
❯ gcc main.c -I/usr/include/python3.11 -lpython3.11 -L/usr/lib
main.c: In function ‘main’:
main.c:6:17: warning: initialization discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
6 | char* ver = Py_GetVersion();
| ^~~~~~~~~~~~~
~/tmp/py_stable_abi via C v13.1.1-gcc
❯ ./a.out
Py version: 3.11.3 (main, Apr 5 2023, 15:52:25) [GCC 12.2.1 20230201]%
Linking with libpython3.so, doesn't work:
~/tmp/py_stable_abi via C v13.1.1-gcc
❮ gcc main.c -I/usr/include/python3.11 -lpython3 -L/usr/lib
main.c: In function ‘main’:
main.c:6:17: warning: initialization discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
6 | char* ver = Py_GetVersion();
| ^~~~~~~~~~~~~
/usr/bin/ld: /tmp/ccfEEFx2.o: in function `main':
main.c:(.text+0x10): undefined reference to `Py_GetVersion'
collect2: error: ld returned 1 exit status
Actually, the libpython3.so doesn't contain any meaningful symbols, and the size is suspiciously small:
/lib🔒
❯ ls libpython* -l
lrwxrwxrwx 1 root root 19 Oct 11 2021 libpython2.7.so -> libpython2.7.so.1.0
-r-xr-xr-x 1 root root 6935488 Oct 11 2021 libpython2.7.so.1.0
lrwxrwxrwx 1 root root 20 Apr 5 23:52 libpython3.11.so -> libpython3.11.so.1.0
-rwxr-xr-x 1 root root 5866336 Apr 5 23:52 libpython3.11.so.1.0
lrwxrwxrwx 1 root root 20 Nov 5 2021 libpython3.7m.so -> libpython3.7m.so.1.0
-rwxr-xr-x 1 root root 3088608 Nov 5 2021 libpython3.7m.so.1.0
lrwxrwxrwx 1 root root 19 May 25 2022 libpython3.9.so -> libpython3.9.so.1.0
-rwxr-xr-x 1 root root 3765312 May 25 2022 libpython3.9.so.1.0
-rwxr-xr-x 1 root root 13816 Apr 5 23:52 libpython3.so
❯ nm -gD libpython3.so
w __cxa_finalize
w __gmon_start__
w _ITM_deregisterTMCloneTable
w _ITM_registerTMCloneTable
The statement to build libpython3.so looks strange, I am not sure what it is trying to do:
cpython on main [?] via C v13.1.1-gcc via 🐍 v3.11.3
❯ ./configure --enable-shared
gcc -shared -Wl,--no-as-needed -o libpython3.so -Wl,-hlibpython3.so libpython3.12.so
Does the above really do anything?
From https://peps.python.org/pep-0384/
On Unix systems, the ABI is typically provided by the python executable itself. PyModule_Create is changed to pass 3 as the API version if the extension module was compiled with Py_LIMITED_API; the version check for the API version will accept either 3 or the current PYTHON_API_VERSION as conforming. If Python is compiled as a shared library, it is installed as both libpython3.so, and libpython3.y.so; applications conforming to this PEP should then link to the former (extension modules can continue to link with no libpython shared object, but rather rely on runtime linking). The ABI version is symbolically available as PYTHON_ABI_VERSION.
The current libpython3.so is clearly not doing the right thing. Applications CANNOT link to it.
Your environment
OS: Arch Linux
Kernel: x86_64 Linux 6.3.1-zen2-1-zen
Checked the python3.9/3.10/3.11 from Arch's official repo, all have this problem.
Tried to build the cpython as well, the same problem.
Activity
sunmy2019 commentedon May 19, 2023
This is indeed a bug now.
This is introduced 12 yrs ago. My guess would be the toolchain has evolved in these years. Linkers nowadays cannot resolve symbols from the dependencies of a shared library.
I need to consult some linker experts.
Before we figure out a solution, link to the
libpython3.12.so.Updated:
We need a shared library with the same symbols as the
libpython3.12.so, but with a different sonamelibpython3.so.beeender commentedon May 19, 2023
Cannot we just copy the
libpython3.12.sotolibpython3.so? Unless there are some differences between them by design.sunmy2019 commentedon May 19, 2023
The soname is different.
itamaro commentedon May 21, 2023
Did this ever work?
I tried playing with this a bit, and can confirm the report is reproducible (with a fresh build from main branch, as well as with a copy of python 3.6 I happened to have around), using gcc 8.5 on Red Hat Linux.
With a certain combination of flags and setting LD_LIBRARY_PATH for the gcc invocation itself, it looked like the linker was at least aware of the existence of the linked libpython, but it refused to add symbols from it if it's not explicitly included in the command line:
Some things that "worked":
Include both python3 and python3.12:
gcc -I. -L. main.c -lpython3 -lpython3.12this results both so's included in the DT NEEDED of the binary
and if I patchelf the binary to drop libpython3.12.so, it still works at runtime because of the transitive dependency
but if we're going through that trouble, then this would be equivalent and arguably cleaner:
Potentially the simplest workaround is to delete the "useless" libpython3.so and replace it with a symlink:
This still leaves us with a binary that specifies the 3.12 DSO, because that's the soname we link against
which brings us back to patchelf'ing it to use the ABI DSO
I think to actually fix it, we need libpython3.so to be a copy of libpython3.12.so with the correct soname, e.g.:
encukou commentedon May 22, 2023
The .so is useless, IMO we should delete it without replacement.
It's not needed for extensions (importable modules), since those normally shouldn't link to libpython -- they're loaded by Python, which already has the correct libpython loaded. (If you link to
libpythonyourself, you'll e.g. get the non-debug version for debug builds, and you definitely don't want that.)For embedding, you can build & distribute Python with the embedding application (and then you don't need stable ABI), or try finding a “system libpython” to link to (which is tricky, but mostly possible -- but you'll end up with the versioned
.soanyway).We can definitely improve the situation for both cases, but I don't think copying or symlinking the current
.sois worth it. (It should have been done -- and tested -- 14 years ago with PEP-384, but I see no point doing it now just to comply with that PEP.)IMO, a good step would be to actually limit the set of symbols
libpython3.soexports, like what's done on Windows. That would help people from accidentally using functions outside the stable ABI. But I don't know if it's actually possible with.so.beeender commentedon May 22, 2023
The stable ABI is still useful IMO. Take PostgresSQL plpython for example, the extension itself doesn't need newer versions of python at compiling time. But on rhel8/Rock8, the plpython package gets stuck with the system's python3.6. To use a new version of python, the extension has to be re-complied. But it is not necessary if the
libpython3.soand stable ABI do what they suppose to do. The extension can be built with a lower version of python, but at runtime, it still can use a newer version of Python to use the modern libs.Yhg1s commentedon May 22, 2023
It is possible, but I don't think it would be useful. If the python binary is built with --enable-shared, it can't be linked to libpython3.so and has to use libpython3.XX.so. Any extension module loaded by the python binary would have access to symbols loaded from the regular libpython3.XX.so even if it's linked to libpython3.so. If the python binary is statically linked with libpython (which e.g. the debian python build does even while supplying libpython.so), the symbols come from the binary itself. We could hide those symbols but only if all extension modules use the Stable ABI.
A possible alternative would be to use link-map lists (a GNU extension; see
dlmopen()) to load each extension module in a separate namespace. However, populating the new namespace with the right symbols would be a bit fragile (it's easy to make a mistake and end up using initialised state from the wrong .so), and I expect it would break the expectations of a number of extension modules. I think it would be a lot easier to verify Stable ABI compliance by inspecting the .so files and the symbols it would load from libpython.(This story is a little different on macOS, btw, which is kinda halfway between Windows and ELF in how symbols are namespaced by default. IIUC our macOS build explicitly links into a flat namespace to make it work like on ELF, but by default macOS would like to know which shared library dependency symbols should come from.)
beeender commentedon Jul 14, 2023
I am trying to create a patch for this. Interestingly, I found there is a script named stable_abi.py, which was invented to do the testing about the stable ABI. But it is not called by anyone.
Where should this test be executed if I want to add it as a part of the CI process?
encukou commentedon Aug 3, 2023
It's already part of a Make rule that's used in CI. Nothing to do on that front! :)
vstinner commentedon Sep 29, 2023
I don't understand well what you are trying to do. You are trying to build a program and link it to Python 3.11. That's not the stable ABI. That's just a program linked to libpython.
The common usage of the stable ABI is to build a C extension and then load it in Python. Please elaborate what you are trying to do.
sunmy2019 commentedon Sep 30, 2023
I think they are trying to do things like:
build a program and link to (
libpython3.sofrom Python3.11)load it with (
libpython3.sofrom 3.12) without libpython3.11.sowhich is not currently achievable. This makes
libpython3.soalone useless in most scenarios.vstinner commentedon Sep 30, 2023
I understand that the use case is to build a program and expect that it will run with any Python version available on the system. The vim text editor managed to implement this use case by targeting the stable ABI, but loading a versioned libpython.
Maybe we should provide a recipe to implement such use case.
In general, you should:
#define Py_LIMITED_API <version>.@sunmy2019 is correct:
libpython3.sois empty and so linking a program tolibpython3.sodoesn't load "any Python available on the system", but it loads and you end up with many missing symbols.If someone wants to support such use case: "load any Python available on the system using libpython3.so", the constraints should be elaborated. Which Python versions would you accept? Is Python 2.7 ok? Is an alpha version of Python 3.13 ok? Is a debug build ok? Should "Python" be loaded from anywhere on the system?
IMO this kind of problem should be solved outside Python itself, with a 3rd party project which would load the "appropriate" Python version. Load a Python program, load libpython, whatever which brings "Python" into the current process.
I saw some funky projects which would run a separate Python process and communicate with this process with IPC! Would it be an acceptable implementation? :-)
aallrd commentedon Oct 10, 2023
We see value in
libpython3.soin our usecase with Red Hat Enterprise Linux (RHEL).Our application is built on RHEL 8 and needs to run on both RHEL 8 and RHEL 9.
We assume that the C API is stable as documented, as long as we restrict ourselves to the
Py_LIMITED_APIAPIs.On RHEL, the system is shipped with a Python distribution version "frozen" for the whole lifecycle of the product:
libpython3.so->libpython3.6m.solibpython3.so->libpython3.9.so.1.0The common component here is
libpython3.so(SONAME), so it would be great if we could directly link our application to it.However, since it does not define any symbols, but only has a
DT_NEEDEDentry we need to do some "hacks" to make it work.My expectation would be that the
libpython3.solibrary exports all thePy_LIMITED_API 3symbols instead of none.Details:
Toolybird commentedon Nov 2, 2023
Here is a related downstream Arch bug report. Not sure if it's all the same issue, but there was definitely a bug in the Autofoo which is now fixed in 3.12.x and above. (Arch is still on 3.11.x).
pythongh-104612: Fix libpython3.so stable ABI on Linux using patchelf
fix: link against versioned Python library
nickalcock commentedon Sep 7, 2026
I think this is what DT_FILTER is for. From the GNU ld documentation:
Thus, if libpython.so was a filter atop the actual (versioned) shared library, it could use filtering to restrict the set of symbols provided by the actual shared library to the set in the stable ABI: programs linking against it would see those symbols and only those symbols, and would get a DT_NEEDED of libpython.so alone.
Whether this actually works I'm not sure -- it's not exactly the most heavily used feature out there (though Solaris made heavy use of it back in the day). I'll see if I can whip a patch up.
h-vetinari commentedon Sep 7, 2026
FWIW, in conda-forge we have concrete use-cases for building against a non-trivial
libpython3.{so,dylib}; for example, LLVM's lldb nowadays supports an option-DLLDB_ENABLE_PYTHON_LIMITED_API=ON, which we would like to use because it'd cut our build matrix down by 80%.On macOS, CPython's default build simply doesn't produce a
libpython3.dylib, so we ended up creating one based onstable_abi.datas followsThis was enough to successfully build
lldbagainstlibpython3.dylib, in a way that we can build once (say, against 3.11) and then be able to substitute any newer python version at runtime.However, I ended up struggling quite a bit on linux, because I always ended up with missing symbols. It took me a while to figure out that
libpython3.sois essentially empty (and then find this issue). AFAIU, the standard GCC toolchains don't work exactly equivalently to what macOS is doing with-Wl,-reexport_library, but conceptually, the principle are sound IMO:-reexport_librarysays the symbols originate in the underlying versioned library.-exported_symbols_listrestricts whatlibpython3.dylibexposes to the Stable ABI.I think it would be very nice to have a similar setup on linux.
itamaro commentedon Nov 19, 2026
I started a DPO discussion topic about this issue and possible approaches.