Stefan Behnel <stefan_ml <at> behnel.de> writes:
> BTW, I noticed that the code in typeobject.c uses "DECREF before set" two
> times, like this:
> 
>               method_cache[h].version = type->tp_version_tag;
>               method_cache[h].value = res;  /* borrowed */
>               Py_INCREF(name);
>               Py_DECREF(method_cache[h].name);
>               method_cache[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.


_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to