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

Skip to content

Commit 1ee1b6f

Browse files
committed
Use identifier API for PyObject_GetAttrString.
1 parent 794d567 commit 1ee1b6f

28 files changed

Lines changed: 499 additions & 357 deletions

Modules/_collectionsmodule.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -767,8 +767,9 @@ static PyObject *
767767
deque_reduce(dequeobject *deque)
768768
{
769769
PyObject *dict, *result, *aslist;
770+
_Py_identifier(__dict__);
770771

771-
dict = PyObject_GetAttrString((PyObject *)deque, "__dict__");
772+
dict = _PyObject_GetAttrId((PyObject *)deque, &PyId___dict__);
772773
if (dict == NULL)
773774
PyErr_Clear();
774775
aslist = PySequence_List((PyObject *)deque);

Modules/_csv.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1317,6 +1317,7 @@ csv_writer(PyObject *module, PyObject *args, PyObject *keyword_args)
13171317
{
13181318
PyObject * output_file, * dialect = NULL;
13191319
WriterObj * self = PyObject_GC_New(WriterObj, &Writer_Type);
1320+
_Py_identifier(write);
13201321

13211322
if (!self)
13221323
return NULL;
@@ -1333,7 +1334,7 @@ csv_writer(PyObject *module, PyObject *args, PyObject *keyword_args)
13331334
Py_DECREF(self);
13341335
return NULL;
13351336
}
1336-
self->writeline = PyObject_GetAttrString(output_file, "write");
1337+
self->writeline = _PyObject_GetAttrId(output_file, &PyId_write);
13371338
if (self->writeline == NULL || !PyCallable_Check(self->writeline)) {
13381339
PyErr_SetString(PyExc_TypeError,
13391340
"argument 1 must have a \"write\" method");

Modules/_datetimemodule.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3077,12 +3077,14 @@ tzinfo_reduce(PyObject *self)
30773077
{
30783078
PyObject *args, *state, *tmp;
30793079
PyObject *getinitargs, *getstate;
3080+
_Py_identifier(__getinitargs__);
3081+
_Py_identifier(__getstate__);
30803082

30813083
tmp = PyTuple_New(0);
30823084
if (tmp == NULL)
30833085
return NULL;
30843086

3085-
getinitargs = PyObject_GetAttrString(self, "__getinitargs__");
3087+
getinitargs = _PyObject_GetAttrId(self, &PyId___getinitargs__);
30863088
if (getinitargs != NULL) {
30873089
args = PyObject_CallObject(getinitargs, tmp);
30883090
Py_DECREF(getinitargs);
@@ -3097,7 +3099,7 @@ tzinfo_reduce(PyObject *self)
30973099
Py_INCREF(args);
30983100
}
30993101

3100-
getstate = PyObject_GetAttrString(self, "__getstate__");
3102+
getstate = _PyObject_GetAttrId(self, &PyId___getstate__);
31013103
if (getstate != NULL) {
31023104
state = PyObject_CallObject(getstate, tmp);
31033105
Py_DECREF(getstate);

Modules/_json.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,7 @@ scanner_init(PyObject *self, PyObject *args, PyObject *kwds)
11261126
PyObject *ctx;
11271127
static char *kwlist[] = {"context", NULL};
11281128
PyScannerObject *s;
1129+
_Py_identifier(strict);
11291130

11301131
assert(PyScanner_Check(self));
11311132
s = (PyScannerObject *)self;

Modules/_pickle.c

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -826,8 +826,9 @@ _Pickler_SetProtocol(PicklerObject *self, PyObject *proto_obj,
826826
static int
827827
_Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
828828
{
829+
_Py_identifier(write);
829830
assert(file != NULL);
830-
self->write = PyObject_GetAttrString(file, "write");
831+
self->write = _PyObject_GetAttrId(file, &PyId_write);
831832
if (self->write == NULL) {
832833
if (PyErr_ExceptionMatches(PyExc_AttributeError))
833834
PyErr_SetString(PyExc_TypeError,
@@ -1173,15 +1174,19 @@ _Unpickler_New(void)
11731174
static int
11741175
_Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
11751176
{
1176-
self->peek = PyObject_GetAttrString(file, "peek");
1177+
_Py_identifier(peek);
1178+
_Py_identifier(read);
1179+
_Py_identifier(readline);
1180+
1181+
self->peek = _PyObject_GetAttrId(file, &PyId_peek);
11771182
if (self->peek == NULL) {
11781183
if (PyErr_ExceptionMatches(PyExc_AttributeError))
11791184
PyErr_Clear();
11801185
else
11811186
return -1;
11821187
}
1183-
self->read = PyObject_GetAttrString(file, "read");
1184-
self->readline = PyObject_GetAttrString(file, "readline");
1188+
self->read = _PyObject_GetAttrId(file, &PyId_read);
1189+
self->readline = _PyObject_GetAttrId(file, &PyId_readline);
11851190
if (self->readline == NULL || self->read == NULL) {
11861191
if (PyErr_ExceptionMatches(PyExc_AttributeError))
11871192
PyErr_SetString(PyExc_TypeError,
@@ -3390,6 +3395,7 @@ Pickler_init(PicklerObject *self, PyObject *args, PyObject *kwds)
33903395
PyObject *file;
33913396
PyObject *proto_obj = NULL;
33923397
PyObject *fix_imports = Py_True;
3398+
_Py_identifier(persistent_id);
33933399

33943400
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OO:Pickler",
33953401
kwlist, &file, &proto_obj, &fix_imports))
@@ -3425,9 +3431,9 @@ Pickler_init(PicklerObject *self, PyObject *args, PyObject *kwds)
34253431
self->fast_nesting = 0;
34263432
self->fast_memo = NULL;
34273433
self->pers_func = NULL;
3428-
if (PyObject_HasAttrString((PyObject *)self, "persistent_id")) {
3429-
self->pers_func = PyObject_GetAttrString((PyObject *)self,
3430-
"persistent_id");
3434+
if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) {
3435+
self->pers_func = _PyObject_GetAttrId((PyObject *)self,
3436+
&PyId_persistent_id);
34313437
if (self->pers_func == NULL)
34323438
return -1;
34333439
}
@@ -4935,8 +4941,9 @@ do_append(UnpicklerObject *self, Py_ssize_t x)
49354941
}
49364942
else {
49374943
PyObject *append_func;
4944+
_Py_identifier(append);
49384945

4939-
append_func = PyObject_GetAttrString(list, "append");
4946+
append_func = _PyObject_GetAttrId(list, &PyId_append);
49404947
if (append_func == NULL)
49414948
return -1;
49424949
for (i = x; i < len; i++) {
@@ -5023,6 +5030,7 @@ load_build(UnpicklerObject *self)
50235030
PyObject *state, *inst, *slotstate;
50245031
PyObject *setstate;
50255032
int status = 0;
5033+
_Py_identifier(__setstate__);
50265034

50275035
/* Stack is ... instance, state. We want to leave instance at
50285036
* the stack top, possibly mutated via instance.__setstate__(state).
@@ -5036,7 +5044,7 @@ load_build(UnpicklerObject *self)
50365044

50375045
inst = self->stack->data[Py_SIZE(self->stack) - 1];
50385046

5039-
setstate = PyObject_GetAttrString(inst, "__setstate__");
5047+
setstate = _PyObject_GetAttrId(inst, &PyId___setstate__);
50405048
if (setstate == NULL) {
50415049
if (PyErr_ExceptionMatches(PyExc_AttributeError))
50425050
PyErr_Clear();
@@ -5079,12 +5087,13 @@ load_build(UnpicklerObject *self)
50795087
PyObject *dict;
50805088
PyObject *d_key, *d_value;
50815089
Py_ssize_t i;
5090+
_Py_identifier(__dict__);
50825091

50835092
if (!PyDict_Check(state)) {
50845093
PyErr_SetString(UnpicklingError, "state is not a dictionary");
50855094
goto error;
50865095
}
5087-
dict = PyObject_GetAttrString(inst, "__dict__");
5096+
dict = _PyObject_GetAttrId(inst, &PyId___dict__);
50885097
if (dict == NULL)
50895098
goto error;
50905099

@@ -5584,8 +5593,9 @@ Unpickler_init(UnpicklerObject *self, PyObject *args, PyObject *kwds)
55845593
return -1;
55855594

55865595
if (PyObject_HasAttrString((PyObject *)self, "persistent_load")) {
5587-
self->pers_func = PyObject_GetAttrString((PyObject *)self,
5588-
"persistent_load");
5596+
_Py_identifier(persistent_load);
5597+
self->pers_func = _PyObject_GetAttrId((PyObject *)self,
5598+
&PyId_persistent_load);
55895599
if (self->pers_func == NULL)
55905600
return -1;
55915601
}

Modules/arraymodule.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2003,14 +2003,16 @@ array_reduce_ex(arrayobject *array, PyObject *value)
20032003
int mformat_code;
20042004
static PyObject *array_reconstructor = NULL;
20052005
long protocol;
2006+
_Py_identifier(_array_reconstructor);
2007+
_Py_identifier(__dict__);
20062008

20072009
if (array_reconstructor == NULL) {
20082010
PyObject *array_module = PyImport_ImportModule("array");
20092011
if (array_module == NULL)
20102012
return NULL;
2011-
array_reconstructor = PyObject_GetAttrString(
2013+
array_reconstructor = _PyObject_GetAttrId(
20122014
array_module,
2013-
"_array_reconstructor");
2015+
&PyId__array_reconstructor);
20142016
Py_DECREF(array_module);
20152017
if (array_reconstructor == NULL)
20162018
return NULL;
@@ -2025,7 +2027,7 @@ array_reduce_ex(arrayobject *array, PyObject *value)
20252027
if (protocol == -1 && PyErr_Occurred())
20262028
return NULL;
20272029

2028-
dict = PyObject_GetAttrString((PyObject *)array, "__dict__");
2030+
dict = _PyObject_GetAttrId((PyObject *)array, &PyId___dict__);
20292031
if (dict == NULL) {
20302032
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
20312033
return NULL;

Modules/parsermodule.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3241,10 +3241,13 @@ PyInit_parser(void)
32413241
copyreg = PyImport_ImportModuleNoBlock("copyreg");
32423242
if (copyreg != NULL) {
32433243
PyObject *func, *pickler;
3244+
_Py_identifier(pickle);
3245+
_Py_identifier(sequence2st);
3246+
_Py_identifier(_pickler);
32443247

3245-
func = PyObject_GetAttrString(copyreg, "pickle");
3246-
pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
3247-
pickler = PyObject_GetAttrString(module, "_pickler");
3248+
func = _PyObject_GetAttrId(copyreg, &PyId_pickle);
3249+
pickle_constructor = _PyObject_GetAttrId(module, &PyId_sequence2st);
3250+
pickler = _PyObject_GetAttrId(module, &PyId__pickler);
32483251
Py_XINCREF(pickle_constructor);
32493252
if ((func != NULL) && (pickle_constructor != NULL)
32503253
&& (pickler != NULL)) {

Modules/posixmodule.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6103,6 +6103,7 @@ wait_helper(pid_t pid, int status, struct rusage *ru)
61036103
{
61046104
PyObject *result;
61056105
static PyObject *struct_rusage;
6106+
_Py_identifier(struct_rusage);
61066107

61076108
if (pid == -1)
61086109
return posix_error();
@@ -6111,7 +6112,7 @@ wait_helper(pid_t pid, int status, struct rusage *ru)
61116112
PyObject *m = PyImport_ImportModuleNoBlock("resource");
61126113
if (m == NULL)
61136114
return NULL;
6114-
struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
6115+
struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage);
61156116
Py_DECREF(m);
61166117
if (struct_rusage == NULL)
61176118
return NULL;

Modules/pyexpat.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -843,9 +843,9 @@ xmlparse_ParseFile(xmlparseobject *self, PyObject *f)
843843
{
844844
int rv = 1;
845845
PyObject *readmethod = NULL;
846+
_Py_identifier(read);
846847

847-
848-
readmethod = PyObject_GetAttrString(f, "read");
848+
readmethod = _PyObject_GetAttrId(f, &PyId_read);
849849
if (readmethod == NULL) {
850850
PyErr_SetString(PyExc_TypeError,
851851
"argument must have 'read' attribute");

Modules/zipimport.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,7 @@ get_decompress_func(void)
908908
static int importing_zlib = 0;
909909
PyObject *zlib;
910910
PyObject *decompress;
911+
_Py_identifier(decompress);
911912

912913
if (importing_zlib != 0)
913914
/* Someone has a zlib.py[co] in their Zip file;
@@ -917,8 +918,8 @@ get_decompress_func(void)
917918
zlib = PyImport_ImportModuleNoBlock("zlib");
918919
importing_zlib = 0;
919920
if (zlib != NULL) {
920-
decompress = PyObject_GetAttrString(zlib,
921-
"decompress");
921+
decompress = _PyObject_GetAttrId(zlib,
922+
&PyId_decompress);
922923
Py_DECREF(zlib);
923924
}
924925
else {

0 commit comments

Comments
 (0)