8000 TST: fix issues with Python 3.12 support [wheel build] · numpy/numpy@155d560 · GitHub
[go: up one dir, main page]

Skip to content

Commit 155d560

Browse files
committed
TST: fix issues with Python 3.12 support [wheel build]
Taken over from PR 23991
1 parent 1648a6e commit 155d560

File tree

9 files changed

+128
-89
lines changed

9 files changed

+128
-89
lines changed

numpy/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
NumPy testing tools
6767
distutils
6868
Enhancements to distutils with support for
69-
Fortran compilers support and more.
69+
Fortran compilers support and more (for Python <= 3.11).
7070
7171
Utilities
7272
---------

numpy/_pytesttester.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,13 @@ def __call__(self, label='fast', verbose=1, extra_argv=None,
135135
# offset verbosity. The "-q" cancels a "-v".
136136
pytest_args += ["-q"]
137137

138-
with warnings.catch_warnings():
139-
warnings.simplefilter("always")
140-
# Filter out distutils cpu warnings (could be localized to
141-
# distutils tests). ASV has problems with top level import,
142-
# so fetch module for suppression here.
143-
from numpy.distutils import cpuinfo
138+
if sys.version_info < (3, 12):
139+
with warnings.catch_warnings():
140+
warnings.simplefilter("always")
141+
# Filter out distutils cpu warnings (could be localized to
142+
# distutils tests). ASV has problems with top level import,
143+
# so fetch module for suppression here.
144+
from numpy.distutils import cpuinfo
144145

145146
with warnings.catch_warnings(record=True):
146147
# Ignore the warning from importing the array_api submodule. This

numpy/core/tests/test_array_interface.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ def get_module(tmp_path):
128128
more_init=more_init)
129129

130130

131+
# FIXME: numpy.testing.extbuild uses `numpy.distutils`, so this won't work on
132+
# Python 3.12 and up.
133+
@pytest.mark.skipif(sys.version_info >= (3, 12), reason="no numpy.distutils")
131134
@pytest.mark.slow
132135
def test_cstruct(get_module):
133136

numpy/core/tests/test_dtype.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,11 @@ def iter_struct_object_dtypes():
756756
yield pytest.param(dt, p, 12, obj, id="<structured subarray 2>")
757757

758758

759+
@pytest.mark.skipif(
760+
sys.version_info >= (3, 12),
761+
reason="Python 3.12 has immortal refcounts, this test will no longer "
762+
"work. See gh-23986"
763+
)
759764
@pytest.mark.skipif(not HAS_REFCOUNT, reason="Python lacks refcounts")
760765
class TestStructuredObjectRefcounting:
761766
"""These tests cover various uses of complicated structured types which

numpy/core/tests/test_regression.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,6 +1464,10 @@ def test_structured_arrays_with_objects1(self):
14641464
x[x.nonzero()] = x.ravel()[:1]
14651465
assert_(x[0, 1] == x[0, 0])
14661466

1467+
@pytest.mark.skipif(
1468+
sys.version_info >= (3, 12),
1469+
reason="Python 3.12 has immortal refcounts, this test no longer works."
1470+
)
14671471
@pytest.mark.skipif(not HAS_REFCOUNT, reason="Python lacks refcounts")
14681472
def test_structured_arrays_with_objects2(self):
14691473
# Ticket #1299 second test

numpy/lib/tests/test_format.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ def test_load_padded_dtype(tmpdir, dt):
527527
assert_array_equal(arr, arr1)
528528

529529

530+
@pytest.mark.skipif(sys.version_info >= (3, 12), reason="see gh-23988")
530531
@pytest.mark.xfail(IS_WASM, reason="Emscripten NODEFS has a buggy dup")
531532
def test_python2_python3_interoperability():
532533
fname = 'win64python2.npy'

numpy/tests/test_ctypeslib.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import sys
2-
import pytest
2+
import sysconfig
33
import weakref
44
from pathlib import Path
55

6+
import pytest
7+
68
import numpy as np
79
from numpy.ctypeslib import ndpointer, load_library, as_array
8-
from numpy.distutils.misc_util import get_shared_lib_extension
910
from numpy.testing import assert_, assert_array_equal, assert_raises, assert_equal
1011

1112
try:
@@ -52,12 +53,9 @@ def test_basic2(self):
5253
# Regression for #801: load_library with a full library name
5354
# (including extension) does not work.
5455
try:
55-
try:
56-
so = get_shared_lib_extension(is_python_ext=True)
57-
# Should succeed
58-
load_library('_multiarray_umath%s' % so, np.core._multiarray_umath.__file__)
59-
except ImportError:
60-
print("No distutils available, skipping test.")
56+
so_ext = sysconfig.get_config_var('EXT_SUFFIX')
57+
load_library('_multiarray_umath%s' % so_ext,
58+
np.core._multiarray_umath.__file__)
6159
except ImportError as e:
6260
msg = ("ctypes is not available on this python: skipping the test"
6361
" (import error was: %s)" % str(e))

numpy/tests/test_public_api.py

Lines changed: 98 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,6 @@ def test_NPY_NO_EXPORT():
127127
"array_api",
128128
"array_api.linalg",
129129
"ctypeslib",
130-
"distutils",
131-
"distutils.cpuinfo",
132-
"distutils.exec_command",
133-
"distutils.misc_util",
134-
"distutils.log",
135-
"distutils.system_info",
136130
"doc",
137131
"doc.constants",
138132
"doc.ufuncs",
@@ -165,6 +159,18 @@ def test_NPY_NO_EXPORT():
165159
"typing.mypy_plugin",
166160
"version",
167161
]]
162+
if sys.version_info < (3, 12):
163+
PUBLIC_MODULES += [
164+
'numpy.' + s for s in [
165+
"distutils",
166+
"distutils.cpuinfo",
167+
"distutils.exec_command",
168+
"distutils.misc_util",
169+
"distutils.log",
170+
"distutils.system_info",
171+
]
172+
]
173+
168174

169175

170176
PUBLIC_ALIASED_MODULES = [
@@ -193,62 +199,6 @@ def test_NPY_NO_EXPORT():
193199
"core.records",
194200
"core.shape_base",
195201
"core.umath",
196-
"core.umath_tests",
197-
"distutils.armccompiler",
198-
"distutils.fujitsuccompiler",
199-
"distutils.ccompiler",
200-
'distutils.ccompiler_opt',
201-
"distutils.command",
202-
"distutils.command.autodist",
203-
"distutils.command.bdist_rpm",
204-
"distutils.command.build",
205-
"distutils.command.build_clib",
206-
"distutils.command.build_ext",
207-
"distutils.command.build_py",
208-
"distutils.command.build_scripts",
209-
"distutils.command.build_src",
210-
"distutils.command.config",
211-
"distutils.command.config_compiler",
212-
"distutils.command.develop",
213-
"distutils.command.egg_info",
214-
"distutils.command.install",
215-
"distutils.command.install_clib",
216-
"distutils.command.install_data",
217-
"distutils.command.install_headers",
218-
"distutils.command.sdist",
219-
"distutils.conv_template",
220-
"distutils.core",
221-
"distutils.extension",
222-
"distutils.fcompiler",
223-
"distutils.fcompiler.absoft",
224-
"distutils.fcompiler.arm",
225-
"distutils.fcompiler.compaq",
226-
"distutils.fcompiler.environment",
227-
"distutils.fcompiler.g95",
228-
"distutils.fcompiler.gnu",
229-
"distutils.fcompiler.hpux",
230-
"distutils.fcompiler.ibm",
231-
"distutils.fcompiler.intel",
232-
"distutils.fcompiler.lahey",
233-
"distutils.fcompiler.mips",
234-
"distutils.fcompiler.nag",
235-
"distutils.fcompiler.none",
236-
"distutils.fcompiler.pathf95",
237-
"distutils.fcompiler.pg",
238-
"distutils.fcompiler.nv",
239-
"distutils.fcompiler.sun",
240-
"distutils.fcompiler.vast",
241-
"distutils.fcompiler.fujitsu",
242-
"distutils.from_template",
243-
"distutils.intelccompiler",
244-
"distutils.lib2def",
245-
"distutils.line_endings",
246-
"distutils.mingw32ccompiler",
247-
"distutils.msvccompiler",
248-
"distutils.npy_pkg_config",
249-
"distutils.numpy_distribution",
250-
"distutils.pathccompiler",
251-
"distutils.unixccompiler",
252202
"f2py.auxfuncs",
253203
"f2py.capi_maps",
254204
"f2py.cb_rules",
@@ -290,6 +240,66 @@ def test_NPY_NO_EXPORT():
290240
"random.bit_generator",
291241
"testing.print_coercion_tables",
292242
]]
243+
if sys.version_info < (3, 12):
244+
PRIVATE_BUT_PRESENT_MODULES += [
245+
'numpy.' + s for s in [
246+
"distutils.armccompiler",
247+
"distutils.fujitsuccompiler",
248+
"distutils.ccompiler",
249+
'distutils.ccompiler_opt',
250+
"distutils.command",
251+
"distutils.command.autodist",
252+
"distutils.command.bdist_rpm",
253+
"distutils.command.build",
254+
"distutils.command.build_clib",
255+
"distutils.command.build_ext",
256+
"distutils.command.build_py",
257+
"distutils.command.build_scripts",
258+
"distutils.command.build_src",
259+
"distutils.command.config",
260+
"distutils.command.config_compiler",
261+
"distutils.command.develop",
262+
"distutils.command.egg_info",
263+
"distutils.command.install",
264+
"distutils.command.install_clib",
265+
"distutils.command.install_data",
266+
"distutils.command.install_headers",
267+
"distutils.command.sdist",
268+
"distutils.conv_template",
269+
"distutils.core",
270+
"distutils.extension",
271+
"distutils.fcompiler",
272+
"distutils.fcompiler.absoft",
273+
"distutils.fcompiler.arm",
274+
"distutils.fcompiler.compaq",
275+
"distutils.fcompiler.environment",
276+
"distutils.fcompiler.g95",
277+
"distutils.fcompiler.gnu",
278+
"distutils.fcompiler.hpux",
279+
"distutils.fcompiler.ibm",
280+
"distutils.fcompiler.intel",
281+
"distutils.fcompiler.lahey",
282+
"distutils.fcompiler.mips",
283+
"distutils.fcompiler.nag",
284+
"distutils.fcompiler.none",
285+
"distutils.fcompiler.pathf95",
286+
"distutils.fcompiler.pg",
287+
"distutils.fcompiler.nv",
288+
"distutils.fcompiler.sun",
289+
"distutils.fcompiler.vast",
290+
"distutils.fcompiler.fujitsu",
291+
"distutils.from_template",
292+
"distutils.intelccompiler",
293+
"distutils.lib2def",
294+
"distutils.line_endings",
295+
"distutils.mingw32ccompiler",
296+
"distutils.msvccompiler",
297+
"distutils.npy_pkg_config",
298+
"distutils.numpy_distribution",
299+
"distutils.pathccompiler",
300+
"distutils.unixccompiler",
301+
]
302+
]
293303

294304

295305
def is_unexpected(name):
@@ -323,10 +333,14 @@ def is_unexpected(name):
323333
"numpy.core.code_generators.verify_c_api_version",
324334
"numpy.core.cversions",
325335
"numpy.core.generate_numpy_api",
326-
"numpy.distutils.msvc9compiler",
336+
"numpy.core.umath_tests",
327337
]
338+
if sys.version_info < (3, 12):
339+
SKIP_LIST += ["numpy.distutils.msvc9compiler"]
328340

329341

342+
# suppressing warnings from deprecated modules
343+
@pytest.mark.filterwarnings("ignore:.*np.compat.*:DeprecationWarning")
330344
def test_all_modules_are_expected():
331345
"""
332346
Test that we don't add anything that looks like a new public module by
@@ -351,9 +365,6 @@ def test_all_modules_are_expected():
351365
# below
352366
SKIP_LIST_2 = [
353367
'numpy.math',
354-
'numpy.distutils.log.sys',
355-
'numpy.distutils.log.logging',
356-
'numpy.distutils.log.warnings',
357368
'numpy.doc.constants.re',
358369
'numpy.doc.constants.textwrap',
359370
'numpy.lib.emath',
@@ -369,6 +380,12 @@ def test_all_modules_are_expected():
369380
'numpy.matlib.ctypeslib',
370381
'numpy.matlib.ma',
371382
]
383+
if sys.version_info < (3, 12):
384+
SKIP_LIST_2 += [
385+
'numpy.distutils.log.sys',
386+
'numpy.distutils.log.logging',
387+
'numpy.distutils.log.warnings',
388+
]
372389

373390

374391
def test_all_modules_are_expected_2():
@@ -472,11 +489,7 @@ def check_importable(module_name):
472489

473490

474491
@pytest.mark.xfail(
475-
hasattr(np.__config__, "_built_with_meson"),
476-
reason = "Meson does not yet support entry points via pyproject.toml",
477-
)
478-
@pytest.mark.xfail(
479-
sysconfig.get_config_var("Py_DEBUG") is not None,
492+
sysconfig.get_config_var("Py_DEBUG") not in (None, 0, "0"),
480493
reason=(
481494
"NumPy possibly built with `USE_DEBUG=True ./tools/travis-test.sh`, "
482495
"which does not expose the `array_api` entry point. "
@@ -488,6 +501,11 @@ def test_array_api_entry_point():
488501
Entry point for Array API implementation can be found with importlib and
489502
returns the numpy.array_api namespace.
490503
"""
504+
# For a development install that did not go through meson-python,
505+
# the entrypoint will not have been installed. So ensure this test fails
506+
# only if numpy is inside site-packages.
507+
numpy_in_sitepackages = sysconfig.get_path('platlib') in np.__file__
508+
491509
eps = importlib.metadata.entry_points()
492510
try:
493511
xp_eps = eps.select(group="array_api")
@@ -497,12 +515,19 @@ def test_array_api_entry_point():
497515
# Array API entry points so that running this test in <=3.9 will
498516
# still work - see https://github.com/numpy/numpy/pull/19800.
499517
xp_eps = eps.get("array_api", [])
500-
assert len(xp_eps) > 0, "No entry points for 'array_api' found"
518+
if len(xp_eps) == 0:
519+
if numpy_in_sitepackages:
520+
msg = "No entry points for 'array_api' found"
521+
raise AssertionError(msg) from None
522+
return
501523

502524
try:
503525
ep = next(ep for ep in xp_eps if ep.name == "numpy")
504526
except StopIteration:
505-
raise AssertionError("'numpy' not in array_api entry points") from None
527+
if numpy_in_sitepackages:
528+
msg = "'numpy' not in array_api entry points"
529+
raise AssertionError(msg) from None
530+
return
506531

507532
xp = ep.load()
508533
msg = (

numpy/typing/tests/test_isfile.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import sys
23
from pathlib import Path
34

45
import numpy as np
@@ -10,7 +11,6 @@
1011
ROOT / "__init__.pyi",
1112
ROOT / "ctypeslib.pyi",
1213
ROOT / "core" / "__init__.pyi",
13-
ROOT / "distutils" / "__init__.pyi",
1414
ROOT / "f2py" / "__init__.pyi",
1515
ROOT / "fft" / "__init__.pyi",
1616
ROOT / "lib" / "__init__.pyi",
@@ -21,6 +21,8 @@
2121
ROOT / "random" / "__init__.pyi",
2222
ROOT / "testing" / "__init__.pyi",
2323
]
24+
if sys.version_info < (3, 12):
25+
FILES += [ROOT / "distutils" / "__init__.pyi"]
2426

2527

2628
class TestIsFile:

0 commit comments

Comments
 (0)
0