• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         closefd = _PyLong_AsInt(fastargs[2]);
91         if (closefd == -1 && PyErr_Occurred()) {
92             goto exit;
93         }
94         if (!--noptargs) {
95             goto skip_optional_pos;
96         }
97     }
98     opener = fastargs[3];
99 skip_optional_pos:
100     return_value = _io_FileIO___init___impl((fileio *)self, nameobj, mode, closefd, opener);
101 
102 exit:
103     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 *
_io_FileIO_fileno(fileio * self,PyObject * Py_UNUSED (ignored))119 _io_FileIO_fileno(fileio *self, PyObject *Py_UNUSED(ignored))
120 {
121     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 *
_io_FileIO_readable(fileio * self,PyObject * Py_UNUSED (ignored))137 _io_FileIO_readable(fileio *self, PyObject *Py_UNUSED(ignored))
138 {
139     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 *
_io_FileIO_writable(fileio * self,PyObject * Py_UNUSED (ignored))155 _io_FileIO_writable(fileio *self, PyObject *Py_UNUSED(ignored))
156 {
157     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 *
_io_FileIO_seekable(fileio * self,PyObject * Py_UNUSED (ignored))173 _io_FileIO_seekable(fileio *self, PyObject *Py_UNUSED(ignored))
174 {
175     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 *
_io_FileIO_readinto(fileio * self,PyObject * arg)191 _io_FileIO_readinto(fileio *self, PyObject *arg)
192 {
193     PyObject *return_value = NULL;
194     Py_buffer buffer = {NULL, NULL};
195 
196     if (PyObject_GetBuffer(arg, &buffer, PyBUF_WRITABLE) < 0) {
197         PyErr_Clear();
198         _PyArg_BadArgument("readinto", "argument", "read-write bytes-like object", arg);
199         goto exit;
200     }
201     if (!PyBuffer_IsContiguous(&buffer, 'C')) {
202         _PyArg_BadArgument("readinto", "argument", "contiguous buffer", arg);
203         goto exit;
204     }
205     return_value = _io_FileIO_readinto_impl(self, &buffer);
206 
207 exit:
208     /* Cleanup for buffer */
209     if (buffer.obj) {
210        PyBuffer_Release(&buffer);
211     }
212 
213     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 *
_io_FileIO_readall(fileio * self,PyObject * Py_UNUSED (ignored))232 _io_FileIO_readall(fileio *self, PyObject *Py_UNUSED(ignored))
233 {
234     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)(void(*)(void))_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 *
_io_FileIO_read(fileio * self,PyObject * const * args,Py_ssize_t nargs)254 _io_FileIO_read(fileio *self, PyObject *const *args, Py_ssize_t nargs)
255 {
256     PyObject *return_value = NULL;
257     Py_ssize_t size = -1;
258 
259     if (!_PyArg_CheckPositional("read", nargs, 0, 1)) {
260         goto exit;
261     }
262     if (nargs < 1) {
263         goto skip_optional;
264     }
265     if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
266         goto exit;
267     }
268 skip_optional:
269     return_value = _io_FileIO_read_impl(self, size);
270 
271 exit:
272     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 *
_io_FileIO_write(fileio * self,PyObject * arg)292 _io_FileIO_write(fileio *self, PyObject *arg)
293 {
294     PyObject *return_value = NULL;
295     Py_buffer b = {NULL, NULL};
296 
297     if (PyObject_GetBuffer(arg, &b, PyBUF_SIMPLE) != 0) {
298         goto exit;
299     }
300     if (!PyBuffer_IsContiguous(&b, 'C')) {
301         _PyArg_BadArgument("write", "argument", "contiguous buffer", arg);
302         goto exit;
303     }
304     return_value = _io_FileIO_write_impl(self, &b);
305 
306 exit:
307     /* Cleanup for b */
308     if (b.obj) {
309        PyBuffer_Release(&b);
310     }
311 
312     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)(void(*)(void))_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 *
_io_FileIO_seek(fileio * self,PyObject * const * args,Py_ssize_t nargs)336 _io_FileIO_seek(fileio *self, PyObject *const *args, Py_ssize_t nargs)
337 {
338     PyObject *return_value = NULL;
339     PyObject *pos;
340     int whence = 0;
341 
342     if (!_PyArg_CheckPositional("seek", nargs, 1, 2)) {
343         goto exit;
344     }
345     pos = args[0];
346     if (nargs < 2) {
347         goto skip_optional;
348     }
349     whence = _PyLong_AsInt(args[1]);
350     if (whence == -1 && PyErr_Occurred()) {
351         goto exit;
352     }
353 skip_optional:
354     return_value = _io_FileIO_seek_impl(self, pos, whence);
355 
356 exit:
357     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 *
_io_FileIO_tell(fileio * self,PyObject * Py_UNUSED (ignored))375 _io_FileIO_tell(fileio *self, PyObject *Py_UNUSED(ignored))
376 {
377     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)(void(*)(void))_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 *
_io_FileIO_truncate(fileio * self,PyObject * const * args,Py_ssize_t nargs)398 _io_FileIO_truncate(fileio *self, PyObject *const *args, Py_ssize_t nargs)
399 {
400     PyObject *return_value = NULL;
401     PyObject *posobj = Py_None;
402 
403     if (!_PyArg_CheckPositional("truncate", nargs, 0, 1)) {
404         goto exit;
405     }
406     if (nargs < 1) {
407         goto skip_optional;
408     }
409     posobj = args[0];
410 skip_optional:
411     return_value = _io_FileIO_truncate_impl(self, posobj);
412 
413 exit:
414     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 *
_io_FileIO_isatty(fileio * self,PyObject * Py_UNUSED (ignored))432 _io_FileIO_isatty(fileio *self, PyObject *Py_UNUSED(ignored))
433 {
434     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=3479912ec0f7e029 input=a9049054013a1b77]*/
441