7
7
.. index ::
8
8
single: File Paths and Files
9
9
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
+
10
22
**********************
11
23
File Paths and Files
12
24
**********************
13
25
14
- This chapter describes reading and writing files from C extensions.
26
+ This chapter describes reading and writing files from CPython extensions.
15
27
16
28
.. index ::
17
29
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
+
18
41
19
42
====================================
20
43
File Paths
@@ -28,14 +51,18 @@ see also `File Objects <https://docs.python.org/3/c-api/file.html>`_
28
51
29
52
In summary:
30
53
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 ``
36
64
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 ``.
39
66
40
67
.. index ::
41
68
single: File Paths; Parsing Arguments
@@ -46,15 +73,13 @@ Parsing File Paths as Arguments
46
73
47
74
The Python API provides functionality for converting Python file paths (a ``str `` or *path-like * object)
48
75
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() `_
53
78
54
79
Here is an example of taking a Python Unicode string representing a file path, converting it to C and then back
55
80
to Python. The stages are:
56
81
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
58
83
a Python ``bytes `` object. Note the use of the ``"O&" `` formatting string that takes a Python object and a
59
84
conversion function.
60
85
- Extract the raws bytes to use as a C path.
@@ -80,7 +105,6 @@ Here is the C code:
80
105
81
106
/* Parse arguments */
82
107
static char *kwlist[] = {"path", NULL};
83
- /* Can be optional output path with "|O&". */
84
108
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&", kwlist, PyUnicode_FSConverter,
85
109
&py_path)) {
86
110
goto except;
0 commit comments