Skip to content

Commit 8c3ecc6

Browse files
authored
bpo-21536: C extensions are no longer linked to libpython (GH-12946)
On Unix, C extensions are no longer linked to libpython. It is now possible to load a C extension built using a shared library Python with a statically linked Python. When Python is embedded, libpython must not be loaded with RTLD_LOCAL, but RTLD_GLOBAL instead. Previously, using RTLD_LOCAL, it was already not possible to load C extensions which were not linked to libpython, like C extensions of the standard library built by the "*shared*" section of Modules/Setup. distutils, python-config and python-config.py have been modified.
1 parent d7befad commit 8c3ecc6

File tree

7 files changed

+28
-26
lines changed

7 files changed

+28
-26
lines changed

Doc/distutils/apiref.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,10 @@ the full reference.
277277
| | simply skip the extension. | |
278278
+------------------------+--------------------------------+---------------------------+
279279

280+
.. versionchanged:: 3.8
281+
282+
On Unix, C extensions are no longer linked to libpython.
283+
280284

281285
.. class:: Distribution
282286

Doc/whatsnew/3.8.rst

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -851,16 +851,19 @@ Changes in the Python API
851851
Changes in the C API
852852
--------------------
853853

854+
* On Unix, C extensions are no longer linked to libpython. When Python is
855+
embedded, ``libpython`` must not be loaded with ``RTLD_LOCAL``, but
856+
``RTLD_GLOBAL`` instead. Previously, using ``RTLD_LOCAL``, it was already not
857+
possible to load C extensions which were not linked to ``libpython``, like C
858+
extensions of the standard library built by the ``*shared*`` section of
859+
``Modules/Setup``.
860+
854861
* Use of ``#`` variants of formats in parsing or building value (e.g.
855862
:c:func:`PyArg_ParseTuple`, :c:func:`Py_BuildValue`, :c:func:`PyObject_CallFunction`,
856863
etc.) without ``PY_SSIZE_T_CLEAN`` defined raises ``DeprecationWarning`` now.
857864
It will be removed in 3.10 or 4.0. Read :ref:`arg-parsing` for detail.
858865
(Contributed by Inada Naoki in :issue:`36381`.)
859866

860-
861-
Changes in the C API
862-
--------------------------
863-
864867
* Instances of heap-allocated types (such as those created with
865868
:c:func:`PyType_FromSpec`) hold a reference to their type object.
866869
Increasing the reference count of these type objects has been moved from

Lib/distutils/command/build_ext.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -714,20 +714,5 @@ def get_libraries(self, ext):
714714
# don't extend ext.libraries, it may be shared with other
715715
# extensions, it is a reference to the original list
716716
return ext.libraries + [pythonlib]
717-
else:
718-
return ext.libraries
719-
elif sys.platform == 'darwin':
720-
# Don't use the default code below
721-
return ext.libraries
722-
elif sys.platform[:3] == 'aix':
723-
# Don't use the default code below
724-
return ext.libraries
725-
else:
726-
from distutils import sysconfig
727-
if sysconfig.get_config_var('Py_ENABLE_SHARED'):
728-
pythonlib = 'python{}.{}{}'.format(
729-
sys.hexversion >> 24, (sys.hexversion >> 16) & 0xff,
730-
sysconfig.get_config_var('ABIFLAGS'))
731-
return ext.libraries + [pythonlib]
732-
else:
733-
return ext.libraries
717+
718+
return ext.libraries

Makefile.pre.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1460,7 +1460,7 @@ libinstall: build_all $(srcdir)/Modules/xxmodule.c
14601460
-PYTHONPATH=$(DESTDIR)$(LIBDEST) $(RUNSHARED) \
14611461
$(PYTHON_FOR_BUILD) -m lib2to3.pgen2.driver $(DESTDIR)$(LIBDEST)/lib2to3/PatternGrammar.txt
14621462

1463-
python-config: $(srcdir)/Misc/python-config.in Misc/python-config.sh
1463+
python-config: $(srcdir)/Misc/python-config.in $(srcdir)/Misc/python-config.sh
14641464
@ # Substitution happens here, as the completely-expanded BINDIR
14651465
@ # is not available in configure
14661466
sed -e "s,@EXENAME@,$(BINDIR)/python$(LDVERSION)$(EXE)," < $(srcdir)/Misc/python-config.in >python-config.py
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
On Unix, C extensions are no longer linked to libpython.
2+
3+
It is now possible to load a C extension built using a shared library Python
4+
with a statically linked Python.
5+
6+
When Python is embedded, ``libpython`` must not be loaded with ``RTLD_LOCAL``,
7+
but ``RTLD_GLOBAL`` instead. Previously, using ``RTLD_LOCAL``, it was already
8+
not possible to load C extensions which were not linked to ``libpython``, like
9+
C extensions of the standard library built by the ``*shared*`` section of
10+
``Modules/Setup``.
11+
12+
distutils, python-config and python-config.py have been modified.

Misc/python-config.in

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ for opt in opt_flags:
4747
print(' '.join(flags))
4848

4949
elif opt in ('--libs', '--ldflags'):
50-
libs = ['-lpython' + pyver + sys.abiflags]
51-
libs += getvar('LIBS').split()
52-
libs += getvar('SYSLIBS').split()
50+
libs = getvar('LIBS').split() + getvar('SYSLIBS').split()
5351
# add the prefix/lib/pythonX.Y/config dir, but only if there is no
5452
# shared library in prefix/lib/.
5553
if opt == '--ldflags':

Misc/python-config.sh.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ LIBM="@LIBM@"
4141
LIBC="@LIBC@"
4242
SYSLIBS="$LIBM $LIBC"
4343
ABIFLAGS="@ABIFLAGS@"
44-
LIBS="-lpython${VERSION}${ABIFLAGS} @LIBS@ $SYSLIBS"
44+
LIBS="@LIBS@ $SYSLIBS"
4545
BASECFLAGS="@BASECFLAGS@"
4646
LDLIBRARY="@LDLIBRARY@"
4747
OPT="@OPT@"

0 commit comments

Comments
 (0)