[Python-Dev] Automatic Scope Macros

Georg Brandl g.brandl at gmx.net
Tue Apr 24 22:59:35 CEST 2007


Calvin Spealman schrieb:
> Could we use and add this macro to object.h? It could be a much
> cleaner and safer way of dealing with new references you want to clean
> up in the same scope. The first one will make sure to decref your new
> reference when you are done with it. The second one will make sure to
> set a borrowed reference to NULL afterward, so you can't access it
> after you are done with it. The second might be useless, but it seems
> like it might be useful for debugging and being sure you stopped using
> a reference when you thought you did.
> 
> Anyone, please let me know if this is a dumb idea.
> 
> /* Obtain a New Reference and clean it up at the end of the scope. */
> #define Py_CLEANREF(_refname, _newref, _scopecode)\
> 	(PyObject *) _refname = _newref;\
> 	_scopecode;\
> 	Py_XDECREF(_refname);
> 
> /* Obtain a Borrowed Reference and forget about it at the end of the scope. */
> #define Py_FORGETREF(_refname, _newref, _scopecode)\
> 	(PyObject *) _refname = _newref;
> 	_scopecode;\
> 	_refname = NULL;

In most cases, you will have error checking in _scopecode which DECREFs the
variable and returns, or jumps to a "cleanup" label. I can't see how this would
be possible with these macros.

Georg



More information about the Python-Dev mailing list