[Python-Dev] Py_SETREF/Py_XSETREF (was: Why is type_modified() in typeobject.c not a public function?) (original) (raw)

Stefan Behnel stefan_ml at behnel.de
Thu May 29 11:55:38 CEST 2008


Antoine Pitrou wrote:

Stefan Behnel <stefanml behnel.de> writes:

BTW, I noticed that the code in typeobject.c uses "DECREF before set" two times, like this:

methodcache[h].version = type->tpversiontag; methodcache[h].value = res; /* borrowed */ PyINCREF(name); PyDECREF(methodcache[h].name); methodcache[h].name = name; Since this is so common, would it be useful to define a macro for the correct construct? Something like _#define PySETREF(var, obj) _ _{ PyObject *tmp = (var); PyINCREF(obj); _ (var) = (obj); PyXDECREF(tmp); } Or, if we want to allow more optimizations, make two versions of it: _#define PySETREF(var, obj) _ _{ PyObject *tmp = (var); PyINCREF(obj); _ (var) = (obj); PyDECREF(tmp); } _#define PyXSETREF(var, obj) _ _{ PyObject *tmp = (var); PyINCREF(obj); _ (var) = (obj); PyXDECREF(tmp); }

Both sound like a good idea to me. Having to think about whether or not DECREF poses a problem in a specific case is just cumbersome.

Stefan



More information about the Python-Dev mailing list