8000 Merge pull request #25193 from HaoZeke/killCompileFunc · numpy/numpy@5b4dab8 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 5b4dab8

Browse files
authored
Merge pull request #25193 from HaoZeke/killCompileFunc
MAINT: Kill all instances of f2py.compile
2 parents d9c0382 + 764c64e commit 5b4dab8

File tree

5 files changed

+17
-270
lines changed

5 files changed

+17
-270
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
The ``f2py.compile()`` helper has been removed because it leaked memory, has
2+
been marked as experimental for several years now, and was implemented as a thin
3+
``subprocess.run`` wrapper. It is also one of the test bottlenecks. See
4+
`gh-25122 <https://github.com/numpy/numpy/issues/25122>`_ for the full
5+
rationale. It also used several ``np.distutils`` features which are too fragile
6+
to be ported to work with ``meson``.
7+
8+
Users are urged to replace calls to ``f2py.compile`` with calls to
9+
``subprocess.run("python", "-m", "numpy.f2py",...`` instead, and to use
10+
environment variables to interact with ``meson``. `Native files
11+
<https://mesonbuild.com/Machine-files.html>`_ are also an option.

doc/source/f2py/code/results/compile_session.dat

Lines changed: 0 additions & 11 deletions
This file was deleted.

doc/source/f2py/usage.rst

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -273,33 +273,16 @@ options.
273273
Python module ``numpy.f2py``
274274
============================
275275

276-
The f2py program is written in Python and can be run from inside your code
277-
to compile Fortran code at runtime, as follows:
278-
279-
.. legacy::
280-
281-
The ``f2py.compile()`` function will be removed from 2.0.x onwards.
282-
Write a custom ``subprocess.run`` wrapper over ``numpy.f2py`` instead.
283-
284-
.. code-block:: python
276+
.. warning::
285277

286-
from numpy import f2py
287-
with open("add.f") as sourcefile:
288-
sourcecode = sourcefile.read()
289-
f2py.compile(sourcecode, modulename='add')
290-
import add
278+
.. versionchanged:: 2.0.0
291279

292-
The source string can be any valid Fortran code. If you want to save
293-
the extension-module source code then a suitable file-name can be
294-
provided by the ``source_fn`` keyword to the compile function.
280+
There used to be a ``f2py.compile`` function, which was removed, users
281+
may wrap ``python -m numpy.f2py`` via ``subprocess.run`` manually, and
282+
set environment variables to interact with ``meson`` as required.
295283

296284
When using ``numpy.f2py`` as a module, the following functions can be invoked.
297285

298-
.. warning::
299-
300-
The current Python interface to the ``f2py`` module is not mature and may
301-
change in the future.
302-
303286
.. automodule:: numpy.f2py
304287
:members:
305288

numpy/f2py/__init__.py

Lines changed: 1 addition & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
99
NO WARRANTY IS EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
1010
"""
11-
__all__ = ['run_main', 'compile', 'get_include']
11+
__all__ = ['run_main', 'get_include']
1212

1313
import sys
1414
import subprocess
@@ -23,119 +23,6 @@
2323
main = f2py2e.main
2424

2525

26-
def compile(source,
27-
modulename='untitled',
28-
extra_args='',
29-
verbose=True,
30-
source_fn=None,
31-
extension='.f',
32-
full_output=False
33-
):
34-
"""
35-
Build extension module from a Fortran 77 source string with f2py.
36-
37-
Parameters
38-
---------- 6D40
39-
source : str or bytes
40-
Fortran source of module / subroutine to compile
41-
42-
.. versionchanged:: 1.16.0
43-
Accept str as well as bytes
44-
45-
modulename : str, optional
46-
The name of the compiled python module
47-
extra_args : str or list, optional
48-
Additional parameters passed to f2py
49-
50-
.. versionchanged:: 1.16.0
51-
A list of args may also be provided.
52-
53-
verbose : bool, optional
54-
Print f2py output to screen
55-
source_fn : str, optional
56-
Name of the file where the fortran source is written.
57-
The default is to use a temporary file with the extension
58-
provided by the ``extension`` parameter
59-
extension : ``{'.f', '.f90'}``, optional
60-
Filename extension if `source_fn` is not provided.
61-
The extension tells which fortran standard is used.
62-
The default is ``.f``, which implies F77 standard.
63-
64-
.. versionadded:: 1.11.0
65-
66-
full_output : bool, optional
67-
If True, return a `subprocess.CompletedProcess` containing
68-
the stdout and stderr of the compile process, instead of just
69-
the status code.
70-
71-
.. versionadded:: 1.20.0
72-
73-
74-
Returns
75-
-------
76-
result : int or `subprocess.CompletedProcess`
77-
0 on success, or a `subprocess.CompletedProcess` if
78-
``full_output=True``
79-
80-
Examples
81-
--------
82-
.. literalinclude:: ../../source/f2py/code/results/compile_session.dat
83-
:language: python
84-
85-
"""
86-
import tempfile
87-
import shlex
88-
89-
warnings.warn(
90-
"f2py.compile() will be removed in NumPy 2.0.x.\n"
91-
"Write a custom subprocess.run wrapper to f2py instead",
92-
VisibleDeprecationWarning,
93-
stacklevel=2,
94-
)
95-
96-
if source_fn is None:
97-
f, fname = tempfile.mkstemp(suffix=extension)
98-
# f is a file descriptor so need to close it
99-
# carefully -- not with .close() directly
100-
os.close(f)
101-
else:
102-
fname = source_fn
103-
104-
if not isinstance(source, str):
105-
source = str(source, 'utf-8')
106-
try:
107-
with open(fname, 'w') as f:
108-
f.write(source)
109-
110-
args = ['-c', '-m', modulename, f.name]
111-
112-
if isinstance(extra_args, str):
113-
is_posix = (os.name == 'posix')
114-
extra_args = shlex.split(extra_args, posix=is_posix)
115-
116-
args.extend(extra_args)
117-
118-
c = [sys.executable,
119-
'-c',
120-
'import numpy.f2py as f2py2e;f2py2e.main()'] + args
121-
try:
122-
cp = subprocess.run(c, capture_output=True)
123-
except OSError:
124-
# preserve historic status code used by exec_command()
125-
cp = subprocess.CompletedProcess(c, 127, stdout=b'', stderr=b'')
126-
else:
127-
if verbose:
128-
print(cp.stdout.decode())
129-
finally:
130-
if source_fn is None:
131-
os.remove(fname)
132-
133-
if full_output:
134-
return cp
135-
else:
136-
return cp.returncode
137-
138-
13926
def get_include():
14027
"""
14128
Return the directory that contains the ``fortranobject.c`` and ``.h`` files.

numpy/f2py/tests/test_compile_function.py

Lines changed: 0 additions & 123 deletions
This file was deleted.

0 commit comments

Comments
 (0)
0