Skip to content

Commit 3409264

Browse files
committed
Lazy setuptools imports
1 parent 79eee67 commit 3409264

File tree

3 files changed

+19
-27
lines changed

3 files changed

+19
-27
lines changed

pytensor/link/c/cmodule.py

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,12 @@
2626
from typing import TYPE_CHECKING, Protocol, cast
2727

2828
import numpy as np
29-
from setuptools._distutils.sysconfig import (
30-
get_config_h_filename,
31-
get_config_var,
32-
get_python_inc,
33-
get_python_lib,
34-
)
3529

3630
# we will abuse the lockfile mechanism when reading and writing the registry
3731
from pytensor.compile.compilelock import lock_ctx
3832
from pytensor.configdefaults import config, gcc_version_str
3933
from pytensor.configparser import BoolParam, StrParam
4034
from pytensor.graph.op import Op
41-
from pytensor.link.c.exceptions import CompileError, MissingGXX
4235
from pytensor.utils import (
4336
LOCAL_BITWIDTH,
4437
flatten,
@@ -266,6 +259,8 @@ def list_code(self, ofile=sys.stdout):
266259

267260
def _get_ext_suffix():
268261
"""Get the suffix for compiled extensions"""
262+
from setuptools._distutils.sysconfig import get_config_var
263+
269264
dist_suffix = get_config_var("EXT_SUFFIX")
270265
if dist_suffix is None:
271266
dist_suffix = get_config_var("SO")
@@ -1697,6 +1692,8 @@ def get_gcc_shared_library_arg():
16971692

16981693

16991694
def std_include_dirs():
1695+
from setuptools._distutils.sysconfig import get_python_inc
1696+
17001697
numpy_inc_dirs = [np.get_include()]
17011698
py_inc = get_python_inc()
17021699
py_plat_spec_inc = get_python_inc(plat_specific=True)
@@ -1709,6 +1706,12 @@ def std_include_dirs():
17091706

17101707
@is_StdLibDirsAndLibsType
17111708
def std_lib_dirs_and_libs() -> tuple[list[str], ...] | None:
1709+
from setuptools._distutils.sysconfig import (
1710+
get_config_var,
1711+
get_python_inc,
1712+
get_python_lib,
1713+
)
1714+
17121715
# We cache the results as on Windows, this trigger file access and
17131716
# this method is called many times.
17141717
if std_lib_dirs_and_libs.data is not None:
@@ -2388,23 +2391,6 @@ def join_options(init_part):
23882391
# xcode's version.
23892392
cxxflags.append("-ld64")
23902393

2391-
if sys.platform == "win32":
2392-
# Workaround for https://github.com/Theano/Theano/issues/4926.
2393-
# https://github.com/python/cpython/pull/11283/ removed the "hypot"
2394-
# redefinition for recent CPython versions (>=2.7.16 and >=3.7.3).
2395-
# The following nullifies that redefinition, if it is found.
2396-
python_version = sys.version_info[:3]
2397-
if (3,) <= python_version < (3, 7, 3):
2398-
config_h_filename = get_config_h_filename()
2399-
try:
2400-
with open(config_h_filename) as config_h:
2401-
if any(
2402-
line.startswith("#define hypot _hypot") for line in config_h
2403-
):
2404-
cxxflags.append("-D_hypot=hypot")
2405-
except OSError:
2406-
pass
2407-
24082394
return cxxflags
24092395

24102396
@classmethod
@@ -2555,8 +2541,9 @@ def compile_str(
25552541
25562542
"""
25572543
# TODO: Do not do the dlimport in this function
2558-
25592544
if not config.cxx:
2545+
from pytensor.link.c.exceptions import MissingGXX
2546+
25602547
raise MissingGXX("g++ not available! We can't compile c code.")
25612548

25622549
if include_dirs is None:
@@ -2586,6 +2573,8 @@ def compile_str(
25862573
cppfile.write("\n")
25872574

25882575
if platform.python_implementation() == "PyPy":
2576+
from setuptools._distutils.sysconfig import get_config_var
2577+
25892578
suffix = "." + get_lib_extension()
25902579

25912580
dist_suffix = get_config_var("SO")
@@ -2642,6 +2631,8 @@ def print_command_line_error():
26422631
status = p_out[2]
26432632

26442633
if status:
2634+
from pytensor.link.c.exceptions import CompileError
2635+
26452636
tf = tempfile.NamedTemporaryFile(
26462637
mode="w", prefix="pytensor_compilation_error_", delete=False
26472638
)

pytensor/link/vm.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
from pytensor.configdefaults import config
2020
from pytensor.graph.basic import Apply, Constant, Variable
2121
from pytensor.link.basic import Container, LocalLinker
22-
from pytensor.link.c.exceptions import MissingGXX
2322
from pytensor.link.utils import (
2423
gc_helper,
2524
get_destroy_dependencies,
@@ -1006,6 +1005,8 @@ def make_vm(
10061005
compute_map,
10071006
updated_vars,
10081007
):
1008+
from pytensor.link.c.exceptions import MissingGXX
1009+
10091010
pre_call_clear = [storage_map[v] for v in self.no_recycling]
10101011

10111012
try:

pytensor/scan/op.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@
7474
from pytensor.graph.replace import clone_replace
7575
from pytensor.graph.utils import InconsistencyError, MissingInputError
7676
from pytensor.link.c.basic import CLinker
77-
from pytensor.link.c.exceptions import MissingGXX
7877
from pytensor.printing import op_debug_information
7978
from pytensor.scan.utils import ScanProfileStats, Validator, forced_replace, safe_new
8079
from pytensor.tensor.basic import as_tensor_variable
@@ -1499,6 +1498,7 @@ def make_thunk(self, node, storage_map, compute_map, no_recycling, impl=None):
14991498
then it must not do so for variables in the no_recycling list.
15001499
15011500
"""
1501+
from pytensor.link.c.exceptions import MissingGXX
15021502

15031503
# Before building the thunk, validate that the inner graph is
15041504
# coherent

0 commit comments

Comments
 (0)