|
8 | 8 |
|
9 | 9 | NO WARRANTY IS EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
|
10 | 10 | """
|
11 |
| -__all__ = ['run_main', 'compile', 'get_include'] |
| 11 | +__all__ = ['run_main', 'get_include'] |
12 | 12 |
|
13 | 13 | import sys
|
14 | 14 | import subprocess
|
|
23 | 23 | main = f2py2e.main
|
24 | 24 |
|
25 | 25 |
|
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 |
| - |
139 | 26 | def get_include():
|
140 | 27 | """
|
141 | 28 | Return the directory that contains the ``fortranobject.c`` and ``.h`` files.
|
|
0 commit comments