Skip to content

Commit

Permalink
Issue python#19512, python#19515: remove shared identifiers, move ide…
Browse files Browse the repository at this point in the history
…ntifiers where they

are used.

Move also _Py_IDENTIFIER() defintions to the top in modified files to remove
identifiers duplicated in the same file.
  • Loading branch information
vstinner committed Nov 7, 2013
1 parent 07e9e38 commit bd303c1
Show file tree
Hide file tree
Showing 17 changed files with 93 additions and 89 deletions.
8 changes: 0 additions & 8 deletions Include/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,6 @@ typedef struct _Py_Identifier {
#define _Py_static_string(varname, value) static _Py_Identifier varname = _Py_static_string_init(value)
#define _Py_IDENTIFIER(varname) _Py_static_string(PyId_##varname, #varname)

/* Common identifiers (ex: _PyId_path is the string "path") */
PyAPI_DATA(_Py_Identifier) _PyId_argv;
PyAPI_DATA(_Py_Identifier) _PyId_builtins;
PyAPI_DATA(_Py_Identifier) _PyId_path;
PyAPI_DATA(_Py_Identifier) _PyId_stdin;
PyAPI_DATA(_Py_Identifier) _PyId_stdout;
PyAPI_DATA(_Py_Identifier) _PyId_stderr;

/*
Type objects contain a string containing the type name (to help somewhat
in debugging), the allocation parameters (see PyObject_New() and
Expand Down
2 changes: 1 addition & 1 deletion Modules/_ctypes/callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ static void
PrintError(char *msg, ...)
{
char buf[512];
PyObject *f = _PySys_GetObjectId(&_PyId_stderr);
PyObject *f = PySys_GetObject("stderr");
va_list marker;

va_start(marker, msg);
Expand Down
2 changes: 1 addition & 1 deletion Modules/_cursesmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2578,7 +2578,7 @@ PyCurses_setupterm(PyObject* self, PyObject *args, PyObject* keywds)
if (fd == -1) {
PyObject* sys_stdout;

sys_stdout = _PySys_GetObjectId(&_PyId_stdout);
sys_stdout = PySys_GetObject("stdout");

if (sys_stdout == NULL || sys_stdout == Py_None) {
PyErr_SetString(
Expand Down
2 changes: 1 addition & 1 deletion Modules/_lsprof.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ normalizeUserObj(PyObject *obj)
}
}
if (modname != NULL) {
if (_PyUnicode_CompareWithId(modname, &_PyId_builtins) != 0) {
if (PyUnicode_CompareWithASCIIString(modname, "builtins") != 0) {
PyObject *result;
result = PyUnicode_FromFormat("<%U.%s>", modname,
fn->m_ml->ml_name);
Expand Down
4 changes: 3 additions & 1 deletion Modules/_threadmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ static PyObject *ThreadError;
static long nb_threads = 0;
static PyObject *str_dict;

_Py_IDENTIFIER(stderr);

/* Lock objects */

typedef struct {
Expand Down Expand Up @@ -1005,7 +1007,7 @@ t_bootstrap(void *boot_raw)
PySys_WriteStderr(
"Unhandled exception in thread started by ");
PyErr_Fetch(&exc, &value, &tb);
file = _PySys_GetObjectId(&_PyId_stderr);
file = _PySys_GetObjectId(&PyId_stderr);
if (file != NULL && file != Py_None)
PyFile_WriteObject(boot->func, file, 0);
else
Expand Down
10 changes: 6 additions & 4 deletions Modules/faulthandler.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
(anyway, the length is smaller than 30 characters) */
#define PUTS(fd, str) write(fd, str, (int)strlen(str))

_Py_IDENTIFIER(enable);
_Py_IDENTIFIER(fileno);
_Py_IDENTIFIER(flush);
_Py_IDENTIFIER(stderr);

#ifdef HAVE_SIGACTION
typedef struct sigaction _Py_sighandler_t;
#else
Expand Down Expand Up @@ -130,13 +135,11 @@ static PyObject*
faulthandler_get_fileno(PyObject *file, int *p_fd)
{
PyObject *result;
_Py_IDENTIFIER(fileno);
_Py_IDENTIFIER(flush);
long fd_long;
int fd;

if (file == NULL || file == Py_None) {
file = _PySys_GetObjectId(&_PyId_stderr);
file = _PySys_GetObjectId(&PyId_stderr);
if (file == NULL) {
PyErr_SetString(PyExc_RuntimeError, "unable to get sys.stderr");
return NULL;
Expand Down Expand Up @@ -1047,7 +1050,6 @@ static int
faulthandler_env_options(void)
{
PyObject *xoptions, *key, *module, *res;
_Py_IDENTIFIER(enable);
char *p;

if (!((p = Py_GETENV("PYTHONFAULTHANDLER")) && *p != '\0')) {
Expand Down
2 changes: 1 addition & 1 deletion Modules/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ RunMainFromImporter(wchar_t *filename)

/* argv0 is usable as an import source, so put it in sys.path[0]
and import __main__ */
sys_path = _PySys_GetObjectId(&_PyId_path);
sys_path = PySys_GetObject("path");
if (sys_path == NULL) {
PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path");
goto error;
Expand Down
2 changes: 1 addition & 1 deletion Modules/syslogmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ syslog_get_argv(void)
Py_ssize_t argv_len, scriptlen;
PyObject *scriptobj;
Py_ssize_t slash;
PyObject *argv = _PySys_GetObjectId(&_PyId_argv);
PyObject *argv = PySys_GetObject("argv");

if (argv == NULL) {
return(NULL);
Expand Down
13 changes: 7 additions & 6 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
extern "C" {
#endif

_Py_IDENTIFIER(Py_Repr);
_Py_IDENTIFIER(__bytes__);
_Py_IDENTIFIER(__dir__);
_Py_IDENTIFIER(__isabstractmethod__);
_Py_IDENTIFIER(builtins);

#ifdef Py_REF_DEBUG
Py_ssize_t _Py_RefTotal;

Expand Down Expand Up @@ -560,7 +566,6 @@ PyObject *
PyObject_Bytes(PyObject *v)
{
PyObject *result, *func;
_Py_IDENTIFIER(__bytes__);

if (v == NULL)
return PyBytes_FromString("<NULL>");
Expand Down Expand Up @@ -949,7 +954,6 @@ _PyObject_IsAbstract(PyObject *obj)
{
int res;
PyObject* isabstract;
_Py_IDENTIFIER(__isabstractmethod__);

if (obj == NULL)
return 0;
Expand Down Expand Up @@ -1124,7 +1128,7 @@ _PyObject_GetBuiltin(const char *name)
{
PyObject *mod_name, *mod, *attr;

mod_name = _PyUnicode_FromId(&_PyId_builtins); /* borrowed */
mod_name = _PyUnicode_FromId(&PyId_builtins); /* borrowed */
if (mod_name == NULL)
return NULL;
mod = PyImport_Import(mod_name);
Expand Down Expand Up @@ -1440,7 +1444,6 @@ static PyObject *
_dir_object(PyObject *obj)
{
PyObject *result, *sorted;
_Py_IDENTIFIER(__dir__);
PyObject *dirfunc = _PyObject_LookupSpecial(obj, &PyId___dir__);

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

_Py_IDENTIFIER(Py_Repr);

int
Py_ReprEnter(PyObject *obj)
{
Expand Down
7 changes: 4 additions & 3 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ _Py_IDENTIFIER(__module__);
_Py_IDENTIFIER(__name__);
_Py_IDENTIFIER(__new__);
_Py_IDENTIFIER(__setitem__);
_Py_IDENTIFIER(builtins);

static PyObject *
slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
Expand Down Expand Up @@ -366,7 +367,7 @@ type_module(PyTypeObject *type, void *context)
if (s != NULL)
return PyUnicode_FromStringAndSize(
type->tp_name, (Py_ssize_t)(s - type->tp_name));
name = _PyUnicode_FromId(&_PyId_builtins);
name = _PyUnicode_FromId(&PyId_builtins);
Py_XINCREF(name);
return name;
}
Expand Down Expand Up @@ -718,7 +719,7 @@ type_repr(PyTypeObject *type)
return NULL;
}

if (mod != NULL && _PyUnicode_CompareWithId(mod, &_PyId_builtins))
if (mod != NULL && _PyUnicode_CompareWithId(mod, &PyId_builtins))
rtn = PyUnicode_FromFormat("<class '%U.%U'>", mod, name);
else
rtn = PyUnicode_FromFormat("<class '%s'>", type->tp_name);
Expand Down Expand Up @@ -3189,7 +3190,7 @@ object_repr(PyObject *self)
Py_XDECREF(mod);
return NULL;
}
if (mod != NULL && _PyUnicode_CompareWithId(mod, &_PyId_builtins))
if (mod != NULL && _PyUnicode_CompareWithId(mod, &PyId_builtins))
rtn = PyUnicode_FromFormat("<%U.%U object at %p>", mod, name, self);
else
rtn = PyUnicode_FromFormat("<%s object at %p>",
Expand Down
6 changes: 4 additions & 2 deletions Python/_warnings.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ static PyObject *_filters; /* List */
static PyObject *_once_registry; /* Dict */
static PyObject *_default_action; /* String */

_Py_IDENTIFIER(argv);
_Py_IDENTIFIER(stderr);

static int
check_matched(PyObject *obj, PyObject *arg)
Expand Down Expand Up @@ -265,7 +267,7 @@ show_warning(PyObject *filename, int lineno, PyObject *text, PyObject
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;
Expand Down Expand Up @@ -562,7 +564,7 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
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) {
Expand Down
28 changes: 15 additions & 13 deletions Python/bltinmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,19 @@ const char *Py_FileSystemDefaultEncoding = NULL; /* set by initfsencoding() */
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)
Expand All @@ -43,8 +53,6 @@ builtin___build_class__(PyObject *self, PyObject *args, PyObject *kwds)
PyObject *cls = NULL;
Py_ssize_t nargs;
int isclass;
_Py_IDENTIFIER(__prepare__);
_Py_IDENTIFIER(metaclass);

assert(args != NULL);
if (!PyTuple_Check(args)) {
Expand Down Expand Up @@ -1547,15 +1555,14 @@ builtin_print(PyObject *self, PyObject *args, PyObject *kwds)
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;
if (!PyArg_ParseTupleAndKeywords(dummy_args, kwds, "|OOOO:print",
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;
Expand Down Expand Up @@ -1640,9 +1647,9 @@ static PyObject *
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;
Expand Down Expand Up @@ -1713,8 +1720,6 @@ builtin_input(PyObject *self, PyObject *args)
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);
Expand Down Expand Up @@ -1843,7 +1848,6 @@ builtin_round(PyObject *self, PyObject *args, PyObject *kwds)
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))
Expand Down Expand Up @@ -1886,7 +1890,6 @@ builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds)
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",
Expand Down Expand Up @@ -1939,7 +1942,6 @@ builtin_vars(PyObject *self, PyObject *args)
Py_INCREF(d);
}
else {
_Py_IDENTIFIER(__dict__);
d = _PyObject_GetAttrId(v, &PyId___dict__);
if (d == NULL) {
PyErr_SetString(PyExc_TypeError,
Expand Down
7 changes: 5 additions & 2 deletions Python/errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ extern char *strerror(int);
extern "C" {
#endif

_Py_IDENTIFIER(builtins);
_Py_IDENTIFIER(stderr);


void
PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback)
Expand Down Expand Up @@ -844,7 +847,7 @@ PyErr_WriteUnraisable(PyObject *obj)

PyErr_Fetch(&t, &v, &tb);

f = _PySys_GetObjectId(&_PyId_stderr);
f = _PySys_GetObjectId(&PyId_stderr);
if (f == NULL || f == Py_None)
goto done;

Expand Down Expand Up @@ -878,7 +881,7 @@ PyErr_WriteUnraisable(PyObject *obj)
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)
Expand Down
2 changes: 1 addition & 1 deletion Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ PyImport_Cleanup(void)

/* 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)
Expand Down
Loading

0 comments on commit bd303c1

Please sign in to comment.