Skip to content

Commit bd303c1

Browse files
committed
Issue python#19512, python#19515: remove shared identifiers, move identifiers where they
are used. Move also _Py_IDENTIFIER() defintions to the top in modified files to remove identifiers duplicated in the same file.
1 parent 07e9e38 commit bd303c1

17 files changed

Lines changed: 93 additions & 89 deletions

Include/object.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,6 @@ typedef struct _Py_Identifier {
147147
#define _Py_static_string(varname, value) static _Py_Identifier varname = _Py_static_string_init(value)
148148
#define _Py_IDENTIFIER(varname) _Py_static_string(PyId_##varname, #varname)
149149

150-
/* Common identifiers (ex: _PyId_path is the string "path") */
151-
PyAPI_DATA(_Py_Identifier) _PyId_argv;
152-
PyAPI_DATA(_Py_Identifier) _PyId_builtins;
153-
PyAPI_DATA(_Py_Identifier) _PyId_path;
154-
PyAPI_DATA(_Py_Identifier) _PyId_stdin;
155-
PyAPI_DATA(_Py_Identifier) _PyId_stdout;
156-
PyAPI_DATA(_Py_Identifier) _PyId_stderr;
157-
158150
/*
159151
Type objects contain a string containing the type name (to help somewhat
160152
in debugging), the allocation parameters (see PyObject_New() and

Modules/_ctypes/callbacks.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ static void
8080
PrintError(char *msg, ...)
8181
{
8282
char buf[512];
83-
PyObject *f = _PySys_GetObjectId(&_PyId_stderr);
83+
PyObject *f = PySys_GetObject("stderr");
8484
va_list marker;
8585

8686
va_start(marker, msg);

Modules/_cursesmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2578,7 +2578,7 @@ PyCurses_setupterm(PyObject* self, PyObject *args, PyObject* keywds)
25782578
if (fd == -1) {
25792579
PyObject* sys_stdout;
25802580

2581-
sys_stdout = _PySys_GetObjectId(&_PyId_stdout);
2581+
sys_stdout = PySys_GetObject("stdout");
25822582

25832583
if (sys_stdout == NULL || sys_stdout == Py_None) {
25842584
PyErr_SetString(

Modules/_lsprof.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ normalizeUserObj(PyObject *obj)
185185
}
186186
}
187187
if (modname != NULL) {
188-
if (_PyUnicode_CompareWithId(modname, &_PyId_builtins) != 0) {
188+
if (PyUnicode_CompareWithASCIIString(modname, "builtins") != 0) {
189189
PyObject *result;
190190
result = PyUnicode_FromFormat("<%U.%s>", modname,
191191
fn->m_ml->ml_name);

Modules/_threadmodule.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ static PyObject *ThreadError;
1717
static long nb_threads = 0;
1818
static PyObject *str_dict;
1919

20+
_Py_IDENTIFIER(stderr);
21+
2022
/* Lock objects */
2123

2224
typedef struct {
@@ -1005,7 +1007,7 @@ t_bootstrap(void *boot_raw)
10051007
PySys_WriteStderr(
10061008
"Unhandled exception in thread started by ");
10071009
PyErr_Fetch(&exc, &value, &tb);
1008-
file = _PySys_GetObjectId(&_PyId_stderr);
1010+
file = _PySys_GetObjectId(&PyId_stderr);
10091011
if (file != NULL && file != Py_None)
10101012
PyFile_WriteObject(boot->func, file, 0);
10111013
else

Modules/faulthandler.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
(anyway, the length is smaller than 30 characters) */
2727
#define PUTS(fd, str) write(fd, str, (int)strlen(str))
2828

29+
_Py_IDENTIFIER(enable);
30+
_Py_IDENTIFIER(fileno);
31+
_Py_IDENTIFIER(flush);
32+
_Py_IDENTIFIER(stderr);
33+
2934
#ifdef HAVE_SIGACTION
3035
typedef struct sigaction _Py_sighandler_t;
3136
#else
@@ -130,13 +135,11 @@ static PyObject*
130135
faulthandler_get_fileno(PyObject *file, int *p_fd)
131136
{
132137
PyObject *result;
133-
_Py_IDENTIFIER(fileno);
134-
_Py_IDENTIFIER(flush);
135138
long fd_long;
136139
int fd;
137140

138141
if (file == NULL || file == Py_None) {
139-
file = _PySys_GetObjectId(&_PyId_stderr);
142+
file = _PySys_GetObjectId(&PyId_stderr);
140143
if (file == NULL) {
141144
PyErr_SetString(PyExc_RuntimeError, "unable to get sys.stderr");
142145
return NULL;
@@ -1047,7 +1050,6 @@ static int
10471050
faulthandler_env_options(void)
10481051
{
10491052
PyObject *xoptions, *key, *module, *res;
1050-
_Py_IDENTIFIER(enable);
10511053
char *p;
10521054

10531055
if (!((p = Py_GETENV("PYTHONFAULTHANDLER")) && *p != '\0')) {

Modules/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ RunMainFromImporter(wchar_t *filename)
261261

262262
/* argv0 is usable as an import source, so put it in sys.path[0]
263263
and import __main__ */
264-
sys_path = _PySys_GetObjectId(&_PyId_path);
264+
sys_path = PySys_GetObject("path");
265265
if (sys_path == NULL) {
266266
PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path");
267267
goto error;

Modules/syslogmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ syslog_get_argv(void)
7171
Py_ssize_t argv_len, scriptlen;
7272
PyObject *scriptobj;
7373
Py_ssize_t slash;
74-
PyObject *argv = _PySys_GetObjectId(&_PyId_argv);
74+
PyObject *argv = PySys_GetObject("argv");
7575

7676
if (argv == NULL) {
7777
return(NULL);

Objects/object.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
extern "C" {
99
#endif
1010

11+
_Py_IDENTIFIER(Py_Repr);
12+
_Py_IDENTIFIER(__bytes__);
13+
_Py_IDENTIFIER(__dir__);
14+
_Py_IDENTIFIER(__isabstractmethod__);
15+
_Py_IDENTIFIER(builtins);
16+
1117
#ifdef Py_REF_DEBUG
1218
Py_ssize_t _Py_RefTotal;
1319

@@ -560,7 +566,6 @@ PyObject *
560566
PyObject_Bytes(PyObject *v)
561567
{
562568
PyObject *result, *func;
563-
_Py_IDENTIFIER(__bytes__);
564569

565570
if (v == NULL)
566571
return PyBytes_FromString("<NULL>");
@@ -949,7 +954,6 @@ _PyObject_IsAbstract(PyObject *obj)
949954
{
950955
int res;
951956
PyObject* isabstract;
952-
_Py_IDENTIFIER(__isabstractmethod__);
953957

954958
if (obj == NULL)
955959
return 0;
@@ -1124,7 +1128,7 @@ _PyObject_GetBuiltin(const char *name)
11241128
{
11251129
PyObject *mod_name, *mod, *attr;
11261130

1127-
mod_name = _PyUnicode_FromId(&_PyId_builtins); /* borrowed */
1131+
mod_name = _PyUnicode_FromId(&PyId_builtins); /* borrowed */
11281132
if (mod_name == NULL)
11291133
return NULL;
11301134
mod = PyImport_Import(mod_name);
@@ -1440,7 +1444,6 @@ static PyObject *
14401444
_dir_object(PyObject *obj)
14411445
{
14421446
PyObject *result, *sorted;
1443-
_Py_IDENTIFIER(__dir__);
14441447
PyObject *dirfunc = _PyObject_LookupSpecial(obj, &PyId___dir__);
14451448

14461449
assert(obj);
@@ -1973,8 +1976,6 @@ _PyObject_DebugTypeStats(FILE *out)
19731976
See dictobject.c and listobject.c for examples of use.
19741977
*/
19751978

1976-
_Py_IDENTIFIER(Py_Repr);
1977-
19781979
int
19791980
Py_ReprEnter(PyObject *obj)
19801981
{

Objects/typeobject.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ _Py_IDENTIFIER(__module__);
5353
_Py_IDENTIFIER(__name__);
5454
_Py_IDENTIFIER(__new__);
5555
_Py_IDENTIFIER(__setitem__);
56+
_Py_IDENTIFIER(builtins);
5657

5758
static PyObject *
5859
slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
@@ -366,7 +367,7 @@ type_module(PyTypeObject *type, void *context)
366367
if (s != NULL)
367368
return PyUnicode_FromStringAndSize(
368369
type->tp_name, (Py_ssize_t)(s - type->tp_name));
369-
name = _PyUnicode_FromId(&_PyId_builtins);
370+
name = _PyUnicode_FromId(&PyId_builtins);
370371
Py_XINCREF(name);
371372
return name;
372373
}
@@ -718,7 +719,7 @@ type_repr(PyTypeObject *type)
718719
return NULL;
719720
}
720721

721-
if (mod != NULL && _PyUnicode_CompareWithId(mod, &_PyId_builtins))
722+
if (mod != NULL && _PyUnicode_CompareWithId(mod, &PyId_builtins))
722723
rtn = PyUnicode_FromFormat("<class '%U.%U'>", mod, name);
723724
else
724725
rtn = PyUnicode_FromFormat("<class '%s'>", type->tp_name);
@@ -3189,7 +3190,7 @@ object_repr(PyObject *self)
31893190
Py_XDECREF(mod);
31903191
return NULL;
31913192
}
3192-
if (mod != NULL && _PyUnicode_CompareWithId(mod, &_PyId_builtins))
3193+
if (mod != NULL && _PyUnicode_CompareWithId(mod, &PyId_builtins))
31933194
rtn = PyUnicode_FromFormat("<%U.%U object at %p>", mod, name, self);
31943195
else
31953196
rtn = PyUnicode_FromFormat("<%s object at %p>",

0 commit comments

Comments
 (0)