Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c35a3f4

Browse files
authored
BLD: fix build failure with PYTHONSAFEPATH=1 (#30731)
1 parent 995a7a0 commit c35a3f4

6 files changed

Lines changed: 48 additions & 20 deletions

File tree

.github/workflows/linux.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ jobs:
158158
- name: Install as editable
159159
env:
160160
PKG_CONFIG_PATH: ${{ github.workspace }}/.openblas
161+
PYTHONSAFEPATH: 1 # regression check for gh-24907
161162
run: |
162163
pip install -e . --no-build-isolation
163164
- name: Run full test suite
@@ -166,6 +167,7 @@ jobs:
166167
# TODO: gcov
167168
env:
168169
PYTHONOPTIMIZE: 2
170+
PYTHONSAFEPATH: 1
169171

170172
armhf_test:
171173
# Tests NumPy on 32-bit ARM hard-float (armhf) via compatibility mode

numpy/_build_utils/tempita.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
#!/usr/bin/env python3
22
import argparse
3+
import importlib.util
34
import os
45
import sys
56

6-
import tempita
7+
8+
def import_tempita():
9+
init_py = os.path.join(os.path.dirname(__file__), 'tempita/__init__.py')
10+
spec = importlib.util.spec_from_file_location('tempita', init_py)
11+
tempita = importlib.util.module_from_spec(spec)
12+
sys.modules['tempita'] = tempita
13+
spec.loader.exec_module(tempita)
14+
return tempita
715

816

917
def process_tempita(fromfile, outfile=None):
@@ -17,6 +25,7 @@ def process_tempita(fromfile, outfile=None):
1725
# We're dealing with a distutils build here, write in-place
1826
outfile = os.path.splitext(fromfile)[0]
1927

28+
tempita = import_tempita()
2029
from_filename = tempita.Template.from_filename
2130
template = from_filename(fromfile, encoding=sys.getdefaultencoding())
2231

numpy/_core/code_generators/generate_numpy_api.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
import argparse
33
import os
44

5-
import genapi
6-
import numpy_api
7-
from genapi import BoolValuesApi, FunctionApi, GlobalVarApi, TypeApi
5+
from . import genapi, numpy_api
6+
from .genapi import BoolValuesApi, FunctionApi, GlobalVarApi, TypeApi
87

98
# use annotated api when running under cpychecker
109
h_template = r"""

numpy/_core/code_generators/generate_ufunc_api.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import argparse
22
import os
33

4-
import genapi
5-
import numpy_api
6-
from genapi import FunctionApi, TypeApi
4+
from . import genapi, numpy_api
5+
from .genapi import FunctionApi, TypeApi
76

87
h_template = r"""
98
#ifdef _UMATHMODULE

numpy/_core/code_generators/generate_umath_doc.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import argparse
22
import os
3-
import sys
43
import textwrap
54

6-
sys.path.insert(0, os.path.dirname(__file__))
7-
import ufunc_docstrings as docstrings
5+
from . import ufunc_docstrings as docstrings
86

9-
sys.path.pop(0)
107

118
def normalize_doc(docstring):
129
docstring = textwrap.dedent(docstring).strip()

numpy/_core/meson.build

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -694,32 +694,54 @@ endif
694694
# building, but we can't add it to the sources of that extension (this C file
695695
# doesn't compile, it's only included in another r file. Hence use the slightly
696696
# hacky --ignore argument to the next custom_target().
697+
codegen_files = [
698+
'code_generators/__init__.py',
699+
'code_generators/genapi.py',
700+
'code_generators/generate_numpy_api.py',
701+
'code_generators/generate_ufunc_api.py',
702+
'code_generators/generate_umath_doc.py',
703+
'code_generators/generate_umath.py',
704+
'code_generators/numpy_api.py',
705+
'code_generators/ufunc_docstrings.py',
706+
]
707+
codegen_env = {
708+
# Explicitly set PYTHONPATH to avoid depending on the current working directory
709+
# at build time, and on unsafe paths that may be prepended to sys.path and can be
710+
# disabled by PYTHONSAFEPATH. See:
711+
# [1] https://github.com/numpy/numpy/issues/24907
712+
# [2] https://docs.python.org/3/library/sys.html#sys.path
713+
'PYTHONPATH': meson.current_source_dir(),
714+
}
715+
697716
src_umath_api_c = custom_target('__umath_generated',
698717
output : '__umath_generated.c',
699-
input : 'code_generators/generate_umath.py',
700-
command: [py, '@INPUT@', '-o', '@OUTPUT@'],
718+
depend_files: codegen_files,
719+
command: [py, '-m', 'code_generators.generate_umath', '-o', '@OUTPUT@'],
720+
env: codegen_env,
701721
)
702722

703723
src_umath_doc_h = custom_target('_umath_doc_generated',
704724
output : '_umath_doc_generated.h',
705-
input : ['code_generators/generate_umath_doc.py',
706-
'code_generators/ufunc_docstrings.py'],
707-
command: [py, '@INPUT0@', '-o', '@OUTPUT@'],
725+
depend_files: codegen_files,
726+
command: [py, '-m', 'code_generators.generate_umath_doc', '-o', '@OUTPUT@'],
727+
env: codegen_env,
708728
)
709729

710730
src_numpy_api = custom_target('__multiarray_api',
711731
output : ['__multiarray_api.c', '__multiarray_api.h'],
712-
input : 'code_generators/generate_numpy_api.py',
713-
command: [py, '@INPUT@', '-o', '@OUTDIR@', '--ignore', src_umath_api_c],
732+
depend_files: codegen_files,
733+
command: [py, '-m', 'code_generators.generate_numpy_api', '-o', '@OUTDIR@', '--ignore', src_umath_api_c],
734+
env: codegen_env,
714735
install: true, # NOTE: setup.py build installs all, but just need .h?
715736
install_dir: np_dir / '_core/include/numpy',
716737
install_tag: 'devel'
717738
)
718739

719740
src_ufunc_api = custom_target('__ufunc_api',
720741
output : ['__ufunc_api.c', '__ufunc_api.h'],
721-
input : 'code_generators/generate_ufunc_api.py',
722-
command: [py, '@INPUT@', '-o', '@OUTDIR@'],
742+
depend_files: codegen_files,
743+
command: [py, '-m', 'code_generators.generate_ufunc_api', '-o', '@OUTDIR@'],
744+
env: codegen_env,
723745
install: true, # NOTE: setup.py build installs all, but just need .h?
724746
install_dir: np_dir / '_core/include/numpy',
725747
install_tag: 'devel'

0 commit comments

Comments
 (0)