• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*[clinic input]
2 preserve
3 [clinic start generated code]*/
4 
5 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
6 #  include "pycore_gc.h"          // PyGC_Head
7 #  include "pycore_runtime.h"     // _Py_ID()
8 #endif
9 #include "pycore_abstract.h"      // _Py_convert_optional_to_ssize_t()
10 #include "pycore_modsupport.h"    // _PyArg_CheckPositional()
11 
12 PyDoc_STRVAR(_io_BytesIO_readable__doc__,
13 "readable($self, /)\n"
14 "--\n"
15 "\n"
16 "Returns True if the IO object can be read.");
17 
18 #define _IO_BYTESIO_READABLE_METHODDEF    \
19     {"readable", (PyCFunction)_io_BytesIO_readable, METH_NOARGS, _io_BytesIO_readable__doc__},
20 
21 static PyObject *
22 _io_BytesIO_readable_impl(bytesio *self);
23 
24 static PyObject *
_io_BytesIO_readable(bytesio * self,PyObject * Py_UNUSED (ignored))25 _io_BytesIO_readable(bytesio *self, PyObject *Py_UNUSED(ignored))
26 {
27     return _io_BytesIO_readable_impl(self);
28 }
29 
30 PyDoc_STRVAR(_io_BytesIO_writable__doc__,
31 "writable($self, /)\n"
32 "--\n"
33 "\n"
34 "Returns True if the IO object can be written.");
35 
36 #define _IO_BYTESIO_WRITABLE_METHODDEF    \
37     {"writable", (PyCFunction)_io_BytesIO_writable, METH_NOARGS, _io_BytesIO_writable__doc__},
38 
39 static PyObject *
40 _io_BytesIO_writable_impl(bytesio *self);
41 
42 static PyObject *
_io_BytesIO_writable(bytesio * self,PyObject * Py_UNUSED (ignored))43 _io_BytesIO_writable(bytesio *self, PyObject *Py_UNUSED(ignored))
44 {
45     return _io_BytesIO_writable_impl(self);
46 }
47 
48 PyDoc_STRVAR(_io_BytesIO_seekable__doc__,
49 "seekable($self, /)\n"
50 "--\n"
51 "\n"
52 "Returns True if the IO object can be seeked.");
53 
54 #define _IO_BYTESIO_SEEKABLE_METHODDEF    \
55     {"seekable", (PyCFunction)_io_BytesIO_seekable, METH_NOARGS, _io_BytesIO_seekable__doc__},
56 
57 static PyObject *
58 _io_BytesIO_seekable_impl(bytesio *self);
59 
60 static PyObject *
_io_BytesIO_seekable(bytesio * self,PyObject * Py_UNUSED (ignored))61 _io_BytesIO_seekable(bytesio *self, PyObject *Py_UNUSED(ignored))
62 {
63     return _io_BytesIO_seekable_impl(self);
64 }
65 
66 PyDoc_STRVAR(_io_BytesIO_flush__doc__,
67 "flush($self, /)\n"
68 "--\n"
69 "\n"
70 "Does nothing.");
71 
72 #define _IO_BYTESIO_FLUSH_METHODDEF    \
73     {"flush", (PyCFunction)_io_BytesIO_flush, METH_NOARGS, _io_BytesIO_flush__doc__},
74 
75 static PyObject *
76 _io_BytesIO_flush_impl(bytesio *self);
77 
78 static PyObject *
_io_BytesIO_flush(bytesio * self,PyObject * Py_UNUSED (ignored))79 _io_BytesIO_flush(bytesio *self, PyObject *Py_UNUSED(ignored))
80 {
81     return _io_BytesIO_flush_impl(self);
82 }
83 
84 PyDoc_STRVAR(_io_BytesIO_getbuffer__doc__,
85 "getbuffer($self, /)\n"
86 "--\n"
87 "\n"
88 "Get a read-write view over the contents of the BytesIO object.");
89 
90 #define _IO_BYTESIO_GETBUFFER_METHODDEF    \
91     {"getbuffer", _PyCFunction_CAST(_io_BytesIO_getbuffer), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io_BytesIO_getbuffer__doc__},
92 
93 static PyObject *
94 _io_BytesIO_getbuffer_impl(bytesio *self, PyTypeObject *cls);
95 
96 static PyObject *
_io_BytesIO_getbuffer(bytesio * self,PyTypeObject * cls,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)97 _io_BytesIO_getbuffer(bytesio *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
98 {
99     if (nargs || (kwnames && PyTuple_GET_SIZE(kwnames))) {
100         PyErr_SetString(PyExc_TypeError, "getbuffer() takes no arguments");
101         return NULL;
102     }
103     return _io_BytesIO_getbuffer_impl(self, cls);
104 }
105 
106 PyDoc_STRVAR(_io_BytesIO_getvalue__doc__,
107 "getvalue($self, /)\n"
108 "--\n"
109 "\n"
110 "Retrieve the entire contents of the BytesIO object.");
111 
112 #define _IO_BYTESIO_GETVALUE_METHODDEF    \
113     {"getvalue", (PyCFunction)_io_BytesIO_getvalue, METH_NOARGS, _io_BytesIO_getvalue__doc__},
114 
115 static PyObject *
116 _io_BytesIO_getvalue_impl(bytesio *self);
117 
118 static PyObject *
_io_BytesIO_getvalue(bytesio * self,PyObject * Py_UNUSED (ignored))119 _io_BytesIO_getvalue(bytesio *self, PyObject *Py_UNUSED(ignored))
120 {
121     return _io_BytesIO_getvalue_impl(self);
122 }
123 
124 PyDoc_STRVAR(_io_BytesIO_isatty__doc__,
125 "isatty($self, /)\n"
126 "--\n"
127 "\n"
128 "Always returns False.\n"
129 "\n"
130 "BytesIO objects are not connected to a TTY-like device.");
131 
132 #define _IO_BYTESIO_ISATTY_METHODDEF    \
133     {"isatty", (PyCFunction)_io_BytesIO_isatty, METH_NOARGS, _io_BytesIO_isatty__doc__},
134 
135 static PyObject *
136 _io_BytesIO_isatty_impl(bytesio *self);
137 
138 static PyObject *
_io_BytesIO_isatty(bytesio * self,PyObject * Py_UNUSED (ignored))139 _io_BytesIO_isatty(bytesio *self, PyObject *Py_UNUSED(ignored))
140 {
141     return _io_BytesIO_isatty_impl(self);
142 }
143 
144 PyDoc_STRVAR(_io_BytesIO_tell__doc__,
145 "tell($self, /)\n"
146 "--\n"
147 "\n"
148 "Current file position, an integer.");
149 
150 #define _IO_BYTESIO_TELL_METHODDEF    \
151     {"tell", (PyCFunction)_io_BytesIO_tell, METH_NOARGS, _io_BytesIO_tell__doc__},
152 
153 static PyObject *
154 _io_BytesIO_tell_impl(bytesio *self);
155 
156 static PyObject *
_io_BytesIO_tell(bytesio * self,PyObject * Py_UNUSED (ignored))157 _io_BytesIO_tell(bytesio *self, PyObject *Py_UNUSED(ignored))
158 {
159     return _io_BytesIO_tell_impl(self);
160 }
161 
162 PyDoc_STRVAR(_io_BytesIO_read__doc__,
163 "read($self, size=-1, /)\n"
164 "--\n"
165 "\n"
166 "Read at most size bytes, returned as a bytes object.\n"
167 "\n"
168 "If the size argument is negative, read until EOF is reached.\n"
169 "Return an empty bytes object at EOF.");
170 
171 #define _IO_BYTESIO_READ_METHODDEF    \
172     {"read", _PyCFunction_CAST(_io_BytesIO_read), METH_FASTCALL, _io_BytesIO_read__doc__},
173 
174 static PyObject *
175 _io_BytesIO_read_impl(bytesio *self, Py_ssize_t size);
176 
177 static PyObject *
_io_BytesIO_read(bytesio * self,PyObject * const * args,Py_ssize_t nargs)178 _io_BytesIO_read(bytesio *self, PyObject *const *args, Py_ssize_t nargs)
179 {
180     PyObject *return_value = NULL;
181     Py_ssize_t size = -1;
182 
183     if (!_PyArg_CheckPositional("read", nargs, 0, 1)) {
184         goto exit;
185     }
186     if (nargs < 1) {
187         goto skip_optional;
188     }
189     if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
190         goto exit;
191     }
192 skip_optional:
193     return_value = _io_BytesIO_read_impl(self, size);
194 
195 exit:
196     return return_value;
197 }
198 
199 PyDoc_STRVAR(_io_BytesIO_read1__doc__,
200 "read1($self, size=-1, /)\n"
201 "--\n"
202 "\n"
203 "Read at most size bytes, returned as a bytes object.\n"
204 "\n"
205 "If the size argument is negative or omitted, read until EOF is reached.\n"
206 "Return an empty bytes object at EOF.");
207 
208 #define _IO_BYTESIO_READ1_METHODDEF    \
209     {"read1", _PyCFunction_CAST(_io_BytesIO_read1), METH_FASTCALL, _io_BytesIO_read1__doc__},
210 
211 static PyObject *
212 _io_BytesIO_read1_impl(bytesio *self, Py_ssize_t size);
213 
214 static PyObject *
_io_BytesIO_read1(bytesio * self,PyObject * const * args,Py_ssize_t nargs)215 _io_BytesIO_read1(bytesio *self, PyObject *const *args, Py_ssize_t nargs)
216 {
217     PyObject *return_value = NULL;
218     Py_ssize_t size = -1;
219 
220     if (!_PyArg_CheckPositional("read1", nargs, 0, 1)) {
221         goto exit;
222     }
223     if (nargs < 1) {
224         goto skip_optional;
225     }
226     if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
227         goto exit;
228     }
229 skip_optional:
230     return_value = _io_BytesIO_read1_impl(self, size);
231 
232 exit:
233     return return_value;
234 }
235 
236 PyDoc_STRVAR(_io_BytesIO_readline__doc__,
237 "readline($self, size=-1, /)\n"
238 "--\n"
239 "\n"
240 "Next line from the file, as a bytes object.\n"
241 "\n"
242 "Retain newline.  A non-negative size argument limits the maximum\n"
243 "number of bytes to return (an incomplete line may be returned then).\n"
244 "Return an empty bytes object at EOF.");
245 
246 #define _IO_BYTESIO_READLINE_METHODDEF    \
247     {"readline", _PyCFunction_CAST(_io_BytesIO_readline), METH_FASTCALL, _io_BytesIO_readline__doc__},
248 
249 static PyObject *
250 _io_BytesIO_readline_impl(bytesio *self, Py_ssize_t size);
251 
252 static PyObject *
_io_BytesIO_readline(bytesio * self,PyObject * const * args,Py_ssize_t nargs)253 _io_BytesIO_readline(bytesio *self, PyObject *const *args, Py_ssize_t nargs)
254 {
255     PyObject *return_value = NULL;
256     Py_ssize_t size = -1;
257 
258     if (!_PyArg_CheckPositional("readline", nargs, 0, 1)) {
259         goto exit;
260     }
261     if (nargs < 1) {
262         goto skip_optional;
263     }
264     if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
265         goto exit;
266     }
267 skip_optional:
268     return_value = _io_BytesIO_readline_impl(self, size);
269 
270 exit:
271     return return_value;
272 }
273 
274 PyDoc_STRVAR(_io_BytesIO_readlines__doc__,
275 "readlines($self, size=None, /)\n"
276 "--\n"
277 "\n"
278 "List of bytes objects, each a line from the file.\n"
279 "\n"
280 "Call readline() repeatedly and return a list of the lines so read.\n"
281 "The optional size argument, if given, is an approximate bound on the\n"
282 "total number of bytes in the lines returned.");
283 
284 #define _IO_BYTESIO_READLINES_METHODDEF    \
285     {"readlines", _PyCFunction_CAST(_io_BytesIO_readlines), METH_FASTCALL, _io_BytesIO_readlines__doc__},
286 
287 static PyObject *
288 _io_BytesIO_readlines_impl(bytesio *self, PyObject *arg);
289 
290 static PyObject *
_io_BytesIO_readlines(bytesio * self,PyObject * const * args,Py_ssize_t nargs)291 _io_BytesIO_readlines(bytesio *self, PyObject *const *args, Py_ssize_t nargs)
292 {
293     PyObject *return_value = NULL;
294     PyObject *arg = Py_None;
295 
296     if (!_PyArg_CheckPositional("readlines", nargs, 0, 1)) {
297         goto exit;
298     }
299     if (nargs < 1) {
300         goto skip_optional;
301     }
302     arg = args[0];
303 skip_optional:
304     return_value = _io_BytesIO_readlines_impl(self, arg);
305 
306 exit:
307     return return_value;
308 }
309 
310 PyDoc_STRVAR(_io_BytesIO_readinto__doc__,
311 "readinto($self, buffer, /)\n"
312 "--\n"
313 "\n"
314 "Read bytes into buffer.\n"
315 "\n"
316 "Returns number of bytes read (0 for EOF), or None if the object\n"
317 "is set not to block and has no data to read.");
318 
319 #define _IO_BYTESIO_READINTO_METHODDEF    \
320     {"readinto", (PyCFunction)_io_BytesIO_readinto, METH_O, _io_BytesIO_readinto__doc__},
321 
322 static PyObject *
323 _io_BytesIO_readinto_impl(bytesio *self, Py_buffer *buffer);
324 
325 static PyObject *
_io_BytesIO_readinto(bytesio * self,PyObject * arg)326 _io_BytesIO_readinto(bytesio *self, PyObject *arg)
327 {
328     PyObject *return_value = NULL;
329     Py_buffer buffer = {NULL, NULL};
330 
331     if (PyObject_GetBuffer(arg, &buffer, PyBUF_WRITABLE) < 0) {
332         _PyArg_BadArgument("readinto", "argument", "read-write bytes-like object", arg);
333         goto exit;
334     }
335     return_value = _io_BytesIO_readinto_impl(self, &buffer);
336 
337 exit:
338     /* Cleanup for buffer */
339     if (buffer.obj) {
340        PyBuffer_Release(&buffer);
341     }
342 
343     return return_value;
344 }
345 
346 PyDoc_STRVAR(_io_BytesIO_truncate__doc__,
347 "truncate($self, size=None, /)\n"
348 "--\n"
349 "\n"
350 "Truncate the file to at most size bytes.\n"
351 "\n"
352 "Size defaults to the current file position, as returned by tell().\n"
353 "The current file position is unchanged.  Returns the new size.");
354 
355 #define _IO_BYTESIO_TRUNCATE_METHODDEF    \
356     {"truncate", _PyCFunction_CAST(_io_BytesIO_truncate), METH_FASTCALL, _io_BytesIO_truncate__doc__},
357 
358 static PyObject *
359 _io_BytesIO_truncate_impl(bytesio *self, Py_ssize_t size);
360 
361 static PyObject *
_io_BytesIO_truncate(bytesio * self,PyObject * const * args,Py_ssize_t nargs)362 _io_BytesIO_truncate(bytesio *self, PyObject *const *args, Py_ssize_t nargs)
363 {
364     PyObject *return_value = NULL;
365     Py_ssize_t size = self->pos;
366 
367     if (!_PyArg_CheckPositional("truncate", nargs, 0, 1)) {
368         goto exit;
369     }
370     if (nargs < 1) {
371         goto skip_optional;
372     }
373     if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
374         goto exit;
375     }
376 skip_optional:
377     return_value = _io_BytesIO_truncate_impl(self, size);
378 
379 exit:
380     return return_value;
381 }
382 
383 PyDoc_STRVAR(_io_BytesIO_seek__doc__,
384 "seek($self, pos, whence=0, /)\n"
385 "--\n"
386 "\n"
387 "Change stream position.\n"
388 "\n"
389 "Seek to byte offset pos relative to position indicated by whence:\n"
390 "     0  Start of stream (the default).  pos should be >= 0;\n"
391 "     1  Current position - pos may be negative;\n"
392 "     2  End of stream - pos usually negative.\n"
393 "Returns the new absolute position.");
394 
395 #define _IO_BYTESIO_SEEK_METHODDEF    \
396     {"seek", _PyCFunction_CAST(_io_BytesIO_seek), METH_FASTCALL, _io_BytesIO_seek__doc__},
397 
398 static PyObject *
399 _io_BytesIO_seek_impl(bytesio *self, Py_ssize_t pos, int whence);
400 
401 static PyObject *
_io_BytesIO_seek(bytesio * self,PyObject * const * args,Py_ssize_t nargs)402 _io_BytesIO_seek(bytesio *self, PyObject *const *args, Py_ssize_t nargs)
403 {
404     PyObject *return_value = NULL;
405     Py_ssize_t pos;
406     int whence = 0;
407 
408     if (!_PyArg_CheckPositional("seek", nargs, 1, 2)) {
409         goto exit;
410     }
411     {
412         Py_ssize_t ival = -1;
413         PyObject *iobj = _PyNumber_Index(args[0]);
414         if (iobj != NULL) {
415             ival = PyLong_AsSsize_t(iobj);
416             Py_DECREF(iobj);
417         }
418         if (ival == -1 && PyErr_Occurred()) {
419             goto exit;
420         }
421         pos = ival;
422     }
423     if (nargs < 2) {
424         goto skip_optional;
425     }
426     whence = PyLong_AsInt(args[1]);
427     if (whence == -1 && PyErr_Occurred()) {
428         goto exit;
429     }
430 skip_optional:
431     return_value = _io_BytesIO_seek_impl(self, pos, whence);
432 
433 exit:
434     return return_value;
435 }
436 
437 PyDoc_STRVAR(_io_BytesIO_write__doc__,
438 "write($self, b, /)\n"
439 "--\n"
440 "\n"
441 "Write bytes to file.\n"
442 "\n"
443 "Return the number of bytes written.");
444 
445 #define _IO_BYTESIO_WRITE_METHODDEF    \
446     {"write", (PyCFunction)_io_BytesIO_write, METH_O, _io_BytesIO_write__doc__},
447 
448 PyDoc_STRVAR(_io_BytesIO_writelines__doc__,
449 "writelines($self, lines, /)\n"
450 "--\n"
451 "\n"
452 "Write lines to the file.\n"
453 "\n"
454 "Note that newlines are not added.  lines can be any iterable object\n"
455 "producing bytes-like objects. This is equivalent to calling write() for\n"
456 "each element.");
457 
458 #define _IO_BYTESIO_WRITELINES_METHODDEF    \
459     {"writelines", (PyCFunction)_io_BytesIO_writelines, METH_O, _io_BytesIO_writelines__doc__},
460 
461 PyDoc_STRVAR(_io_BytesIO_close__doc__,
462 "close($self, /)\n"
463 "--\n"
464 "\n"
465 "Disable all I/O operations.");
466 
467 #define _IO_BYTESIO_CLOSE_METHODDEF    \
468     {"close", (PyCFunction)_io_BytesIO_close, METH_NOARGS, _io_BytesIO_close__doc__},
469 
470 static PyObject *
471 _io_BytesIO_close_impl(bytesio *self);
472 
473 static PyObject *
_io_BytesIO_close(bytesio * self,PyObject * Py_UNUSED (ignored))474 _io_BytesIO_close(bytesio *self, PyObject *Py_UNUSED(ignored))
475 {
476     return _io_BytesIO_close_impl(self);
477 }
478 
479 PyDoc_STRVAR(_io_BytesIO___init____doc__,
480 "BytesIO(initial_bytes=b\'\')\n"
481 "--\n"
482 "\n"
483 "Buffered I/O implementation using an in-memory bytes buffer.");
484 
485 static int
486 _io_BytesIO___init___impl(bytesio *self, PyObject *initvalue);
487 
488 static int
_io_BytesIO___init__(PyObject * self,PyObject * args,PyObject * kwargs)489 _io_BytesIO___init__(PyObject *self, PyObject *args, PyObject *kwargs)
490 {
491     int return_value = -1;
492     #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
493 
494     #define NUM_KEYWORDS 1
495     static struct {
496         PyGC_Head _this_is_not_used;
497         PyObject_VAR_HEAD
498         PyObject *ob_item[NUM_KEYWORDS];
499     } _kwtuple = {
500         .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
501         .ob_item = { &_Py_ID(initial_bytes), },
502     };
503     #undef NUM_KEYWORDS
504     #define KWTUPLE (&_kwtuple.ob_base.ob_base)
505 
506     #else  // !Py_BUILD_CORE
507     #  define KWTUPLE NULL
508     #endif  // !Py_BUILD_CORE
509 
510     static const char * const _keywords[] = {"initial_bytes", NULL};
511     static _PyArg_Parser _parser = {
512         .keywords = _keywords,
513         .fname = "BytesIO",
514         .kwtuple = KWTUPLE,
515     };
516     #undef KWTUPLE
517     PyObject *argsbuf[1];
518     PyObject * const *fastargs;
519     Py_ssize_t nargs = PyTuple_GET_SIZE(args);
520     Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
521     PyObject *initvalue = NULL;
522 
523     fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 0, 1, 0, argsbuf);
524     if (!fastargs) {
525         goto exit;
526     }
527     if (!noptargs) {
528         goto skip_optional_pos;
529     }
530     initvalue = fastargs[0];
531 skip_optional_pos:
532     return_value = _io_BytesIO___init___impl((bytesio *)self, initvalue);
533 
534 exit:
535     return return_value;
536 }
537 /*[clinic end generated code: output=ef116925b8b9e535 input=a9049054013a1b77]*/
538