Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 767046a

Browse files
committed
Replace {Get,Set,Has}AttrString with *AttrId.
1 parent 7903913 commit 767046a

5 files changed

Lines changed: 25 additions & 14 deletions

File tree

Modules/_io/_iomodule.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ io_open(PyObject *self, PyObject *args, PyObject *kwds)
233233

234234
_Py_IDENTIFIER(isatty);
235235
_Py_IDENTIFIER(fileno);
236+
_Py_IDENTIFIER(mode);
236237

237238
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|sizzziO:open", kwlist,
238239
&file, &mode, &buffering,
@@ -440,7 +441,7 @@ io_open(PyObject *self, PyObject *args, PyObject *kwds)
440441
if (wrapper == NULL)
441442
goto error;
442443

443-
if (PyObject_SetAttrString(wrapper, "mode", modeobj) < 0)
444+
if (_PyObject_SetAttrId(wrapper, &PyId_mode, modeobj) < 0)
444445
goto error;
445446
Py_DECREF(modeobj);
446447
return wrapper;

Modules/_io/bufferedio.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ _Py_IDENTIFIER(close);
1717
_Py_IDENTIFIER(_dealloc_warn);
1818
_Py_IDENTIFIER(flush);
1919
_Py_IDENTIFIER(isatty);
20+
_Py_IDENTIFIER(mode);
21+
_Py_IDENTIFIER(name);
2022
_Py_IDENTIFIER(peek);
2123
_Py_IDENTIFIER(read);
2224
_Py_IDENTIFIER(read1);
@@ -556,14 +558,14 @@ static PyObject *
556558
buffered_name_get(buffered *self, void *context)
557559
{
558560
CHECK_INITIALIZED(self)
559-
return PyObject_GetAttrString(self->raw, "name");
561+
return _PyObject_GetAttrId(self->raw, &PyId_name);
560562
}
561563

562564
static PyObject *
563565
buffered_mode_get(buffered *self, void *context)
564566
{
565567
CHECK_INITIALIZED(self)
566-
return PyObject_GetAttrString(self->raw, "mode");
568+
return _PyObject_GetAttrId(self->raw, &PyId_mode);
567569
}
568570

569571
/* Lower-level APIs */
@@ -1301,7 +1303,7 @@ buffered_repr(buffered *self)
13011303
{
13021304
PyObject *nameobj, *res;
13031305

1304-
nameobj = PyObject_GetAttrString((PyObject *) self, "name");
1306+
nameobj = _PyObject_GetAttrId((PyObject *) self, &PyId_name);
13051307
if (nameobj == NULL) {
13061308
if (PyErr_ExceptionMatches(PyExc_AttributeError))
13071309
PyErr_Clear();

Modules/_io/fileio.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -993,12 +993,13 @@ mode_string(fileio *self)
993993
static PyObject *
994994
fileio_repr(fileio *self)
995995
{
996+
_Py_IDENTIFIER(name);
996997
PyObject *nameobj, *res;
997998

998999
if (self->fd < 0)
9991000
return PyUnicode_FromFormat("<_io.FileIO [closed]>");
10001001

1001-
nameobj = PyObject_GetAttrString((PyObject *) self, "name");
1002+
nameobj = _PyObject_GetAttrId((PyObject *) self, &PyId_name);
10021003
if (nameobj == NULL) {
10031004
if (PyErr_ExceptionMatches(PyExc_AttributeError))
10041005
PyErr_Clear();

Modules/_io/iobase.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ PyDoc_STRVAR(iobase_doc,
5959
of the IOBase object rather than the virtual `closed` attribute as returned
6060
by whatever subclass. */
6161

62+
_Py_IDENTIFIER(__IOBase_closed);
6263
#define IS_CLOSED(self) \
63-
PyObject_HasAttrString(self, "__IOBase_closed")
64+
_PyObject_HasAttrId(self, &PyId___IOBase_closed)
6465

6566
/* Internal methods */
6667
static PyObject *
@@ -192,12 +193,13 @@ static PyObject *
192193
iobase_close(PyObject *self, PyObject *args)
193194
{
194195
PyObject *res;
196+
_Py_IDENTIFIER(__IOBase_closed);
195197

196198
if (IS_CLOSED(self))
197199
Py_RETURN_NONE;
198200

199201
res = PyObject_CallMethodObjArgs(self, _PyIO_str_flush, NULL);
200-
PyObject_SetAttrString(self, "__IOBase_closed", Py_True);
202+
_PyObject_SetAttrId(self, &PyId___IOBase_closed, Py_True);
201203
if (res == NULL) {
202204
return NULL;
203205
}
@@ -467,12 +469,13 @@ iobase_readline(PyObject *self, PyObject *args)
467469
PyObject *buffer, *result;
468470
Py_ssize_t old_size = -1;
469471
_Py_IDENTIFIER(read);
472+
_Py_IDENTIFIER(peek);
470473

471474
if (!PyArg_ParseTuple(args, "|O&:readline", &_PyIO_ConvertSsize_t, &limit)) {
472475
return NULL;
473476
}
474477

475-
if (PyObject_HasAttrString(self, "peek"))
478+
if (_PyObject_HasAttrId(self, &PyId_peek))
476479
has_peek = 1;
477480

478481
buffer = PyByteArray_FromStringAndSize(NULL, 0);

Modules/_io/textio.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ _Py_IDENTIFIER(fileno);
1919
_Py_IDENTIFIER(flush);
2020
_Py_IDENTIFIER(getpreferredencoding);
2121
_Py_IDENTIFIER(isatty);
22+
_Py_IDENTIFIER(mode);
23+
_Py_IDENTIFIER(name);
24+
_Py_IDENTIFIER(raw);
2225
_Py_IDENTIFIER(read);
26+
_Py_IDENTIFIER(read1);
2327
_Py_IDENTIFIER(readable);
2428
_Py_IDENTIFIER(replace);
2529
_Py_IDENTIFIER(reset);
@@ -999,7 +1003,7 @@ textiowrapper_init(textio *self, PyObject *args, PyObject *kwds)
9991003
ci = _PyCodec_Lookup(encoding);
10001004
if (ci == NULL)
10011005
goto error;
1002-
res = PyObject_GetAttrString(ci, "name");
1006+
res = _PyObject_GetAttrId(ci, &PyId_name);
10031007
Py_DECREF(ci);
10041008
if (res == NULL) {
10051009
if (PyErr_ExceptionMatches(PyExc_AttributeError))
@@ -1026,7 +1030,7 @@ textiowrapper_init(textio *self, PyObject *args, PyObject *kwds)
10261030
if (Py_TYPE(buffer) == &PyBufferedReader_Type ||
10271031
Py_TYPE(buffer) == &PyBufferedWriter_Type ||
10281032
Py_TYPE(buffer) == &PyBufferedRandom_Type) {
1029-
raw = PyObject_GetAttrString(buffer, "raw");
1033+
raw = _PyObject_GetAttrId(buffer, &PyId_raw);
10301034
/* Cache the raw FileIO object to speed up 'closed' checks */
10311035
if (raw == NULL) {
10321036
if (PyErr_ExceptionMatches(PyExc_AttributeError))
@@ -1046,7 +1050,7 @@ textiowrapper_init(textio *self, PyObject *args, PyObject *kwds)
10461050
self->seekable = self->telling = PyObject_IsTrue(res);
10471051
Py_DECREF(res);
10481052

1049-
self->has_read1 = PyObject_HasAttrString(buffer, "read1");
1053+
self->has_read1 = _PyObject_HasAttrId(buffer, &PyId_read1);
10501054

10511055
self->encoding_start_of_stream = 0;
10521056
if (self->seekable && self->encoder) {
@@ -2401,7 +2405,7 @@ textiowrapper_repr(textio *self)
24012405
res = PyUnicode_FromString("<_io.TextIOWrapper");
24022406
if (res == NULL)
24032407
return NULL;
2404-
nameobj = PyObject_GetAttrString((PyObject *) self, "name");
2408+
nameobj = _PyObject_GetAttrId((PyObject *) self, &PyId_name);
24052409
if (nameobj == NULL) {
24062410
if (PyErr_ExceptionMatches(PyExc_AttributeError))
24072411
PyErr_Clear();
@@ -2417,7 +2421,7 @@ textiowrapper_repr(textio *self)
24172421
if (res == NULL)
24182422
return NULL;
24192423
}
2420-
modeobj = PyObject_GetAttrString((PyObject *) self, "mode");
2424+
modeobj = _PyObject_GetAttrId((PyObject *) self, &PyId_mode);
24212425
if (modeobj == NULL) {
24222426
if (PyErr_ExceptionMatches(PyExc_AttributeError))
24232427
PyErr_Clear();
@@ -2578,7 +2582,7 @@ static PyObject *
25782582
textiowrapper_name_get(textio *self, void *context)
25792583
{
25802584
CHECK_INITIALIZED(self);
2581-
return PyObject_GetAttrString(self->buffer, "name");
2585+
return _PyObject_GetAttrId(self->buffer, &PyId_name);
25822586
}
25832587

25842588
static PyObject *

0 commit comments

Comments
 (0)