Branch data Line data Source code
1 : : /*[clinic input]
2 : : preserve
3 : : [clinic start generated code]*/
4 : :
5 : : PyDoc_STRVAR(_io_FileIO_close__doc__,
6 : : "close($self, /)\n"
7 : : "--\n"
8 : : "\n"
9 : : "Close the file.\n"
10 : : "\n"
11 : : "A closed file cannot be used for further I/O operations. close() may be\n"
12 : : "called more than once without error.");
13 : :
14 : : #define _IO_FILEIO_CLOSE_METHODDEF \
15 : : {"close", (PyCFunction)_io_FileIO_close, METH_NOARGS, _io_FileIO_close__doc__},
16 : :
17 : : static PyObject *
18 : : _io_FileIO_close_impl(fileio *self);
19 : :
20 : : static PyObject *
21 : 305855 : _io_FileIO_close(fileio *self, PyObject *Py_UNUSED(ignored))
22 : : {
23 : 305855 : return _io_FileIO_close_impl(self);
24 : : }
25 : :
26 : : PyDoc_STRVAR(_io_FileIO___init____doc__,
27 : : "FileIO(file, mode=\'r\', closefd=True, opener=None)\n"
28 : : "--\n"
29 : : "\n"
30 : : "Open a file.\n"
31 : : "\n"
32 : : "The mode can be \'r\' (default), \'w\', \'x\' or \'a\' for reading,\n"
33 : : "writing, exclusive creation or appending. The file will be created if it\n"
34 : : "doesn\'t exist when opened for writing or appending; it will be truncated\n"
35 : : "when opened for writing. A FileExistsError will be raised if it already\n"
36 : : "exists when opened for creating. Opening a file for creating implies\n"
37 : : "writing so this mode behaves in a similar way to \'w\'.Add a \'+\' to the mode\n"
38 : : "to allow simultaneous reading and writing. A custom opener can be used by\n"
39 : : "passing a callable as *opener*. The underlying file descriptor for the file\n"
40 : : "object is then obtained by calling opener with (*name*, *flags*).\n"
41 : : "*opener* must return an open file descriptor (passing os.open as *opener*\n"
42 : : "results in functionality similar to passing None).");
43 : :
44 : : static int
45 : : _io_FileIO___init___impl(fileio *self, PyObject *nameobj, const char *mode,
46 : : int closefd, PyObject *opener);
47 : :
48 : : static int
49 : 314651 : _io_FileIO___init__(PyObject *self, PyObject *args, PyObject *kwargs)
50 : : {
51 : 314651 : int return_value = -1;
52 : : static const char * const _keywords[] = {"file", "mode", "closefd", "opener", NULL};
53 : : static _PyArg_Parser _parser = {NULL, _keywords, "FileIO", 0};
54 : : PyObject *argsbuf[4];
55 : : PyObject * const *fastargs;
56 : 314651 : Py_ssize_t nargs = PyTuple_GET_SIZE(args);
57 [ + + ]: 314651 : Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1;
58 : : PyObject *nameobj;
59 : 314651 : const char *mode = "r";
60 : 314651 : int closefd = 1;
61 : 314651 : PyObject *opener = Py_None;
62 : :
63 [ + + + - : 314651 : fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 4, 0, argsbuf);
+ - + - ]
64 [ - + ]: 314651 : if (!fastargs) {
65 : 0 : goto exit;
66 : : }
67 : 314651 : nameobj = fastargs[0];
68 [ + + ]: 314651 : if (!noptargs) {
69 : 8 : goto skip_optional_pos;
70 : : }
71 [ + + ]: 314643 : if (fastargs[1]) {
72 [ + + ]: 314641 : if (!PyUnicode_Check(fastargs[1])) {
73 : 1 : _PyArg_BadArgument("FileIO", "argument 'mode'", "str", fastargs[1]);
74 : 1 : goto exit;
75 : : }
76 : : Py_ssize_t mode_length;
77 : 314640 : mode = PyUnicode_AsUTF8AndSize(fastargs[1], &mode_length);
78 [ - + ]: 314640 : if (mode == NULL) {
79 : 0 : goto exit;
80 : : }
81 [ - + ]: 314640 : if (strlen(mode) != (size_t)mode_length) {
82 : 0 : PyErr_SetString(PyExc_ValueError, "embedded null character");
83 : 0 : goto exit;
84 : : }
85 [ + + ]: 314640 : if (!--noptargs) {
86 : 7723 : goto skip_optional_pos;
87 : : }
88 : : }
89 [ + - ]: 306919 : if (fastargs[2]) {
90 : 306919 : closefd = _PyLong_AsInt(fastargs[2]);
91 [ - + - - ]: 306919 : if (closefd == -1 && PyErr_Occurred()) {
92 : 0 : goto exit;
93 : : }
94 [ + + ]: 306919 : if (!--noptargs) {
95 : 3 : goto skip_optional_pos;
96 : : }
97 : : }
98 : 306916 : opener = fastargs[3];
99 : 314650 : skip_optional_pos:
100 : 314650 : return_value = _io_FileIO___init___impl((fileio *)self, nameobj, mode, closefd, opener);
101 : :
102 : 314650 : exit:
103 : 314650 : return return_value;
104 : : }
105 : :
106 : : PyDoc_STRVAR(_io_FileIO_fileno__doc__,
107 : : "fileno($self, /)\n"
108 : : "--\n"
109 : : "\n"
110 : : "Return the underlying file descriptor (an integer).");
111 : :
112 : : #define _IO_FILEIO_FILENO_METHODDEF \
113 : : {"fileno", (PyCFunction)_io_FileIO_fileno, METH_NOARGS, _io_FileIO_fileno__doc__},
114 : :
115 : : static PyObject *
116 : : _io_FileIO_fileno_impl(fileio *self);
117 : :
118 : : static PyObject *
119 : 63705 : _io_FileIO_fileno(fileio *self, PyObject *Py_UNUSED(ignored))
120 : : {
121 : 63705 : return _io_FileIO_fileno_impl(self);
122 : : }
123 : :
124 : : PyDoc_STRVAR(_io_FileIO_readable__doc__,
125 : : "readable($self, /)\n"
126 : : "--\n"
127 : : "\n"
128 : : "True if file was opened in a read mode.");
129 : :
130 : : #define _IO_FILEIO_READABLE_METHODDEF \
131 : : {"readable", (PyCFunction)_io_FileIO_readable, METH_NOARGS, _io_FileIO_readable__doc__},
132 : :
133 : : static PyObject *
134 : : _io_FileIO_readable_impl(fileio *self);
135 : :
136 : : static PyObject *
137 : 268501 : _io_FileIO_readable(fileio *self, PyObject *Py_UNUSED(ignored))
138 : : {
139 : 268501 : return _io_FileIO_readable_impl(self);
140 : : }
141 : :
142 : : PyDoc_STRVAR(_io_FileIO_writable__doc__,
143 : : "writable($self, /)\n"
144 : : "--\n"
145 : : "\n"
146 : : "True if file was opened in a write mode.");
147 : :
148 : : #define _IO_FILEIO_WRITABLE_METHODDEF \
149 : : {"writable", (PyCFunction)_io_FileIO_writable, METH_NOARGS, _io_FileIO_writable__doc__},
150 : :
151 : : static PyObject *
152 : : _io_FileIO_writable_impl(fileio *self);
153 : :
154 : : static PyObject *
155 : 61927 : _io_FileIO_writable(fileio *self, PyObject *Py_UNUSED(ignored))
156 : : {
157 : 61927 : return _io_FileIO_writable_impl(self);
158 : : }
159 : :
160 : : PyDoc_STRVAR(_io_FileIO_seekable__doc__,
161 : : "seekable($self, /)\n"
162 : : "--\n"
163 : : "\n"
164 : : "True if file supports random-access.");
165 : :
166 : : #define _IO_FILEIO_SEEKABLE_METHODDEF \
167 : : {"seekable", (PyCFunction)_io_FileIO_seekable, METH_NOARGS, _io_FileIO_seekable__doc__},
168 : :
169 : : static PyObject *
170 : : _io_FileIO_seekable_impl(fileio *self);
171 : :
172 : : static PyObject *
173 : 83936 : _io_FileIO_seekable(fileio *self, PyObject *Py_UNUSED(ignored))
174 : : {
175 : 83936 : return _io_FileIO_seekable_impl(self);
176 : : }
177 : :
178 : : PyDoc_STRVAR(_io_FileIO_readinto__doc__,
179 : : "readinto($self, buffer, /)\n"
180 : : "--\n"
181 : : "\n"
182 : : "Same as RawIOBase.readinto().");
183 : :
184 : : #define _IO_FILEIO_READINTO_METHODDEF \
185 : : {"readinto", (PyCFunction)_io_FileIO_readinto, METH_O, _io_FileIO_readinto__doc__},
186 : :
187 : : static PyObject *
188 : : _io_FileIO_readinto_impl(fileio *self, Py_buffer *buffer);
189 : :
190 : : static PyObject *
191 : 171618 : _io_FileIO_readinto(fileio *self, PyObject *arg)
192 : : {
193 : 171618 : PyObject *return_value = NULL;
194 : 171618 : Py_buffer buffer = {NULL, NULL};
195 : :
196 [ + + ]: 171618 : if (PyObject_GetBuffer(arg, &buffer, PyBUF_WRITABLE) < 0) {
197 : 2 : PyErr_Clear();
198 : 2 : _PyArg_BadArgument("readinto", "argument", "read-write bytes-like object", arg);
199 : 2 : goto exit;
200 : : }
201 [ - + ]: 171616 : if (!PyBuffer_IsContiguous(&buffer, 'C')) {
202 : 0 : _PyArg_BadArgument("readinto", "argument", "contiguous buffer", arg);
203 : 0 : goto exit;
204 : : }
205 : 171616 : return_value = _io_FileIO_readinto_impl(self, &buffer);
206 : :
207 : 171613 : exit:
208 : : /* Cleanup for buffer */
209 [ + + ]: 171613 : if (buffer.obj) {
210 : 171611 : PyBuffer_Release(&buffer);
211 : : }
212 : :
213 : 171613 : return return_value;
214 : : }
215 : :
216 : : PyDoc_STRVAR(_io_FileIO_readall__doc__,
217 : : "readall($self, /)\n"
218 : : "--\n"
219 : : "\n"
220 : : "Read all data from the file, returned as bytes.\n"
221 : : "\n"
222 : : "In non-blocking mode, returns as much as is immediately available,\n"
223 : : "or None if no data is available. Return an empty bytes object at EOF.");
224 : :
225 : : #define _IO_FILEIO_READALL_METHODDEF \
226 : : {"readall", (PyCFunction)_io_FileIO_readall, METH_NOARGS, _io_FileIO_readall__doc__},
227 : :
228 : : static PyObject *
229 : : _io_FileIO_readall_impl(fileio *self);
230 : :
231 : : static PyObject *
232 : 180621 : _io_FileIO_readall(fileio *self, PyObject *Py_UNUSED(ignored))
233 : : {
234 : 180621 : return _io_FileIO_readall_impl(self);
235 : : }
236 : :
237 : : PyDoc_STRVAR(_io_FileIO_read__doc__,
238 : : "read($self, size=-1, /)\n"
239 : : "--\n"
240 : : "\n"
241 : : "Read at most size bytes, returned as bytes.\n"
242 : : "\n"
243 : : "Only makes one system call, so less data may be returned than requested.\n"
244 : : "In non-blocking mode, returns None if no data is available.\n"
245 : : "Return an empty bytes object at EOF.");
246 : :
247 : : #define _IO_FILEIO_READ_METHODDEF \
248 : : {"read", _PyCFunction_CAST(_io_FileIO_read), METH_FASTCALL, _io_FileIO_read__doc__},
249 : :
250 : : static PyObject *
251 : : _io_FileIO_read_impl(fileio *self, Py_ssize_t size);
252 : :
253 : : static PyObject *
254 : 605 : _io_FileIO_read(fileio *self, PyObject *const *args, Py_ssize_t nargs)
255 : : {
256 : 605 : PyObject *return_value = NULL;
257 : 605 : Py_ssize_t size = -1;
258 : :
259 [ + - - + : 605 : if (!_PyArg_CheckPositional("read", nargs, 0, 1)) {
- - ]
260 : 0 : goto exit;
261 : : }
262 [ + + ]: 605 : if (nargs < 1) {
263 : 158 : goto skip_optional;
264 : : }
265 [ - + ]: 447 : if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
266 : 0 : goto exit;
267 : : }
268 : 447 : skip_optional:
269 : 605 : return_value = _io_FileIO_read_impl(self, size);
270 : :
271 : 605 : exit:
272 : 605 : return return_value;
273 : : }
274 : :
275 : : PyDoc_STRVAR(_io_FileIO_write__doc__,
276 : : "write($self, b, /)\n"
277 : : "--\n"
278 : : "\n"
279 : : "Write buffer b to file, return number of bytes written.\n"
280 : : "\n"
281 : : "Only makes one system call, so not all of the data may be written.\n"
282 : : "The number of bytes actually written is returned. In non-blocking mode,\n"
283 : : "returns None if the write would block.");
284 : :
285 : : #define _IO_FILEIO_WRITE_METHODDEF \
286 : : {"write", (PyCFunction)_io_FileIO_write, METH_O, _io_FileIO_write__doc__},
287 : :
288 : : static PyObject *
289 : : _io_FileIO_write_impl(fileio *self, Py_buffer *b);
290 : :
291 : : static PyObject *
292 : 292279 : _io_FileIO_write(fileio *self, PyObject *arg)
293 : : {
294 : 292279 : PyObject *return_value = NULL;
295 : 292279 : Py_buffer b = {NULL, NULL};
296 : :
297 [ + + ]: 292279 : if (PyObject_GetBuffer(arg, &b, PyBUF_SIMPLE) != 0) {
298 : 3 : goto exit;
299 : : }
300 [ - + ]: 292276 : if (!PyBuffer_IsContiguous(&b, 'C')) {
301 : 0 : _PyArg_BadArgument("write", "argument", "contiguous buffer", arg);
302 : 0 : goto exit;
303 : : }
304 : 292276 : return_value = _io_FileIO_write_impl(self, &b);
305 : :
306 : 292279 : exit:
307 : : /* Cleanup for b */
308 [ + + ]: 292279 : if (b.obj) {
309 : 292276 : PyBuffer_Release(&b);
310 : : }
311 : :
312 : 292279 : return return_value;
313 : : }
314 : :
315 : : PyDoc_STRVAR(_io_FileIO_seek__doc__,
316 : : "seek($self, pos, whence=0, /)\n"
317 : : "--\n"
318 : : "\n"
319 : : "Move to new file position and return the file position.\n"
320 : : "\n"
321 : : "Argument offset is a byte count. Optional argument whence defaults to\n"
322 : : "SEEK_SET or 0 (offset from start of file, offset should be >= 0); other values\n"
323 : : "are SEEK_CUR or 1 (move relative to current position, positive or negative),\n"
324 : : "and SEEK_END or 2 (move relative to end of file, usually negative, although\n"
325 : : "many platforms allow seeking beyond the end of a file).\n"
326 : : "\n"
327 : : "Note that not all file objects are seekable.");
328 : :
329 : : #define _IO_FILEIO_SEEK_METHODDEF \
330 : : {"seek", _PyCFunction_CAST(_io_FileIO_seek), METH_FASTCALL, _io_FileIO_seek__doc__},
331 : :
332 : : static PyObject *
333 : : _io_FileIO_seek_impl(fileio *self, PyObject *pos, int whence);
334 : :
335 : : static PyObject *
336 : 52609 : _io_FileIO_seek(fileio *self, PyObject *const *args, Py_ssize_t nargs)
337 : : {
338 : 52609 : PyObject *return_value = NULL;
339 : : PyObject *pos;
340 : 52609 : int whence = 0;
341 : :
342 [ + + - + : 52609 : if (!_PyArg_CheckPositional("seek", nargs, 1, 2)) {
+ - ]
343 : 1 : goto exit;
344 : : }
345 : 52608 : pos = args[0];
346 [ + + ]: 52608 : if (nargs < 2) {
347 : 44 : goto skip_optional;
348 : : }
349 : 52564 : whence = _PyLong_AsInt(args[1]);
350 [ - + - - ]: 52564 : if (whence == -1 && PyErr_Occurred()) {
351 : 0 : goto exit;
352 : : }
353 : 52564 : skip_optional:
354 : 52608 : return_value = _io_FileIO_seek_impl(self, pos, whence);
355 : :
356 : 52609 : exit:
357 : 52609 : return return_value;
358 : : }
359 : :
360 : : PyDoc_STRVAR(_io_FileIO_tell__doc__,
361 : : "tell($self, /)\n"
362 : : "--\n"
363 : : "\n"
364 : : "Current file position.\n"
365 : : "\n"
366 : : "Can raise OSError for non seekable files.");
367 : :
368 : : #define _IO_FILEIO_TELL_METHODDEF \
369 : : {"tell", (PyCFunction)_io_FileIO_tell, METH_NOARGS, _io_FileIO_tell__doc__},
370 : :
371 : : static PyObject *
372 : : _io_FileIO_tell_impl(fileio *self);
373 : :
374 : : static PyObject *
375 : 349412 : _io_FileIO_tell(fileio *self, PyObject *Py_UNUSED(ignored))
376 : : {
377 : 349412 : return _io_FileIO_tell_impl(self);
378 : : }
379 : :
380 : : #if defined(HAVE_FTRUNCATE)
381 : :
382 : : PyDoc_STRVAR(_io_FileIO_truncate__doc__,
383 : : "truncate($self, size=None, /)\n"
384 : : "--\n"
385 : : "\n"
386 : : "Truncate the file to at most size bytes and return the truncated size.\n"
387 : : "\n"
388 : : "Size defaults to the current file position, as returned by tell().\n"
389 : : "The current file position is changed to the value of size.");
390 : :
391 : : #define _IO_FILEIO_TRUNCATE_METHODDEF \
392 : : {"truncate", _PyCFunction_CAST(_io_FileIO_truncate), METH_FASTCALL, _io_FileIO_truncate__doc__},
393 : :
394 : : static PyObject *
395 : : _io_FileIO_truncate_impl(fileio *self, PyObject *posobj);
396 : :
397 : : static PyObject *
398 : 114 : _io_FileIO_truncate(fileio *self, PyObject *const *args, Py_ssize_t nargs)
399 : : {
400 : 114 : PyObject *return_value = NULL;
401 : 114 : PyObject *posobj = Py_None;
402 : :
403 [ + - - + : 114 : if (!_PyArg_CheckPositional("truncate", nargs, 0, 1)) {
- - ]
404 : 0 : goto exit;
405 : : }
406 [ + + ]: 114 : if (nargs < 1) {
407 : 8 : goto skip_optional;
408 : : }
409 : 106 : posobj = args[0];
410 : 114 : skip_optional:
411 : 114 : return_value = _io_FileIO_truncate_impl(self, posobj);
412 : :
413 : 114 : exit:
414 : 114 : return return_value;
415 : : }
416 : :
417 : : #endif /* defined(HAVE_FTRUNCATE) */
418 : :
419 : : PyDoc_STRVAR(_io_FileIO_isatty__doc__,
420 : : "isatty($self, /)\n"
421 : : "--\n"
422 : : "\n"
423 : : "True if the file is connected to a TTY device.");
424 : :
425 : : #define _IO_FILEIO_ISATTY_METHODDEF \
426 : : {"isatty", (PyCFunction)_io_FileIO_isatty, METH_NOARGS, _io_FileIO_isatty__doc__},
427 : :
428 : : static PyObject *
429 : : _io_FileIO_isatty_impl(fileio *self);
430 : :
431 : : static PyObject *
432 : 305860 : _io_FileIO_isatty(fileio *self, PyObject *Py_UNUSED(ignored))
433 : : {
434 : 305860 : return _io_FileIO_isatty_impl(self);
435 : : }
436 : :
437 : : #ifndef _IO_FILEIO_TRUNCATE_METHODDEF
438 : : #define _IO_FILEIO_TRUNCATE_METHODDEF
439 : : #endif /* !defined(_IO_FILEIO_TRUNCATE_METHODDEF) */
440 : : /*[clinic end generated code: output=fdcf0f9277d44415 input=a9049054013a1b77]*/
|