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

View file

@ -767,8 +767,9 @@ static PyObject *
deque_reduce(dequeobject *deque)
{
PyObject *dict, *result, *aslist;
_Py_identifier(__dict__);
dict = PyObject_GetAttrString((PyObject *)deque, "__dict__");
dict = _PyObject_GetAttrId((PyObject *)deque, &PyId___dict__);
if (dict == NULL)
PyErr_Clear();
aslist = PySequence_List((PyObject *)deque);

View file

@ -1317,6 +1317,7 @@ csv_writer(PyObject *module, PyObject *args, PyObject *keyword_args)
{
PyObject * output_file, * dialect = NULL;
WriterObj * self = PyObject_GC_New(WriterObj, &Writer_Type);
_Py_identifier(write);
if (!self)
return NULL;
@ -1333,7 +1334,7 @@ csv_writer(PyObject *module, PyObject *args, PyObject *keyword_args)
Py_DECREF(self);
return NULL;
}
self->writeline = PyObject_GetAttrString(output_file, "write");
self->writeline = _PyObject_GetAttrId(output_file, &PyId_write);
if (self->writeline == NULL || !PyCallable_Check(self->writeline)) {
PyErr_SetString(PyExc_TypeError,
"argument 1 must have a \"write\" method");

View file

@ -3077,12 +3077,14 @@ tzinfo_reduce(PyObject *self)
{
PyObject *args, *state, *tmp;
PyObject *getinitargs, *getstate;
_Py_identifier(__getinitargs__);
_Py_identifier(__getstate__);
tmp = PyTuple_New(0);
if (tmp == NULL)
return NULL;
getinitargs = PyObject_GetAttrString(self, "__getinitargs__");
getinitargs = _PyObject_GetAttrId(self, &PyId___getinitargs__);
if (getinitargs != NULL) {
args = PyObject_CallObject(getinitargs, tmp);
Py_DECREF(getinitargs);
@ -3097,7 +3099,7 @@ tzinfo_reduce(PyObject *self)
Py_INCREF(args);
}
getstate = PyObject_GetAttrString(self, "__getstate__");
getstate = _PyObject_GetAttrId(self, &PyId___getstate__);
if (getstate != NULL) {
state = PyObject_CallObject(getstate, tmp);
Py_DECREF(getstate);

View file

@ -1126,6 +1126,7 @@ scanner_init(PyObject *self, PyObject *args, PyObject *kwds)
PyObject *ctx;
static char *kwlist[] = {"context", NULL};
PyScannerObject *s;
_Py_identifier(strict);
assert(PyScanner_Check(self));
s = (PyScannerObject *)self;

View file

@ -826,8 +826,9 @@ _Pickler_SetProtocol(PicklerObject *self, PyObject *proto_obj,
static int
_Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
{
_Py_identifier(write);
assert(file != NULL);
self->write = PyObject_GetAttrString(file, "write");
self->write = _PyObject_GetAttrId(file, &PyId_write);
if (self->write == NULL) {
if (PyErr_ExceptionMatches(PyExc_AttributeError))
PyErr_SetString(PyExc_TypeError,
@ -1173,15 +1174,19 @@ _Unpickler_New(void)
static int
_Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
{
self->peek = PyObject_GetAttrString(file, "peek");
_Py_identifier(peek);
_Py_identifier(read);
_Py_identifier(readline);
self->peek = _PyObject_GetAttrId(file, &PyId_peek);
if (self->peek == NULL) {
if (PyErr_ExceptionMatches(PyExc_AttributeError))
PyErr_Clear();
else
return -1;
}
self->read = PyObject_GetAttrString(file, "read");
self->readline = PyObject_GetAttrString(file, "readline");
self->read = _PyObject_GetAttrId(file, &PyId_read);
self->readline = _PyObject_GetAttrId(file, &PyId_readline);
if (self->readline == NULL || self->read == NULL) {
if (PyErr_ExceptionMatches(PyExc_AttributeError))
PyErr_SetString(PyExc_TypeError,
@ -3390,6 +3395,7 @@ Pickler_init(PicklerObject *self, PyObject *args, PyObject *kwds)
PyObject *file;
PyObject *proto_obj = NULL;
PyObject *fix_imports = Py_True;
_Py_identifier(persistent_id);
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OO:Pickler",
kwlist, &file, &proto_obj, &fix_imports))
@ -3425,9 +3431,9 @@ Pickler_init(PicklerObject *self, PyObject *args, PyObject *kwds)
self->fast_nesting = 0;
self->fast_memo = NULL;
self->pers_func = NULL;
if (PyObject_HasAttrString((PyObject *)self, "persistent_id")) {
self->pers_func = PyObject_GetAttrString((PyObject *)self,
"persistent_id");
if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) {
self->pers_func = _PyObject_GetAttrId((PyObject *)self,
&PyId_persistent_id);
if (self->pers_func == NULL)
return -1;
}
@ -4935,8 +4941,9 @@ do_append(UnpicklerObject *self, Py_ssize_t x)
}
else {
PyObject *append_func;
_Py_identifier(append);
append_func = PyObject_GetAttrString(list, "append");
append_func = _PyObject_GetAttrId(list, &PyId_append);
if (append_func == NULL)
return -1;
for (i = x; i < len; i++) {
@ -5023,6 +5030,7 @@ load_build(UnpicklerObject *self)
PyObject *state, *inst, *slotstate;
PyObject *setstate;
int status = 0;
_Py_identifier(__setstate__);
/* Stack is ... instance, state. We want to leave instance at
* the stack top, possibly mutated via instance.__setstate__(state).
@ -5036,7 +5044,7 @@ load_build(UnpicklerObject *self)
inst = self->stack->data[Py_SIZE(self->stack) - 1];
setstate = PyObject_GetAttrString(inst, "__setstate__");
setstate = _PyObject_GetAttrId(inst, &PyId___setstate__);
if (setstate == NULL) {
if (PyErr_ExceptionMatches(PyExc_AttributeError))
PyErr_Clear();
@ -5079,12 +5087,13 @@ load_build(UnpicklerObject *self)
PyObject *dict;
PyObject *d_key, *d_value;
Py_ssize_t i;
_Py_identifier(__dict__);
if (!PyDict_Check(state)) {
PyErr_SetString(UnpicklingError, "state is not a dictionary");
goto error;
}
dict = PyObject_GetAttrString(inst, "__dict__");
dict = _PyObject_GetAttrId(inst, &PyId___dict__);
if (dict == NULL)
goto error;
@ -5584,8 +5593,9 @@ Unpickler_init(UnpicklerObject *self, PyObject *args, PyObject *kwds)
return -1;
if (PyObject_HasAttrString((PyObject *)self, "persistent_load")) {
self->pers_func = PyObject_GetAttrString((PyObject *)self,
"persistent_load");
_Py_identifier(persistent_load);
self->pers_func = _PyObject_GetAttrId((PyObject *)self,
&PyId_persistent_load);
if (self->pers_func == NULL)
return -1;
}

View file

@ -2003,14 +2003,16 @@ array_reduce_ex(arrayobject *array, PyObject *value)
int mformat_code;
static PyObject *array_reconstructor = NULL;
long protocol;
_Py_identifier(_array_reconstructor);
_Py_identifier(__dict__);
if (array_reconstructor == NULL) {
PyObject *array_module = PyImport_ImportModule("array");
if (array_module == NULL)
return NULL;
array_reconstructor = PyObject_GetAttrString(
array_reconstructor = _PyObject_GetAttrId(
array_module,
"_array_reconstructor");
&PyId__array_reconstructor);
Py_DECREF(array_module);
if (array_reconstructor == NULL)
return NULL;
@ -2025,7 +2027,7 @@ array_reduce_ex(arrayobject *array, PyObject *value)
if (protocol == -1 && PyErr_Occurred())
return NULL;
dict = PyObject_GetAttrString((PyObject *)array, "__dict__");
dict = _PyObject_GetAttrId((PyObject *)array, &PyId___dict__);
if (dict == NULL) {
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
return NULL;

View file

@ -3241,10 +3241,13 @@ PyInit_parser(void)
copyreg = PyImport_ImportModuleNoBlock("copyreg");
if (copyreg != NULL) {
PyObject *func, *pickler;
_Py_identifier(pickle);
_Py_identifier(sequence2st);
_Py_identifier(_pickler);
func = PyObject_GetAttrString(copyreg, "pickle");
pickle_constructor = PyObject_GetAttrString(module, "sequence2st");
pickler = PyObject_GetAttrString(module, "_pickler");
func = _PyObject_GetAttrId(copyreg, &PyId_pickle);
pickle_constructor = _PyObject_GetAttrId(module, &PyId_sequence2st);
pickler = _PyObject_GetAttrId(module, &PyId__pickler);
Py_XINCREF(pickle_constructor);
if ((func != NULL) && (pickle_constructor != NULL)
&& (pickler != NULL)) {

View file

@ -6103,6 +6103,7 @@ wait_helper(pid_t pid, int status, struct rusage *ru)
{
PyObject *result;
static PyObject *struct_rusage;
_Py_identifier(struct_rusage);
if (pid == -1)
return posix_error();
@ -6111,7 +6112,7 @@ wait_helper(pid_t pid, int status, struct rusage *ru)
PyObject *m = PyImport_ImportModuleNoBlock("resource");
if (m == NULL)
return NULL;
struct_rusage = PyObject_GetAttrString(m, "struct_rusage");
struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage);
Py_DECREF(m);
if (struct_rusage == NULL)
return NULL;

View file

@ -843,9 +843,9 @@ xmlparse_ParseFile(xmlparseobject *self, PyObject *f)
{
int rv = 1;
PyObject *readmethod = NULL;
_Py_identifier(read);
readmethod = PyObject_GetAttrString(f, "read");
readmethod = _PyObject_GetAttrId(f, &PyId_read);
if (readmethod == NULL) {
PyErr_SetString(PyExc_TypeError,
"argument must have 'read' attribute");

View file

@ -908,6 +908,7 @@ get_decompress_func(void)
static int importing_zlib = 0;
PyObject *zlib;
PyObject *decompress;
_Py_identifier(decompress);
if (importing_zlib != 0)
/* Someone has a zlib.py[co] in their Zip file;
@ -917,8 +918,8 @@ get_decompress_func(void)
zlib = PyImport_ImportModuleNoBlock("zlib");
importing_zlib = 0;
if (zlib != NULL) {
decompress = PyObject_GetAttrString(zlib,
"decompress");
decompress = _PyObject_GetAttrId(zlib,
&PyId_decompress);
Py_DECREF(zlib);
}
else {

View file

@ -2699,13 +2699,15 @@ static PyObject *
bytearray_reduce(PyByteArrayObject *self)
{
PyObject *latin1, *dict;
_Py_identifier(__dict__);
if (self->ob_bytes)
latin1 = PyUnicode_DecodeLatin1(self->ob_bytes,
Py_SIZE(self), NULL);
else
latin1 = PyUnicode_FromString("");
dict = PyObject_GetAttrString((PyObject *)self, "__dict__");
dict = _PyObject_GetAttrId((PyObject *)self, &PyId___dict__);
if (dict == NULL) {
PyErr_Clear();
dict = Py_None;

View file

@ -14,6 +14,8 @@ static int numfree = 0;
#define PyMethod_MAXFREELIST 256
#endif
_Py_identifier(__name__);
PyObject *
PyMethod_Function(PyObject *im)
{
@ -226,7 +228,7 @@ method_repr(PyMethodObject *a)
return NULL;
}
funcname = PyObject_GetAttrString(func, "__name__");
funcname = _PyObject_GetAttrId(func, &PyId___name__);
if (funcname == NULL) {
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
return NULL;
@ -240,7 +242,7 @@ method_repr(PyMethodObject *a)
if (klass == NULL)
klassname = NULL;
else {
klassname = PyObject_GetAttrString(klass, "__name__");
klassname = _PyObject_GetAttrId(klass, &PyId___name__);
if (klassname == NULL) {
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
return NULL;
@ -542,7 +544,7 @@ instancemethod_repr(PyObject *self)
return NULL;
}
funcname = PyObject_GetAttrString(func, "__name__");
funcname = _PyObject_GetAttrId(func, &PyId___name__);
if (funcname == NULL) {
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
return NULL;

View file

@ -1299,7 +1299,8 @@ property_init(PyObject *self, PyObject *args, PyObject *kwds)
/* if no docstring given and the getter has one, use that one */
if ((doc == NULL || doc == Py_None) && get != NULL) {
PyObject *get_doc = PyObject_GetAttrString(get, "__doc__");
_Py_identifier(__doc__);
PyObject *get_doc = _PyObject_GetAttrId(get, &PyId___doc__);
if (get_doc) {
if (Py_TYPE(self) == &PyProperty_Type) {
Py_XDECREF(prop->prop_doc);

View file

@ -59,8 +59,9 @@ PyFile_GetLine(PyObject *f, int n)
{
PyObject *reader;
PyObject *args;
_Py_identifier(readline);
reader = PyObject_GetAttrString(f, "readline");
reader = _PyObject_GetAttrId(f, &PyId_readline);
if (reader == NULL)
return NULL;
if (n <= 0)
@ -127,11 +128,13 @@ int
PyFile_WriteObject(PyObject *v, PyObject *f, int flags)
{
PyObject *writer, *value, *args, *result;
_Py_identifier(write);
if (f == NULL) {
PyErr_SetString(PyExc_TypeError, "writeobject with NULL file");
return -1;
}
writer = PyObject_GetAttrString(f, "write");
writer = _PyObject_GetAttrId(f, &PyId_write);
if (writer == NULL)
return -1;
if (flags & Py_PRINT_RAW) {
@ -194,11 +197,12 @@ PyObject_AsFileDescriptor(PyObject *o)
{
int fd;
PyObject *meth;
_Py_identifier(fileno);
if (PyLong_Check(o)) {
fd = PyLong_AsLong(o);
}
else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL)
else if ((meth = _PyObject_GetAttrId(o, &PyId_fileno)) != NULL)
{
PyObject *fno = PyEval_CallObject(meth, NULL);
Py_DECREF(meth);

View file

@ -415,8 +415,9 @@ module_clear(PyModuleObject *m)
static PyObject *
module_dir(PyObject *self, PyObject *args)
{
_Py_identifier(__dict__);
PyObject *result = NULL;
PyObject *dict = PyObject_GetAttrString(self, "__dict__");
PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__);
if (dict != NULL) {
if (PyDict_Check(dict))

View file

@ -1933,6 +1933,7 @@ static PyObject *
set_reduce(PySetObject *so)
{
PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
_Py_identifier(__dict__);
keys = PySequence_List((PyObject *)so);
if (keys == NULL)
@ -1940,7 +1941,7 @@ set_reduce(PySetObject *so)
args = PyTuple_Pack(1, keys);
if (args == NULL)
goto done;
dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
dict = _PyObject_GetAttrId((PyObject *)so, &PyId___dict__);
if (dict == NULL) {
PyErr_Clear();
dict = Py_None;

View file

@ -35,6 +35,9 @@ struct method_cache_entry {
static struct method_cache_entry method_cache[1 << MCACHE_SIZE_EXP];
static unsigned int next_version_tag = 0;
_Py_identifier(__dict__);
_Py_identifier(__class__);
unsigned int
PyType_ClearCache(void)
{
@ -1281,7 +1284,8 @@ tail_contains(PyObject *list, int whence, PyObject *o) {
static PyObject *
class_name(PyObject *cls)
{
PyObject *name = PyObject_GetAttrString(cls, "__name__");
_Py_identifier(__name__);
PyObject *name = _PyObject_GetAttrId(cls, &PyId___name__);
if (name == NULL) {
PyErr_Clear();
Py_XDECREF(name);
@ -1709,15 +1713,14 @@ get_builtin_base_with_dict(PyTypeObject *type)
static PyObject *
get_dict_descriptor(PyTypeObject *type)
{
static PyObject *dict_str;
PyObject *dict_str;
PyObject *descr;
if (dict_str == NULL) {
dict_str = PyUnicode_InternFromString("__dict__");
if (dict_str == NULL)
return NULL;
}
dict_str = _PyUnicode_FromId(&PyId___dict__);
if (dict_str == NULL)
return NULL;
descr = _PyType_Lookup(type, dict_str);
Py_DECREF(dict_str);
if (descr == NULL || !PyDescr_IsData(descr))
return NULL;
@ -2596,12 +2599,13 @@ merge_class_dict(PyObject *dict, PyObject *aclass)
{
PyObject *classdict;
PyObject *bases;
_Py_identifier(__bases__);
assert(PyDict_Check(dict));
assert(aclass);
/* Merge in the type's dict (if any). */
classdict = PyObject_GetAttrString(aclass, "__dict__");
classdict = _PyObject_GetAttrId(aclass, &PyId___dict__);
if (classdict == NULL)
PyErr_Clear();
else {
@ -2612,7 +2616,7 @@ merge_class_dict(PyObject *dict, PyObject *aclass)
}
/* Recursively merge in the base types' (if any) dicts. */
bases = PyObject_GetAttrString(aclass, "__bases__");
bases = _PyObject_GetAttrId(aclass, &PyId___bases__);
if (bases == NULL)
PyErr_Clear();
else {
@ -3540,7 +3544,7 @@ object_dir(PyObject *self, PyObject *args)
PyObject *itsclass = NULL;
/* Get __dict__ (which may or may not be a real dict...) */
dict = PyObject_GetAttrString(self, "__dict__");
dict = _PyObject_GetAttrId(self, &PyId___dict__);
if (dict == NULL) {
PyErr_Clear();
dict = PyDict_New();
@ -3560,7 +3564,7 @@ object_dir(PyObject *self, PyObject *args)
goto error;
/* Merge in attrs reachable from its class. */
itsclass = PyObject_GetAttrString(self, "__class__");
itsclass = _PyObject_GetAttrId(self, &PyId___class__);
if (itsclass == NULL)
/* XXX(tomer): Perhaps fall back to obj->ob_type if no
__class__ exists? */
@ -6304,16 +6308,15 @@ supercheck(PyTypeObject *type, PyObject *obj)
}
else {
/* Try the slow way */
static PyObject *class_str = NULL;
PyObject *class_str = NULL;
PyObject *class_attr;
if (class_str == NULL) {
class_str = PyUnicode_FromString("__class__");
if (class_str == NULL)
return NULL;
}
class_str = _PyUnicode_FromId(&PyId___class__);
if (class_str == NULL)
return NULL;
class_attr = PyObject_GetAttr(obj, class_str);
Py_DECREF(class_str);
if (class_attr != NULL &&
PyType_Check(class_attr) &&

View file

@ -157,11 +157,12 @@ static PyObject *
weakref_repr(PyWeakReference *self)
{
PyObject *name, *repr;
_Py_identifier(__name__);
if (PyWeakref_GET_OBJECT(self) == Py_None)
return PyUnicode_FromFormat("<weakref at %p; dead>", self);
name = PyObject_GetAttrString(PyWeakref_GET_OBJECT(self), "__name__");
name = _PyObject_GetAttrId(PyWeakref_GET_OBJECT(self), &PyId___name__);
if (name == NULL || !PyUnicode_Check(name)) {
if (name == NULL)
PyErr_Clear();

View file

@ -85,8 +85,16 @@ class EmitVisitor(asdl.VisitorBase):
def __init__(self, file):
self.file = file
self.identifiers = set()
super(EmitVisitor, self).__init__()
def emit_identifier(self, name):
name = str(name)
if name in self.identifiers:
return
self.emit("_Py_identifier(%s);" % name, 0)
self.identifiers.add(name)
def emit(self, s, depth, reflow=True):
# XXX reflow long lines?
if reflow:
@ -486,12 +494,12 @@ def isSimpleType(self, field):
def visitField(self, field, name, sum=None, prod=None, depth=0):
ctype = get_c_type(field.type)
self.emit("if (PyObject_HasAttrString(obj, \"%s\")) {" % field.name, depth)
self.emit("if (_PyObject_HasAttrId(obj, &PyId_%s)) {" % field.name, depth)
self.emit("int res;", depth+1)
if field.seq:
self.emit("Py_ssize_t len;", depth+1)
self.emit("Py_ssize_t i;", depth+1)
self.emit("tmp = PyObject_GetAttrString(obj, \"%s\");" % field.name, depth+1)
self.emit("tmp = _PyObject_GetAttrId(obj, &PyId_%s);" % field.name, depth+1)
self.emit("if (tmp == NULL) goto failed;", depth+1)
if field.seq:
self.emit("if (!PyList_Check(tmp)) {", depth+1)
@ -553,6 +561,8 @@ def visitProduct(self, prod, name):
self.emit("static PyTypeObject *%s_type;" % name, 0)
self.emit("static PyObject* ast2obj_%s(void*);" % name, 0)
if prod.fields:
for f in prod.fields:
self.emit_identifier(f.name)
self.emit("static char *%s_fields[]={" % name,0)
for f in prod.fields:
self.emit('"%s",' % f.name, 1)
@ -561,6 +571,8 @@ def visitProduct(self, prod, name):
def visitSum(self, sum, name):
self.emit("static PyTypeObject *%s_type;" % name, 0)
if sum.attributes:
for a in sum.attributes:
self.emit_identifier(a.name)
self.emit("static char *%s_attributes[] = {" % name, 0)
for a in sum.attributes:
self.emit('"%s",' % a.name, 1)
@ -580,6 +592,8 @@ def visitSum(self, sum, name):
def visitConstructor(self, cons, name):
self.emit("static PyTypeObject *%s_type;" % cons.name, 0)
if cons.fields:
for t in cons.fields:
self.emit_identifier(t.name)
self.emit("static char *%s_fields[]={" % cons.name, 0)
for t in cons.fields:
self.emit('"%s",' % t.name, 1)
@ -592,10 +606,11 @@ def visitModule(self, mod):
static int
ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
{
_Py_identifier(_fields);
Py_ssize_t i, numfields = 0;
int res = -1;
PyObject *key, *value, *fields;
fields = PyObject_GetAttrString((PyObject*)Py_TYPE(self), "_fields");
fields = _PyObject_GetAttrId((PyObject*)Py_TYPE(self), &PyId__fields);
if (!fields)
PyErr_Clear();
if (fields) {
@ -645,7 +660,8 @@ def visitModule(self, mod):
ast_type_reduce(PyObject *self, PyObject *unused)
{
PyObject *res;
PyObject *dict = PyObject_GetAttrString(self, "__dict__");
_Py_identifier(__dict__);
PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__);
if (dict == NULL) {
if (PyErr_ExceptionMatches(PyExc_AttributeError))
PyErr_Clear();

View file

@ -463,6 +463,7 @@ fp_setreadl(struct tok_state *tok, const char* enc)
{
PyObject *readline = NULL, *stream = NULL, *io = NULL;
_Py_identifier(open);
_Py_identifier(readline);
int fd;
io = PyImport_ImportModuleNoBlock("io");
@ -481,7 +482,7 @@ fp_setreadl(struct tok_state *tok, const char* enc)
goto cleanup;
Py_XDECREF(tok->decoding_readline);
readline = PyObject_GetAttrString(stream, "readline");
readline = _PyObject_GetAttrId(stream, &PyId_readline);
tok->decoding_readline = readline;
/* The file has been reopened; parsing will restart from

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;