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