Use interned strings to optimise comparisons in settattro() of C implementation.

This commit is contained in:
Graham Dumpleton
2013-09-21 21:48:04 +10:00
parent 43bd3cd559
commit bdff638fc1

View File

@@ -1183,21 +1183,21 @@ static PyObject *WraptObjectProxy_getattro(
static int WraptObjectProxy_setattro(
WraptObjectProxyObject *self, PyObject *name, PyObject *value)
{
PyObject *self_prefix = NULL;
PyObject *attr_wrapped = NULL;
static PyObject *self_prefix = NULL;
static PyObject *attr_wrapped = NULL;
PyObject *match = NULL;
if (!self_prefix) {
#if PY_MAJOR_VERSION >= 3
self_prefix = PyUnicode_FromString("_self_");
self_prefix = PyUnicode_InternFromString("_self_");
#else
self_prefix = PyString_FromString("_self_");
self_prefix = PyString_InternFromString("_self_");
#endif
}
match = PyEval_CallMethod(name, "startswith", "(O)", self_prefix);
Py_DECREF(self_prefix);
if (match == Py_True) {
Py_DECREF(match);
@@ -1206,11 +1206,13 @@ static int WraptObjectProxy_setattro(
Py_XDECREF(match);
if (!attr_wrapped) {
#if PY_MAJOR_VERSION >= 3
attr_wrapped = PyUnicode_FromString("__wrapped__");
attr_wrapped = PyUnicode_InternFromString("__wrapped__");
#else
attr_wrapped = PyString_FromString("__wrapped__");
attr_wrapped = PyString_InternFromString("__wrapped__");
#endif
}
if (PyObject_RichCompareBool(name, attr_wrapped, Py_EQ) == 1) {
Py_DECREF(attr_wrapped);
@@ -1218,8 +1220,6 @@ static int WraptObjectProxy_setattro(
return PyObject_GenericSetAttr((PyObject *)self, name, value);
}
Py_DECREF(attr_wrapped);
if (!self->wrapped) {
PyErr_SetString(PyExc_ValueError, "wrapper has not been initialised");
return -1;