[Python-3000] Python/C++ question

Giovanni Bajo rasky at develer.com
Tue Dec 12 12:04:28 CET 2006


Martin v. Löwis wrote:

>> To me, the killer feature would be that in C++ you can implement a smart 
>> pointer which takes care of incref/decref automatically for 99% of the code. 
>> This would be a terrific tool for the extension/core writers.
> 
> Of course, this would also break in presence of binary-incompatible
> compilers, in particular when it relates to exception handling.

Details? Is that a problem, given that you can't compile Python core and 
extensions with different MSVC versions?

We could also avoid the smart pointer, but use cleanups anyway. For example:


struct ScopedIncRef
{
public:
     ScopedIncRef(PyObject *o) { Py_INCREF(o); }
     ~ScopedIncRef() { Py_DECREF(o); }
};

#define WITH_INCREF(o)
    for (ScopedIncRef _sc_ = ScopedIncRef(o), \
         bool _stat_ = true; \
         _stat_; \
         _stat_ = false)


Usage:

WITH_INCREF(o)
    // do something with o

or:

WITH_INCREF(o)
{
   // do
   // something
   // with o
}



A variadic version of this macro is also possible, of course:

WITH_INCREF(o1,o2,o3)
    // do something with them


(even if it requires using compiler-specific preprocessor extensions,
available at least on MSVC and GCC).
-- 
Giovanni Bajo



More information about the Python-3000 mailing list