Repository: sagemath/sage

Issue:
sage.env.sage_include_directories: Don't use distutils and SAGE_LIB
<div id="comment:0"></div>

- `src/sage/env.py` uses `distutils.sysconfig.get_python_inc()` which is deprecated, it has no replacement in sysconfig.

 Suggestion from fbissey: "we could use sysconfig.get_config_var('INCLUDEPY') which should give the same value." [#33135 comment:7](https://github.com/sagemath/sage/issues/33135#comment:7)

We also remove the use of `SAGE_LIB` in preparation of namespace packages.

This is the last use of `distutils` in the Sage library, except for uses in `src/sage/features/__init__.py` and `src/sage/misc/cython.py`, which are merely fallbacks when `setuptools` is not present.

 Comment from arojas: "I don't see any code in sagelib or cython that would emit this. Is this still needed at all?"

Follow-up: Once these uses of distutils are removed, their modules can be removed from the regex in the call to filterwarnings introduced by #33135.

Part of 
- Meta-ticket #31295: Replace imports from deprecated `distutils`
- Meta-ticket #33037: Remove use of `SAGE_LIB` and `SAGE_EXTCODE` variables


Depends on #32873

CC:  @antonio-rojas @dimpase

Component: **misc**

Author: **Frédéric Chapoton, Matthias Koeppe**


_Issue created by migration from https://trac.sagemath.org/ticket/33137_




Discussion:
<div id="comment:1" align="right">comment:1</div>

`get_config_var` as the other advantage to be usable from python 3.8 and 3.9. So we could use it now.

```
fbissey@tarazed ~ $ python3.9
Python 3.9.9 (main, Dec  1 2021, 10:05:37) 
[GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import distutils.sysconfig
>>> distutils.sysconfig.get_python_inc()
'/usr/include/python3.9'
>>> import sysconfig
>>> sysconfig.get_config_var('INCLUDEPY')
'/usr/include/python3.9'
>>> 
fbissey@tarazed ~ $ python3.8
Python 3.8.12 (default, Nov  4 2021, 22:08:42) 
[GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sysconfig
>>> sysconfig.get_config_var('INCLUDEPY')
'/usr/include/python3.8'
>>> 
```
Commit: **[`35d230a`](https://github.com/sagemath/sagetrac-mirror/commit/35d230ae7636c32d3fbe217d76cc7bbb47b3c24a)**
Branch: **[public/ticket-33137](https://github.com/sagemath/sagetrac-mirror/tree/public/ticket-33137)**
<div id="comment:2" align="right">comment:2</div>

une tentative. 

---
New commits:
<table><tr><td><a href="https://github.com/sagemath/sagetrac-mirror/commit/35d230ae7636c32d3fbe217d76cc7bbb47b3c24a"><code>35d230a</code></a></td><td><code>less distutils in env.py and features</code></td></tr></table>

<div id="comment:3" align="right">comment:3</div>

I'd have done the first part last night [exactly that code] but I couldn't push to trac. Turns out my ssh key is now too old and needs replacing :)
<div id="comment:4" align="right">comment:4</div>

Salut,

pour le reste, on peut aussi virer le bloc, plutot que de mettre RuntimeError
<div id="comment:5" align="right">comment:5</div>

Replying to [@fchapoton](#comment%3A4):
> Salut,
> 
> pour le reste, on peut aussi virer le bloc, plutot que de mettre RuntimeError

Toi, tu vas encore te faire taper sur les doigts parce que tu parle Français sur un système où l'Anglais est la "lingua Franca" (je sais, c'est bizarre d'écrire ça).

Otherwise, I am not too fussed about removing that block. I cannot see a test showing what it is supposed to catch and how. Antonio says it is a dead code path and there is clearly no proof otherwise anywhere.
Dependencies: **#32873**

Resolving patch:
diff --git a/src/sage/env.py b/src/sage/env.py
--- a/src/sage/env.py
+++ b/src/sage/env.py
@@ -384,17 +384,20 @@ def sage_include_directories(use_sources=False):
         sage: any(os.path.isfile(os.path.join(d, file)) for d in dirs)
         True
     """
-    import distutils.sysconfig
-
-    TOP = SAGE_SRC if use_sources else SAGE_LIB
-
-    dirs = [TOP,
-            distutils.sysconfig.get_python_inc()]
+    if use_sources:
+        dirs = [SAGE_SRC]
+    else:
+        import sage
+        dirs = [os.path.dirname(directory)
+                for directory in sage.__path__]
     try:
         import numpy
-        dirs.insert(1, numpy.get_include())
+        dirs.append(numpy.get_include())
     except ModuleNotFoundError:
         pass
+
+    dirs.append(sysconfig.get_config_var('INCLUDEPY'))
+
     return dirs
 
 def get_cblas_pc_module_name() -> str:


Test patch: