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