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 {