Issue #19512, #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.
diff --git a/Python/_warnings.c b/Python/_warnings.c
index de60128..6013d7d 100644
--- a/Python/_warnings.c
+++ b/Python/_warnings.c
@@ -13,6 +13,8 @@
 static PyObject *_once_registry;  /* Dict */
 static PyObject *_default_action; /* String */
 
+_Py_IDENTIFIER(argv);
+_Py_IDENTIFIER(stderr);
 
 static int
 check_matched(PyObject *obj, PyObject *arg)
@@ -265,7 +267,7 @@
     if (name == NULL)  /* XXX Can an object lack a '__name__' attribute? */
         goto error;
 
-    f_stderr = _PySys_GetObjectId(&_PyId_stderr);
+    f_stderr = _PySys_GetObjectId(&PyId_stderr);
     if (f_stderr == NULL) {
         fprintf(stderr, "lost sys.stderr\n");
         goto error;
@@ -562,7 +564,7 @@
     else {
         *filename = NULL;
         if (*module != Py_None && PyUnicode_CompareWithASCIIString(*module, "__main__") == 0) {
-            PyObject *argv = _PySys_GetObjectId(&_PyId_argv);
+            PyObject *argv = _PySys_GetObjectId(&PyId_argv);
             /* PyList_Check() is needed because sys.argv is set to None during
                Python finalization */
             if (argv != NULL && PyList_Check(argv) && PyList_Size(argv) > 0) {
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index 7f3593c..96ccd64 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -32,9 +32,19 @@
 int Py_HasFileSystemDefaultEncoding = 0;
 #endif
 
+_Py_IDENTIFIER(__builtins__);
+_Py_IDENTIFIER(__dict__);
+_Py_IDENTIFIER(__prepare__);
+_Py_IDENTIFIER(__round__);
+_Py_IDENTIFIER(encoding);
+_Py_IDENTIFIER(errors);
 _Py_IDENTIFIER(fileno);
 _Py_IDENTIFIER(flush);
-_Py_IDENTIFIER(__builtins__);
+_Py_IDENTIFIER(metaclass);
+_Py_IDENTIFIER(sort);
+_Py_IDENTIFIER(stdin);
+_Py_IDENTIFIER(stdout);
+_Py_IDENTIFIER(stderr);
 
 static PyObject *
 builtin___build_class__(PyObject *self, PyObject *args, PyObject *kwds)
@@ -43,8 +53,6 @@
     PyObject *cls = NULL;
     Py_ssize_t nargs;
     int isclass;
-    _Py_IDENTIFIER(__prepare__);
-    _Py_IDENTIFIER(metaclass);
 
     assert(args != NULL);
     if (!PyTuple_Check(args)) {
@@ -1547,7 +1555,6 @@
     static PyObject *dummy_args;
     PyObject *sep = NULL, *end = NULL, *file = NULL, *flush = NULL;
     int i, err;
-    _Py_IDENTIFIER(flush);
 
     if (dummy_args == NULL && !(dummy_args = PyTuple_New(0)))
         return NULL;
@@ -1555,7 +1562,7 @@
                                      kwlist, &sep, &end, &file, &flush))
         return NULL;
     if (file == NULL || file == Py_None) {
-        file = _PySys_GetObjectId(&_PyId_stdout);
+        file = _PySys_GetObjectId(&PyId_stdout);
         if (file == NULL) {
             PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
             return NULL;
@@ -1640,9 +1647,9 @@
 builtin_input(PyObject *self, PyObject *args)
 {
     PyObject *promptarg = NULL;
-    PyObject *fin = _PySys_GetObjectId(&_PyId_stdin);
-    PyObject *fout = _PySys_GetObjectId(&_PyId_stdout);
-    PyObject *ferr = _PySys_GetObjectId(&_PyId_stderr);
+    PyObject *fin = _PySys_GetObjectId(&PyId_stdin);
+    PyObject *fout = _PySys_GetObjectId(&PyId_stdout);
+    PyObject *ferr = _PySys_GetObjectId(&PyId_stderr);
     PyObject *tmp;
     long fd;
     int tty;
@@ -1713,8 +1720,6 @@
         char *stdin_encoding_str, *stdin_errors_str;
         PyObject *result;
         size_t len;
-        _Py_IDENTIFIER(encoding);
-        _Py_IDENTIFIER(errors);
 
         stdin_encoding = _PyObject_GetAttrId(fin, &PyId_encoding);
         stdin_errors = _PyObject_GetAttrId(fin, &PyId_errors);
@@ -1843,7 +1848,6 @@
     PyObject *ndigits = NULL;
     static char *kwlist[] = {"number", "ndigits", 0};
     PyObject *number, *round, *result;
-    _Py_IDENTIFIER(__round__);
 
     if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:round",
                                      kwlist, &number, &ndigits))
@@ -1886,7 +1890,6 @@
     PyObject *callable;
     static char *kwlist[] = {"iterable", "key", "reverse", 0};
     int reverse;
-    _Py_IDENTIFIER(sort);
 
     /* args 1-3 should match listsort in Objects/listobject.c */
     if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|Oi:sorted",
@@ -1939,7 +1942,6 @@
         Py_INCREF(d);
     }
     else {
-        _Py_IDENTIFIER(__dict__);
         d = _PyObject_GetAttrId(v, &PyId___dict__);
         if (d == NULL) {
             PyErr_SetString(PyExc_TypeError,
diff --git a/Python/errors.c b/Python/errors.c
index 93e4724..90dc729 100644
--- a/Python/errors.c
+++ b/Python/errors.c
@@ -20,6 +20,9 @@
 extern "C" {
 #endif
 
+_Py_IDENTIFIER(builtins);
+_Py_IDENTIFIER(stderr);
+
 
 void
 PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback)
@@ -844,7 +847,7 @@
 
     PyErr_Fetch(&t, &v, &tb);
 
-    f = _PySys_GetObjectId(&_PyId_stderr);
+    f = _PySys_GetObjectId(&PyId_stderr);
     if (f == NULL || f == Py_None)
         goto done;
 
@@ -878,7 +881,7 @@
             goto done;
     }
     else {
-        if (_PyUnicode_CompareWithId(moduleName, &_PyId_builtins) != 0) {
+        if (_PyUnicode_CompareWithId(moduleName, &PyId_builtins) != 0) {
             if (PyFile_WriteObject(moduleName, f, Py_PRINT_RAW) < 0)
                 goto done;
             if (PyFile_WriteString(".", f) < 0)
diff --git a/Python/import.c b/Python/import.c
index aea29e2..c96106f 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -310,7 +310,7 @@
 
     /* XXX Perhaps these precautions are obsolete. Who knows? */
 
-    value = _PyDict_GetItemId(modules, &_PyId_builtins);
+    value = PyDict_GetItemString(modules, "builtins");
     if (value != NULL && PyModule_Check(value)) {
         dict = PyModule_GetDict(value);
         if (Py_VerboseFlag)
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index 5115c57..922446e 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -35,21 +35,16 @@
 #define PATH_MAX MAXPATHLEN
 #endif
 
-/* Common identifiers */
-_Py_Identifier _PyId_argv = _Py_static_string_init("argv");
-_Py_Identifier _PyId_builtins = _Py_static_string_init("builtins");
-_Py_Identifier _PyId_path = _Py_static_string_init("path");
-_Py_Identifier _PyId_stdin = _Py_static_string_init("stdin");
-_Py_Identifier _PyId_stdout = _Py_static_string_init("stdout");
-_Py_Identifier _PyId_stderr = _Py_static_string_init("stderr");
-
-/* local identifiers */
+_Py_IDENTIFIER(builtins);
 _Py_IDENTIFIER(excepthook);
-_Py_IDENTIFIER(ps1);
-_Py_IDENTIFIER(ps2);
+_Py_IDENTIFIER(last_traceback);
 _Py_IDENTIFIER(last_type);
 _Py_IDENTIFIER(last_value);
-_Py_IDENTIFIER(last_traceback);
+_Py_IDENTIFIER(ps1);
+_Py_IDENTIFIER(ps2);
+_Py_IDENTIFIER(stdin);
+_Py_IDENTIFIER(stdout);
+_Py_IDENTIFIER(stderr);
 _Py_static_string(PyId_string, "<string>");
 
 #ifdef Py_REF_DEBUG
@@ -429,7 +424,7 @@
     pstderr = PyFile_NewStdPrinter(fileno(stderr));
     if (pstderr == NULL)
         Py_FatalError("Py_Initialize: can't set preliminary stderr");
-    _PySys_SetObjectId(&_PyId_stderr, pstderr);
+    _PySys_SetObjectId(&PyId_stderr, pstderr);
     PySys_SetObject("__stderr__", pstderr);
     Py_DECREF(pstderr);
 
@@ -514,8 +509,8 @@
 static void
 flush_std_files(void)
 {
-    PyObject *fout = _PySys_GetObjectId(&_PyId_stdout);
-    PyObject *ferr = _PySys_GetObjectId(&_PyId_stderr);
+    PyObject *fout = _PySys_GetObjectId(&PyId_stdout);
+    PyObject *ferr = _PySys_GetObjectId(&PyId_stderr);
     PyObject *tmp;
     _Py_IDENTIFIER(flush);
 
@@ -793,7 +788,7 @@
         pstderr = PyFile_NewStdPrinter(fileno(stderr));
         if (pstderr == NULL)
             Py_FatalError("Py_Initialize: can't set preliminary stderr");
-        _PySys_SetObjectId(&_PyId_stderr, pstderr);
+        _PySys_SetObjectId(&PyId_stderr, pstderr);
         PySys_SetObject("__stderr__", pstderr);
         Py_DECREF(pstderr);
 
@@ -1187,7 +1182,7 @@
             goto error;
     } /* if (fd < 0) */
     PySys_SetObject("__stdin__", std);
-    _PySys_SetObjectId(&_PyId_stdin, std);
+    _PySys_SetObjectId(&PyId_stdin, std);
     Py_DECREF(std);
 
     /* Set sys.stdout */
@@ -1202,7 +1197,7 @@
             goto error;
     } /* if (fd < 0) */
     PySys_SetObject("__stdout__", std);
-    _PySys_SetObjectId(&_PyId_stdout, std);
+    _PySys_SetObjectId(&PyId_stdout, std);
     Py_DECREF(std);
 
 #if 1 /* Disable this if you have trouble debugging bootstrap stuff */
@@ -1236,7 +1231,7 @@
         Py_DECREF(std);
         goto error;
     }
-    if (_PySys_SetObjectId(&_PyId_stderr, std) < 0) {
+    if (_PySys_SetObjectId(&PyId_stderr, std) < 0) {
         Py_DECREF(std);
         goto error;
     }
@@ -1368,7 +1363,7 @@
 
     if (fp == stdin) {
         /* Fetch encoding from sys.stdin if possible. */
-        v = _PySys_GetObjectId(&_PyId_stdin);
+        v = _PySys_GetObjectId(&PyId_stdin);
         if (v && v != Py_None) {
             oenc = _PyObject_GetAttrId(v, &PyId_encoding);
             if (oenc)
@@ -1774,7 +1769,7 @@
     if (PyLong_Check(value))
         exitcode = (int)PyLong_AsLong(value);
     else {
-        PyObject *sys_stderr = _PySys_GetObjectId(&_PyId_stderr);
+        PyObject *sys_stderr = _PySys_GetObjectId(&PyId_stderr);
         if (sys_stderr != NULL && sys_stderr != Py_None) {
             PyFile_WriteObject(value, sys_stderr, Py_PRINT_RAW);
         } else {
@@ -1938,7 +1933,7 @@
             err = PyFile_WriteString("<unknown>", f);
         }
         else {
-            if (_PyUnicode_CompareWithId(moduleName, &_PyId_builtins) != 0)
+            if (_PyUnicode_CompareWithId(moduleName, &PyId_builtins) != 0)
             {
                 err = PyFile_WriteObject(moduleName, f, Py_PRINT_RAW);
                 err += PyFile_WriteString(".", f);
@@ -2033,7 +2028,7 @@
 PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
 {
     PyObject *seen;
-    PyObject *f = _PySys_GetObjectId(&_PyId_stderr);
+    PyObject *f = _PySys_GetObjectId(&PyId_stderr);
     if (PyExceptionInstance_Check(value)
         && tb != NULL && PyTraceBack_Check(tb)) {
         /* Put the traceback on the exception, otherwise it won't get
@@ -2130,7 +2125,7 @@
     /* Save the current exception */
     PyErr_Fetch(&type, &value, &traceback);
 
-    f = _PySys_GetObjectId(&_PyId_stderr);
+    f = _PySys_GetObjectId(&PyId_stderr);
     if (f != NULL) {
         r = _PyObject_CallMethodId(f, &PyId_flush, "");
         if (r)
@@ -2138,7 +2133,7 @@
         else
             PyErr_Clear();
     }
-    f = _PySys_GetObjectId(&_PyId_stdout);
+    f = _PySys_GetObjectId(&PyId_stdout);
     if (f != NULL) {
         r = _PyObject_CallMethodId(f, &PyId_flush, "");
         if (r)
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index de33d55..3ebb6c9 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -41,6 +41,16 @@
 #include <langinfo.h>
 #endif
 
+_Py_IDENTIFIER(_);
+_Py_IDENTIFIER(__sizeof__);
+_Py_IDENTIFIER(buffer);
+_Py_IDENTIFIER(builtins);
+_Py_IDENTIFIER(encoding);
+_Py_IDENTIFIER(path);
+_Py_IDENTIFIER(stdout);
+_Py_IDENTIFIER(stderr);
+_Py_IDENTIFIER(write);
+
 PyObject *
 _PySys_GetObjectId(_Py_Identifier *key)
 {
@@ -104,8 +114,6 @@
     PyObject *encoded, *escaped_str, *repr_str, *buffer, *result;
     char *stdout_encoding_str;
     int ret;
-    _Py_IDENTIFIER(encoding);
-    _Py_IDENTIFIER(buffer);
 
     stdout_encoding = _PyObject_GetAttrId(outf, &PyId_encoding);
     if (stdout_encoding == NULL)
@@ -126,7 +134,6 @@
 
     buffer = _PyObject_GetAttrId(outf, &PyId_buffer);
     if (buffer) {
-        _Py_IDENTIFIER(write);
         result = _PyObject_CallMethodId(buffer, &PyId_write, "(O)", encoded);
         Py_DECREF(buffer);
         Py_DECREF(encoded);
@@ -165,8 +172,6 @@
     PyObject *builtins;
     static PyObject *newline = NULL;
     int err;
-    _Py_IDENTIFIER(_);
-    _Py_IDENTIFIER(builtins);
 
     builtins = _PyDict_GetItemId(modules, &PyId_builtins);
     if (builtins == NULL) {
@@ -183,7 +188,7 @@
     }
     if (_PyObject_SetAttrId(builtins, &PyId__, Py_None) != 0)
         return NULL;
-    outf = _PySys_GetObjectId(&_PyId_stdout);
+    outf = _PySys_GetObjectId(&PyId_stdout);
     if (outf == NULL || outf == Py_None) {
         PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
         return NULL;
@@ -854,7 +859,6 @@
     static char *kwlist[] = {"object", "default", 0};
     PyObject *o, *dflt = NULL;
     PyObject *method;
-    _Py_IDENTIFIER(__sizeof__);
 
     if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:getsizeof",
                                      kwlist, &o, &dflt))
@@ -1825,7 +1829,7 @@
     PyObject *v;
     if ((v = makepathobject(path, DELIM)) == NULL)
         Py_FatalError("can't create sys.path");
-    if (_PySys_SetObjectId(&_PyId_path, v) != 0)
+    if (_PySys_SetObjectId(&PyId_path, v) != 0)
         Py_FatalError("can't assign sys.path");
     Py_DECREF(v);
 }
@@ -1894,7 +1898,7 @@
     wchar_t fullpath[MAX_PATH];
 #endif
 
-    path = _PySys_GetObjectId(&_PyId_path);
+    path = _PySys_GetObjectId(&PyId_path);
     if (path == NULL)
         return;
 
@@ -2004,7 +2008,6 @@
 {
     PyObject *writer = NULL, *args = NULL, *result = NULL;
     int err;
-    _Py_IDENTIFIER(write);
 
     if (file == NULL)
         return -1;
@@ -2109,7 +2112,7 @@
     va_list va;
 
     va_start(va, format);
-    sys_write(&_PyId_stdout, stdout, format, va);
+    sys_write(&PyId_stdout, stdout, format, va);
     va_end(va);
 }
 
@@ -2119,7 +2122,7 @@
     va_list va;
 
     va_start(va, format);
-    sys_write(&_PyId_stderr, stderr, format, va);
+    sys_write(&PyId_stderr, stderr, format, va);
     va_end(va);
 }
 
@@ -2151,7 +2154,7 @@
     va_list va;
 
     va_start(va, format);
-    sys_format(&_PyId_stdout, stdout, format, va);
+    sys_format(&PyId_stdout, stdout, format, va);
     va_end(va);
 }
 
@@ -2161,6 +2164,6 @@
     va_list va;
 
     va_start(va, format);
-    sys_format(&_PyId_stderr, stderr, format, va);
+    sys_format(&PyId_stderr, stderr, format, va);
     va_end(va);
 }
diff --git a/Python/traceback.c b/Python/traceback.c
index b0bed5f..e9169ce 100644
--- a/Python/traceback.c
+++ b/Python/traceback.c
@@ -21,6 +21,11 @@
 /* Function from Parser/tokenizer.c */
 extern char * PyTokenizer_FindEncodingFilename(int, PyObject *);
 
+_Py_IDENTIFIER(TextIOWrapper);
+_Py_IDENTIFIER(close);
+_Py_IDENTIFIER(open);
+_Py_IDENTIFIER(path);
+
 static PyObject *
 tb_dir(PyTracebackObject *self)
 {
@@ -152,7 +157,6 @@
     const char* filepath;
     Py_ssize_t len;
     PyObject* result;
-    _Py_IDENTIFIER(open);
 
     filebytes = PyUnicode_EncodeFSDefault(filename);
     if (filebytes == NULL) {
@@ -169,7 +173,7 @@
         tail++;
     taillen = strlen(tail);
 
-    syspath = _PySys_GetObjectId(&_PyId_path);
+    syspath = _PySys_GetObjectId(&PyId_path);
     if (syspath == NULL || !PyList_Check(syspath))
         goto error;
     npath = PyList_Size(syspath);
@@ -232,9 +236,6 @@
     char buf[MAXPATHLEN+1];
     int kind;
     void *data;
-    _Py_IDENTIFIER(close);
-    _Py_IDENTIFIER(open);
-    _Py_IDENTIFIER(TextIOWrapper);
 
     /* open the file */
     if (filename == NULL)