Repository: scipy/scipy

Issue:
BUG: `import scipy._lib._testutils` raises exception in some MacOS contexts
### Describe your issue.

I believe SciPy has the same issue that NumPy had some months back, where some MacOS versions fail to parse in the expected way by `packaging.tags`. Perhaps this is an upstream problem, but I saw that NumPy decided to implement a workaround, so I thought SciPy might consider it too. https://github.com/numpy/numpy/issues/24074

https://github.com/scipy/scipy/blob/main/scipy/_lib/_testutils.py appears to use the same logic as NumPy did.

Here's a reproducible example raising the same `ValueError` as described in the NumPy issue. See the "Run Binaries" step here:
https://github.com/davemfish/scipy-macos-repro/actions/runs/7891947591/job/21537356024

In my case I was only able to reproduce it by running a PyInstaller binary build of the offending code.


### Reproducing Code Example

```python
import scipy._lib._testutils
```


### Error message

```shell
Traceback (most recent call last):
  File "repro.py", line 1, in <module>
    import scipy._lib._testutils
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "PyInstaller/loader/pyimod02_importers.py", line 419, in exec_module
  File "scipy/__init__.py", line 67, in <module>
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "PyInstaller/loader/pyimod02_importers.py", line 419, in exec_module
  File "scipy/_lib/__init__.py", line 12, in <module>
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "PyInstaller/loader/pyimod02_importers.py", line 419, in exec_module
  File "scipy/_lib/_testutils.py", line 21, in <module>
  File "packaging/tags.py", line 543, in sys_tags
  File "packaging/tags.py", line 212, in cpython_tags
  File "packaging/tags.py", line 412, in mac_platforms
ValueError: invalid literal for int() with base 10: ''
```


### SciPy/NumPy/Python version and system information

```shell
Platform: macOS-12.7.3-x86_64-i386-64bit

List of packages in environment: "/Users/runner/micromamba/envs/env"
  Name                       Version   Build               Channel    
────────────────────────────────────────────────────────────────────────
  altgraph                   0.17.4    pyhd8ed1ab_0        conda-forge
  bzip2                      1.0.8     h10d778d_5          conda-forge
  ca-certificates            2024.2.2  h8857fd0_0          conda-forge
  importlib-metadata         7.0.1     pyha770c72_0        conda-forge
  importlib_metadata         7.0.1     hd8ed1ab_0          conda-forge
  libblas                    3.9.0     21_osx64_openblas   conda-forge
  libcblas                   3.9.0     21_osx64_openblas   conda-forge
  libcxx                     16.0.6    hd57cbcb_0          conda-forge
  libexpat                   2.5.0     hf0c8a7f_1          conda-forge
  libffi                     3.4.2     h0d85af4_5          conda-forge
  libgfortran                5.0.0     13_2_0_h97931a8_3   conda-forge
  libgfortran5               13.2.0    h2873a65_3          conda-forge
  liblapack                  3.9.0     21_osx64_openblas   conda-forge
  libopenblas                0.3.26    openmp_hfef2a42_0   conda-forge
  libsqlite                  3.45.1    h92b6c6a_0          conda-forge
  libzlib                    1.2.13    h8a1eda9_5          conda-forge
  llvm-openmp                17.0.6    hb6ac08f_0          conda-forge
  macholib                   1.16.3    pyhd8ed1ab_0        conda-forge
  ncurses                    6.4       h93d8f39_2          conda-forge
  numpy                      1.26.4    py311hc43a94b_0     conda-forge
  openssl                    3.2.1     hd75f5a5_0          conda-forge
  packaging                  23.2      pyhd8ed1ab_0        conda-forge
  pip                        24.0      pyhd8ed1ab_0        conda-forge
  pyinstaller                6.4.0     py311h8c2310c_0     conda-forge
  pyinstaller-hooks-contrib  2024.1    pyhd8ed1ab_0        conda-forge
  python                     3.11.7    h9f0c242_1_cpython  conda-forge
  python_abi                 3.11      4_cp311             conda-forge
  readline                   8.2       h9e318b2_1          conda-forge
  scipy                      1.12.0    py311h86d0cd9_2     conda-forge
  setuptools                 69.0.3    pyhd8ed1ab_0        conda-forge
  tk                         8.6.13    h1abcd95_1          conda-forge
  tzdata                     2024a     h0c530f3_0          conda-forge
  wheel                      0.42.0    pyhd8ed1ab_0        conda-forge
  xz                         5.2.6     h775f41a_0          conda-forge
  zipp                       3.17.0    pyhd8ed1ab_0        conda-forge
```



Discussion:

Resolving patch:
diff --git a/scipy/_lib/_testutils.py b/scipy/_lib/_testutils.py
--- a/scipy/_lib/_testutils.py
+++ b/scipy/_lib/_testutils.py
@@ -15,17 +15,13 @@
 
 
 IS_MUSL = False
-try:
-    # Note that packaging is not a dependency, hence we need this try-except:
-    from packaging.tags import sys_tags
-    _tags = list(sys_tags())
-    if 'musllinux' in _tags[0].platform:
-        IS_MUSL = True
-except ImportError:
-    # fallback to sysconfig (might be flaky)
-    v = sysconfig.get_config_var('HOST_GNU_TYPE') or ''
-    if 'musl' in v:
-        IS_MUSL = True
+# alternate way is
+# from packaging.tags import sys_tags
+#     _tags = list(sys_tags())
+#     if 'musllinux' in _tags[0].platform:
+_v = sysconfig.get_config_var('HOST_GNU_TYPE') or ''
+if 'musl' in _v:
+    IS_MUSL = True
 
 
 class FPUModeChangeWarning(RuntimeWarning):


Test patch: