diff options
| -rw-r--r-- | doc/source/f2py/run_main_session.dat | 2 | ||||
| -rw-r--r-- | doc/source/f2py/usage.rst | 29 | ||||
| -rw-r--r-- | numpy/f2py/__init__.py | 22 | ||||
| -rwxr-xr-x | numpy/f2py/f2py2e.py | 21 | ||||
| -rw-r--r-- | numpy/f2py/tests/test_regression.py | 14 |
5 files changed, 55 insertions, 33 deletions
diff --git a/doc/source/f2py/run_main_session.dat b/doc/source/f2py/run_main_session.dat index b9a7e1b0d..be6cacd22 100644 --- a/doc/source/f2py/run_main_session.dat +++ b/doc/source/f2py/run_main_session.dat @@ -8,7 +8,7 @@ Post-processing... Building modules... Building module "scalar"... Wrote C/API module "scalar" to file "./scalarmodule.c" ->>> printr(r) +>>> print(r) {'scalar': {'h': ['/home/users/pearu/src_cvs/f2py/src/fortranobject.h'], 'csrc': ['./scalarmodule.c', '/home/users/pearu/src_cvs/f2py/src/fortranobject.c']}} diff --git a/doc/source/f2py/usage.rst b/doc/source/f2py/usage.rst index 0f5068e0e..5043ec430 100644 --- a/doc/source/f2py/usage.rst +++ b/doc/source/f2py/usage.rst @@ -214,32 +214,7 @@ Python module ``numpy.f2py`` The current Python interface to the ``f2py`` module is not mature and may change in the future. -The following functions are provided by the ``numpy.f2py`` module: -``run_main(<list>)`` - Equivalent to running:: +.. automodule:: numpy.f2py + :members: - f2py <args> - - where ``<args>=string.join(<list>,' ')``, but in Python. Unless - ``-h`` is used, this function returns a dictionary containing - information on generated modules and their dependencies on source - files. For example, the command ``f2py -m scalar scalar.f`` can be - executed from Python as follows - - .. include:: run_main_session.dat - :literal: - - You cannot build extension modules with this function, that is, - using ``-c`` is not allowed. Use ``compile`` command instead, see - below. - -``compile(source, modulename='untitled', extra_args='', verbose=1, source_fn=None)`` - Build extension module from Fortran 77 source string ``source``. - Return 0 if successful. - Note that this function actually calls ``f2py -c ..`` from shell to - ensure safety of the current Python process. - For example, - - .. include:: compile_session.dat - :literal: diff --git a/numpy/f2py/__init__.py b/numpy/f2py/__init__.py index 23a4b7c41..d146739bb 100644 --- a/numpy/f2py/__init__.py +++ b/numpy/f2py/__init__.py @@ -28,12 +28,16 @@ def compile(source, extension='.f' ): """ - Build extension module from processing source with f2py. + Build extension module from a Fortran 77 source string with f2py. Parameters ---------- - source : str + source : str or bytes Fortran source of module / subroutine to compile + + .. versionchanged:: 1.16.0 + Accept str as well as bytes + modulename : str, optional The name of the compiled python module extra_args : str or list, optional @@ -55,6 +59,16 @@ def compile(source, .. versionadded:: 1.11.0 + Returns + ------- + result : int + 0 on success + + Examples + -------- + .. include:: compile_session.dat + :literal: + """ import tempfile import shlex @@ -67,9 +81,11 @@ def compile(source, else: fname = source_fn + if not isinstance(source, str): + source = str(source, 'utf-8') try: with open(fname, 'w') as f: - f.write(str(source)) + f.write(source) args = ['-c', '-m', modulename, f.name] diff --git a/numpy/f2py/f2py2e.py b/numpy/f2py/f2py2e.py index 8750ed0b3..47223151f 100755 --- a/numpy/f2py/f2py2e.py +++ b/numpy/f2py/f2py2e.py @@ -396,8 +396,25 @@ def dict_append(d_out, d_in): def run_main(comline_list): - """Run f2py as if string.join(comline_list,' ') is used as a command line. - In case of using -h flag, return None. + """ + Equivalent to running:: + + f2py <args> + + where ``<args>=string.join(<list>,' ')``, but in Python. Unless + ``-h`` is used, this function returns a dictionary containing + information on generated modules and their dependencies on source + files. For example, the command ``f2py -m scalar scalar.f`` can be + executed from Python as follows + + You cannot build extension modules with this function, that is, + using ``-c`` is not allowed. Use ``compile`` command instead + + Examples + -------- + .. include:: run_main_session.dat + :literal: + """ crackfortran.reset_global_f2py_vars() f2pydir = os.path.dirname(os.path.abspath(cfuncs.__file__)) diff --git a/numpy/f2py/tests/test_regression.py b/numpy/f2py/tests/test_regression.py index 3adae635d..7b622d5b1 100644 --- a/numpy/f2py/tests/test_regression.py +++ b/numpy/f2py/tests/test_regression.py @@ -27,3 +27,17 @@ class TestIntentInOut(util.F2PyTest): x = np.arange(3, dtype=np.float32) self.module.foo(x) assert_equal(x, [3, 1, 2]) + +@pytest.mark.parametrize('code', [ + 'program test_f2py\nend program test_f2py', + b'program test_f2py\nend program test_f2py', + ]) +def test_compile(tmpdir, code): + # Make sure we can compile str and bytes gh-12796 + cwd = os.getcwd() + try: + os.chdir(str(tmpdir)) + ret = np.f2py.compile(code, modulename='test1_f2py', extension='.f90') + assert_equal(ret, 0) + finally: + os.chdir(cwd) |
