Skip to content

Commit c991f24

Browse files
authored
bpo-36146: Don't run code at setup.py top level (GH-12127)
* Move set_compiler_flags() calls and concurrent.future hack from module top-level to main() * Remove unused variables 'macros' and 'libraries' from detect_multiprocessing(). * Move SUMMARY and CLASSIFIERS constants at the top, move set_compiler_flags() function below these constants. * Add some empty new lines to respect PEP 8.
1 parent 5ec33a1 commit c991f24

File tree

1 file changed

+64
-76
lines changed

1 file changed

+64
-76
lines changed

setup.py

Lines changed: 64 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# Autodetecting setup.py script for building the Python extensions
2-
#
32

43
import argparse
54
import importlib._bootstrap
@@ -20,32 +19,19 @@
2019
from distutils.errors import CCompilerError, DistutilsError
2120
from distutils.spawn import find_executable
2221

23-
CROSS_COMPILING = "_PYTHON_HOST_PLATFORM" in os.environ
24-
25-
# Set common compiler and linker flags derived from the Makefile,
26-
# reserved for building the interpreter and the stdlib modules.
27-
# See bpo-21121 and bpo-35257
28-
def set_compiler_flags(compiler_flags, compiler_py_flags_nodist):
29-
flags = sysconfig.get_config_var(compiler_flags)
30-
py_flags_nodist = sysconfig.get_config_var(compiler_py_flags_nodist)
31-
sysconfig.get_config_vars()[compiler_flags] = flags + ' ' + py_flags_nodist
32-
33-
set_compiler_flags('CFLAGS', 'PY_CFLAGS_NODIST')
34-
set_compiler_flags('LDFLAGS', 'PY_LDFLAGS_NODIST')
35-
36-
class Dummy:
37-
"""Hack for parallel build"""
38-
ProcessPoolExecutor = None
39-
sys.modules['concurrent.futures.process'] = Dummy
4022

4123
def get_platform():
42-
# cross build
24+
# Cross compiling
4325
if "_PYTHON_HOST_PLATFORM" in os.environ:
4426
return os.environ["_PYTHON_HOST_PLATFORM"]
27+
4528
# Get value of sys.platform
4629
if sys.platform.startswith('osf1'):
4730
return 'osf1'
4831
return sys.platform
32+
33+
34+
CROSS_COMPILING = ("_PYTHON_HOST_PLATFORM" in os.environ)
4935
HOST_PLATFORM = get_platform()
5036
MS_WINDOWS = (HOST_PLATFORM == 'win32')
5137
CYGWIN = (HOST_PLATFORM == 'cygwin')
@@ -59,6 +45,45 @@ def get_platform():
5945
# This global variable is used to hold the list of modules to be disabled.
6046
DISABLED_MODULE_LIST = []
6147

48+
49+
SUMMARY = """
50+
Python is an interpreted, interactive, object-oriented programming
51+
language. It is often compared to Tcl, Perl, Scheme or Java.
52+
53+
Python combines remarkable power with very clear syntax. It has
54+
modules, classes, exceptions, very high level dynamic data types, and
55+
dynamic typing. There are interfaces to many system calls and
56+
libraries, as well as to various windowing systems (X11, Motif, Tk,
57+
Mac, MFC). New built-in modules are easily written in C or C++. Python
58+
is also usable as an extension language for applications that need a
59+
programmable interface.
60+
61+
The Python implementation is portable: it runs on many brands of UNIX,
62+
on Windows, DOS, Mac, Amiga... If your favorite system isn't
63+
listed here, it may still be supported, if there's a C compiler for
64+
it. Ask around on comp.lang.python -- or just try compiling Python
65+
yourself.
66+
"""
67+
68+
CLASSIFIERS = """
69+
Development Status :: 6 - Mature
70+
License :: OSI Approved :: Python Software Foundation License
71+
Natural Language :: English
72+
Programming Language :: C
73+
Programming Language :: Python
74+
Topic :: Software Development
75+
"""
76+
77+
78+
# Set common compiler and linker flags derived from the Makefile,
79+
# reserved for building the interpreter and the stdlib modules.
80+
# See bpo-21121 and bpo-35257
81+
def set_compiler_flags(compiler_flags, compiler_py_flags_nodist):
82+
flags = sysconfig.get_config_var(compiler_flags)
83+
py_flags_nodist = sysconfig.get_config_var(compiler_py_flags_nodist)
84+
sysconfig.get_config_vars()[compiler_flags] = flags + ' ' + py_flags_nodist
85+
86+
6287
def add_dir_to_list(dirlist, dir):
6388
"""Add the directory 'dir' to the list 'dirlist' (after any relative
6489
directories) if:
@@ -74,6 +99,7 @@ def add_dir_to_list(dirlist, dir):
7499
return
75100
dirlist.insert(0, dir)
76101

102+
77103
def sysroot_paths(make_vars, subdirs):
78104
"""Get the paths of sysroot sub-directories.
79105
@@ -99,6 +125,7 @@ def sysroot_paths(make_vars, subdirs):
99125
break
100126
return dirs
101127

128+
102129
def macosx_sdk_root():
103130
"""
104131
Return the directory of the current OSX SDK,
@@ -112,6 +139,7 @@ def macosx_sdk_root():
112139
sysroot = m.group(1)
113140
return sysroot
114141

142+
115143
def is_macosx_sdk_path(path):
116144
"""
117145
Returns True if 'path' can be located in an OSX SDK
@@ -120,6 +148,7 @@ def is_macosx_sdk_path(path):
120148
or path.startswith('/System/')
121149
or path.startswith('/Library/') )
122150

151+
123152
def find_file(filename, std_dirs, paths):
124153
"""Searches for the directory where a given file is located,
125154
and returns a possibly-empty list of additional directories, or None
@@ -159,6 +188,7 @@ def find_file(filename, std_dirs, paths):
159188
# Not found anywhere
160189
return None
161190

191+
162192
def find_library_file(compiler, libname, std_dirs, paths):
163193
result = compiler.find_library_file(std_dirs + paths, libname)
164194
if result is None:
@@ -211,12 +241,14 @@ def find_library_file(compiler, libname, std_dirs, paths):
211241
else:
212242
assert False, "Internal error: Path not found in std_dirs or paths"
213243

244+
214245
def module_enabled(extlist, modname):
215246
"""Returns whether the module 'modname' is present in the list
216247
of extensions 'extlist'."""
217248
extlist = [ext for ext in extlist if ext.name == modname]
218249
return len(extlist)
219250

251+
220252
def find_module_file(module, dirlist):
221253
"""Find a module in a set of possible folders. If it is not found
222254
return the unadorned filename"""
@@ -227,6 +259,7 @@ def find_module_file(module, dirlist):
227259
log.info("WARNING: multiple copies of %s found", module)
228260
return os.path.join(list[0], module)
229261

262+
230263
class PyBuildExt(build_ext):
231264

232265
def __init__(self, dist):
@@ -1558,44 +1591,17 @@ def detect_multibytecodecs(self):
15581591
def detect_multiprocessing(self):
15591592
# Richard Oudkerk's multiprocessing module
15601593
if MS_WINDOWS:
1561-
macros = dict()
1562-
libraries = ['ws2_32']
1563-
1564-
elif MACOS: # Mac OSX
1565-
macros = dict()
1566-
libraries = []
1567-
1568-
elif CYGWIN:
1569-
macros = dict()
1570-
libraries = []
1571-
1572-
elif HOST_PLATFORM.startswith('openbsd'):
1573-
macros = dict()
1574-
libraries = []
1575-
1576-
elif HOST_PLATFORM.startswith('netbsd'):
1577-
macros = dict()
1578-
libraries = []
1579-
1580-
else: # Linux and other unices
1581-
macros = dict()
1582-
libraries = ['rt']
1583-
1584-
if MS_WINDOWS:
1585-
multiprocessing_srcs = [ '_multiprocessing/multiprocessing.c',
1586-
'_multiprocessing/semaphore.c',
1587-
]
1594+
multiprocessing_srcs = ['_multiprocessing/multiprocessing.c',
1595+
'_multiprocessing/semaphore.c']
15881596

15891597
else:
1590-
multiprocessing_srcs = [ '_multiprocessing/multiprocessing.c',
1591-
]
1598+
multiprocessing_srcs = ['_multiprocessing/multiprocessing.c']
15921599
if (sysconfig.get_config_var('HAVE_SEM_OPEN') and not
15931600
sysconfig.get_config_var('POSIX_SEMAPHORES_NOT_ENABLED')):
15941601
multiprocessing_srcs.append('_multiprocessing/semaphore.c')
15951602
if (sysconfig.get_config_var('HAVE_SHM_OPEN') and
15961603
sysconfig.get_config_var('HAVE_SHM_UNLINK')):
1597-
posixshmem_srcs = [ '_multiprocessing/posixshmem.c',
1598-
]
1604+
posixshmem_srcs = ['_multiprocessing/posixshmem.c']
15991605
libs = []
16001606
if sysconfig.get_config_var('SHM_NEEDS_LIBRT'):
16011607
# need to link with librt to get shm_open()
@@ -1606,7 +1612,6 @@ def detect_multiprocessing(self):
16061612
include_dirs=["Modules/_multiprocessing"]))
16071613

16081614
self.add(Extension('_multiprocessing', multiprocessing_srcs,
1609-
define_macros=list(macros.items()),
16101615
include_dirs=["Modules/_multiprocessing"]))
16111616

16121617
def detect_uuid(self):
@@ -2303,6 +2308,7 @@ def set_dir_modes(self, dirname, mode):
23032308
log.info("changing mode of %s to %o", dirpath, mode)
23042309
if not self.dry_run: os.chmod(dirpath, mode)
23052310

2311+
23062312
class PyBuildScripts(build_scripts):
23072313
def copy_scripts(self):
23082314
outfiles, updated_files = build_scripts.copy_scripts(self)
@@ -2322,35 +2328,17 @@ def copy_scripts(self):
23222328
newupdated_files.append(newfilename)
23232329
return newoutfiles, newupdated_files
23242330

2325-
SUMMARY = """
2326-
Python is an interpreted, interactive, object-oriented programming
2327-
language. It is often compared to Tcl, Perl, Scheme or Java.
23282331

2329-
Python combines remarkable power with very clear syntax. It has
2330-
modules, classes, exceptions, very high level dynamic data types, and
2331-
dynamic typing. There are interfaces to many system calls and
2332-
libraries, as well as to various windowing systems (X11, Motif, Tk,
2333-
Mac, MFC). New built-in modules are easily written in C or C++. Python
2334-
is also usable as an extension language for applications that need a
2335-
programmable interface.
2332+
def main():
2333+
set_compiler_flags('CFLAGS', 'PY_CFLAGS_NODIST')
2334+
set_compiler_flags('LDFLAGS', 'PY_LDFLAGS_NODIST')
23362335

2337-
The Python implementation is portable: it runs on many brands of UNIX,
2338-
on Windows, DOS, Mac, Amiga... If your favorite system isn't
2339-
listed here, it may still be supported, if there's a C compiler for
2340-
it. Ask around on comp.lang.python -- or just try compiling Python
2341-
yourself.
2342-
"""
2336+
class DummyProcess:
2337+
"""Hack for parallel build"""
2338+
ProcessPoolExecutor = None
23432339

2344-
CLASSIFIERS = """
2345-
Development Status :: 6 - Mature
2346-
License :: OSI Approved :: Python Software Foundation License
2347-
Natural Language :: English
2348-
Programming Language :: C
2349-
Programming Language :: Python
2350-
Topic :: Software Development
2351-
"""
2340+
sys.modules['concurrent.futures.process'] = DummyProcess
23522341

2353-
def main():
23542342
# turn off warnings when deprecated modules are imported
23552343
import warnings
23562344
warnings.filterwarnings("ignore",category=DeprecationWarning)

0 commit comments

Comments
 (0)