Debian Codegraph @

Modules/_io/clinic/bytesio.c.h

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