Skip to content

Commit 46874c2

Browse files
authored
bpo-39487: Merge duplicated _Py_IDENTIFIER identifiers in C code (GH-18254)
Moving repetitive `_Py_IDENTIFIER` instances to a global location helps identify them more easily in regards to sub-interpreter support.
1 parent c232c91 commit 46874c2

File tree

12 files changed

+24
-34
lines changed

12 files changed

+24
-34
lines changed

Objects/bytesobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Py_ssize_t _Py_null_strings, _Py_one_strings;
2525
static PyBytesObject *characters[UCHAR_MAX + 1];
2626
static PyBytesObject *nullstring;
2727

28+
_Py_IDENTIFIER(__bytes__);
29+
2830
/* PyBytesObject_SIZE gives the basic size of a string; any memory allocation
2931
for a string of length n should request PyBytesObject_SIZE + n bytes.
3032
@@ -543,7 +545,6 @@ static PyObject *
543545
format_obj(PyObject *v, const char **pbuf, Py_ssize_t *plen)
544546
{
545547
PyObject *func, *result;
546-
_Py_IDENTIFIER(__bytes__);
547548
/* is it a bytes object? */
548549
if (PyBytes_Check(v)) {
549550
*pbuf = PyBytes_AS_STRING(v);
@@ -2485,7 +2486,6 @@ bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
24852486
PyObject *func;
24862487
Py_ssize_t size;
24872488
static char *kwlist[] = {"source", "encoding", "errors", 0};
2488-
_Py_IDENTIFIER(__bytes__);
24892489

24902490
if (type != &PyBytes_Type)
24912491
return bytes_subtype_new(type, args, kwds);

Objects/descrobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include "pycore_tupleobject.h"
77
#include "structmember.h" /* Why is this not included in Python.h? */
88

9+
_Py_IDENTIFIER(getattr);
10+
911
/*[clinic input]
1012
class mappingproxy "mappingproxyobject *" "&PyDictProxy_Type"
1113
class property "propertyobject *" "&PyProperty_Type"
@@ -571,7 +573,6 @@ descr_get_qualname(PyDescrObject *descr, void *Py_UNUSED(ignored))
571573
static PyObject *
572574
descr_reduce(PyDescrObject *descr, PyObject *Py_UNUSED(ignored))
573575
{
574-
_Py_IDENTIFIER(getattr);
575576
return Py_BuildValue("N(OO)", _PyEval_GetBuiltinId(&PyId_getattr),
576577
PyDescr_TYPE(descr), PyDescr_NAME(descr));
577578
}
@@ -1240,7 +1241,6 @@ wrapper_repr(wrapperobject *wp)
12401241
static PyObject *
12411242
wrapper_reduce(wrapperobject *wp, PyObject *Py_UNUSED(ignored))
12421243
{
1243-
_Py_IDENTIFIER(getattr);
12441244
return Py_BuildValue("N(OO)", _PyEval_GetBuiltinId(&PyId_getattr),
12451245
wp->self, PyDescr_NAME(wp->descr));
12461246
}

Objects/fileobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@
2525
extern "C" {
2626
#endif
2727

28+
_Py_IDENTIFIER(open);
29+
2830
/* External C interface */
2931

3032
PyObject *
3133
PyFile_FromFd(int fd, const char *name, const char *mode, int buffering, const char *encoding,
3234
const char *errors, const char *newline, int closefd)
3335
{
3436
PyObject *io, *stream;
35-
_Py_IDENTIFIER(open);
3637

3738
/* import _io in case we are being used to open io.py */
3839
io = PyImport_ImportModule("_io");
@@ -547,7 +548,6 @@ PyObject *
547548
PyFile_OpenCodeObject(PyObject *path)
548549
{
549550
PyObject *iomod, *f = NULL;
550-
_Py_IDENTIFIER(open);
551551

552552
if (!PyUnicode_Check(path)) {
553553
PyErr_Format(PyExc_TypeError, "'path' must be 'str', not '%.200s'",

Objects/iterobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ typedef struct {
1111
PyObject *it_seq; /* Set to NULL when iterator is exhausted */
1212
} seqiterobject;
1313

14+
_Py_IDENTIFIER(iter);
15+
1416
PyObject *
1517
PySeqIter_New(PyObject *seq)
1618
{
@@ -104,7 +106,6 @@ PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(
104106
static PyObject *
105107
iter_reduce(seqiterobject *it, PyObject *Py_UNUSED(ignored))
106108
{
107-
_Py_IDENTIFIER(iter);
108109
if (it->it_seq != NULL)
109110
return Py_BuildValue("N(O)n", _PyEval_GetBuiltinId(&PyId_iter),
110111
it->it_seq, it->it_index);
@@ -244,7 +245,6 @@ calliter_iternext(calliterobject *it)
244245
static PyObject *
245246
calliter_reduce(calliterobject *it, PyObject *Py_UNUSED(ignored))
246247
{
247-
_Py_IDENTIFIER(iter);
248248
if (it->it_callable != NULL && it->it_sentinel != NULL)
249249
return Py_BuildValue("N(OO)", _PyEval_GetBuiltinId(&PyId_iter),
250250
it->it_callable, it->it_sentinel);

Objects/moduleobject.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
static Py_ssize_t max_module_number;
99

10+
_Py_IDENTIFIER(__doc__);
11+
_Py_IDENTIFIER(__name__);
12+
_Py_IDENTIFIER(__spec__);
13+
1014
typedef struct {
1115
PyObject_HEAD
1216
PyObject *md_dict;
@@ -58,11 +62,8 @@ static int
5862
module_init_dict(PyModuleObject *mod, PyObject *md_dict,
5963
PyObject *name, PyObject *doc)
6064
{
61-
_Py_IDENTIFIER(__name__);
62-
_Py_IDENTIFIER(__doc__);
6365
_Py_IDENTIFIER(__package__);
6466
_Py_IDENTIFIER(__loader__);
65-
_Py_IDENTIFIER(__spec__);
6667

6768
if (md_dict == NULL)
6869
return -1;
@@ -461,7 +462,6 @@ int
461462
PyModule_SetDocString(PyObject *m, const char *doc)
462463
{
463464
PyObject *v;
464-
_Py_IDENTIFIER(__doc__);
465465

466466
v = PyUnicode_FromString(doc);
467467
if (v == NULL || _PyObject_SetAttrId(m, &PyId___doc__, v) != 0) {
@@ -488,7 +488,6 @@ PyModule_GetDict(PyObject *m)
488488
PyObject*
489489
PyModule_GetNameObject(PyObject *m)
490490
{
491-
_Py_IDENTIFIER(__name__);
492491
PyObject *d;
493492
PyObject *name;
494493
if (!PyModule_Check(m)) {
@@ -741,10 +740,8 @@ module_getattro(PyModuleObject *m, PyObject *name)
741740
if (getattr) {
742741
return _PyObject_CallOneArg(getattr, name);
743742
}
744-
_Py_IDENTIFIER(__name__);
745743
mod_name = _PyDict_GetItemId(m->md_dict, &PyId___name__);
746744
if (mod_name && PyUnicode_Check(mod_name)) {
747-
_Py_IDENTIFIER(__spec__);
748745
Py_INCREF(mod_name);
749746
PyObject *spec = _PyDict_GetItemId(m->md_dict, &PyId___spec__);
750747
Py_XINCREF(spec);

Objects/odictobject.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,8 @@ struct _odictnode {
526526
#define _odict_FOREACH(od, node) \
527527
for (node = _odict_FIRST(od); node != NULL; node = _odictnode_NEXT(node))
528528

529+
_Py_IDENTIFIER(items);
530+
529531
/* Return the index into the hash table, regardless of a valid node. */
530532
static Py_ssize_t
531533
_odict_get_index_raw(PyODictObject *od, PyObject *key, Py_hash_t hash)
@@ -896,7 +898,6 @@ static PyObject *
896898
odict_reduce(register PyODictObject *od, PyObject *Py_UNUSED(ignored))
897899
{
898900
_Py_IDENTIFIER(__dict__);
899-
_Py_IDENTIFIER(items);
900901
PyObject *dict = NULL, *result = NULL;
901902
PyObject *items_iter, *items, *args = NULL;
902903

@@ -1375,7 +1376,6 @@ static PyObject *
13751376
odict_repr(PyODictObject *self)
13761377
{
13771378
int i;
1378-
_Py_IDENTIFIER(items);
13791379
PyObject *pieces = NULL, *result = NULL;
13801380

13811381
if (PyODict_SIZE(self) == 0)
@@ -2195,7 +2195,6 @@ mutablemapping_update(PyObject *self, PyObject *args, PyObject *kwargs)
21952195
{
21962196
int res = 0;
21972197
Py_ssize_t len;
2198-
_Py_IDENTIFIER(items);
21992198
_Py_IDENTIFIER(keys);
22002199

22012200
/* first handle args, if any */

Objects/rangeobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ typedef struct {
1818
PyObject *length;
1919
} rangeobject;
2020

21+
_Py_IDENTIFIER(iter);
22+
2123
/* Helper function for validating step. Always returns a new reference or
2224
NULL on error.
2325
*/
@@ -757,7 +759,6 @@ PyDoc_STRVAR(length_hint_doc,
757759
static PyObject *
758760
rangeiter_reduce(rangeiterobject *r, PyObject *Py_UNUSED(ignored))
759761
{
760-
_Py_IDENTIFIER(iter);
761762
PyObject *start=NULL, *stop=NULL, *step=NULL;
762763
PyObject *range;
763764

@@ -915,7 +916,6 @@ longrangeiter_len(longrangeiterobject *r, PyObject *no_args)
915916
static PyObject *
916917
longrangeiter_reduce(longrangeiterobject *r, PyObject *Py_UNUSED(ignored))
917918
{
918-
_Py_IDENTIFIER(iter);
919919
PyObject *product, *stop=NULL;
920920
PyObject *range;
921921

Objects/typeobject.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ _Py_IDENTIFIER(__new__);
7474
_Py_IDENTIFIER(__set_name__);
7575
_Py_IDENTIFIER(__setitem__);
7676
_Py_IDENTIFIER(builtins);
77+
_Py_IDENTIFIER(mro);
7778

7879
static PyObject *
7980
slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
@@ -310,7 +311,6 @@ type_mro_modified(PyTypeObject *type, PyObject *bases) {
310311
return;
311312

312313
if (custom) {
313-
_Py_IDENTIFIER(mro);
314314
mro_meth = lookup_maybe_method(
315315
(PyObject *)type, &PyId_mro, &unbound);
316316
if (mro_meth == NULL)
@@ -1891,7 +1891,6 @@ mro_invoke(PyTypeObject *type)
18911891
int custom = (Py_TYPE(type) != &PyType_Type);
18921892

18931893
if (custom) {
1894-
_Py_IDENTIFIER(mro);
18951894
int unbound;
18961895
PyObject *mro_meth = lookup_method((PyObject *)type, &PyId_mro,
18971896
&unbound);

Python/_warnings.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ typedef struct _warnings_runtime_state WarningsState;
2424
/* Forward declaration of the _warnings module definition. */
2525
static struct PyModuleDef warningsmodule;
2626

27+
_Py_IDENTIFIER(__name__);
28+
2729
/* Given a module object, get its per-module state. */
2830
static WarningsState *
2931
_Warnings_GetState()
@@ -484,7 +486,6 @@ show_warning(PyObject *filename, int lineno, PyObject *text,
484486
PyObject *f_stderr;
485487
PyObject *name;
486488
char lineno_str[128];
487-
_Py_IDENTIFIER(__name__);
488489

489490
PyOS_snprintf(lineno_str, sizeof(lineno_str), ":%d: ", lineno);
490491

@@ -818,7 +819,6 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
818819
PyObject **module, PyObject **registry)
819820
{
820821
_Py_IDENTIFIER(__warningregistry__);
821-
_Py_IDENTIFIER(__name__);
822822
PyObject *globals;
823823

824824
/* Setup globals, filename and lineno. */
@@ -969,7 +969,6 @@ get_source_line(PyObject *module_globals, int lineno)
969969
{
970970
_Py_IDENTIFIER(get_source);
971971
_Py_IDENTIFIER(__loader__);
972-
_Py_IDENTIFIER(__name__);
973972
PyObject *loader;
974973
PyObject *module_name;
975974
PyObject *get_source;

Python/ceval.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
# error "ceval.c must be build with Py_BUILD_CORE define for best performance"
4040
#endif
4141

42+
_Py_IDENTIFIER(__name__);
4243

4344
/* Forward declarations */
4445
Py_LOCAL_INLINE(PyObject *) call_function(
@@ -5032,7 +5033,6 @@ static PyObject *
50325033
import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
50335034
{
50345035
PyObject *x;
5035-
_Py_IDENTIFIER(__name__);
50365036
PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
50375037

50385038
if (_PyObject_LookupAttr(v, name, &x) != 0) {
@@ -5108,7 +5108,6 @@ import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
51085108
{
51095109
_Py_IDENTIFIER(__all__);
51105110
_Py_IDENTIFIER(__dict__);
5111-
_Py_IDENTIFIER(__name__);
51125111
PyObject *all, *dict, *name, *value;
51135112
int skip_leading_underscores = 0;
51145113
int pos, err;

0 commit comments

Comments
 (0)