Skip to content

Commit

Permalink
Python bindings: honour GDAL_PYTHON_BINDINGS_WITHOUT_NUMPY=YES/1/ON/T…
Browse files Browse the repository at this point in the history
…RUE or NO/0/OFF/FALSE

Fixes OSGeo#11853
  • Loading branch information
rouault committed Feb 20, 2025
1 parent f18aaa7 commit 6378b44
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions swig/python/setup.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -186,20 +186,32 @@ if sys.platform == 'win32':

numpy_include_dir = '.'
numpy_error_msg = ""
try:
numpy_include_dir = get_numpy_include()
HAVE_NUMPY = numpy_include_dir != '.'

do_numpy_detection = True
if "GDAL_PYTHON_BINDINGS_WITHOUT_NUMPY" in os.environ:
v = os.environ["GDAL_PYTHON_BINDINGS_WITHOUT_NUMPY"].upper()
if v in ('YES', '1', 'ON', 'TRUE'):
do_numpy_detection = False
elif v not in ('NO', '0', 'OFF', 'FALSE'):
raise Exception("Unrecognized value for GDAL_PYTHON_BINDINGS_WITHOUT_NUMPY")

if do_numpy_detection:
try:
numpy_include_dir = get_numpy_include()
HAVE_NUMPY = numpy_include_dir != '.'
if not HAVE_NUMPY:
numpy_error_msg = "numpy found, but numpy headers were not found!"
except ImportError:
HAVE_NUMPY = False
numpy_error_msg = "numpy not available!"

if not HAVE_NUMPY:
numpy_error_msg = "numpy found, but numpy headers were not found!"
except ImportError:
if "GDAL_PYTHON_BINDINGS_WITHOUT_NUMPY" in os.environ:
print("WARNING: " + numpy_error_msg + " Array support will not be enabled.")
else:
raise Exception(numpy_error_msg + " This error may happen if you build/install using setup.py directly, but should normally not happen if you install using pip install. If you still want to build the bindings without numpy support, define the GDAL_PYTHON_BINDINGS_WITHOUT_NUMPY environment variable")
else:
HAVE_NUMPY = False
numpy_error_msg = "numpy not available!"

if not HAVE_NUMPY:
if "GDAL_PYTHON_BINDINGS_WITHOUT_NUMPY" in os.environ:
print("WARNING: " + numpy_error_msg + " Array support will not be enabled.")
else:
raise Exception(numpy_error_msg + " This error may happen if you build/install using setup.py directly, but should normally not happen if you install using pip install. If you still want to build the bindings without numpy support, define the GDAL_PYTHON_BINDINGS_WITHOUT_NUMPY environment variable")

class gdal_ext(build_ext):

Expand Down

0 comments on commit 6378b44

Please sign in to comment.