Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8cf4eae

Browse files
committed
First (uncontroversial) part of issue 9807.
* Expose the build flags to Python as sys.abiflags * Shared library libpythonX.Y<abiflags>.so * python-config --abiflags * Make two distutils tests that failed with --enable-shared (even before this patch) succeed. * Fix a few small style issues.
1 parent d8d835b commit 8cf4eae

9 files changed

Lines changed: 386 additions & 299 deletions

File tree

Doc/library/sys.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,11 @@ always available.
955955
module for informational purposes; modifying this value has no effect on the
956956
registry keys used by Python. Availability: Windows.
957957

958+
.. data:: abiflags
959+
960+
On POSIX systems where Python is build with the standard ``configure``
961+
script, this contains the ABI flags as specified by :pep:`3149`.
962+
958963
.. rubric:: Citations
959964

960965
.. [C99] ISO/IEC 9899:1999. "Programming languages -- C." A public draft of this standard is available at http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf .

Lib/distutils/command/build_ext.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -754,9 +754,9 @@ def get_libraries(self, ext):
754754
else:
755755
from distutils import sysconfig
756756
if sysconfig.get_config_var('Py_ENABLE_SHARED'):
757-
template = "python%d.%d"
758-
pythonlib = (template %
759-
(sys.hexversion >> 24, (sys.hexversion >> 16) & 0xff))
757+
pythonlib = 'python{}.{}{}'.format(
758+
sys.hexversion >> 24, (sys.hexversion >> 16) & 0xff,
759+
sys.abiflags)
760760
return ext.libraries + [pythonlib]
761761
else:
762762
return ext.libraries

Lib/distutils/tests/test_build_ext.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
import sys
22
import os
3-
import tempfile
43
import shutil
54
from io import StringIO
65

7-
from distutils.core import Extension, Distribution
6+
from distutils.core import Distribution
87
from distutils.command.build_ext import build_ext
98
from distutils import sysconfig
109
from distutils.tests.support import TempdirManager
1110
from distutils.tests.support import LoggingSilencer
1211
from distutils.extension import Extension
13-
from distutils.errors import (UnknownFileError, DistutilsSetupError,
14-
CompileError)
12+
from distutils.errors import (
13+
CompileError, DistutilsSetupError, UnknownFileError)
1514

1615
import unittest
1716
from test import support
@@ -42,13 +41,28 @@ def setUp(self):
4241
from distutils.command import build_ext
4342
build_ext.USER_BASE = site.USER_BASE
4443

44+
def _fixup_command(self, cmd):
45+
# When Python was build with --enable-shared, -L. is not good enough
46+
# to find the libpython<blah>.so. This is because regrtest runs it
47+
# under a tempdir, not in the top level where the .so lives. By the
48+
# time we've gotten here, Python's already been chdir'd to the
49+
# tempdir.
50+
#
51+
# To further add to the fun, we can't just add library_dirs to the
52+
# Extension() instance because that doesn't get plumbed through to the
53+
# final compiler command.
54+
if not sys.platform.startswith('win'):
55+
library_dir = sysconfig.get_config_var('srcdir')
56+
cmd.library_dirs = [('.' if library_dir is None else library_dir)]
57+
4558
def test_build_ext(self):
4659
global ALREADY_TESTED
4760
xx_c = os.path.join(self.tmp_dir, 'xxmodule.c')
4861
xx_ext = Extension('xx', [xx_c])
4962
dist = Distribution({'name': 'xx', 'ext_modules': [xx_ext]})
5063
dist.package_dir = self.tmp_dir
5164
cmd = build_ext(dist)
65+
self._fixup_command(cmd)
5266
if os.name == "nt":
5367
# On Windows, we must build a debug version iff running
5468
# a debug build of Python
@@ -235,7 +249,8 @@ def test_check_extensions_list(self):
235249
cmd.finalize_options()
236250

237251
#'extensions' option must be a list of Extension instances
238-
self.assertRaises(DistutilsSetupError, cmd.check_extensions_list, 'foo')
252+
self.assertRaises(DistutilsSetupError,
253+
cmd.check_extensions_list, 'foo')
239254

240255
# each element of 'ext_modules' option must be an
241256
# Extension instance or 2-tuple
@@ -302,6 +317,7 @@ def test_get_outputs(self):
302317
dist = Distribution({'name': 'xx',
303318
'ext_modules': [ext]})
304319
cmd = build_ext(dist)
320+
self._fixup_command(cmd)
305321
cmd.ensure_finalized()
306322
self.assertEquals(len(cmd.get_outputs()), 1)
307323

Lib/test/test_sys.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,8 @@ def test_attributes(self):
468468
self.assertTrue(vi > (1,0,0))
469469
self.assertIsInstance(sys.float_repr_style, str)
470470
self.assertIn(sys.float_repr_style, ('short', 'legacy'))
471+
if not sys.platform.startswith('win'):
472+
self.assertIsInstance(sys.abiflags, str)
471473

472474
def test_43581(self):
473475
# Can't use sys.stdout, as this is a StringIO object when

Makefile.pre.in

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ AR= @AR@
3636
RANLIB= @RANLIB@
3737
SVNVERSION= @SVNVERSION@
3838
SOABI= @SOABI@
39+
LDVERSION= @LDVERSION@
3940

4041
GNULD= @GNULD@
4142

@@ -102,6 +103,7 @@ MANDIR= @mandir@
102103
INCLUDEDIR= @includedir@
103104
CONFINCLUDEDIR= $(exec_prefix)/include
104105
SCRIPTDIR= $(prefix)/lib
106+
ABIFLAGS= @ABIFLAGS@
105107

106108
# Detailed destination directories
107109
BINLIBDEST= $(LIBDIR)/python$(VERSION)
@@ -445,7 +447,7 @@ $(LIBRARY): $(LIBRARY_OBJS)
445447
$(AR) $(ARFLAGS) $@ $(MODOBJS)
446448
$(RANLIB) $@
447449

448-
libpython$(VERSION).so: $(LIBRARY_OBJS)
450+
libpython$(LDVERSION).so: $(LIBRARY_OBJS)
449451
if test $(INSTSONAME) != $(LDLIBRARY); then \
450452
$(BLDSHARED) -Wl,-h$(INSTSONAME) -o $(INSTSONAME) $(LIBRARY_OBJS) $(MODLIBS) $(SHLIBS) $(LIBC) $(LIBM) $(LDLAST); \
451453
$(LN) -f $(INSTSONAME) $@; \
@@ -566,6 +568,11 @@ Python/dynload_shlib.o: $(srcdir)/Python/dynload_shlib.c Makefile
566568
-DSOABI='"$(SOABI)"' \
567569
-o $@ $(srcdir)/Python/dynload_shlib.c
568570

571+
Python/sysmodule.o: $(srcdir)/Python/sysmodule.c Makefile
572+
$(CC) -c $(PY_CORE_CFLAGS) \
573+
-DABIFLAGS='"$(ABIFLAGS)"' \
574+
-o $@ $(srcdir)/Python/sysmodule.c
575+
569576
$(IO_OBJS): $(IO_H)
570577

571578
# Use a stamp file to prevent make -j invoking pgen twice

Misc/python-config.in

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import sys
66
import sysconfig
77

88
valid_opts = ['prefix', 'exec-prefix', 'includes', 'libs', 'cflags',
9-
'ldflags', 'extension-suffix', 'help']
9+
'ldflags', 'extension-suffix', 'help', 'abiflags']
1010

1111
def exit_with_usage(code=1):
1212
print("Usage: {0} [{1}]".format(
@@ -56,3 +56,6 @@ for opt in opt_flags:
5656

5757
elif opt == '--extension-suffix':
5858
print(sysconfig.get_config_var('SO'))
59+
60+
elif opt == '--abiflags':
61+
print(sys.abiflags)

Python/sysmodule.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,6 +1520,10 @@ _PySys_Init(void)
15201520
PyLong_FromVoidPtr(PyWin_DLLhModule));
15211521
SET_SYS_FROM_STRING("winver",
15221522
PyUnicode_FromString(PyWin_DLLVersionString));
1523+
#endif
1524+
#ifdef ABIFLAGS
1525+
SET_SYS_FROM_STRING("abiflags",
1526+
PyUnicode_FromString(ABIFLAGS));
15231527
#endif
15241528
if (warnoptions == NULL) {
15251529
warnoptions = PyList_New(0);

0 commit comments

Comments
 (0)