Skip to content

Commit ed41124

Browse files
gh-101819: Adapt _io._Buffered* methods to Argument Clinic (#104367)
1 parent d0a738c commit ed41124

File tree

2 files changed

+328
-56
lines changed

2 files changed

+328
-56
lines changed

Modules/_io/bufferedio.c

+110-48
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,13 @@ buffered_dealloc(buffered *self)
419419
Py_DECREF(tp);
420420
}
421421

422+
/*[clinic input]
423+
_io._Buffered.__sizeof__
424+
[clinic start generated code]*/
425+
422426
static PyObject *
423-
buffered_sizeof(buffered *self, PyObject *Py_UNUSED(ignored))
427+
_io__Buffered___sizeof___impl(buffered *self)
428+
/*[clinic end generated code: output=0231ef7f5053134e input=753c782d808d34df]*/
424429
{
425430
size_t res = _PyObject_SIZE(Py_TYPE(self));
426431
if (self->buffer) {
@@ -450,8 +455,17 @@ buffered_clear(buffered *self)
450455
/* Because this can call arbitrary code, it shouldn't be called when
451456
the refcount is 0 (that is, not directly from tp_dealloc unless
452457
the refcount has been temporarily re-incremented). */
458+
/*[clinic input]
459+
_io._Buffered._dealloc_warn
460+
461+
source: object
462+
/
463+
464+
[clinic start generated code]*/
465+
453466
static PyObject *
454-
buffered_dealloc_warn(buffered *self, PyObject *source)
467+
_io__Buffered__dealloc_warn(buffered *self, PyObject *source)
468+
/*[clinic end generated code: output=690dcc3df8967162 input=8f845f2a4786391c]*/
455469
{
456470
if (self->ok && self->raw) {
457471
PyObject *r;
@@ -471,9 +485,13 @@ buffered_dealloc_warn(buffered *self, PyObject *source)
471485
*/
472486

473487
/* Flush and close */
488+
/*[clinic input]
489+
_io._Buffered.flush as _io__Buffered_simple_flush
490+
[clinic start generated code]*/
474491

475492
static PyObject *
476-
buffered_simple_flush(buffered *self, PyObject *args)
493+
_io__Buffered_simple_flush_impl(buffered *self)
494+
/*[clinic end generated code: output=29ebb3820db1bdfd input=f33ef045e7250767]*/
477495
{
478496
CHECK_INITIALIZED(self)
479497
return PyObject_CallMethodNoArgs(self->raw, &_Py_ID(flush));
@@ -500,8 +518,13 @@ buffered_closed_get(buffered *self, void *context)
500518
return PyObject_GetAttr(self->raw, &_Py_ID(closed));
501519
}
502520

521+
/*[clinic input]
522+
_io._Buffered.close
523+
[clinic start generated code]*/
524+
503525
static PyObject *
504-
buffered_close(buffered *self, PyObject *args)
526+
_io__Buffered_close_impl(buffered *self)
527+
/*[clinic end generated code: output=7280b7b42033be0c input=d20b83d1ddd7d805]*/
505528
{
506529
PyObject *res = NULL;
507530
int r;
@@ -520,7 +543,7 @@ buffered_close(buffered *self, PyObject *args)
520543
}
521544

522545
if (self->finalizing) {
523-
PyObject *r = buffered_dealloc_warn(self, (PyObject *) self);
546+
PyObject *r = _io__Buffered__dealloc_warn(self, (PyObject *) self);
524547
if (r)
525548
Py_DECREF(r);
526549
else
@@ -560,10 +583,13 @@ buffered_close(buffered *self, PyObject *args)
560583
return res;
561584
}
562585

563-
/* detach */
586+
/*[clinic input]
587+
_io._Buffered.detach
588+
[clinic start generated code]*/
564589

565590
static PyObject *
566-
buffered_detach(buffered *self, PyObject *Py_UNUSED(ignored))
591+
_io__Buffered_detach_impl(buffered *self)
592+
/*[clinic end generated code: output=dd0fc057b8b779f7 input=482762a345cc9f44]*/
567593
{
568594
PyObject *raw, *res;
569595
CHECK_INITIALIZED(self)
@@ -580,22 +606,37 @@ buffered_detach(buffered *self, PyObject *Py_UNUSED(ignored))
580606

581607
/* Inquiries */
582608

609+
/*[clinic input]
610+
_io._Buffered.seekable
611+
[clinic start generated code]*/
612+
583613
static PyObject *
584-
buffered_seekable(buffered *self, PyObject *Py_UNUSED(ignored))
614+
_io__Buffered_seekable_impl(buffered *self)
615+
/*[clinic end generated code: output=90172abb5ceb6e8f input=7d35764f5fb5262b]*/
585616
{
586617
CHECK_INITIALIZED(self)
587618
return PyObject_CallMethodNoArgs(self->raw, &_Py_ID(seekable));
588619
}
589620

621+
/*[clinic input]
622+
_io._Buffered.readable
623+
[clinic start generated code]*/
624+
590625
static PyObject *
591-
buffered_readable(buffered *self, PyObject *Py_UNUSED(ignored))
626+
_io__Buffered_readable_impl(buffered *self)
627+
/*[clinic end generated code: output=92afa07661ecb698 input=640619addb513b8b]*/
592628
{
593629
CHECK_INITIALIZED(self)
594630
return PyObject_CallMethodNoArgs(self->raw, &_Py_ID(readable));
595631
}
596632

633+
/*[clinic input]
634+
_io._Buffered.writable
635+
[clinic start generated code]*/
636+
597637
static PyObject *
598-
buffered_writable(buffered *self, PyObject *Py_UNUSED(ignored))
638+
_io__Buffered_writable_impl(buffered *self)
639+
/*[clinic end generated code: output=4e3eee8d6f9d8552 input=b35ea396b2201554]*/
599640
{
600641
CHECK_INITIALIZED(self)
601642
return PyObject_CallMethodNoArgs(self->raw, &_Py_ID(writable));
@@ -617,15 +658,25 @@ buffered_mode_get(buffered *self, void *context)
617658

618659
/* Lower-level APIs */
619660

661+
/*[clinic input]
662+
_io._Buffered.fileno
663+
[clinic start generated code]*/
664+
620665
static PyObject *
621-
buffered_fileno(buffered *self, PyObject *Py_UNUSED(ignored))
666+
_io__Buffered_fileno_impl(buffered *self)
667+
/*[clinic end generated code: output=b717648d58a95ee3 input=768ea30b3f6314a7]*/
622668
{
623669
CHECK_INITIALIZED(self)
624670
return PyObject_CallMethodNoArgs(self->raw, &_Py_ID(fileno));
625671
}
626672

673+
/*[clinic input]
674+
_io._Buffered.isatty
675+
[clinic start generated code]*/
676+
627677
static PyObject *
628-
buffered_isatty(buffered *self, PyObject *Py_UNUSED(ignored))
678+
_io__Buffered_isatty_impl(buffered *self)
679+
/*[clinic end generated code: output=c20e55caae67baea input=9ea007b11559bee4]*/
629680
{
630681
CHECK_INITIALIZED(self)
631682
return PyObject_CallMethodNoArgs(self->raw, &_Py_ID(isatty));
@@ -830,8 +881,13 @@ buffered_flush_and_rewind_unlocked(buffered *self)
830881
Py_RETURN_NONE;
831882
}
832883

884+
/*[clinic input]
885+
_io._Buffered.flush
886+
[clinic start generated code]*/
887+
833888
static PyObject *
834-
buffered_flush(buffered *self, PyObject *args)
889+
_io__Buffered_flush_impl(buffered *self)
890+
/*[clinic end generated code: output=da2674ef1ce71f3a input=fda63444697c6bf4]*/
835891
{
836892
PyObject *res;
837893

@@ -1205,8 +1261,13 @@ _io__Buffered_readline_impl(buffered *self, Py_ssize_t size)
12051261
}
12061262

12071263

1264+
/*[clinic input]
1265+
_io._Buffered.tell
1266+
[clinic start generated code]*/
1267+
12081268
static PyObject *
1209-
buffered_tell(buffered *self, PyObject *Py_UNUSED(ignored))
1269+
_io__Buffered_tell_impl(buffered *self)
1270+
/*[clinic end generated code: output=386972ae84716c1e input=ad61e04a6b349573]*/
12101271
{
12111272
Py_off_t pos;
12121273

@@ -1318,20 +1379,21 @@ _io__Buffered_seek_impl(buffered *self, PyObject *targetobj, int whence)
13181379

13191380
/*[clinic input]
13201381
_io._Buffered.truncate
1382+
cls: defining_class
13211383
pos: object = None
13221384
/
13231385
[clinic start generated code]*/
13241386

13251387
static PyObject *
1326-
_io__Buffered_truncate_impl(buffered *self, PyObject *pos)
1327-
/*[clinic end generated code: output=667ca03c60c270de input=8a1be34d57cca2d3]*/
1388+
_io__Buffered_truncate_impl(buffered *self, PyTypeObject *cls, PyObject *pos)
1389+
/*[clinic end generated code: output=fe3882fbffe79f1a input=f5b737d97d76303f]*/
13281390
{
13291391
PyObject *res = NULL;
13301392

13311393
CHECK_INITIALIZED(self)
13321394
CHECK_CLOSED(self, "truncate of closed file")
13331395
if (!self->writable) {
1334-
_PyIO_State *state = IO_STATE();
1396+
_PyIO_State *state = get_io_state_by_cls(cls);
13351397
return bufferediobase_unsupported(state, "truncate");
13361398
}
13371399
if (!ENTER_BUFFERED(self))
@@ -2427,14 +2489,14 @@ PyTypeObject PyBufferedIOBase_Type = {
24272489

24282490
static PyMethodDef bufferedreader_methods[] = {
24292491
/* BufferedIOMixin methods */
2430-
{"detach", (PyCFunction)buffered_detach, METH_NOARGS},
2431-
{"flush", (PyCFunction)buffered_simple_flush, METH_NOARGS},
2432-
{"close", (PyCFunction)buffered_close, METH_NOARGS},
2433-
{"seekable", (PyCFunction)buffered_seekable, METH_NOARGS},
2434-
{"readable", (PyCFunction)buffered_readable, METH_NOARGS},
2435-
{"fileno", (PyCFunction)buffered_fileno, METH_NOARGS},
2436-
{"isatty", (PyCFunction)buffered_isatty, METH_NOARGS},
2437-
{"_dealloc_warn", (PyCFunction)buffered_dealloc_warn, METH_O},
2492+
_IO__BUFFERED_DETACH_METHODDEF
2493+
_IO__BUFFERED_SIMPLE_FLUSH_METHODDEF
2494+
_IO__BUFFERED_CLOSE_METHODDEF
2495+
_IO__BUFFERED_SEEKABLE_METHODDEF
2496+
_IO__BUFFERED_READABLE_METHODDEF
2497+
_IO__BUFFERED_FILENO_METHODDEF
2498+
_IO__BUFFERED_ISATTY_METHODDEF
2499+
_IO__BUFFERED__DEALLOC_WARN_METHODDEF
24382500

24392501
_IO__BUFFERED_READ_METHODDEF
24402502
_IO__BUFFERED_PEEK_METHODDEF
@@ -2443,9 +2505,9 @@ static PyMethodDef bufferedreader_methods[] = {
24432505
_IO__BUFFERED_READINTO1_METHODDEF
24442506
_IO__BUFFERED_READLINE_METHODDEF
24452507
_IO__BUFFERED_SEEK_METHODDEF
2446-
{"tell", (PyCFunction)buffered_tell, METH_NOARGS},
2508+
_IO__BUFFERED_TELL_METHODDEF
24472509
_IO__BUFFERED_TRUNCATE_METHODDEF
2448-
{"__sizeof__", (PyCFunction)buffered_sizeof, METH_NOARGS},
2510+
_IO__BUFFERED___SIZEOF___METHODDEF
24492511
{NULL, NULL}
24502512
};
24512513

@@ -2489,20 +2551,20 @@ PyType_Spec bufferedreader_spec = {
24892551

24902552
static PyMethodDef bufferedwriter_methods[] = {
24912553
/* BufferedIOMixin methods */
2492-
{"close", (PyCFunction)buffered_close, METH_NOARGS},
2493-
{"detach", (PyCFunction)buffered_detach, METH_NOARGS},
2494-
{"seekable", (PyCFunction)buffered_seekable, METH_NOARGS},
2495-
{"writable", (PyCFunction)buffered_writable, METH_NOARGS},
2496-
{"fileno", (PyCFunction)buffered_fileno, METH_NOARGS},
2497-
{"isatty", (PyCFunction)buffered_isatty, METH_NOARGS},
2498-
{"_dealloc_warn", (PyCFunction)buffered_dealloc_warn, METH_O},
2554+
_IO__BUFFERED_CLOSE_METHODDEF
2555+
_IO__BUFFERED_DETACH_METHODDEF
2556+
_IO__BUFFERED_SEEKABLE_METHODDEF
2557+
_IO__BUFFERED_WRITABLE_METHODDEF
2558+
_IO__BUFFERED_FILENO_METHODDEF
2559+
_IO__BUFFERED_ISATTY_METHODDEF
2560+
_IO__BUFFERED__DEALLOC_WARN_METHODDEF
24992561

25002562
_IO_BUFFEREDWRITER_WRITE_METHODDEF
25012563
_IO__BUFFERED_TRUNCATE_METHODDEF
2502-
{"flush", (PyCFunction)buffered_flush, METH_NOARGS},
2564+
_IO__BUFFERED_FLUSH_METHODDEF
25032565
_IO__BUFFERED_SEEK_METHODDEF
2504-
{"tell", (PyCFunction)buffered_tell, METH_NOARGS},
2505-
{"__sizeof__", (PyCFunction)buffered_sizeof, METH_NOARGS},
2566+
_IO__BUFFERED_TELL_METHODDEF
2567+
_IO__BUFFERED___SIZEOF___METHODDEF
25062568
{NULL, NULL}
25072569
};
25082570

@@ -2596,19 +2658,19 @@ PyType_Spec bufferedrwpair_spec = {
25962658

25972659
static PyMethodDef bufferedrandom_methods[] = {
25982660
/* BufferedIOMixin methods */
2599-
{"close", (PyCFunction)buffered_close, METH_NOARGS},
2600-
{"detach", (PyCFunction)buffered_detach, METH_NOARGS},
2601-
{"seekable", (PyCFunction)buffered_seekable, METH_NOARGS},
2602-
{"readable", (PyCFunction)buffered_readable, METH_NOARGS},
2603-
{"writable", (PyCFunction)buffered_writable, METH_NOARGS},
2604-
{"fileno", (PyCFunction)buffered_fileno, METH_NOARGS},
2605-
{"isatty", (PyCFunction)buffered_isatty, METH_NOARGS},
2606-
{"_dealloc_warn", (PyCFunction)buffered_dealloc_warn, METH_O},
2661+
_IO__BUFFERED_CLOSE_METHODDEF
2662+
_IO__BUFFERED_DETACH_METHODDEF
2663+
_IO__BUFFERED_SEEKABLE_METHODDEF
2664+
_IO__BUFFERED_READABLE_METHODDEF
2665+
_IO__BUFFERED_WRITABLE_METHODDEF
2666+
_IO__BUFFERED_FILENO_METHODDEF
2667+
_IO__BUFFERED_ISATTY_METHODDEF
2668+
_IO__BUFFERED__DEALLOC_WARN_METHODDEF
26072669

2608-
{"flush", (PyCFunction)buffered_flush, METH_NOARGS},
2670+
_IO__BUFFERED_FLUSH_METHODDEF
26092671

26102672
_IO__BUFFERED_SEEK_METHODDEF
2611-
{"tell", (PyCFunction)buffered_tell, METH_NOARGS},
2673+
_IO__BUFFERED_TELL_METHODDEF
26122674
_IO__BUFFERED_TRUNCATE_METHODDEF
26132675
_IO__BUFFERED_READ_METHODDEF
26142676
_IO__BUFFERED_READ1_METHODDEF
@@ -2617,7 +2679,7 @@ static PyMethodDef bufferedrandom_methods[] = {
26172679
_IO__BUFFERED_READLINE_METHODDEF
26182680
_IO__BUFFERED_PEEK_METHODDEF
26192681
_IO_BUFFEREDWRITER_WRITE_METHODDEF
2620-
{"__sizeof__", (PyCFunction)buffered_sizeof, METH_NOARGS},
2682+
_IO__BUFFERED___SIZEOF___METHODDEF
26212683
{NULL, NULL}
26222684
};
26232685

0 commit comments

Comments
 (0)