Skip to content

Commit 5126bb8

Browse files
committed
DOC: Address reviewer comments
1 parent 8ee4592 commit 5126bb8

File tree

5 files changed

+73
-72
lines changed

5 files changed

+73
-72
lines changed

doc/source/f2py/distutils.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Using via `numpy.distutils`
44

55
.. currentmodule:: numpy.distutils.core
66

7-
:mod:`numpy.distutils` is part of NumPy; and extends the standard Python
7+
:mod:`numpy.distutils` is part of NumPy, and extends the standard Python
88
``distutils`` module to deal with Fortran sources and F2PY signature files, e.g.
99
compile Fortran sources, call F2PY to construct extension modules, etc.
1010

@@ -34,7 +34,7 @@ Extensions to ``distutils``
3434

3535
* :class:`Extension` class argument ``sources`` may contain Fortran source
3636
files. In addition, the list ``sources`` may contain at most one
37-
F2PY signature file; in this case, the name of an Extension module must
37+
F2PY signature file, and in this case, the name of an Extension module must
3838
match with the ``<modulename>`` used in signature file. It is
3939
assumed that an F2PY signature file contains exactly one ``python
4040
module`` block.
@@ -52,7 +52,7 @@ Extensions to ``distutils``
5252
``config_fc``
5353
to change Fortran compiler options.
5454

55-
as well as ``build_ext`` and ``build_clib`` commands which are also enhanced
55+
Additionally, the ``build_ext`` and ``build_clib`` commands are also enhanced
5656
to support Fortran sources.
5757

5858
Run

doc/source/f2py/f2py.getting-started.rst

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ Depending on the situation, these steps can be carried out in a single composite
2828
command or step-by-step; in which case some steps can be omitted or combined
2929
with others.
3030

31-
Below we describe three typical approaches of using F2PY. These can be read in
31+
Below, we describe three typical approaches of using F2PY. These can be read in
3232
order of increasing effort, but also cater to different access levels depending
33-
on whether the Fortran code can be modified freely.
33+
on whether the Fortran code can be freely modified.
3434

3535
The following example Fortran 77 code will be used for
3636
illustration, save it as ``fib1.f``:
@@ -42,17 +42,17 @@ illustration, save it as ``fib1.f``:
4242
The quick way
4343
==============
4444

45-
The quickest way to expose the Fortran subroutine ``FIB`` to Python is
46-
to run
45+
The quickest way to wrap the Fortran subroutine ``FIB`` for use in Python is to
46+
run
4747

4848
::
4949

5050
python -m numpy.f2py -c fib1.f -m fib1
5151

52-
This command builds (see the ``-c`` flag, or execute ``python -m numpy.f2py``
53-
without arguments to see the explanation of command line options) an extension
54-
module ``fib1.so`` (see the ``-m`` flag) to the current directory. Now, in Python
55-
the Fortran subroutine ``FIB`` is accessible via ``fib1.fib``::
52+
This command compiles and wraps ``fib1.f`` (``-c``) to create the extension
53+
module ``fib1.so`` (``-m``) in the current directory. A list of command line
54+
options can be seen by executing ``python -m numpy.f2py``. Now, in Python the
55+
Fortran subroutine ``FIB`` is accessible via ``fib1.fib``::
5656

5757
>>> import numpy as np
5858
>>> import fib1
@@ -116,9 +116,8 @@ the Fortran subroutine ``FIB`` is accessible via ``fib1.fib``::
116116
>>> print(a)
117117
[1 1 1 1 1 1 1 1]
118118

119-
Clearly, this is not an expected behaviour; especially since Fortran
120-
typically passes by reference. The fact that the above example worked with
121-
``dtype=float`` is considered accidental.
119+
Clearly, this is unexpected, as Fortran typically passes by reference. That
120+
the above example worked with ``dtype=float`` is considered accidental.
122121

123122
F2PY provides an ``intent(inplace)`` attribute that modifies
124123
the attributes of an input array so that any changes made by
@@ -131,30 +130,30 @@ the Fortran subroutine ``FIB`` is accessible via ``fib1.fib``::
131130
>>> print(a)
132131
[ 0. 1. 1. 2. 3. 5. 8. 13.]
133132

134-
However, the recommended way to get changes made by Fortran subroutine
135-
propagate to Python is to use ``intent(out)`` attribute. This approach is
133+
However, the recommended way to have changes made by Fortran subroutine
134+
propagate to Python is to use the ``intent(out)`` attribute. That approach is
136135
more efficient and also cleaner.
137136

138137
* The usage of ``fib1.fib`` in Python is very similar to using ``FIB`` in
139-
Fortran. However, using *in situ* output arguments in Python indicates a
140-
poor style as there are no safety mechanisms in Python to protect against
141-
wrong argument types. When using Fortran or C, compilers naturally discover
142-
any type mismatches during the compilation process, but in Python the types
143-
must be checked at runtime. So, using *in situ* output arguments in Python
144-
may give rise to difficult to find bugs, not to mention the fact that the
138+
Fortran. However, using *in situ* output arguments in Python is poor style,
139+
as there are no safety mechanisms in Python to protect against wrong
140+
argument types. When using Fortran or C, compilers discover any type
141+
mismatches during the compilation process, but in Python the types must be
142+
checked at runtime. Consequently, using *in situ* output arguments in Python
143+
may lead to difficult to find bugs, not to mention the fact that the
145144
codes will be less readable when all required type checks are implemented.
146145

147-
Though the approach to exposing Fortran routines to Python discussed so far is
146+
Though the approach to wrapping Fortran routines for Python discussed so far is
148147
very straightforward, it has several drawbacks (see the comments above).
149-
These drawbacks are due to the fact that there is no way for F2PY to determine
148+
The drawbacks are due to the fact that there is no way for F2PY to determine
150149
the actual intention of the arguments; that is there is ambiguity in
151-
distinguishing between input, output arguments. So, F2PY conservatively
152-
assumes that all arguments are input arguments by default.
150+
distinguishing between input and output arguments. Consequently, F2PY assumes
151+
that all arguments are input arguments by default.
153152

154-
However, there are ways (see below) how to remove this ambiguity; essentially
155-
by "teaching" F2PY about the true intentions (among other things) of function
156-
arguments; subsequently F2PY is then able to generate more Pythonic (more
157-
explicit, easier to use, and less error prone) wrappers to Fortran functions.
153+
However, there are ways (see below) to remove this ambiguity by "teaching"
154+
F2PY about the true intentions of function arguments, and F2PY is then able to
155+
generate more explicit, easier to use, and less error prone wrappers for
156+
Fortran functions.
158157

159158
The smart way
160159
==============
@@ -175,13 +174,13 @@ one.
175174
:language: fortran
176175

177176
* Next, we'll teach F2PY that the argument ``n`` is an input argument (using the
178-
``intent(in)`` attribute) and that the result, i.e. the contents of ``a``
179-
after calling Fortran function ``FIB``, should be returned to Python (using
177+
``intent(in)`` attribute) and that the result, i.e., the contents of ``a``
178+
after calling the Fortran function ``FIB``, should be returned to Python (using
180179
the ``intent(out)`` attribute). In addition, an array ``a`` should be created
181180
dynamically using the size determined by the input argument ``n`` (using the
182181
``depend(n)`` attribute to indicate this dependence relation).
183182

184-
The content of a suitably modified version of ``fib1.pyf`` (saved as
183+
The contents of a suitably modified version of ``fib1.pyf`` (saved as
185184
``fib2.pyf``) is as follows:
186185

187186
.. literalinclude:: ./code/fib2.pyf
@@ -214,13 +213,12 @@ In Python::
214213

215214
.. note::
216215

217-
* Clearly, the signature of ``fib2.fib`` now more closely corresponds to the
216+
* The signature of ``fib2.fib`` now more closely corresponds to the
218217
intention of Fortran subroutine ``FIB``: given the number ``n``,
219218
``fib2.fib`` returns the first ``n`` Fibonacci numbers as a NumPy array.
220-
Also, the new Python signature ``fib2.fib`` rules out the surprises that we
221-
experienced with ``fib1.fib``.
219+
The new Python signature ``fib2.fib`` also rules out the unexpected behaviour in ``fib1.fib``.
222220

223-
* Note that by default using a single ``intent(out)`` also implies
221+
* Note that by default, using a single ``intent(out)`` also implies
224222
``intent(hide)``. Arguments that have the ``intent(hide)`` attribute
225223
specified will not be listed in the argument list of a wrapper function.
226224

@@ -233,11 +231,11 @@ modifications to their source codes are not desirable nor even
233231
possible.
234232

235233
However, if editing Fortran codes is acceptable, then the generation of an
236-
intermediate signature file can be skipped in most cases. Namely, F2PY specific
237-
attributes can be inserted directly to Fortran source codes using the so-called
238-
F2PY directive. A F2PY directive consists of special comment lines (starting
239-
with ``Cf2py`` or ``!f2py``, for example) which are ignored by Fortran compilers
240-
but interpreted by F2PY as normal lines.
234+
intermediate signature file can be skipped in most cases. F2PY specific
235+
attributes can be inserted directly into Fortran source codes using F2PY
236+
directives. A F2PY directive consists of special comment lines (starting with
237+
``Cf2py`` or ``!f2py``, for example) which are ignored by Fortran compilers but
238+
interpreted by F2PY as normal lines.
241239

242240
Consider a modified version of the previous Fortran code with F2PY directives,
243241
saved as ``fib3.f``:

doc/source/f2py/python-usage.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ is passed to Fortran routine as a scalar argument.
4040

4141
.. note::
4242

43-
* When type-casting is required and there is possible loss of
44-
information via narrowing (e.g. when type-casting float to integer or complex to
45-
float), F2PY *does not* raise an exception.
43+
* When type-casting is required and there is possible loss of information via
44+
narrowing e.g. when type-casting float to integer or complex to float, F2PY
45+
*does not* raise an exception.
4646

4747
* For complex to real type-casting only the real part of a complex number is used.
4848

@@ -66,7 +66,7 @@ In Python:
6666
String arguments
6767
=================
6868

69-
F2PY generated wrapper functions accept (almost) any Python object as
69+
F2PY generated wrapper functions accept almost any Python object as
7070
a string argument, since ``str`` is applied for non-string objects.
7171
Exceptions are NumPy arrays that must have type code ``'c'`` or
7272
``'1'`` when used as string arguments.
@@ -112,12 +112,12 @@ proper type, is used as the array argument.
112112

113113
There are two types of proper-contiguous NumPy arrays:
114114

115-
* Fortran-contiguous arrays refer to when data is stored column-wise,
115+
* Fortran-contiguous arrays refer to data that is stored columnwise,
116116
i.e. the indexing of data as stored in memory starts from the lowest
117117
dimension;
118-
* C-contiguous or simply contiguous arrays are when data is stored
119-
row-wise, i.e. the indexing of data as stored in memory starts from the
120-
highest dimension.
118+
* C-contiguous, or simply contiguous arrays, refer to data that is stored
119+
rowwise, i.e. the indexing of data as stored in memory starts from the highest
120+
dimension.
121121

122122
For one-dimensional arrays these notions coincide.
123123

@@ -220,7 +220,7 @@ and applies a function ``func`` to its elements.
220220

221221
The Fortran code expects that the function ``func`` has been defined externally.
222222
In order to use a Python function for ``func``, it must have an attribute
223-
``intent(callback)`` and it must be specified before the ``external`` statement.
223+
``intent(callback)`` and, it must be specified before the ``external`` statement.
224224

225225
Finally, build an extension module using ``f2py -c -m foo calculate.f``
226226

doc/source/f2py/signature-file.rst

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ is a subset of Fortran 90/95). F2PY introduces some extensions to the Fortran
99
90/95 language specification that help in the design of the Fortran to Python
1010
interface, making it more "Pythonic".
1111

12-
Signature files may contain arbitrary Fortran code (so that any Fortran 90/95
13-
codes can be treated as signature files). F2PY silently ignores
12+
Signature files may contain arbitrary Fortran code so that any Fortran 90/95
13+
codes can be treated as signature files. F2PY silently ignores
1414
Fortran constructs that are irrelevant for creating the interface.
1515
However, this also means that syntax errors are not caught by F2PY and will only
1616
be caught when the library is built.
@@ -32,9 +32,9 @@ generates.
3232

3333
.. warning::
3434

35-
Exception: if ``<modulename>`` contains a substring ``__user__``, then
36-
the corresponding ``python module`` block describes the signatures of
37-
so-called call-back functions (see :ref:`Call-back arguments`).
35+
Exception: if ``<modulename>`` contains a substring ``__user__``, then the
36+
corresponding ``python module`` block describes the signatures of call-back
37+
functions (see :ref:`Call-back arguments`).
3838

3939
A ``python module`` block has the following structure::
4040

@@ -59,7 +59,7 @@ A ``python module`` block has the following structure::
5959

6060
Here brackets ``[]`` indicate an optional section, dots ``...`` indicate one or
6161
more of a previous section. So, ``[]...`` is to be read as zero or more of a
62-
previous part.
62+
previous section.
6363

6464

6565
Fortran/C routine signatures
@@ -246,12 +246,12 @@ Other statements
246246

247247
.. note::
248248

249-
Tip: The ``entry`` statement can be used to describe the signature
250-
of an arbitrary routine allowing F2PY to generate a number of
249+
The ``entry`` statement can be used to describe the signature of an
250+
arbitrary subroutine or function allowing F2PY to generate a number of
251251
wrappers from only one routine block signature. There are few
252252
restrictions while doing this: ``fortranname`` cannot be used,
253-
``callstatement`` and ``callprotoargument`` can be used only if
254-
they are valid for all entry routines, etc.
253+
``callstatement`` and ``callprotoargument`` can be used only if they are
254+
valid for all entry routines, etc.
255255

256256
F2PY statements
257257
^^^^^^^^^^^^^^^^
@@ -510,9 +510,10 @@ The following attributes are used by F2PY:
510510

511511
.. note::
512512

513-
If ``check(..)`` is not used then F2PY generates a few standard checks (e.g.
514-
in a case of an array argument, it checks for the proper shape and size)
515-
automatically. Use ``check()`` to disable checks generated by F2PY.
513+
If ``check(..)`` is not used then F2PY automatically generates a few
514+
standard checks (e.g. in a case of an array argument, it checks for the
515+
proper shape and size). Use ``check()`` to disable checks
516+
generated by F2PY.
516517

517518
``depend([<names>])``
518519
This declares that the corresponding argument depends on the values
@@ -537,7 +538,7 @@ The following attributes are used by F2PY:
537538

538539
``external``
539540
The corresponding argument is a function provided by user. The
540-
signature of this so-called call-back function can be defined
541+
signature of this call-back function can be defined
541542

542543
- in ``__user__`` module block,
543544
- or by demonstrative (or real, if the signature file is a real Fortran
@@ -590,7 +591,7 @@ Extensions
590591
F2PY directives
591592
^^^^^^^^^^^^^^^^
592593

593-
The so-called F2PY directives allow using F2PY signature file constructs in
594+
The F2PY directives allow using F2PY signature file constructs in
594595
Fortran 77/90 source codes. With this feature one can (almost) completely skip
595596
the intermediate signature file generation and apply F2PY directly to Fortran
596597
source codes.

doc/source/f2py/usage.rst

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@ Signature file generation
3333
[ skip: <fortran functions> : ]]... \
3434
[<fortran files> ...]
3535
36-
Note that a Fortran source file can contain many routines, and it is often
37-
not necessary to allow all routines be usable from Python. So, either specify
38-
which routines should be wrapped (in the ``only: .. :`` part) or which routines
39-
F2PY should ignored (in the ``skip: .. :`` part).
36+
.. note::
37+
38+
A Fortran source file can contain many routines, and it is often
39+
not necessary to allow all routines be usable from Python. In such cases,
40+
either specify which routines should be wrapped (in the ``only: .. :`` part)
41+
or which routines F2PY should ignored (in the ``skip: .. :`` part).
4042

4143
If ``<filename.pyf>`` is specified as ``stdout`` then signatures
42-
are emitted to standard output instead of a file.
44+
are written to standard output instead of a file.
4345

4446
Among other options (see below), the following can be used
4547
in this mode:
@@ -68,7 +70,7 @@ Extension module construction
6870

6971
``--debug-capi``
7072
Adds debugging hooks to the extension module. When using this extension
71-
module, various diagnostic information about the wrapper is printed to
73+
module, various diagnostic information about the wrapper is written to
7274
the standard output, for example, the values of variables, the steps taken,
7375
etc.
7476

0 commit comments

Comments
 (0)