-
-
Notifications
You must be signed in to change notification settings - Fork 33.9k
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.