Index: Objects/classobject.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Objects/classobject.c,v retrieving revision 2.154 diff -u -d -r2.154 classobject.c --- Objects/classobject.c 2001/10/26 17:56:51 2.154 +++ Objects/classobject.c 2002/01/11 16:36:49 @@ -201,32 +201,34 @@ class_getattr(register PyClassObject *op, PyObject *name) { register PyObject *v; - register char *sname = PyString_AsString(name); + register char *sname = PyString_AS_STRING(name); PyClassObject *class; descrgetfunc f; if (sname[0] == '_' && sname[1] == '_') { - if (strcmp(sname, "__dict__") == 0) { - if (PyEval_GetRestricted()) { - PyErr_SetString(PyExc_RuntimeError, - "class.__dict__ not accessible in restricted mode"); - return NULL; + int n = PyString_GET_SIZE(name); + if ((n == 8) && (sname[7] == '_') && (sname[6] == '_')) { /* "__XXXX__" */ + if ((sname[5] == 't') && (sname[4] == 'c') && (sname[3] == 'i') && (sname[2] == 'd')) { /* "__dict__" */ + if (PyEval_GetRestricted()) { + PyErr_SetString(PyExc_RuntimeError, + "class.__dict__ not accessible in restricted mode"); + return NULL; + } + Py_INCREF(op->cl_dict); + return op->cl_dict; + } else if ((sname[5] == 'e') && (sname[4] == 'm') && (sname[3] == 'a') && (sname[2] == 'n')) { /* "__name__" */ + if (op->cl_name == NULL) + v = Py_None; + else + v = op->cl_name; + Py_INCREF(v); + return v; } - Py_INCREF(op->cl_dict); - return op->cl_dict; - } - if (strcmp(sname, "__bases__") == 0) { + } else if ((n == 9) && (sname[8] == '_') && (sname[7] == '_') && (sname[6] == 's') && (sname[5] == 'e') && (sname[4] == 's') && (sname[3] == 'a') && (sname[2] == 'b')) { /* "__bases__" */ Py_INCREF(op->cl_bases); return op->cl_bases; } - if (strcmp(sname, "__name__") == 0) { - if (op->cl_name == NULL) - v = Py_None; - else - v = op->cl_name; - Py_INCREF(v); - return v; - } + } v = class_lookup(op, name, &class); if (v == NULL) { @@ -312,31 +314,33 @@ "classes are read-only in restricted mode"); return -1; } - sname = PyString_AsString(name); - if (sname[0] == '_' && sname[1] == '_') { - int n = PyString_Size(name); - if (sname[n-1] == '_' && sname[n-2] == '_') { - char *err = NULL; - if (strcmp(sname, "__dict__") == 0) + sname = PyString_AS_STRING(name); + if (sname[0] == '_' && sname[1] == '_') { /* "__..." */ + int n = PyString_GET_SIZE(name); + char *err = NULL; + if ((n == 8) && (sname[7] == '_') && (sname[6] == '_')) { /* "__XXXX__" */ + if ((sname[5] == 't') && (sname[4] == 'c') && (sname[3] == 'i') && (sname[2] == 'd')) /* "__dict__" */ err = set_dict(op, v); - else if (strcmp(sname, "__bases__") == 0) - err = set_bases(op, v); - else if (strcmp(sname, "__name__") == 0) + else if ((sname[5] == 'e') && (sname[4] == 'm') && (sname[3] == 'a') && (sname[2] == 'n')) /* "__name__" */ err = set_name(op, v); - else if (strcmp(sname, "__getattr__") == 0) - set_slot(&op->cl_getattr, v); - else if (strcmp(sname, "__setattr__") == 0) - set_slot(&op->cl_setattr, v); - else if (strcmp(sname, "__delattr__") == 0) + } else if ((n == 9) && (sname[8] == '_') && (sname[7] == '_') && (sname[6] == 's') && (sname[5] == 'e') && (sname[4] == 's') && (sname[3] == 'a') && (sname[2] == 'b')) /* "__bases__" */ + err = set_bases(op, v); + else if ((n == 11) && (sname[10] == '_') && (sname[9] == '_') && (sname[8] == 'r') && (sname[7] == 't') && (sname[6] == 't') && (sname[5] == 'a') && (sname[3] == 'e')) /* "__?e?attr__" */ + if (sname[4] == 't') { /* "__?etattr__" */ + if (sname[2] == 'g') /* "__getattr__" */ + set_slot(&op->cl_getattr, v); + else if (sname[2] == 's') /* "__setattr__" */ + set_slot(&op->cl_setattr, v); + } else if ((sname[4] == 'l') && (sname[2] == 'd')) /* "__delattr__" */ set_slot(&op->cl_delattr, v); - /* For the last three, we fall through to update the - dictionary as well. */ - if (err != NULL) { - if (*err == '\0') - return 0; - PyErr_SetString(PyExc_TypeError, err); - return -1; - } + + /* For the last three, we fall through to update the + dictionary as well. */ + if (err != NULL) { + if (*err == '\0') + return 0; + PyErr_SetString(PyExc_TypeError, err); + return -1; } } if (v == NULL) { @@ -386,11 +390,11 @@ n = PyString_Size(name); res = PyString_FromStringAndSize((char *)NULL, m+1+n); if (res != NULL) { - char *s = PyString_AsString(res); - memcpy(s, PyString_AsString(mod), m); + char *s = PyString_AS_STRING(res); + memcpy(s, PyString_AS_STRING(mod), m); s += m; *s++ = '.'; - memcpy(s, PyString_AsString(name), n); + memcpy(s, PyString_AS_STRING(name), n); } return res; } @@ -657,18 +661,19 @@ instance_getattr1(register PyInstanceObject *inst, PyObject *name) { register PyObject *v; - register char *sname = PyString_AsString(name); + register char *sname = PyString_AS_STRING(name); if (sname[0] == '_' && sname[1] == '_') { - if (strcmp(sname, "__dict__") == 0) { + int n = PyString_GET_SIZE(name); + if (( n == 8) && (sname[7] == '_') && (sname[6] == '_') && (sname[2] == 'd') && (sname[3] == 'i') && (sname[4] == 'c') && (sname[5] == 't')) { + /* '__dict__' */ if (PyEval_GetRestricted()) { PyErr_SetString(PyExc_RuntimeError, - "instance.__dict__ not accessible in restricted mode"); + "instance.__dict__ not accessible in restricted mode"); return NULL; } Py_INCREF(inst->in_dict); return inst->in_dict; - } - if (strcmp(sname, "__class__") == 0) { + } else if ((n == 9) && (sname[8] == '_') && (sname[7] == '_') && (sname[2] == 'c') && (sname[3] == 'l') && (sname[4] == 'a') && (sname[5] == 's') && (sname[6] == 's')) { /* '__class__' */ Py_INCREF(inst->in_class); return (PyObject *)inst->in_class; } @@ -745,44 +750,41 @@ instance_setattr(PyInstanceObject *inst, PyObject *name, PyObject *v) { PyObject *func, *args, *res, *tmp; - char *sname = PyString_AsString(name); + char *sname = PyString_AS_STRING(name); if (sname[0] == '_' && sname[1] == '_') { - int n = PyString_Size(name); - if (sname[n-1] == '_' && sname[n-2] == '_') { - if (strcmp(sname, "__dict__") == 0) { - if (PyEval_GetRestricted()) { - PyErr_SetString(PyExc_RuntimeError, - "__dict__ not accessible in restricted mode"); - return -1; - } - if (v == NULL || !PyDict_Check(v)) { - PyErr_SetString(PyExc_TypeError, - "__dict__ must be set to a dictionary"); - return -1; - } - tmp = inst->in_dict; - Py_INCREF(v); - inst->in_dict = v; - Py_DECREF(tmp); - return 0; + int n = PyString_GET_SIZE(name); + if ((n == 8) && (sname[7] == '_') && (sname[6] == '_') && (sname[5] == 't') && (sname[4] == 'c') && (sname[3] == 'i') && (sname[2] == 'd')) { /* "__dict__" */ + if (PyEval_GetRestricted()) { + PyErr_SetString(PyExc_RuntimeError, + "__dict__ not accessible in restricted mode"); + return -1; } - if (strcmp(sname, "__class__") == 0) { - if (PyEval_GetRestricted()) { - PyErr_SetString(PyExc_RuntimeError, - "__class__ not accessible in restricted mode"); - return -1; - } - if (v == NULL || !PyClass_Check(v)) { - PyErr_SetString(PyExc_TypeError, - "__class__ must be set to a class"); - return -1; - } - tmp = (PyObject *)(inst->in_class); - Py_INCREF(v); - inst->in_class = (PyClassObject *)v; - Py_DECREF(tmp); - return 0; + if (v == NULL || !PyDict_Check(v)) { + PyErr_SetString(PyExc_TypeError, + "__dict__ must be set to a dictionary"); + return -1; } + tmp = inst->in_dict; + Py_INCREF(v); + inst->in_dict = v; + Py_DECREF(tmp); + return 0; + } else if ((n == 9) && (sname[8] == '_') && (sname[7] == '_') && (sname[2] == 'c') && (sname[3] == 'l') && (sname[4] == 'a') && (sname[5] == 's') && (sname[6] == 's')) { /* " __class__" */ + if (PyEval_GetRestricted()) { + PyErr_SetString(PyExc_RuntimeError, + "__class__ not accessible in restricted mode"); + return -1; + } + if (v == NULL || !PyClass_Check(v)) { + PyErr_SetString(PyExc_TypeError, + "__class__ must be set to a class"); + return -1; + } + tmp = (PyObject *)(inst->in_class); + Py_INCREF(v); + inst->in_class = (PyClassObject *)v; + Py_DECREF(tmp); + return 0; } } if (v == NULL)