/src/Python-3.8.3/Modules/_io/clinic/_iomodule.c.h
Line | Count | Source (jump to first uncovered line) |
1 | | /*[clinic input] |
2 | | preserve |
3 | | [clinic start generated code]*/ |
4 | | |
5 | | PyDoc_STRVAR(_io_open__doc__, |
6 | | "open($module, /, file, mode=\'r\', buffering=-1, encoding=None,\n" |
7 | | " errors=None, newline=None, closefd=True, opener=None)\n" |
8 | | "--\n" |
9 | | "\n" |
10 | | "Open file and return a stream. Raise OSError upon failure.\n" |
11 | | "\n" |
12 | | "file is either a text or byte string giving the name (and the path\n" |
13 | | "if the file isn\'t in the current working directory) of the file to\n" |
14 | | "be opened or an integer file descriptor of the file to be\n" |
15 | | "wrapped. (If a file descriptor is given, it is closed when the\n" |
16 | | "returned I/O object is closed, unless closefd is set to False.)\n" |
17 | | "\n" |
18 | | "mode is an optional string that specifies the mode in which the file\n" |
19 | | "is opened. It defaults to \'r\' which means open for reading in text\n" |
20 | | "mode. Other common values are \'w\' for writing (truncating the file if\n" |
21 | | "it already exists), \'x\' for creating and writing to a new file, and\n" |
22 | | "\'a\' for appending (which on some Unix systems, means that all writes\n" |
23 | | "append to the end of the file regardless of the current seek position).\n" |
24 | | "In text mode, if encoding is not specified the encoding used is platform\n" |
25 | | "dependent: locale.getpreferredencoding(False) is called to get the\n" |
26 | | "current locale encoding. (For reading and writing raw bytes use binary\n" |
27 | | "mode and leave encoding unspecified.) The available modes are:\n" |
28 | | "\n" |
29 | | "========= ===============================================================\n" |
30 | | "Character Meaning\n" |
31 | | "--------- ---------------------------------------------------------------\n" |
32 | | "\'r\' open for reading (default)\n" |
33 | | "\'w\' open for writing, truncating the file first\n" |
34 | | "\'x\' create a new file and open it for writing\n" |
35 | | "\'a\' open for writing, appending to the end of the file if it exists\n" |
36 | | "\'b\' binary mode\n" |
37 | | "\'t\' text mode (default)\n" |
38 | | "\'+\' open a disk file for updating (reading and writing)\n" |
39 | | "\'U\' universal newline mode (deprecated)\n" |
40 | | "========= ===============================================================\n" |
41 | | "\n" |
42 | | "The default mode is \'rt\' (open for reading text). For binary random\n" |
43 | | "access, the mode \'w+b\' opens and truncates the file to 0 bytes, while\n" |
44 | | "\'r+b\' opens the file without truncation. The \'x\' mode implies \'w\' and\n" |
45 | | "raises an `FileExistsError` if the file already exists.\n" |
46 | | "\n" |
47 | | "Python distinguishes between files opened in binary and text modes,\n" |
48 | | "even when the underlying operating system doesn\'t. Files opened in\n" |
49 | | "binary mode (appending \'b\' to the mode argument) return contents as\n" |
50 | | "bytes objects without any decoding. In text mode (the default, or when\n" |
51 | | "\'t\' is appended to the mode argument), the contents of the file are\n" |
52 | | "returned as strings, the bytes having been first decoded using a\n" |
53 | | "platform-dependent encoding or using the specified encoding if given.\n" |
54 | | "\n" |
55 | | "\'U\' mode is deprecated and will raise an exception in future versions\n" |
56 | | "of Python. It has no effect in Python 3. Use newline to control\n" |
57 | | "universal newlines mode.\n" |
58 | | "\n" |
59 | | "buffering is an optional integer used to set the buffering policy.\n" |
60 | | "Pass 0 to switch buffering off (only allowed in binary mode), 1 to select\n" |
61 | | "line buffering (only usable in text mode), and an integer > 1 to indicate\n" |
62 | | "the size of a fixed-size chunk buffer. When no buffering argument is\n" |
63 | | "given, the default buffering policy works as follows:\n" |
64 | | "\n" |
65 | | "* Binary files are buffered in fixed-size chunks; the size of the buffer\n" |
66 | | " is chosen using a heuristic trying to determine the underlying device\'s\n" |
67 | | " \"block size\" and falling back on `io.DEFAULT_BUFFER_SIZE`.\n" |
68 | | " On many systems, the buffer will typically be 4096 or 8192 bytes long.\n" |
69 | | "\n" |
70 | | "* \"Interactive\" text files (files for which isatty() returns True)\n" |
71 | | " use line buffering. Other text files use the policy described above\n" |
72 | | " for binary files.\n" |
73 | | "\n" |
74 | | "encoding is the name of the encoding used to decode or encode the\n" |
75 | | "file. This should only be used in text mode. The default encoding is\n" |
76 | | "platform dependent, but any encoding supported by Python can be\n" |
77 | | "passed. See the codecs module for the list of supported encodings.\n" |
78 | | "\n" |
79 | | "errors is an optional string that specifies how encoding errors are to\n" |
80 | | "be handled---this argument should not be used in binary mode. Pass\n" |
81 | | "\'strict\' to raise a ValueError exception if there is an encoding error\n" |
82 | | "(the default of None has the same effect), or pass \'ignore\' to ignore\n" |
83 | | "errors. (Note that ignoring encoding errors can lead to data loss.)\n" |
84 | | "See the documentation for codecs.register or run \'help(codecs.Codec)\'\n" |
85 | | "for a list of the permitted encoding error strings.\n" |
86 | | "\n" |
87 | | "newline controls how universal newlines works (it only applies to text\n" |
88 | | "mode). It can be None, \'\', \'\\n\', \'\\r\', and \'\\r\\n\'. It works as\n" |
89 | | "follows:\n" |
90 | | "\n" |
91 | | "* On input, if newline is None, universal newlines mode is\n" |
92 | | " enabled. Lines in the input can end in \'\\n\', \'\\r\', or \'\\r\\n\', and\n" |
93 | | " these are translated into \'\\n\' before being returned to the\n" |
94 | | " caller. If it is \'\', universal newline mode is enabled, but line\n" |
95 | | " endings are returned to the caller untranslated. If it has any of\n" |
96 | | " the other legal values, input lines are only terminated by the given\n" |
97 | | " string, and the line ending is returned to the caller untranslated.\n" |
98 | | "\n" |
99 | | "* On output, if newline is None, any \'\\n\' characters written are\n" |
100 | | " translated to the system default line separator, os.linesep. If\n" |
101 | | " newline is \'\' or \'\\n\', no translation takes place. If newline is any\n" |
102 | | " of the other legal values, any \'\\n\' characters written are translated\n" |
103 | | " to the given string.\n" |
104 | | "\n" |
105 | | "If closefd is False, the underlying file descriptor will be kept open\n" |
106 | | "when the file is closed. This does not work when a file name is given\n" |
107 | | "and must be True in that case.\n" |
108 | | "\n" |
109 | | "A custom opener can be used by passing a callable as *opener*. The\n" |
110 | | "underlying file descriptor for the file object is then obtained by\n" |
111 | | "calling *opener* with (*file*, *flags*). *opener* must return an open\n" |
112 | | "file descriptor (passing os.open as *opener* results in functionality\n" |
113 | | "similar to passing None).\n" |
114 | | "\n" |
115 | | "open() returns a file object whose type depends on the mode, and\n" |
116 | | "through which the standard file operations such as reading and writing\n" |
117 | | "are performed. When open() is used to open a file in a text mode (\'w\',\n" |
118 | | "\'r\', \'wt\', \'rt\', etc.), it returns a TextIOWrapper. When used to open\n" |
119 | | "a file in a binary mode, the returned class varies: in read binary\n" |
120 | | "mode, it returns a BufferedReader; in write binary and append binary\n" |
121 | | "modes, it returns a BufferedWriter, and in read/write mode, it returns\n" |
122 | | "a BufferedRandom.\n" |
123 | | "\n" |
124 | | "It is also possible to use a string or bytearray as a file for both\n" |
125 | | "reading and writing. For strings StringIO can be used like a file\n" |
126 | | "opened in a text mode, and for bytes a BytesIO can be used like a file\n" |
127 | | "opened in a binary mode."); |
128 | | |
129 | | #define _IO_OPEN_METHODDEF \ |
130 | | {"open", (PyCFunction)(void(*)(void))_io_open, METH_FASTCALL|METH_KEYWORDS, _io_open__doc__}, |
131 | | |
132 | | static PyObject * |
133 | | _io_open_impl(PyObject *module, PyObject *file, const char *mode, |
134 | | int buffering, const char *encoding, const char *errors, |
135 | | const char *newline, int closefd, PyObject *opener); |
136 | | |
137 | | static PyObject * |
138 | | _io_open(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
139 | 278 | { |
140 | 278 | PyObject *return_value = NULL; |
141 | 278 | static const char * const _keywords[] = {"file", "mode", "buffering", "encoding", "errors", "newline", "closefd", "opener", NULL}; |
142 | 278 | static _PyArg_Parser _parser = {NULL, _keywords, "open", 0}; |
143 | 278 | PyObject *argsbuf[8]; |
144 | 278 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1; |
145 | 278 | PyObject *file; |
146 | 278 | const char *mode = "r"; |
147 | 278 | int buffering = -1; |
148 | 278 | const char *encoding = NULL; |
149 | 278 | const char *errors = NULL; |
150 | 278 | const char *newline = NULL; |
151 | 278 | int closefd = 1; |
152 | 278 | PyObject *opener = Py_None; |
153 | | |
154 | 278 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 8, 0, argsbuf); |
155 | 278 | if (!args) { |
156 | 0 | goto exit; |
157 | 0 | } |
158 | 278 | file = args[0]; |
159 | 278 | if (!noptargs) { |
160 | 0 | goto skip_optional_pos; |
161 | 0 | } |
162 | 278 | if (args[1]) { |
163 | 278 | if (!PyUnicode_Check(args[1])) { |
164 | 0 | _PyArg_BadArgument("open", "argument 'mode'", "str", args[1]); |
165 | 0 | goto exit; |
166 | 0 | } |
167 | 278 | Py_ssize_t mode_length; |
168 | 278 | mode = PyUnicode_AsUTF8AndSize(args[1], &mode_length); |
169 | 278 | if (mode == NULL) { |
170 | 0 | goto exit; |
171 | 0 | } |
172 | 278 | if (strlen(mode) != (size_t)mode_length) { |
173 | 0 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
174 | 0 | goto exit; |
175 | 0 | } |
176 | 278 | if (!--noptargs) { |
177 | 236 | goto skip_optional_pos; |
178 | 236 | } |
179 | 278 | } |
180 | 42 | if (args[2]) { |
181 | 42 | if (PyFloat_Check(args[2])) { |
182 | 0 | PyErr_SetString(PyExc_TypeError, |
183 | 0 | "integer argument expected, got float" ); |
184 | 0 | goto exit; |
185 | 0 | } |
186 | 42 | buffering = _PyLong_AsInt(args[2]); |
187 | 42 | if (buffering == -1 && PyErr_Occurred()) { |
188 | 0 | goto exit; |
189 | 0 | } |
190 | 42 | if (!--noptargs) { |
191 | 0 | goto skip_optional_pos; |
192 | 0 | } |
193 | 42 | } |
194 | 42 | if (args[3]) { |
195 | 42 | if (args[3] == Py_None) { |
196 | 42 | encoding = NULL; |
197 | 42 | } |
198 | 0 | else if (PyUnicode_Check(args[3])) { |
199 | 0 | Py_ssize_t encoding_length; |
200 | 0 | encoding = PyUnicode_AsUTF8AndSize(args[3], &encoding_length); |
201 | 0 | if (encoding == NULL) { |
202 | 0 | goto exit; |
203 | 0 | } |
204 | 0 | if (strlen(encoding) != (size_t)encoding_length) { |
205 | 0 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
206 | 0 | goto exit; |
207 | 0 | } |
208 | 0 | } |
209 | 0 | else { |
210 | 0 | _PyArg_BadArgument("open", "argument 'encoding'", "str or None", args[3]); |
211 | 0 | goto exit; |
212 | 0 | } |
213 | 42 | if (!--noptargs) { |
214 | 0 | goto skip_optional_pos; |
215 | 0 | } |
216 | 42 | } |
217 | 42 | if (args[4]) { |
218 | 42 | if (args[4] == Py_None) { |
219 | 42 | errors = NULL; |
220 | 42 | } |
221 | 0 | else if (PyUnicode_Check(args[4])) { |
222 | 0 | Py_ssize_t errors_length; |
223 | 0 | errors = PyUnicode_AsUTF8AndSize(args[4], &errors_length); |
224 | 0 | if (errors == NULL) { |
225 | 0 | goto exit; |
226 | 0 | } |
227 | 0 | if (strlen(errors) != (size_t)errors_length) { |
228 | 0 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
229 | 0 | goto exit; |
230 | 0 | } |
231 | 0 | } |
232 | 0 | else { |
233 | 0 | _PyArg_BadArgument("open", "argument 'errors'", "str or None", args[4]); |
234 | 0 | goto exit; |
235 | 0 | } |
236 | 42 | if (!--noptargs) { |
237 | 0 | goto skip_optional_pos; |
238 | 0 | } |
239 | 42 | } |
240 | 42 | if (args[5]) { |
241 | 42 | if (args[5] == Py_None) { |
242 | 42 | newline = NULL; |
243 | 42 | } |
244 | 0 | else if (PyUnicode_Check(args[5])) { |
245 | 0 | Py_ssize_t newline_length; |
246 | 0 | newline = PyUnicode_AsUTF8AndSize(args[5], &newline_length); |
247 | 0 | if (newline == NULL) { |
248 | 0 | goto exit; |
249 | 0 | } |
250 | 0 | if (strlen(newline) != (size_t)newline_length) { |
251 | 0 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
252 | 0 | goto exit; |
253 | 0 | } |
254 | 0 | } |
255 | 0 | else { |
256 | 0 | _PyArg_BadArgument("open", "argument 'newline'", "str or None", args[5]); |
257 | 0 | goto exit; |
258 | 0 | } |
259 | 42 | if (!--noptargs) { |
260 | 0 | goto skip_optional_pos; |
261 | 0 | } |
262 | 42 | } |
263 | 42 | if (args[6]) { |
264 | 42 | if (PyFloat_Check(args[6])) { |
265 | 0 | PyErr_SetString(PyExc_TypeError, |
266 | 0 | "integer argument expected, got float" ); |
267 | 0 | goto exit; |
268 | 0 | } |
269 | 42 | closefd = _PyLong_AsInt(args[6]); |
270 | 42 | if (closefd == -1 && PyErr_Occurred()) { |
271 | 0 | goto exit; |
272 | 0 | } |
273 | 42 | if (!--noptargs) { |
274 | 42 | goto skip_optional_pos; |
275 | 42 | } |
276 | 42 | } |
277 | 0 | opener = args[7]; |
278 | 278 | skip_optional_pos: |
279 | 278 | return_value = _io_open_impl(module, file, mode, buffering, encoding, errors, newline, closefd, opener); |
280 | | |
281 | 278 | exit: |
282 | 278 | return return_value; |
283 | 278 | } |
284 | | |
285 | | PyDoc_STRVAR(_io_open_code__doc__, |
286 | | "open_code($module, /, path)\n" |
287 | | "--\n" |
288 | | "\n" |
289 | | "Opens the provided file with the intent to import the contents.\n" |
290 | | "\n" |
291 | | "This may perform extra validation beyond open(), but is otherwise interchangeable\n" |
292 | | "with calling open(path, \'rb\')."); |
293 | | |
294 | | #define _IO_OPEN_CODE_METHODDEF \ |
295 | | {"open_code", (PyCFunction)(void(*)(void))_io_open_code, METH_FASTCALL|METH_KEYWORDS, _io_open_code__doc__}, |
296 | | |
297 | | static PyObject * |
298 | | _io_open_code_impl(PyObject *module, PyObject *path); |
299 | | |
300 | | static PyObject * |
301 | | _io_open_code(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
302 | 235 | { |
303 | 235 | PyObject *return_value = NULL; |
304 | 235 | static const char * const _keywords[] = {"path", NULL}; |
305 | 235 | static _PyArg_Parser _parser = {NULL, _keywords, "open_code", 0}; |
306 | 235 | PyObject *argsbuf[1]; |
307 | 235 | PyObject *path; |
308 | | |
309 | 235 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf); |
310 | 235 | if (!args) { |
311 | 0 | goto exit; |
312 | 0 | } |
313 | 235 | if (!PyUnicode_Check(args[0])) { |
314 | 0 | _PyArg_BadArgument("open_code", "argument 'path'", "str", args[0]); |
315 | 0 | goto exit; |
316 | 0 | } |
317 | 235 | if (PyUnicode_READY(args[0]) == -1) { |
318 | 0 | goto exit; |
319 | 0 | } |
320 | 235 | path = args[0]; |
321 | 235 | return_value = _io_open_code_impl(module, path); |
322 | | |
323 | 235 | exit: |
324 | 235 | return return_value; |
325 | 235 | } |
326 | | /*[clinic end generated code: output=3df6bc6d91697545 input=a9049054013a1b77]*/ |