[Python-checkins] CVS: python/dist/src/Objects funcobject.c,2.39,2.40

Barry Warsaw bwarsaw@users.sourceforge.net
Tue, 14 Aug 2001 11:24:00 -0700


Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv30602

Modified Files:
	funcobject.c 
Log Message:
func_getattro(), func_setattro(): Implement the new semantics for
    setting and deleting a function's __dict__ attribute.  Deleting
    it, or setting it to a non-dictionary result in a TypeError.  Note
    that getting it the first time magically initializes it to an
    empty dict so that func.__dict__ will always appear to be a
    dictionary (never None).

    Closes SF bug #446645.


Index: funcobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/funcobject.c,v
retrieving revision 2.39
retrieving revision 2.40
diff -C2 -d -r2.39 -r2.40
*** funcobject.c	2001/08/02 04:15:00	2.39
--- funcobject.c	2001/08/14 18:23:58	2.40
***************
*** 152,156 ****
  		return NULL;
  	}
! 
  	return PyObject_GenericGetAttr(op, name);
  }
--- 152,167 ----
  		return NULL;
  	}
! 	/* If func_dict is being accessed but no attribute has been set
! 	 * yet, then initialize it to the empty dictionary.
! 	 */
! 	if ((!strcmp(sname, "func_dict") || !strcmp(sname, "__dict__"))
! 	     && ((PyFunctionObject*)op)->func_dict == NULL)
! 	{
! 		PyFunctionObject* funcop = (PyFunctionObject*)op;
! 		
! 		funcop->func_dict = PyDict_New();
! 		if (funcop->func_dict == NULL)
! 			return NULL;
! 	}
  	return PyObject_GenericGetAttr(op, name);
  }
***************
*** 191,207 ****
  	}
  	else if (!strcmp(sname, "func_dict") || !strcmp(sname, "__dict__")) {
! 		/* legal to del f.func_dict.  Can only set func_dict to
! 		 * NULL or a dictionary.
  		 */
! 		if (value == Py_None)
! 			value = NULL;
! 		if (value != NULL && !PyDict_Check(value)) {
  			PyErr_SetString(
  				PyExc_TypeError,
! 				"func_dict must be set to a dict object");
  			return -1;
  		}
  	}
- 
  	return PyObject_GenericSetAttr(op, name, value);
  }
--- 202,221 ----
  	}
  	else if (!strcmp(sname, "func_dict") || !strcmp(sname, "__dict__")) {
! 		/* It is illegal to del f.func_dict.  Can only set
! 		 * func_dict to a dictionary.
  		 */
! 		if (value == NULL) {
  			PyErr_SetString(
  				PyExc_TypeError,
! 				"function's dictionary may not be deleted");
  			return -1;
  		}
+ 		if (!PyDict_Check(value)) {
+ 			PyErr_SetString(
+ 				PyExc_TypeError,
+ 				"setting function's dictionary to a non-dict");
+ 			return -1;
+ 		}
  	}
  	return PyObject_GenericSetAttr(op, name, value);
  }