From 66cb824d8a266077841e27df955235886f118139 Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Tue, 26 May 2020 19:11:06 +0100 Subject: Defer ctypes imports in _dtypes_ctypes module --- numpy/core/_dtype_ctypes.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/numpy/core/_dtype_ctypes.py b/numpy/core/_dtype_ctypes.py index 708241289..017f13ff8 100644 --- a/numpy/core/_dtype_ctypes.py +++ b/numpy/core/_dtype_ctypes.py @@ -22,8 +22,6 @@ Unfortunately, this fails because: * PEP3118 cannot represent unions, but both numpy and ctypes can * ctypes cannot handle big-endian structs with PEP3118 (bpo-32780) """ -import _ctypes -import ctypes import numpy as np @@ -39,6 +37,7 @@ def _from_ctypes_structure(t): "ctypes bitfields have no dtype equivalent") if hasattr(t, "_pack_"): + import ctypes formats = [] offsets = [] names = [] @@ -79,6 +78,7 @@ def _from_ctypes_scalar(t): def _from_ctypes_union(t): + import ctypes formats = [] offsets = [] names = [] @@ -98,6 +98,7 @@ def dtype_from_ctypes_type(t): """ Construct a dtype object from a ctypes type """ + import _ctypes if issubclass(t, _ctypes.Array): return _from_ctypes_array(t) elif issubclass(t, _ctypes._Pointer): -- cgit v1.2.1 From 6541b11c4014650a675f016ba3b2e7a35e5998b8 Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Tue, 26 May 2020 19:13:38 +0100 Subject: Defer ctypes import in generated _distributor_init.py --- tools/openblas_support.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tools/openblas_support.py b/tools/openblas_support.py index 6b2ad0f8c..cbb6a5e43 100644 --- a/tools/openblas_support.py +++ b/tools/openblas_support.py @@ -207,12 +207,12 @@ def make_init(dirname): and is created as part of the scripts that build the wheel. ''' import os - from ctypes import WinDLL import glob if os.name == 'nt': # convention for storing / loading the DLL from # numpy/.libs/, if present try: + from ctypes import WinDLL basedir = os.path.dirname(__file__) except: pass @@ -221,16 +221,16 @@ def make_init(dirname): DLL_filenames = [] if os.path.isdir(libs_dir): for filename in glob.glob(os.path.join(libs_dir, - '*openblas*dll')): + '*openblas*dll')): # NOTE: would it change behavior to load ALL # DLLs at this path vs. the name restriction? WinDLL(os.path.abspath(filename)) DLL_filenames.append(filename) - if len(DLL_filenames) > 1: - import warnings - warnings.warn("loaded more than 1 DLL from .libs:\\n%s" % - "\\n".join(DLL_filenames), - stacklevel=1) + if len(DLL_filenames) > 1: + import warnings + warnings.warn("loaded more than 1 DLL from .libs:\\n%s" % + "\\n".join(DLL_filenames), + stacklevel=1) """)) def test_setup(arches): -- cgit v1.2.1 From 739de19d3bc61583c342678f386f917725b33c25 Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Tue, 26 May 2020 19:36:08 +0100 Subject: Add comment for delayed imports --- numpy/core/_dtype_ctypes.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/numpy/core/_dtype_ctypes.py b/numpy/core/_dtype_ctypes.py index 017f13ff8..6d7cbb244 100644 --- a/numpy/core/_dtype_ctypes.py +++ b/numpy/core/_dtype_ctypes.py @@ -23,6 +23,9 @@ Unfortunately, this fails because: * ctypes cannot handle big-endian structs with PEP3118 (bpo-32780) """ +# We delay-import ctypes for distributions that do not include it. +# While this module is not used unless the user passes in ctypes +# members, it is eagerly imported from numpy/core/__init__.py. import numpy as np -- cgit v1.2.1