Skip to content

Commit 4b1298d

Browse files
committed
Add missing edits up to page 98.
1 parent a22de63 commit 4b1298d

File tree

2 files changed

+49
-23
lines changed

2 files changed

+49
-23
lines changed

doc/sphinx/source/files.rst

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,37 @@
77
.. index::
88
single: File Paths and Files
99

10+
..
11+
Links, mostly to the Python documentation.
12+
Specific container links are just before the appropriate section.
13+
14+
.. _PyUnicode_FSConverter(): https://docs.python.org/3/c-api/unicode.html#c.PyUnicode_FSConverter
15+
.. _PyUnicode_FSDecoder(): https://docs.python.org/3/c-api/unicode.html#c.PyUnicode_FSDecoder
16+
.. _PyUnicode_DecodeFSDefaultAndSize(): https://docs.python.org/3/c-api/unicode.html#c.PyUnicode_DecodeFSDefaultAndSize
17+
.. _PyUnicode_DecodeFSDefault(): https://docs.python.org/3/c-api/unicode.html#c.PyUnicode_DecodeFSDefault
18+
.. _PyUnicode_EncodeFSDefault(): https://docs.python.org/3/c-api/unicode.html#c.PyUnicode_EncodeFSDefault
19+
20+
.. _PyArg_ParseTupleAndKeywords(): https://docs.python.org/3/c-api/arg.html#c.PyArg_ParseTupleAndKeywords
21+
1022
**********************
1123
File Paths and Files
1224
**********************
1325

14-
This chapter describes reading and writing files from C extensions.
26+
This chapter describes reading and writing files from CPython extensions.
1527

1628
.. index::
1729
single: File Paths
30+
single: File Path Codecs; PyUnicode_FSConverter()
31+
single: File Path Codecs; PyUnicode_FSDecoder()
32+
single: File Path Codecs; PyUnicode_DecodeFSDefaultAndSize()
33+
single: File Path Codecs; PyUnicode_DecodeFSDefault()
34+
single: File Path Codecs; PyUnicode_EncodeFSDefault()
35+
single: PyUnicode_FSConverter()
36+
single: PyUnicode_FSDecoder()
37+
single: PyUnicode_DecodeFSDefaultAndSize()
38+
single: PyUnicode_DecodeFSDefault()
39+
single: PyUnicode_EncodeFSDefault()
40+
1841

1942
====================================
2043
File Paths
@@ -28,14 +51,18 @@ see also `File Objects <https://docs.python.org/3/c-api/file.html>`_
2851

2952
In summary:
3053

31-
- ``PyUnicode_FSConverter`` Converts a Python a ``str`` or *path-like* object to a Python ``bytes`` object.
32-
- ``PyUnicode_FSDecoder`` Converts a Python ``bytes`` object to a Python ``str``.
33-
- ``PyUnicode_DecodeFSDefaultAndSize`` Takes a C string and length and returns a Python ``str``.
34-
- ``PyUnicode_DecodeFSDefault`` Takes a null terminated C string and length and returns a Python ``str``.
35-
- ``PyUnicode_EncodeFSDefault`` Takes a Python ``str`` and return a Python ``bytes`` object.
54+
- `PyUnicode_FSConverter()`_ Converts a Python a ``str`` or *path-like* object to a Python ``bytes`` object.
55+
- `PyUnicode_FSDecoder()`_ Converts a Python ``bytes`` object to a Python ``str``.
56+
- `PyUnicode_DecodeFSDefaultAndSize()`_ Takes a C string and length and returns a Python ``str``.
57+
- `PyUnicode_DecodeFSDefault()`_ Takes a null terminated C string and length and returns a Python ``str``.
58+
- `PyUnicode_EncodeFSDefault()`_ Takes a Python ``str`` and return a Python ``bytes`` object.
59+
60+
The example code is in:
61+
- ``src/cpy/cFile.cpp``
62+
- ``src/cpy/PythonFileWrapper.h``
63+
- ``src/cpy/PythonFileWrapper.cpp``
3664

37-
The example code is in ``src/cpy/cFile.cpp``, ``src/cpy/PythonFileWrapper.h`` and
38-
``src/cpy/PythonFileWrapper.cpp`` and the tests are in ``tests/unit/test_c_file.py``.
65+
The Python tests are in ``tests/unit/test_c_file.py``.
3966

4067
.. index::
4168
single: File Paths; Parsing Arguments
@@ -46,15 +73,13 @@ Parsing File Paths as Arguments
4673

4774
The Python API provides functionality for converting Python file paths (a ``str`` or *path-like* object)
4875
to C file paths (``char *``).
49-
From Python to C;
50-
`PyUnicode_FSConverter <https://docs.python.org/3/c-api/unicode.html#c.PyUnicode_FSConverter>`_
51-
and the reverse from C to Python
52-
`PyUnicode_DecodeFSDefaultAndSize <https://docs.python.org/3/c-api/unicode.html#c.PyUnicode_DecodeFSDefaultAndSize>`_
76+
From Python to C; `PyUnicode_FSConverter()`_
77+
and the reverse from C to Python `PyUnicode_DecodeFSDefaultAndSize()`_
5378

5479
Here is an example of taking a Python Unicode string representing a file path, converting it to C and then back
5580
to Python. The stages are:
5681

57-
- Use ``PyArg_ParseTupleAndKeywords`` and ``PyUnicode_FSConverter`` to convert the path-like Python object to
82+
- Use `PyArg_ParseTupleAndKeywords()`_ and `PyUnicode_FSConverter()`_ to convert the path-like Python object to
5883
a Python ``bytes`` object. Note the use of the ``"O&"`` formatting string that takes a Python object and a
5984
conversion function.
6085
- Extract the raws bytes to use as a C path.
@@ -80,7 +105,6 @@ Here is the C code:
80105
81106
/* Parse arguments */
82107
static char *kwlist[] = {"path", NULL};
83-
/* Can be optional output path with "|O&". */
84108
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&", kwlist, PyUnicode_FSConverter,
85109
&py_path)) {
86110
goto except;

doc/sphinx/source/parsing_arguments.rst

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
.. toctree::
55
:maxdepth: 3
66

7+
.. Links to the Python documentation
8+
9+
.. _PyArg_ParseTuple(): https://docs.python.org/3/c-api/arg.html#c.PyArg_ParseTuple
10+
.. _PyArg_ParseTupleAndKeywords(): https://docs.python.org/3/c-api/arg.html#c.PyArg_ParseTupleAndKeywords
11+
712
.. index::
813
single: Parsing Arguments
914

@@ -133,8 +138,7 @@ Parsing the Arguments
133138
------------------------------
134139

135140
Once whe have the C function correctly declared then the arguments have to parsed according to their types.
136-
This is done using the `PyArg_ParseTuple <https://docs.python.org/3/c-api/arg.html#c.PyArg_ParseTuple>`_
137-
and `PyArg_ParseTupleAndKeywords <https://docs.python.org/3/c-api/arg.html#c.PyArg_ParseTupleAndKeywords>`_
141+
This is done using the `PyArg_ParseTuple()`_ and `PyArg_ParseTupleAndKeywords()`_
138142
(ignoring “old-style” functions which use `PyArg_Parse <https://docs.python.org/3/c-api/arg.html#c.PyArg_Parse>`_).
139143

140144
These use formatting strings that can become bewilderingly complex so this tutorial uses examples as an introduction.
@@ -237,8 +241,6 @@ There is no parsing required here, a single ``PyObject`` is expected:
237241
Py_RETURN_NONE;
238242
}
239243
240-
241-
242244
This function can be added to the module with the ``METH_O`` flag:
243245

244246
.. code-block:: c
@@ -449,7 +451,7 @@ Keyword Arguments and C++11
449451

450452
C++11 compilers warn when creating non-const ``char*`` from string literals as we have done with the keyword array
451453
above.
452-
The solution is to declare these ``const char*`` however ``PyArg_ParseTupleAndKeywords`` expects a ``char **``.
454+
The solution is to declare these ``const char*`` however `PyArg_ParseTupleAndKeywords()`_ expects a ``char **``.
453455
The solution is to cast away const in the call:
454456

455457
.. code-block:: c
@@ -560,11 +562,11 @@ Suppose we want the functional equivalent of the Python function signature
560562
561563
This is achieved by combining two techniques:
562564

563-
- Positional only: The strings in the ``*kwlist`` passed to ``PyArg_ParseTupleAndKeywords`` are empty.
564-
- Keyword only: The formatting string passed to ``PyArg_ParseTupleAndKeywords`` uses the ``'$'`` character.
565+
- Positional only: The strings in the ``*kwlist`` passed to `PyArg_ParseTupleAndKeywords()`_ are empty.
566+
- Keyword only: The formatting string passed to `PyArg_ParseTupleAndKeywords()`_ uses the ``'$'`` character.
565567

566568
A function using either positional only or keyword only arguments must use the flags ``METH_VARARGS | METH_KEYWORDS``
567-
and uses ``PyArg_ParseTupleAndKeywords``. Currently, all keyword-only arguments must also be optional arguments, so ``'|'`` must always be
569+
and uses `PyArg_ParseTupleAndKeywords()`_. Currently, all keyword-only arguments must also be optional arguments, so ``'|'`` must always be
568570
specified before ``'$'`` in the format string.
569571

570572
Here is the C code:
@@ -610,7 +612,7 @@ Parsing Arguments With Functional Conversion to C
610612

611613
Often you want to convert a Python argument to a C value(s) in a way that is not covered by the format strings
612614
provided by the Python C API. To do this you can provide a special conversion function in C and give it to
613-
``PyArg_ParseTuple`` or ``PyArg_ParseTupleAndKeywords``.
615+
`PyArg_ParseTuple()`_ or `PyArg_ParseTupleAndKeywords()`_.
614616

615617
In this example we are expecting the Python argument to be a list of integers and we want the sum of them as
616618
a C ``long``. First create a C function that takes a Python list, checks it and sums the values.

0 commit comments

Comments
 (0)