Skip to content

Commit 24b7dba

Browse files
authored
[mypyc] Postpone when decide setuptools or distutils (#7897)
No need to make this decision at module load time. Push it back to when we need to instantiate things.
1 parent 5269422 commit 24b7dba

File tree

2 files changed

+23
-30
lines changed

2 files changed

+23
-30
lines changed

mypyc/build.py

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@
2626
import re
2727

2828
from typing import List, Tuple, Any, Optional, Dict, Union, Set, cast
29-
MYPY = False
30-
if MYPY:
31-
from typing import NoReturn
29+
from typing_extensions import TYPE_CHECKING, NoReturn, Type
3230

3331
from mypy.main import process_options
3432
from mypy.errors import CompileError
@@ -42,21 +40,23 @@
4240

4341
from mypyc import emitmodule
4442

43+
if TYPE_CHECKING:
44+
from distutils.core import Extension # noqa
4545

46-
# We can work with either setuptools or distutils, and pick setuptools
47-
# if it has been imported.
48-
assert 'setuptools' in sys.modules or 'distutils' in sys.modules, (
49-
"'setuptools' or 'distutils' must be imported before mypyc.build")
50-
USE_SETUPTOOLS = 'setuptools' in sys.modules
46+
from distutils import sysconfig, ccompiler
5147

52-
if not USE_SETUPTOOLS:
53-
from distutils.core import setup, Extension
54-
from distutils.command.build_ext import build_ext # type: ignore
55-
else:
56-
from setuptools import setup, Extension # type: ignore # noqa
57-
from setuptools.command.build_ext import build_ext # type: ignore
5848

59-
from distutils import sysconfig, ccompiler
49+
def get_extension() -> Type['Extension']:
50+
# We can work with either setuptools or distutils, and pick setuptools
51+
# if it has been imported.
52+
use_setuptools = 'setuptools' in sys.modules
53+
54+
if not use_setuptools:
55+
from distutils.core import Extension
56+
else:
57+
from setuptools import Extension # type: ignore # noqa
58+
59+
return Extension
6060

6161

6262
def setup_mypycify_vars() -> None:
@@ -73,7 +73,7 @@ def setup_mypycify_vars() -> None:
7373
vars['CFLAGS'] = vars['CFLAGS'].replace('-arch i386', '')
7474

7575

76-
def fail(message: str) -> 'NoReturn':
76+
def fail(message: str) -> NoReturn:
7777
# TODO: Is there something else we should do to fail?
7878
sys.exit(message)
7979

@@ -221,7 +221,7 @@ def build_using_shared_lib(sources: List[BuildSource],
221221
deps: List[str],
222222
build_dir: str,
223223
extra_compile_args: List[str],
224-
) -> List[Extension]:
224+
) -> List['Extension']:
225225
"""Produce the list of extension modules when a shared library is needed.
226226
227227
This creates one shared library extension module that all of the
@@ -233,7 +233,7 @@ def build_using_shared_lib(sources: List[BuildSource],
233233
extension module that exports the real initialization functions in
234234
Capsules stored in module attributes.
235235
"""
236-
extensions = [Extension(
236+
extensions = [get_extension()(
237237
shared_lib_name(group_name),
238238
sources=cfiles,
239239
include_dirs=[include_dir()],
@@ -251,7 +251,7 @@ def build_using_shared_lib(sources: List[BuildSource],
251251
assert source.path
252252
if os.path.split(source.path)[1] == '__init__.py':
253253
full_module_name += '.__init__'
254-
extensions.append(Extension(
254+
extensions.append(get_extension()(
255255
full_module_name,
256256
sources=[shim_file],
257257
extra_compile_args=extra_compile_args,
@@ -263,12 +263,12 @@ def build_using_shared_lib(sources: List[BuildSource],
263263
def build_single_module(sources: List[BuildSource],
264264
cfiles: List[str],
265265
extra_compile_args: List[str],
266-
) -> List[Extension]:
266+
) -> List['Extension']:
267267
"""Produce the list of extension modules for a standalone extension.
268268
269269
This contains just one module, since there is no need for a shared module.
270270
"""
271-
return [Extension(
271+
return [get_extension()(
272272
sources[0].module,
273273
sources=cfiles,
274274
include_dirs=[include_dir()],
@@ -367,7 +367,7 @@ def mypycify(
367367
multi_file: bool = False,
368368
separate: Union[bool, List[Tuple[List[str], Optional[str]]]] = False,
369369
skip_cgen_input: Optional[Any] = None
370-
) -> List[Extension]:
370+
) -> List['Extension']:
371371
"""Main entry point to building using mypyc.
372372
373373
This produces a list of Extension objects that should be passed as the
@@ -500,9 +500,3 @@ def mypycify(
500500
group_sources, cfilenames + shared_cfilenames, cflags))
501501

502502
return extensions
503-
504-
505-
# For backwards compatibility we define this as an alias. Previous
506-
# versions used to require using it, and it is conceivable that future
507-
# versions will need it also.
508-
MypycifyBuildExt = build_ext

setup.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def run(self):
140140
del sys.modules['mypy.git']
141141
sys.path.insert(0, use_other_mypyc)
142142

143-
from mypyc.build import mypycify, MypycifyBuildExt
143+
from mypyc.build import mypycify
144144
opt_level = os.getenv('MYPYC_OPT_LEVEL', '3')
145145
force_multifile = os.getenv('MYPYC_MULTI_FILE', '') == '1'
146146
ext_modules = mypycify(
@@ -151,7 +151,6 @@ def run(self):
151151
# our Appveyor builds run out of memory sometimes.
152152
multi_file=sys.platform == 'win32' or force_multifile,
153153
)
154-
cmdclass['build_ext'] = MypycifyBuildExt
155154
else:
156155
ext_modules = []
157156

0 commit comments

Comments
 (0)