[Python-Dev] Automatic Scope Macros

Calvin Spealman ironfroggy at gmail.com
Tue Apr 24 17:36:13 CEST 2007

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;\

/* Obtain a Borrowed Reference and forget about it at the end of the scope. */
#define Py_FORGETREF(_refname, _newref, _scopecode)\
	(PyObject *) _refname = _newref;
	_refname = NULL;

Read my blog! I depend on your acceptance of my opinion! I am interesting!

More information about the Python-Dev mailing list