Skip to content

Commit de988ef

Browse files
Use the C API for calling a method
PyObject_CallMethodNoArgs() and PyObject_CallMethodOneArg() look up the method and call it in one step, without creating a bound method object.
1 parent 7e279ce commit de988ef

1 file changed

Lines changed: 20 additions & 32 deletions

File tree

Modules/_io/bufferedio.c

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2333,34 +2333,22 @@ 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 *const *args,
2338-
Py_ssize_t nargs)
2340+
_forward_call(buffered *self, PyObject *name, PyObject *arg)
23392341
{
2340-
PyObject *func, *ret;
23412342
if (self == NULL) {
23422343
PyErr_SetString(PyExc_ValueError,
23432344
"I/O operation on uninitialized object");
23442345
return NULL;
23452346
}
23462347

2347-
func = PyObject_GetAttr((PyObject *)self, name);
2348-
if (func == NULL) {
2349-
PyErr_SetObject(PyExc_AttributeError, name);
2350-
return NULL;
2348+
if (arg == NULL) {
2349+
return PyObject_CallMethodNoArgs((PyObject *)self, name);
23512350
}
2352-
2353-
ret = PyObject_Vectorcall(func, args, nargs, NULL);
2354-
Py_DECREF(func);
2355-
return ret;
2356-
}
2357-
2358-
/* The optional argument is only passed if it is specified, so that the
2359-
default of the underlying method is used otherwise. */
2360-
static PyObject *
2361-
_forward_call_opt(buffered *self, PyObject *name, PyObject *arg)
2362-
{
2363-
return _forward_call(self, name, &arg, arg != NULL);
2351+
return PyObject_CallMethodOneArg((PyObject *)self, name, arg);
23642352
}
23652353

23662354
/*[clinic input]
@@ -2373,7 +2361,7 @@ static PyObject *
23732361
_io_BufferedRWPair_read_impl(rwpair *self, PyObject *size)
23742362
/*[clinic end generated code: output=0668e3c5dbd3e93d input=eddb5e52aba9ebe5]*/
23752363
{
2376-
return _forward_call_opt(self->reader, &_Py_ID(read), size);
2364+
return _forward_call(self->reader, &_Py_ID(read), size);
23772365
}
23782366

23792367
/*[clinic input]
@@ -2386,7 +2374,7 @@ static PyObject *
23862374
_io_BufferedRWPair_peek_impl(rwpair *self, PyObject *size)
23872375
/*[clinic end generated code: output=190a267bd694efa0 input=36af95964bebe355]*/
23882376
{
2389-
return _forward_call_opt(self->reader, &_Py_ID(peek), size);
2377+
return _forward_call(self->reader, &_Py_ID(peek), size);
23902378
}
23912379

23922380
/*[clinic input]
@@ -2399,7 +2387,7 @@ static PyObject *
23992387
_io_BufferedRWPair_read1_impl(rwpair *self, PyObject *size)
24002388
/*[clinic end generated code: output=17ec19608f2bb825 input=9e94db423e490b58]*/
24012389
{
2402-
return _forward_call_opt(self->reader, &_Py_ID(read1), size);
2390+
return _forward_call(self->reader, &_Py_ID(read1), size);
24032391
}
24042392

24052393
/*[clinic input]
@@ -2412,7 +2400,7 @@ static PyObject *
24122400
_io_BufferedRWPair_readinto_impl(rwpair *self, PyObject *buffer)
24132401
/*[clinic end generated code: output=16c86b071015f7a4 input=ccd86ce2666261f7]*/
24142402
{
2415-
return _forward_call(self->reader, &_Py_ID(readinto), &buffer, 1);
2403+
return _forward_call(self->reader, &_Py_ID(readinto), buffer);
24162404
}
24172405

24182406
/*[clinic input]
@@ -2425,7 +2413,7 @@ static PyObject *
24252413
_io_BufferedRWPair_readinto1_impl(rwpair *self, PyObject *buffer)
24262414
/*[clinic end generated code: output=f1577b6f54c2b02a input=613d9bf127f88a4a]*/
24272415
{
2428-
return _forward_call(self->reader, &_Py_ID(readinto1), &buffer, 1);
2416+
return _forward_call(self->reader, &_Py_ID(readinto1), buffer);
24292417
}
24302418

24312419
/*[clinic input]
@@ -2438,7 +2426,7 @@ static PyObject *
24382426
_io_BufferedRWPair_write_impl(rwpair *self, PyObject *buffer)
24392427
/*[clinic end generated code: output=6f7509a747410c68 input=66c602422e3ec36f]*/
24402428
{
2441-
return _forward_call(self->writer, &_Py_ID(write), &buffer, 1);
2429+
return _forward_call(self->writer, &_Py_ID(write), buffer);
24422430
}
24432431

24442432
/*[clinic input]
@@ -2449,7 +2437,7 @@ static PyObject *
24492437
_io_BufferedRWPair_flush_impl(rwpair *self)
24502438
/*[clinic end generated code: output=0b2dcbe828718d6b input=e853da796ee61df1]*/
24512439
{
2452-
return _forward_call(self->writer, &_Py_ID(flush), NULL, 0);
2440+
return _forward_call(self->writer, &_Py_ID(flush), NULL);
24532441
}
24542442

24552443
/*[clinic input]
@@ -2460,7 +2448,7 @@ static PyObject *
24602448
_io_BufferedRWPair_readable_impl(rwpair *self)
24612449
/*[clinic end generated code: output=615967d4aa58f122 input=0475ed73d0a3167f]*/
24622450
{
2463-
return _forward_call(self->reader, &_Py_ID(readable), NULL, 0);
2451+
return _forward_call(self->reader, &_Py_ID(readable), NULL);
24642452
}
24652453

24662454
/*[clinic input]
@@ -2471,7 +2459,7 @@ static PyObject *
24712459
_io_BufferedRWPair_writable_impl(rwpair *self)
24722460
/*[clinic end generated code: output=c5a43c84e0195c11 input=3cfd44fb4757082f]*/
24732461
{
2474-
return _forward_call(self->writer, &_Py_ID(writable), NULL, 0);
2462+
return _forward_call(self->writer, &_Py_ID(writable), NULL);
24752463
}
24762464

24772465
/*[clinic input]
@@ -2483,14 +2471,14 @@ _io_BufferedRWPair_close_impl(rwpair *self)
24832471
/*[clinic end generated code: output=5924ba5ecc78752a input=4087d69f2d8fc368]*/
24842472
{
24852473
PyObject *exc = NULL;
2486-
PyObject *ret = _forward_call(self->writer, &_Py_ID(close), NULL, 0);
2474+
PyObject *ret = _forward_call(self->writer, &_Py_ID(close), NULL);
24872475
if (ret == NULL) {
24882476
exc = PyErr_GetRaisedException();
24892477
}
24902478
else {
24912479
Py_DECREF(ret);
24922480
}
2493-
ret = _forward_call(self->reader, &_Py_ID(close), NULL, 0);
2481+
ret = _forward_call(self->reader, &_Py_ID(close), NULL);
24942482
if (exc != NULL) {
24952483
_PyErr_ChainExceptions1(exc);
24962484
Py_CLEAR(ret);
@@ -2506,15 +2494,15 @@ static PyObject *
25062494
_io_BufferedRWPair_isatty_impl(rwpair *self)
25072495
/*[clinic end generated code: output=d017c621ed879cb7 input=92833e3d60586e14]*/
25082496
{
2509-
PyObject *ret = _forward_call(self->writer, &_Py_ID(isatty), NULL, 0);
2497+
PyObject *ret = _forward_call(self->writer, &_Py_ID(isatty), NULL);
25102498

25112499
if (ret != Py_False) {
25122500
/* either True or exception */
25132501
return ret;
25142502
}
25152503
Py_DECREF(ret);
25162504

2517-
return _forward_call(self->reader, &_Py_ID(isatty), NULL, 0);
2505+
return _forward_call(self->reader, &_Py_ID(isatty), NULL);
25182506
}
25192507

25202508
static PyObject *

0 commit comments

Comments
 (0)