Use identifier API for PyObject_GetAttrString.

This commit is contained in:
Martin v. Löwis 2011-10-10 18:11:30 +02:00
parent 794d567b17
commit 1ee1b6fe0d
28 changed files with 499 additions and 357 deletions

File diff suppressed because it is too large Load diff

View file

@ -247,10 +247,11 @@ show_warning(PyObject *filename, int lineno, PyObject *text, PyObject
PyObject *f_stderr;
PyObject *name;
char lineno_str[128];
_Py_identifier(__name__);
PyOS_snprintf(lineno_str, sizeof(lineno_str), ":%d: ", lineno);
name = PyObject_GetAttrString(category, "__name__");
name = _PyObject_GetAttrId(category, &PyId___name__);
if (name == NULL) /* XXX Can an object lack a '__name__' attribute? */
return;

View file

@ -41,6 +41,7 @@ builtin___build_class__(PyObject *self, PyObject *args, PyObject *kwds)
PyObject *func, *name, *bases, *mkw, *meta, *prep, *ns, *cell;
PyObject *cls = NULL;
Py_ssize_t nargs;
_Py_identifier(__prepare__);
assert(args != NULL);
if (!PyTuple_Check(args)) {
@ -95,7 +96,7 @@ builtin___build_class__(PyObject *self, PyObject *args, PyObject *kwds)
}
Py_INCREF(meta);
}
prep = PyObject_GetAttrString(meta, "__prepare__");
prep = _PyObject_GetAttrId(meta, &PyId___prepare__);
if (prep == NULL) {
if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
PyErr_Clear();
@ -1613,8 +1614,9 @@ builtin_input(PyObject *self, PyObject *args)
char *stdin_encoding_str;
PyObject *result;
size_t len;
_Py_identifier(encoding);
stdin_encoding = PyObject_GetAttrString(fin, "encoding");
stdin_encoding = _PyObject_GetAttrId(fin, &PyId_encoding);
if (!stdin_encoding)
/* stdin is a text stream, so it must have an
encoding. */
@ -1633,7 +1635,7 @@ builtin_input(PyObject *self, PyObject *args)
PyObject *stringpo;
PyObject *stdout_encoding;
char *stdout_encoding_str;
stdout_encoding = PyObject_GetAttrString(fout, "encoding");
stdout_encoding = _PyObject_GetAttrId(fout, &PyId_encoding);
if (stdout_encoding == NULL) {
Py_DECREF(stdin_encoding);
return NULL;
@ -1788,6 +1790,7 @@ 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",
@ -1798,7 +1801,7 @@ builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds)
if (newlist == NULL)
return NULL;
callable = PyObject_GetAttrString(newlist, "sort");
callable = _PyObject_GetAttrId(newlist, &PyId_sort);
if (callable == NULL) {
Py_DECREF(newlist);
return NULL;
@ -1844,7 +1847,8 @@ builtin_vars(PyObject *self, PyObject *args)
Py_INCREF(d);
}
else {
d = PyObject_GetAttrString(v, "__dict__");
_Py_identifier(__dict__);
d = _PyObject_GetAttrId(v, &PyId___dict__);
if (d == NULL) {
PyErr_SetString(PyExc_TypeError,
"vars() argument must have __dict__ attribute");

View file

@ -465,9 +465,11 @@ PyObject *PyCodec_LookupError(const char *name)
static void wrong_exception_type(PyObject *exc)
{
PyObject *type = PyObject_GetAttrString(exc, "__class__");
_Py_identifier(__class__);
_Py_identifier(__name__);
PyObject *type = _PyObject_GetAttrId(exc, &PyId___class__);
if (type != NULL) {
PyObject *name = PyObject_GetAttrString(type, "__name__");
PyObject *name = _PyObject_GetAttrId(type, &PyId___name__);
Py_DECREF(type);
if (name != NULL) {
PyErr_Format(PyExc_TypeError,

View file

@ -707,6 +707,7 @@ PyErr_NewExceptionWithDoc(const char *name, const char *doc,
void
PyErr_WriteUnraisable(PyObject *obj)
{
_Py_identifier(__module__);
PyObject *f, *t, *v, *tb;
PyErr_Fetch(&t, &v, &tb);
f = PySys_GetObject("stderr");
@ -723,7 +724,7 @@ PyErr_WriteUnraisable(PyObject *obj)
className = dot+1;
}
moduleName = PyObject_GetAttrString(t, "__module__");
moduleName = _PyObject_GetAttrId(t, &PyId___module__);
if (moduleName == NULL)
PyFile_WriteString("<unknown>", f);
else {

View file

@ -154,7 +154,7 @@ static const struct filedescr _PyImport_StandardFiletab[] = {
};
static PyObject *initstr = NULL;
_Py_identifier(__path__);
/* Initialize things */
@ -248,8 +248,9 @@ _PyImportHooks_Init(void)
PySys_WriteStderr("# can't import zipimport\n");
}
else {
PyObject *zipimporter = PyObject_GetAttrString(zimpimport,
"zipimporter");
_Py_identifier(zipimporter);
PyObject *zipimporter = _PyObject_GetAttrId(zimpimport,
&PyId_zipimporter);
Py_DECREF(zimpimport);
if (zipimporter == NULL) {
PyErr_Clear(); /* No zipimporter object -- okay */
@ -3203,7 +3204,7 @@ ensure_fromlist(PyObject *mod, PyObject *fromlist, PyObject *name,
PyObject *fullname;
Py_ssize_t fromlist_len;
if (!PyObject_HasAttrString(mod, "__path__"))
if (!_PyObject_HasAttrId(mod, &PyId___path__))
return 1;
fromlist_len = PySequence_Size(fromlist);
@ -3221,11 +3222,12 @@ ensure_fromlist(PyObject *mod, PyObject *fromlist, PyObject *name,
}
if (PyUnicode_READ_CHAR(item, 0) == '*') {
PyObject *all;
_Py_identifier(__all__);
Py_DECREF(item);
/* See if the package defines __all__ */
if (recursive)
continue; /* Avoid endless recursion */
all = PyObject_GetAttrString(mod, "__all__");
all = _PyObject_GetAttrId(mod, &PyId___all__);
if (all == NULL)
PyErr_Clear();
else {
@ -3313,7 +3315,7 @@ import_submodule(PyObject *mod, PyObject *subname, PyObject *fullname)
if (mod == Py_None)
path_list = NULL;
else {
path_list = PyObject_GetAttrString(mod, "__path__");
path_list = _PyObject_GetAttrId(mod, &PyId___path__);
if (path_list == NULL) {
PyErr_Clear();
Py_INCREF(Py_None);
@ -3424,7 +3426,7 @@ PyImport_ReloadModule(PyObject *m)
goto error;
}
Py_DECREF(parentname);
path_list = PyObject_GetAttrString(parent, "__path__");
path_list = _PyObject_GetAttrId(parent, &PyId___path__);
if (path_list == NULL)
PyErr_Clear();
subname++;

View file

@ -141,12 +141,13 @@ get_codec_name(const char *encoding)
{
char *name_utf8, *name_str;
PyObject *codec, *name = NULL;
_Py_identifier(name);
codec = _PyCodec_Lookup(encoding);
if (!codec)
goto error;
name = PyObject_GetAttrString(codec, "name");
name = _PyObject_GetAttrId(codec, &PyId_name);
Py_CLEAR(codec);
if (!name)
goto error;
@ -830,7 +831,8 @@ create_stdio(PyObject* io,
goto error;
if (buffering) {
raw = PyObject_GetAttrString(buf, "raw");
_Py_identifier(raw);
raw = _PyObject_GetAttrId(buf, &PyId_raw);
if (raw == NULL)
goto error;
}
@ -1115,13 +1117,14 @@ PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags
PyArena *arena;
char *ps1 = "", *ps2 = "", *enc = NULL;
int errcode = 0;
_Py_identifier(encoding);
if (fp == stdin) {
/* Fetch encoding from sys.stdin */
v = PySys_GetObject("stdin");
if (v == NULL || v == Py_None)
return -1;
oenc = PyObject_GetAttrString(v, "encoding");
oenc = _PyObject_GetAttrId(v, &PyId_encoding);
if (!oenc)
return -1;
enc = _PyUnicode_AsString(oenc);
@ -1318,6 +1321,11 @@ parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
{
long hold;
PyObject *v;
_Py_identifier(msg);
_Py_identifier(filename);
_Py_identifier(lineno);
_Py_identifier(offset);
_Py_identifier(text);
/* old style errors */
if (PyTuple_Check(err))
@ -1326,11 +1334,11 @@ parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
/* new style errors. `err' is an instance */
if (! (v = PyObject_GetAttrString(err, "msg")))
if (! (v = _PyObject_GetAttrId(err, &PyId_msg)))
goto finally;
*message = v;
if (!(v = PyObject_GetAttrString(err, "filename")))
if (!(v = _PyObject_GetAttrId(err, &PyId_filename)))
goto finally;
if (v == Py_None)
*filename = NULL;
@ -1338,7 +1346,7 @@ parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
goto finally;
Py_DECREF(v);
if (!(v = PyObject_GetAttrString(err, "lineno")))
if (!(v = _PyObject_GetAttrId(err, &PyId_lineno)))
goto finally;
hold = PyLong_AsLong(v);
Py_DECREF(v);
@ -1347,7 +1355,7 @@ parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
goto finally;
*lineno = (int)hold;
if (!(v = PyObject_GetAttrString(err, "offset")))
if (!(v = _PyObject_GetAttrId(err, &PyId_offset)))
goto finally;
if (v == Py_None) {
*offset = -1;
@ -1362,7 +1370,7 @@ parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
*offset = (int)hold;
}
if (!(v = PyObject_GetAttrString(err, "text")))
if (!(v = _PyObject_GetAttrId(err, &PyId_text)))
goto finally;
if (v == Py_None)
*text = NULL;
@ -1431,7 +1439,8 @@ handle_system_exit(void)
goto done;
if (PyExceptionInstance_Check(value)) {
/* The error code should be in the `code' attribute. */
PyObject *code = PyObject_GetAttrString(value, "code");
_Py_identifier(code);
PyObject *code = _PyObject_GetAttrId(value, &PyId_code);
if (code) {
Py_DECREF(value);
value = code;
@ -1588,6 +1597,7 @@ print_exception(PyObject *f, PyObject *value)
else {
PyObject* moduleName;
char* className;
_Py_identifier(__module__);
assert(PyExceptionClass_Check(type));
className = PyExceptionClass_Name(type);
if (className != NULL) {
@ -1596,7 +1606,7 @@ print_exception(PyObject *f, PyObject *value)
className = dot+1;
}
moduleName = PyObject_GetAttrString(type, "__module__");
moduleName = _PyObject_GetAttrId(type, &PyId___module__);
if (moduleName == NULL || !PyUnicode_Check(moduleName))
{
Py_XDECREF(moduleName);

View file

@ -79,8 +79,10 @@ sys_displayhook_unencodable(PyObject *outf, PyObject *o)
PyObject *encoded, *escaped_str, *repr_str, *buffer, *result;
char *stdout_encoding_str;
int ret;
_Py_identifier(encoding);
_Py_identifier(buffer);
stdout_encoding = PyObject_GetAttrString(outf, "encoding");
stdout_encoding = _PyObject_GetAttrId(outf, &PyId_encoding);
if (stdout_encoding == NULL)
goto error;
stdout_encoding_str = _PyUnicode_AsString(stdout_encoding);
@ -97,7 +99,7 @@ sys_displayhook_unencodable(PyObject *outf, PyObject *o)
if (encoded == NULL)
goto error;
buffer = PyObject_GetAttrString(outf, "buffer");
buffer = _PyObject_GetAttrId(outf, &PyId_buffer);
if (buffer) {
_Py_identifier(write);
result = _PyObject_CallMethodId(buffer, &PyId_write, "(O)", encoded);
@ -1841,11 +1843,12 @@ sys_pyfile_write_unicode(PyObject *unicode, PyObject *file)
{
PyObject *writer = NULL, *args = NULL, *result = NULL;
int err;
_Py_identifier(write);
if (file == NULL)
return -1;
writer = PyObject_GetAttrString(file, "write");
writer = _PyObject_GetAttrId(file, &PyId_write);
if (writer == NULL)
goto error;