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

Antoine Pitrou solipsis at pitrou.net
Thu May 29 11:22:02 CEST 2008


Stefan Behnel <stefan_ml 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 Py_SETREF(var, obj)
{ PyObject *tmp = (var); Py_INCREF(obj);
(var) = (obj); Py_XDECREF(tmp); }

Or, if we want to allow more optimizations, make two versions of it:

#define Py_SETREF(var, obj)
{ PyObject *tmp = (var); Py_INCREF(obj);
(var) = (obj); Py_DECREF(tmp); }

#define Py_XSETREF(var, obj)
{ PyObject *tmp = (var); Py_INCREF(obj);
(var) = (obj); Py_XDECREF(tmp); }

Regards

Antoine.



More information about the Python-Dev mailing list