[Python-Dev] Add Py_SETREF and Py_XSETREF to the stable C API

Raymond Hettinger raymond.hettinger at gmail.com
Wed Nov 8 21:08:44 EST 2017


> On Nov 8, 2017, at 8:30 AM, Serhiy Storchaka <storchaka at gmail.com> wrote:
> 
> Macros Py_SETREF and Py_XSETREF were introduced in 3.6 and backported to all maintained versions ([1] and [2]). Despite their names they are private. I think that they are enough stable now and would be helpful in third-party code. Are there any objections against adding them to the stable C API? [3]

I have mixed feeling about this.  You and Victor seem to really like these macros, but they have been problematic for me.  I'm not sure whether it is a conceptual issue or a naming issue, but the presence of these macros impairs my ability to read code and determine whether the refcounts are correct.  I usually end-up replacing the code with the unrolled macro so that I can count the refs across all the code paths.

The other issue is that when there are multiple occurrences of these macros for multiple variables, it interferes with my best practice of deferring all decrefs until the data structures are in a fully consistent state.  Any one of these can cause arbitrary code to run.  I greatly prefer putting all the decrefs at the end to increase my confidence that it is okay to run other code that might reenter the current code.  Pure python functions effectively have this built-in because the locals all get decreffed at the end of the function when a return-statement is encountered.  That practice helps me avoid hard to spot re-entrancy issues.

Lastly, I think we should have a preference to not grow the stable C API.  Bigger APIs are harder to learn and remember, not so much for you and Victor who use these frequently, but for everyone else who has to lookup all the macros whose function isn't immediately self-evident.


Raymond



More information about the Python-Dev mailing list