CPython detecting read-only attributes.

Jeremy Lowery jslowery at hotmail.com
Sat Sep 14 16:35:08 EDT 2002


I believe I found a hacky way to do this. I am sure that this isn't the
fastest way.  I wonder if it can easily be shot down.

static int
proto_setattro(HyProtoObject *self, PyObject *name, PyObject *value)
{
 int res;
 PyObject *tmp;
 PyObject *etype, *evalue, *etb;

 res = PyObject_GenericSetAttr((PyObject *)self, name, value);
 if(res != -1)
  return res;
 /* if we can get the attribute with GetAttr, then it must be readonly.
  */

 PyErr_Fetch(&etype, &evalue, &etb);
 if(etype == NULL) {
  PyErr_SetString(PyExc_SystemError, "No error object found after a failed "
   "attribute lookup.");
  return -1;
 }

 tmp = PyObject_GenericGetAttr((PyObject*)self, name);
 if(tmp != NULL) {
  /* reference found, readonly */
  Py_DECREF(tmp);
  PyErr_Restore(etype, evalue, etb);
  return -1;
 }

 PyErr_Clear();

/* continue with custom processing */

J Lowery







More information about the Python-list mailing list