Branch data Line data Source code
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 : :
10 : :
11 : : PyDoc_STRVAR(_io_BytesIO_readable__doc__,
12 : : "readable($self, /)\n"
13 : : "--\n"
14 : : "\n"
15 : : "Returns True if the IO object can be read.");
16 : :
17 : : #define _IO_BYTESIO_READABLE_METHODDEF \
18 : : {"readable", (PyCFunction)_io_BytesIO_readable, METH_NOARGS, _io_BytesIO_readable__doc__},
19 : :
20 : : static PyObject *
21 : : _io_BytesIO_readable_impl(bytesio *self);
22 : :
23 : : static PyObject *
24 : 0 : _io_BytesIO_readable(bytesio *self, PyObject *Py_UNUSED(ignored))
25 : : {
26 : 0 : return _io_BytesIO_readable_impl(self);
27 : : }
28 : :
29 : : PyDoc_STRVAR(_io_BytesIO_writable__doc__,
30 : : "writable($self, /)\n"
31 : : "--\n"
32 : : "\n"
33 : : "Returns True if the IO object can be written.");
34 : :
35 : : #define _IO_BYTESIO_WRITABLE_METHODDEF \
36 : : {"writable", (PyCFunction)_io_BytesIO_writable, METH_NOARGS, _io_BytesIO_writable__doc__},
37 : :
38 : : static PyObject *
39 : : _io_BytesIO_writable_impl(bytesio *self);
40 : :
41 : : static PyObject *
42 : 0 : _io_BytesIO_writable(bytesio *self, PyObject *Py_UNUSED(ignored))
43 : : {
44 : 0 : return _io_BytesIO_writable_impl(self);
45 : : }
46 : :
47 : : PyDoc_STRVAR(_io_BytesIO_seekable__doc__,
48 : : "seekable($self, /)\n"
49 : : "--\n"
50 : : "\n"
51 : : "Returns True if the IO object can be seeked.");
52 : :
53 : : #define _IO_BYTESIO_SEEKABLE_METHODDEF \
54 : : {"seekable", (PyCFunction)_io_BytesIO_seekable, METH_NOARGS, _io_BytesIO_seekable__doc__},
55 : :
56 : : static PyObject *
57 : : _io_BytesIO_seekable_impl(bytesio *self);
58 : :
59 : : static PyObject *
60 : 0 : _io_BytesIO_seekable(bytesio *self, PyObject *Py_UNUSED(ignored))
61 : : {
62 : 0 : return _io_BytesIO_seekable_impl(self);
63 : : }
64 : :
65 : : PyDoc_STRVAR(_io_BytesIO_flush__doc__,
66 : : "flush($self, /)\n"
67 : : "--\n"
68 : : "\n"
69 : : "Does nothing.");
70 : :
71 : : #define _IO_BYTESIO_FLUSH_METHODDEF \
72 : : {"flush", (PyCFunction)_io_BytesIO_flush, METH_NOARGS, _io_BytesIO_flush__doc__},
73 : :
74 : : static PyObject *
75 : : _io_BytesIO_flush_impl(bytesio *self);
76 : :
77 : : static PyObject *
78 : 0 : _io_BytesIO_flush(bytesio *self, PyObject *Py_UNUSED(ignored))
79 : : {
80 : 0 : return _io_BytesIO_flush_impl(self);
81 : : }
82 : :
83 : : PyDoc_STRVAR(_io_BytesIO_getbuffer__doc__,
84 : : "getbuffer($self, /)\n"
85 : : "--\n"
86 : : "\n"
87 : : "Get a read-write view over the contents of the BytesIO object.");
88 : :
89 : : #define _IO_BYTESIO_GETBUFFER_METHODDEF \
90 : : {"getbuffer", (PyCFunction)_io_BytesIO_getbuffer, METH_NOARGS, _io_BytesIO_getbuffer__doc__},
91 : :
92 : : static PyObject *
93 : : _io_BytesIO_getbuffer_impl(bytesio *self);
94 : :
95 : : static PyObject *
96 : 0 : _io_BytesIO_getbuffer(bytesio *self, PyObject *Py_UNUSED(ignored))
97 : : {
98 : 0 : return _io_BytesIO_getbuffer_impl(self);
99 : : }
100 : :
101 : : PyDoc_STRVAR(_io_BytesIO_getvalue__doc__,
102 : : "getvalue($self, /)\n"
103 : : "--\n"
104 : : "\n"
105 : : "Retrieve the entire contents of the BytesIO object.");
106 : :
107 : : #define _IO_BYTESIO_GETVALUE_METHODDEF \
108 : : {"getvalue", (PyCFunction)_io_BytesIO_getvalue, METH_NOARGS, _io_BytesIO_getvalue__doc__},
109 : :
110 : : static PyObject *
111 : : _io_BytesIO_getvalue_impl(bytesio *self);
112 : :
113 : : static PyObject *
114 : 0 : _io_BytesIO_getvalue(bytesio *self, PyObject *Py_UNUSED(ignored))
115 : : {
116 : 0 : return _io_BytesIO_getvalue_impl(self);
117 : : }
118 : :
119 : : PyDoc_STRVAR(_io_BytesIO_isatty__doc__,
120 : : "isatty($self, /)\n"
121 : : "--\n"
122 : : "\n"
123 : : "Always returns False.\n"
124 : : "\n"
125 : : "BytesIO objects are not connected to a TTY-like device.");
126 : :
127 : : #define _IO_BYTESIO_ISATTY_METHODDEF \
128 : : {"isatty", (PyCFunction)_io_BytesIO_isatty, METH_NOARGS, _io_BytesIO_isatty__doc__},
129 : :
130 : : static PyObject *
131 : : _io_BytesIO_isatty_impl(bytesio *self);
132 : :
133 : : static PyObject *
134 : 0 : _io_BytesIO_isatty(bytesio *self, PyObject *Py_UNUSED(ignored))
135 : : {
136 : 0 : return _io_BytesIO_isatty_impl(self);
137 : : }
138 : :
139 : : PyDoc_STRVAR(_io_BytesIO_tell__doc__,
140 : : "tell($self, /)\n"
141 : : "--\n"
142 : : "\n"
143 : : "Current file position, an integer.");
144 : :
145 : : #define _IO_BYTESIO_TELL_METHODDEF \
146 : : {"tell", (PyCFunction)_io_BytesIO_tell, METH_NOARGS, _io_BytesIO_tell__doc__},
147 : :
148 : : static PyObject *
149 : : _io_BytesIO_tell_impl(bytesio *self);
150 : :
151 : : static PyObject *
152 : 0 : _io_BytesIO_tell(bytesio *self, PyObject *Py_UNUSED(ignored))
153 : : {
154 : 0 : return _io_BytesIO_tell_impl(self);
155 : : }
156 : :
157 : : PyDoc_STRVAR(_io_BytesIO_read__doc__,
158 : : "read($self, size=-1, /)\n"
159 : : "--\n"
160 : : "\n"
161 : : "Read at most size bytes, returned as a bytes object.\n"
162 : : "\n"
163 : : "If the size argument is negative, read until EOF is reached.\n"
164 : : "Return an empty bytes object at EOF.");
165 : :
166 : : #define _IO_BYTESIO_READ_METHODDEF \
167 : : {"read", _PyCFunction_CAST(_io_BytesIO_read), METH_FASTCALL, _io_BytesIO_read__doc__},
168 : :
169 : : static PyObject *
170 : : _io_BytesIO_read_impl(bytesio *self, Py_ssize_t size);
171 : :
172 : : static PyObject *
173 : 0 : _io_BytesIO_read(bytesio *self, PyObject *const *args, Py_ssize_t nargs)
174 : : {
175 : 0 : PyObject *return_value = NULL;
176 : 0 : Py_ssize_t size = -1;
177 : :
178 [ # # # # : 0 : if (!_PyArg_CheckPositional("read", nargs, 0, 1)) {
# # ]
179 : 0 : goto exit;
180 : : }
181 [ # # ]: 0 : if (nargs < 1) {
182 : 0 : goto skip_optional;
183 : : }
184 [ # # ]: 0 : if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
185 : 0 : goto exit;
186 : : }
187 : 0 : skip_optional:
188 : 0 : return_value = _io_BytesIO_read_impl(self, size);
189 : :
190 : 0 : exit:
191 : 0 : return return_value;
192 : : }
193 : :
194 : : PyDoc_STRVAR(_io_BytesIO_read1__doc__,
195 : : "read1($self, size=-1, /)\n"
196 : : "--\n"
197 : : "\n"
198 : : "Read at most size bytes, returned as a bytes object.\n"
199 : : "\n"
200 : : "If the size argument is negative or omitted, read until EOF is reached.\n"
201 : : "Return an empty bytes object at EOF.");
202 : :
203 : : #define _IO_BYTESIO_READ1_METHODDEF \
204 : : {"read1", _PyCFunction_CAST(_io_BytesIO_read1), METH_FASTCALL, _io_BytesIO_read1__doc__},
205 : :
206 : : static PyObject *
207 : : _io_BytesIO_read1_impl(bytesio *self, Py_ssize_t size);
208 : :
209 : : static PyObject *
210 : 0 : _io_BytesIO_read1(bytesio *self, PyObject *const *args, Py_ssize_t nargs)
211 : : {
212 : 0 : PyObject *return_value = NULL;
213 : 0 : Py_ssize_t size = -1;
214 : :
215 [ # # # # : 0 : if (!_PyArg_CheckPositional("read1", nargs, 0, 1)) {
# # ]
216 : 0 : goto exit;
217 : : }
218 [ # # ]: 0 : if (nargs < 1) {
219 : 0 : goto skip_optional;
220 : : }
221 [ # # ]: 0 : if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
222 : 0 : goto exit;
223 : : }
224 : 0 : skip_optional:
225 : 0 : return_value = _io_BytesIO_read1_impl(self, size);
226 : :
227 : 0 : exit:
228 : 0 : return return_value;
229 : : }
230 : :
231 : : PyDoc_STRVAR(_io_BytesIO_readline__doc__,
232 : : "readline($self, size=-1, /)\n"
233 : : "--\n"
234 : : "\n"
235 : : "Next line from the file, as a bytes object.\n"
236 : : "\n"
237 : : "Retain newline. A non-negative size argument limits the maximum\n"
238 : : "number of bytes to return (an incomplete line may be returned then).\n"
239 : : "Return an empty bytes object at EOF.");
240 : :
241 : : #define _IO_BYTESIO_READLINE_METHODDEF \
242 : : {"readline", _PyCFunction_CAST(_io_BytesIO_readline), METH_FASTCALL, _io_BytesIO_readline__doc__},
243 : :
244 : : static PyObject *
245 : : _io_BytesIO_readline_impl(bytesio *self, Py_ssize_t size);
246 : :
247 : : static PyObject *
248 : 0 : _io_BytesIO_readline(bytesio *self, PyObject *const *args, Py_ssize_t nargs)
249 : : {
250 : 0 : PyObject *return_value = NULL;
251 : 0 : Py_ssize_t size = -1;
252 : :
253 [ # # # # : 0 : if (!_PyArg_CheckPositional("readline", nargs, 0, 1)) {
# # ]
254 : 0 : goto exit;
255 : : }
256 [ # # ]: 0 : if (nargs < 1) {
257 : 0 : goto skip_optional;
258 : : }
259 [ # # ]: 0 : if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
260 : 0 : goto exit;
261 : : }
262 : 0 : skip_optional:
263 : 0 : return_value = _io_BytesIO_readline_impl(self, size);
264 : :
265 : 0 : exit:
266 : 0 : return return_value;
267 : : }
268 : :
269 : : PyDoc_STRVAR(_io_BytesIO_readlines__doc__,
270 : : "readlines($self, size=None, /)\n"
271 : : "--\n"
272 : : "\n"
273 : : "List of bytes objects, each a line from the file.\n"
274 : : "\n"
275 : : "Call readline() repeatedly and return a list of the lines so read.\n"
276 : : "The optional size argument, if given, is an approximate bound on the\n"
277 : : "total number of bytes in the lines returned.");
278 : :
279 : : #define _IO_BYTESIO_READLINES_METHODDEF \
280 : : {"readlines", _PyCFunction_CAST(_io_BytesIO_readlines), METH_FASTCALL, _io_BytesIO_readlines__doc__},
281 : :
282 : : static PyObject *
283 : : _io_BytesIO_readlines_impl(bytesio *self, PyObject *arg);
284 : :
285 : : static PyObject *
286 : 0 : _io_BytesIO_readlines(bytesio *self, PyObject *const *args, Py_ssize_t nargs)
287 : : {
288 : 0 : PyObject *return_value = NULL;
289 : 0 : PyObject *arg = Py_None;
290 : :
291 [ # # # # : 0 : if (!_PyArg_CheckPositional("readlines", nargs, 0, 1)) {
# # ]
292 : 0 : goto exit;
293 : : }
294 [ # # ]: 0 : if (nargs < 1) {
295 : 0 : goto skip_optional;
296 : : }
297 : 0 : arg = args[0];
298 : 0 : skip_optional:
299 : 0 : return_value = _io_BytesIO_readlines_impl(self, arg);
300 : :
301 : 0 : exit:
302 : 0 : return return_value;
303 : : }
304 : :
305 : : PyDoc_STRVAR(_io_BytesIO_readinto__doc__,
306 : : "readinto($self, buffer, /)\n"
307 : : "--\n"
308 : : "\n"
309 : : "Read bytes into buffer.\n"
310 : : "\n"
311 : : "Returns number of bytes read (0 for EOF), or None if the object\n"
312 : : "is set not to block and has no data to read.");
313 : :
314 : : #define _IO_BYTESIO_READINTO_METHODDEF \
315 : : {"readinto", (PyCFunction)_io_BytesIO_readinto, METH_O, _io_BytesIO_readinto__doc__},
316 : :
317 : : static PyObject *
318 : : _io_BytesIO_readinto_impl(bytesio *self, Py_buffer *buffer);
319 : :
320 : : static PyObject *
321 : 0 : _io_BytesIO_readinto(bytesio *self, PyObject *arg)
322 : : {
323 : 0 : PyObject *return_value = NULL;
324 : 0 : Py_buffer buffer = {NULL, NULL};
325 : :
326 [ # # ]: 0 : if (PyObject_GetBuffer(arg, &buffer, PyBUF_WRITABLE) < 0) {
327 : 0 : PyErr_Clear();
328 : 0 : _PyArg_BadArgument("readinto", "argument", "read-write bytes-like object", arg);
329 : 0 : goto exit;
330 : : }
331 [ # # ]: 0 : if (!PyBuffer_IsContiguous(&buffer, 'C')) {
332 : 0 : _PyArg_BadArgument("readinto", "argument", "contiguous buffer", arg);
333 : 0 : goto exit;
334 : : }
335 : 0 : return_value = _io_BytesIO_readinto_impl(self, &buffer);
336 : :
337 : 0 : exit:
338 : : /* Cleanup for buffer */
339 [ # # ]: 0 : if (buffer.obj) {
340 : 0 : PyBuffer_Release(&buffer);
341 : : }
342 : :
343 : 0 : 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 *
362 : 0 : _io_BytesIO_truncate(bytesio *self, PyObject *const *args, Py_ssize_t nargs)
363 : : {
364 : 0 : PyObject *return_value = NULL;
365 : 0 : Py_ssize_t size = self->pos;
366 : :
367 [ # # # # : 0 : if (!_PyArg_CheckPositional("truncate", nargs, 0, 1)) {
# # ]
368 : 0 : goto exit;
369 : : }
370 [ # # ]: 0 : if (nargs < 1) {
371 : 0 : goto skip_optional;
372 : : }
373 [ # # ]: 0 : if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
374 : 0 : goto exit;
375 : : }
376 : 0 : skip_optional:
377 : 0 : return_value = _io_BytesIO_truncate_impl(self, size);
378 : :
379 : 0 : exit:
380 : 0 : 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 *
402 : 0 : _io_BytesIO_seek(bytesio *self, PyObject *const *args, Py_ssize_t nargs)
403 : : {
404 : 0 : PyObject *return_value = NULL;
405 : : Py_ssize_t pos;
406 : 0 : int whence = 0;
407 : :
408 [ # # # # : 0 : if (!_PyArg_CheckPositional("seek", nargs, 1, 2)) {
# # ]
409 : 0 : goto exit;
410 : : }
411 : : {
412 : 0 : Py_ssize_t ival = -1;
413 : 0 : PyObject *iobj = _PyNumber_Index(args[0]);
414 [ # # ]: 0 : if (iobj != NULL) {
415 : 0 : ival = PyLong_AsSsize_t(iobj);
416 : 0 : Py_DECREF(iobj);
417 : : }
418 [ # # # # ]: 0 : if (ival == -1 && PyErr_Occurred()) {
419 : 0 : goto exit;
420 : : }
421 : 0 : pos = ival;
422 : : }
423 [ # # ]: 0 : if (nargs < 2) {
424 : 0 : goto skip_optional;
425 : : }
426 : 0 : whence = _PyLong_AsInt(args[1]);
427 [ # # # # ]: 0 : if (whence == -1 && PyErr_Occurred()) {
428 : 0 : goto exit;
429 : : }
430 : 0 : skip_optional:
431 : 0 : return_value = _io_BytesIO_seek_impl(self, pos, whence);
432 : :
433 : 0 : exit:
434 : 0 : 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 *
474 : 0 : _io_BytesIO_close(bytesio *self, PyObject *Py_UNUSED(ignored))
475 : : {
476 : 0 : 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
489 : 0 : _io_BytesIO___init__(PyObject *self, PyObject *args, PyObject *kwargs)
490 : : {
491 : 0 : 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 : 0 : Py_ssize_t nargs = PyTuple_GET_SIZE(args);
520 [ # # ]: 0 : Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
521 : 0 : PyObject *initvalue = NULL;
522 : :
523 [ # # # # : 0 : fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 0, 1, 0, argsbuf);
# # # # ]
524 [ # # ]: 0 : if (!fastargs) {
525 : 0 : goto exit;
526 : : }
527 [ # # ]: 0 : if (!noptargs) {
528 : 0 : goto skip_optional_pos;
529 : : }
530 : 0 : initvalue = fastargs[0];
531 : 0 : skip_optional_pos:
532 : 0 : return_value = _io_BytesIO___init___impl((bytesio *)self, initvalue);
533 : :
534 : 0 : exit:
535 : 0 : return return_value;
536 : : }
537 : : /*[clinic end generated code: output=a44770efbaeb80dd input=a9049054013a1b77]*/
|