Skip to content

Commit 16292fc

Browse files
gh-155496: Use Argument Clinic for more functions of the _io module (GH-155511)
1 parent 0546b5f commit 16292fc

18 files changed

Lines changed: 1227 additions & 251 deletions

‎Lib/test/test_descr.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3405,7 +3405,7 @@ def test_descrdoc(self):
34053405
from _io import FileIO
34063406
def check(descr, what):
34073407
self.assertEqual(descr.__doc__, what)
3408-
check(FileIO.closed, "True if the file is closed") # getset descriptor
3408+
check(FileIO.closed, "True if the file is closed.") # getset descriptor
34093409
check(complex.real, "the real part of a complex number") # member descriptor
34103410

34113411
def test_doc_descriptor(self):

‎Lib/test/test_inspect/test_inspect.py‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6280,11 +6280,7 @@ def test_gc_module_has_signatures(self):
62806280
self._test_module_has_signatures(gc, no_signature)
62816281

62826282
def test_io_module_has_signatures(self):
6283-
methods_no_signature = {
6284-
'BufferedRWPair': {'read', 'peek', 'read1', 'readinto', 'readinto1', 'write'},
6285-
}
6286-
self._test_module_has_signatures(io,
6287-
methods_no_signature=methods_no_signature)
6283+
self._test_module_has_signatures(io)
62886284

62896285
def test_itertools_module_has_signatures(self):
62906286
import itertools

‎Lib/test/test_io/test_textio.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,13 +1458,13 @@ def test_chunk_size(self):
14581458
t._CHUNK_SIZE = 0
14591459
with self.assertRaises(TypeError):
14601460
t._CHUNK_SIZE = 'x'
1461-
with self.assertRaises(ValueError):
1461+
with self.assertRaises(OverflowError):
14621462
t._CHUNK_SIZE = sys.maxsize + 1
1463-
with self.assertRaises(ValueError):
1463+
with self.assertRaises(OverflowError):
14641464
t._CHUNK_SIZE = -sys.maxsize - 2
1465-
with self.assertRaises(ValueError):
1465+
with self.assertRaises(OverflowError):
14661466
t._CHUNK_SIZE = 2**1000
1467-
with self.assertRaises(ValueError):
1467+
with self.assertRaises(OverflowError):
14681468
t._CHUNK_SIZE = -2**1000
14691469
with self.assertRaisesRegex(AttributeError, 'cannot be deleted'):
14701470
del t._CHUNK_SIZE
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:meth:`io.RawIOBase.readinto` and :meth:`io.RawIOBase.write` now raise
2+
:exc:`TypeError` instead of :exc:`NotImplementedError`
3+
if they are called without the required argument.

‎Modules/_io/bufferedio.c‎

Lines changed: 110 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2333,94 +2333,143 @@ bufferedrwpair_dealloc(PyObject *op)
23332333
Py_DECREF(tp);
23342334
}
23352335

2336+
/* Call the method of the underlying reader or writer. The argument is
2337+
only passed if it is not NULL, so that the default of that method is
2338+
used otherwise. */
23362339
static PyObject *
2337-
_forward_call(buffered *self, PyObject *name, PyObject *args)
2340+
_forward_call(buffered *self, PyObject *name, PyObject *arg)
23382341
{
2339-
PyObject *func, *ret;
23402342
if (self == NULL) {
23412343
PyErr_SetString(PyExc_ValueError,
23422344
"I/O operation on uninitialized object");
23432345
return NULL;
23442346
}
23452347

2346-
func = PyObject_GetAttr((PyObject *)self, name);
2347-
if (func == NULL) {
2348-
PyErr_SetObject(PyExc_AttributeError, name);
2349-
return NULL;
2348+
if (arg == NULL) {
2349+
return PyObject_CallMethodNoArgs((PyObject *)self, name);
23502350
}
2351-
2352-
ret = PyObject_CallObject(func, args);
2353-
Py_DECREF(func);
2354-
return ret;
2351+
return PyObject_CallMethodOneArg((PyObject *)self, name, arg);
23552352
}
23562353

2354+
/*[clinic input]
2355+
_io.BufferedRWPair.read
2356+
size: object(c_default="NULL") = -1
2357+
/
2358+
[clinic start generated code]*/
2359+
23572360
static PyObject *
2358-
bufferedrwpair_read(PyObject *op, PyObject *args)
2361+
_io_BufferedRWPair_read_impl(rwpair *self, PyObject *size)
2362+
/*[clinic end generated code: output=0668e3c5dbd3e93d input=eddb5e52aba9ebe5]*/
23592363
{
2360-
rwpair *self = rwpair_CAST(op);
2361-
return _forward_call(self->reader, &_Py_ID(read), args);
2364+
return _forward_call(self->reader, &_Py_ID(read), size);
23622365
}
23632366

2367+
/*[clinic input]
2368+
_io.BufferedRWPair.peek
2369+
size: object(c_default="NULL") = 0
2370+
/
2371+
[clinic start generated code]*/
2372+
23642373
static PyObject *
2365-
bufferedrwpair_peek(PyObject *op, PyObject *args)
2374+
_io_BufferedRWPair_peek_impl(rwpair *self, PyObject *size)
2375+
/*[clinic end generated code: output=190a267bd694efa0 input=36af95964bebe355]*/
23662376
{
2367-
rwpair *self = rwpair_CAST(op);
2368-
return _forward_call(self->reader, &_Py_ID(peek), args);
2377+
return _forward_call(self->reader, &_Py_ID(peek), size);
23692378
}
23702379

2380+
/*[clinic input]
2381+
_io.BufferedRWPair.read1
2382+
size: object(c_default="NULL") = -1
2383+
/
2384+
[clinic start generated code]*/
2385+
23712386
static PyObject *
2372-
bufferedrwpair_read1(PyObject *op, PyObject *args)
2387+
_io_BufferedRWPair_read1_impl(rwpair *self, PyObject *size)
2388+
/*[clinic end generated code: output=17ec19608f2bb825 input=9e94db423e490b58]*/
23732389
{
2374-
rwpair *self = rwpair_CAST(op);
2375-
return _forward_call(self->reader, &_Py_ID(read1), args);
2390+
return _forward_call(self->reader, &_Py_ID(read1), size);
23762391
}
23772392

2393+
/*[clinic input]
2394+
_io.BufferedRWPair.readinto
2395+
buffer: object
2396+
/
2397+
[clinic start generated code]*/
2398+
23782399
static PyObject *
2379-
bufferedrwpair_readinto(PyObject *op, PyObject *args)
2400+
_io_BufferedRWPair_readinto_impl(rwpair *self, PyObject *buffer)
2401+
/*[clinic end generated code: output=16c86b071015f7a4 input=ccd86ce2666261f7]*/
23802402
{
2381-
rwpair *self = rwpair_CAST(op);
2382-
return _forward_call(self->reader, &_Py_ID(readinto), args);
2403+
return _forward_call(self->reader, &_Py_ID(readinto), buffer);
23832404
}
23842405

2406+
/*[clinic input]
2407+
_io.BufferedRWPair.readinto1
2408+
buffer: object
2409+
/
2410+
[clinic start generated code]*/
2411+
23852412
static PyObject *
2386-
bufferedrwpair_readinto1(PyObject *op, PyObject *args)
2413+
_io_BufferedRWPair_readinto1_impl(rwpair *self, PyObject *buffer)
2414+
/*[clinic end generated code: output=f1577b6f54c2b02a input=613d9bf127f88a4a]*/
23872415
{
2388-
rwpair *self = rwpair_CAST(op);
2389-
return _forward_call(self->reader, &_Py_ID(readinto1), args);
2416+
return _forward_call(self->reader, &_Py_ID(readinto1), buffer);
23902417
}
23912418

2419+
/*[clinic input]
2420+
_io.BufferedRWPair.write
2421+
buffer: object
2422+
/
2423+
[clinic start generated code]*/
2424+
23922425
static PyObject *
2393-
bufferedrwpair_write(PyObject *op, PyObject *args)
2426+
_io_BufferedRWPair_write_impl(rwpair *self, PyObject *buffer)
2427+
/*[clinic end generated code: output=6f7509a747410c68 input=66c602422e3ec36f]*/
23942428
{
2395-
rwpair *self = rwpair_CAST(op);
2396-
return _forward_call(self->writer, &_Py_ID(write), args);
2429+
return _forward_call(self->writer, &_Py_ID(write), buffer);
23972430
}
23982431

2432+
/*[clinic input]
2433+
_io.BufferedRWPair.flush
2434+
[clinic start generated code]*/
2435+
23992436
static PyObject *
2400-
bufferedrwpair_flush(PyObject *op, PyObject *Py_UNUSED(dummy))
2437+
_io_BufferedRWPair_flush_impl(rwpair *self)
2438+
/*[clinic end generated code: output=0b2dcbe828718d6b input=e853da796ee61df1]*/
24012439
{
2402-
rwpair *self = rwpair_CAST(op);
24032440
return _forward_call(self->writer, &_Py_ID(flush), NULL);
24042441
}
24052442

2443+
/*[clinic input]
2444+
_io.BufferedRWPair.readable
2445+
[clinic start generated code]*/
2446+
24062447
static PyObject *
2407-
bufferedrwpair_readable(PyObject *op, PyObject *Py_UNUSED(dummy))
2448+
_io_BufferedRWPair_readable_impl(rwpair *self)
2449+
/*[clinic end generated code: output=615967d4aa58f122 input=0475ed73d0a3167f]*/
24082450
{
2409-
rwpair *self = rwpair_CAST(op);
24102451
return _forward_call(self->reader, &_Py_ID(readable), NULL);
24112452
}
24122453

2454+
/*[clinic input]
2455+
_io.BufferedRWPair.writable
2456+
[clinic start generated code]*/
2457+
24132458
static PyObject *
2414-
bufferedrwpair_writable(PyObject *op, PyObject *Py_UNUSED(dummy))
2459+
_io_BufferedRWPair_writable_impl(rwpair *self)
2460+
/*[clinic end generated code: output=c5a43c84e0195c11 input=3cfd44fb4757082f]*/
24152461
{
2416-
rwpair *self = rwpair_CAST(op);
24172462
return _forward_call(self->writer, &_Py_ID(writable), NULL);
24182463
}
24192464

2465+
/*[clinic input]
2466+
_io.BufferedRWPair.close
2467+
[clinic start generated code]*/
2468+
24202469
static PyObject *
2421-
bufferedrwpair_close(PyObject *op, PyObject *Py_UNUSED(dummy))
2470+
_io_BufferedRWPair_close_impl(rwpair *self)
2471+
/*[clinic end generated code: output=5924ba5ecc78752a input=4087d69f2d8fc368]*/
24222472
{
2423-
rwpair *self = rwpair_CAST(op);
24242473
PyObject *exc = NULL;
24252474
PyObject *ret = _forward_call(self->writer, &_Py_ID(close), NULL);
24262475
if (ret == NULL) {
@@ -2437,10 +2486,14 @@ bufferedrwpair_close(PyObject *op, PyObject *Py_UNUSED(dummy))
24372486
return ret;
24382487
}
24392488

2489+
/*[clinic input]
2490+
_io.BufferedRWPair.isatty
2491+
[clinic start generated code]*/
2492+
24402493
static PyObject *
2441-
bufferedrwpair_isatty(PyObject *op, PyObject *Py_UNUSED(dummy))
2494+
_io_BufferedRWPair_isatty_impl(rwpair *self)
2495+
/*[clinic end generated code: output=d017c621ed879cb7 input=92833e3d60586e14]*/
24422496
{
2443-
rwpair *self = rwpair_CAST(op);
24442497
PyObject *ret = _forward_call(self->writer, &_Py_ID(isatty), NULL);
24452498

24462499
if (ret != Py_False) {
@@ -2452,10 +2505,15 @@ bufferedrwpair_isatty(PyObject *op, PyObject *Py_UNUSED(dummy))
24522505
return _forward_call(self->reader, &_Py_ID(isatty), NULL);
24532506
}
24542507

2508+
/*[clinic input]
2509+
@getter
2510+
_io.BufferedRWPair.closed
2511+
[clinic start generated code]*/
2512+
24552513
static PyObject *
2456-
bufferedrwpair_closed_get(PyObject *op, void *Py_UNUSED(dummy))
2514+
_io_BufferedRWPair_closed_get_impl(rwpair *self)
2515+
/*[clinic end generated code: output=4117400c74766f21 input=8248430ac54e5b25]*/
24572516
{
2458-
rwpair *self = rwpair_CAST(op);
24592517
if (self->writer == NULL) {
24602518
PyErr_SetString(PyExc_RuntimeError,
24612519
"the BufferedRWPair object is being garbage-collected");
@@ -2670,20 +2728,20 @@ PyType_Spec _Py_bufferedwriter_spec = {
26702728
};
26712729

26722730
static PyMethodDef bufferedrwpair_methods[] = {
2673-
{"read", bufferedrwpair_read, METH_VARARGS},
2674-
{"peek", bufferedrwpair_peek, METH_VARARGS},
2675-
{"read1", bufferedrwpair_read1, METH_VARARGS},
2676-
{"readinto", bufferedrwpair_readinto, METH_VARARGS},
2677-
{"readinto1", bufferedrwpair_readinto1, METH_VARARGS},
2731+
_IO_BUFFEREDRWPAIR_READ_METHODDEF
2732+
_IO_BUFFEREDRWPAIR_PEEK_METHODDEF
2733+
_IO_BUFFEREDRWPAIR_READ1_METHODDEF
2734+
_IO_BUFFEREDRWPAIR_READINTO_METHODDEF
2735+
_IO_BUFFEREDRWPAIR_READINTO1_METHODDEF
26782736

2679-
{"write", bufferedrwpair_write, METH_VARARGS},
2680-
{"flush", bufferedrwpair_flush, METH_NOARGS},
2737+
_IO_BUFFEREDRWPAIR_WRITE_METHODDEF
2738+
_IO_BUFFEREDRWPAIR_FLUSH_METHODDEF
26812739

2682-
{"readable", bufferedrwpair_readable, METH_NOARGS},
2683-
{"writable", bufferedrwpair_writable, METH_NOARGS},
2740+
_IO_BUFFEREDRWPAIR_READABLE_METHODDEF
2741+
_IO_BUFFEREDRWPAIR_WRITABLE_METHODDEF
26842742

2685-
{"close", bufferedrwpair_close, METH_NOARGS},
2686-
{"isatty", bufferedrwpair_isatty, METH_NOARGS},
2743+
_IO_BUFFEREDRWPAIR_CLOSE_METHODDEF
2744+
_IO_BUFFEREDRWPAIR_ISATTY_METHODDEF
26872745

26882746
{NULL, NULL}
26892747
};
@@ -2695,7 +2753,7 @@ static PyMemberDef bufferedrwpair_members[] = {
26952753
};
26962754

26972755
static PyGetSetDef bufferedrwpair_getset[] = {
2698-
{"closed", bufferedrwpair_closed_get, NULL, NULL},
2756+
_IO_BUFFEREDRWPAIR_CLOSED_GETSETDEF
26992757
{NULL}
27002758
};
27012759

0 commit comments

Comments
 (0)