From fcb493ba3c9c653540355fc113605076ab7ee2b6 Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Mon, 3 Jul 2023 18:15:31 +0100 Subject: [PATCH 1/3] [WIP] deal with not having distutils --- mypyc/build.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/mypyc/build.py b/mypyc/build.py index 5fc041e2dcf2..1ca27b0e1387 100644 --- a/mypyc/build.py +++ b/mypyc/build.py @@ -41,7 +41,7 @@ from mypyc.options import CompilerOptions if TYPE_CHECKING: - from distutils.core import Extension as _distutils_Extension + from distutils.core import Extension as _distutils_Extension # type: ignore from typing_extensions import TypeAlias from setuptools import Extension as _setuptools_Extension @@ -56,14 +56,14 @@ if sys.version_info >= (3, 12): # Raise on Python 3.12, since distutils will go away forever raise -from distutils import ccompiler, sysconfig +from distutils import ccompiler, sysconfig # type: ignore -def get_extension() -> type[Extension]: +def get_extension() -> type[Extension]: # type: ignore[no-any-unimported] # We can work with either setuptools or distutils, and pick setuptools # if it has been imported. use_setuptools = "setuptools" in sys.modules - extension_class: type[Extension] + extension_class: type[Extension] # type: ignore[no-any-unimported] if not use_setuptools: import distutils.core @@ -241,7 +241,7 @@ def generate_c( return ctext, "\n".join(format_modules(modules)) -def build_using_shared_lib( +def build_using_shared_lib( # type: ignore[no-any-unimported] sources: list[BuildSource], group_name: str, cfiles: list[str], @@ -289,7 +289,7 @@ def build_using_shared_lib( return extensions -def build_single_module( +def build_single_module( # type: ignore[no-any-unimported] sources: list[BuildSource], cfiles: list[str], extra_compile_args: list[str] ) -> list[Extension]: """Produce the list of extension modules for a standalone extension. @@ -443,7 +443,7 @@ def mypyc_build( return groups, group_cfilenames -def mypycify( +def mypycify( # type: ignore[no-any-unimported] paths: list[str], *, only_compile_paths: Iterable[str] | None = None, From ea64a4d67604f1f17336bb58467180b3a4292cbe Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Mon, 3 Jul 2023 19:59:41 +0100 Subject: [PATCH 2/3] Clean up --- mypyc/build.py | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/mypyc/build.py b/mypyc/build.py index 1ca27b0e1387..37e8f735d3fb 100644 --- a/mypyc/build.py +++ b/mypyc/build.py @@ -40,36 +40,43 @@ from mypyc.namegen import exported_name from mypyc.options import CompilerOptions -if TYPE_CHECKING: - from distutils.core import Extension as _distutils_Extension # type: ignore - from typing_extensions import TypeAlias +if sys.version_info < (3, 12): + if TYPE_CHECKING: + from distutils.core import Extension as _distutils_Extension + from typing_extensions import TypeAlias - from setuptools import Extension as _setuptools_Extension + from setuptools import Extension as _setuptools_Extension - Extension: TypeAlias = Union[_setuptools_Extension, _distutils_Extension] + Extension: TypeAlias = Union[_setuptools_Extension, _distutils_Extension] - -try: - # Import setuptools so that it monkey-patch overrides distutils + try: + # Import setuptools so that it monkey-patch overrides distutils + import setuptools + except ImportError: + pass + from distutils import ccompiler, sysconfig +else: import setuptools -except ImportError: - if sys.version_info >= (3, 12): - # Raise on Python 3.12, since distutils will go away forever - raise -from distutils import ccompiler, sysconfig # type: ignore + from setuptools import Extension + from setuptools._distutils import ccompiler as _ccompiler # type: ignore[attr-defined] + from setuptools._distutils import sysconfig as _sysconfig # type: ignore[attr-defined] + ccompiler = _ccompiler + sysconfig = _sysconfig -def get_extension() -> type[Extension]: # type: ignore[no-any-unimported] +def get_extension() -> type[Extension]: # We can work with either setuptools or distutils, and pick setuptools # if it has been imported. use_setuptools = "setuptools" in sys.modules - extension_class: type[Extension] # type: ignore[no-any-unimported] + extension_class: type[Extension] - if not use_setuptools: + if sys.version_info < (3, 12) and not use_setuptools: import distutils.core extension_class = distutils.core.Extension else: + if not use_setuptools: + sys.exit("error: setuptools not installed") extension_class = setuptools.Extension return extension_class @@ -241,7 +248,7 @@ def generate_c( return ctext, "\n".join(format_modules(modules)) -def build_using_shared_lib( # type: ignore[no-any-unimported] +def build_using_shared_lib( sources: list[BuildSource], group_name: str, cfiles: list[str], @@ -289,7 +296,7 @@ def build_using_shared_lib( # type: ignore[no-any-unimported] return extensions -def build_single_module( # type: ignore[no-any-unimported] +def build_single_module( sources: list[BuildSource], cfiles: list[str], extra_compile_args: list[str] ) -> list[Extension]: """Produce the list of extension modules for a standalone extension. @@ -443,7 +450,7 @@ def mypyc_build( return groups, group_cfilenames -def mypycify( # type: ignore[no-any-unimported] +def mypycify( paths: list[str], *, only_compile_paths: Iterable[str] | None = None, From 8b214bec89731b4b65e38d0ddcf6dc9c8d93c065 Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Mon, 3 Jul 2023 20:00:14 +0100 Subject: [PATCH 3/3] Black --- mypyc/build.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/mypyc/build.py b/mypyc/build.py index 37e8f735d3fb..9889577d4add 100644 --- a/mypyc/build.py +++ b/mypyc/build.py @@ -58,8 +58,11 @@ else: import setuptools from setuptools import Extension - from setuptools._distutils import ccompiler as _ccompiler # type: ignore[attr-defined] - from setuptools._distutils import sysconfig as _sysconfig # type: ignore[attr-defined] + from setuptools._distutils import ( + ccompiler as _ccompiler, # type: ignore[attr-defined] + sysconfig as _sysconfig, # type: ignore[attr-defined] + ) + ccompiler = _ccompiler sysconfig = _sysconfig