Skip to content

Commit

Permalink
Get rid of numpy.distutils
Browse files Browse the repository at this point in the history
Since numpy 1.26.0, numpy.distutils is being removed in python 3.12, let's try to have a
solution that work for 3.12 *and* previous version without numpy.distutils.

Fix #2148
  • Loading branch information
serge-sans-paille committed Oct 5, 2023
1 parent b4a3abc commit e642a98
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 59 deletions.
46 changes: 3 additions & 43 deletions pythran/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,6 @@ def get_include():
return (os.path.dirname(os.path.dirname(__file__)) or '.') + '/pythran'


class silent(object):
'''
Silent sys.stderr at the system level
'''

def __enter__(self):
try:
self.prevfd = os.dup(sys.stderr.fileno())
os.close(sys.stderr.fileno())
except io.UnsupportedOperation:
self.prevfd = None

self.prevstream = sys.stderr
sys.stderr = open(os.devnull, 'r')

def __exit__(self, exc_type, exc_value, traceback):
sys.stderr.close()
sys.stderr = self.prevstream
if self.prevfd:
os.dup2(self.prevfd, sys.stderr.fileno())
os.close(self.prevfd)


def get_paths_cfg(
sys_file='pythran.cfg',
platform_file='pythran-{}.cfg'.format(sys.platform),
Expand Down Expand Up @@ -246,26 +223,9 @@ def parse_define(define):
extension['define_macros'].append('PYTHRAN_BLAS_NONE')

if user_blas not in reserved_blas_entries:
if sys.version_info < (3, 12):
# `numpy.distutils` not present for Python >= 3.12
try:
import numpy.distutils.system_info as numpy_sys
# Numpy can pollute stdout with checks
with silent():
numpy_blas = numpy_sys.get_info(user_blas)
# required to cope with atlas missing extern "C"
extension['define_macros'].append('PYTHRAN_BLAS_{}'
.format(user_blas.upper()))
extension['libraries'].extend(numpy_blas.get('libraries', []))
extension['library_dirs'].extend(
numpy_blas.get('library_dirs', []))
extension['include_dirs'].extend(
numpy_blas.get('include_dirs', []))
except Exception as exc:
raise RuntimeError(
"The likely cause of this failure is an incompatibility "
"between `setuptools` and `numpy.distutils. "
) from exc
# required to cope with atlas missing extern "C"
extension['define_macros'].append('PYTHRAN_BLAS_{}'
.format(user_blas.upper()))

# final macro normalization
extension["define_macros"] = [
Expand Down
8 changes: 1 addition & 7 deletions pythran/dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@
import os

from distutils.command.build_ext import build_ext as LegacyBuildExt

try:
# `numpy.distutils` is deprecated, and won't be present on Python >=3.12
# If it is installed, we need to use it though, so try-import it:
from numpy.distutils.extension import Extension
except ImportError:
from distutils.extension import Extension
from distutils.extension import Extension



Expand Down
7 changes: 5 additions & 2 deletions pythran/tests/test_distutils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from subprocess import check_call
import glob
import os
import re
import shutil
Expand Down Expand Up @@ -126,9 +127,11 @@ def test_setup_build3(self):
libdir = os.path.join(base, 'lib')
if not os.path.isdir(libdir):
libdir = os.path.join(base, 'lib64')
demo3_site = glob.glob(
os.path.join(libdir, python_version, 'site-packages', 'demo3*')
)[0]
check_call([python, '-c', 'import a'],
cwd=os.path.join(libdir, python_version, 'site-packages',
'demo3'))
cwd=demo3_site)
check_call([python, 'setup.py', 'clean'],
cwd=os.path.join(cwd, 'test_distutils_numpy'))
shutil.rmtree(os.path.join(cwd, 'test_distutils_numpy', 'demo_install3'))
Expand Down
5 changes: 3 additions & 2 deletions pythran/tests/test_distutils_numpy/setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from numpy.distutils.core import setup
from numpy.distutils.command.build_ext import build_ext as npy_build_ext
from distutils.core import setup
from distutils.command.build_ext import build_ext as npy_build_ext

from pythran.dist import PythranExtension, PythranBuildExt

module1 = PythranExtension('demo3.a', sources = ['demo3/a.py'])
Expand Down
6 changes: 1 addition & 5 deletions pythran/toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@

from distutils.errors import CompileError
from distutils import sysconfig
try:
# `numpy.distutils is deprecated, may not be present, or broken
from numpy.distutils.core import setup
except Exception:
from distutils.core import setup
from distutils.core import setup

from tempfile import mkdtemp, NamedTemporaryFile
import gast as ast
Expand Down

0 comments on commit e642a98

Please sign in to comment.