[Python-Dev] cpython: Add Py_RETURN_NOTIMPLEMENTED macro. Fixes #12724.

Guido van Rossum guido at python.org
Tue Aug 16 01:52:43 CEST 2011


On Mon, Aug 15, 2011 at 4:39 PM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> On Tue, Aug 16, 2011 at 9:13 AM, Ethan Furman <ethan at stoneleaf.us> wrote:
>> Barry Warsaw wrote:
>>> So I'd opt for #1, unless we can agree on a better color for the bikeshed.
>>
>> My apologies if this is just noise, but are there RETURN macros that don't
>> do an INCREF?
>
> No, Py_RETURN_NONE is the only previous example, and it was added to
> simplify the very common idiom of:
>
>    Py_INCREF(Py_None);
>    return Py_None;
>
> It was added originally because it helped to avoid *two* common bugs:
>
>  return Py_None; # segfault waiting to happen
>
>  return NULL; # Just plain wrong, but not picked up until tests are
> run and hence irritating
>
> I'd say NotImplemented is the second most common instance of that kind
> of direct incref-and-return (since operator methods need to return it
> to correctly support type coercion), although, as Antoine noted,
> Py_True and Py_False would be up there as well.

I betcha if you extend your search to "return <variable>" preceded by
"INCREF(variable)" you'll find a whole lot more examples. :-)

-- 
--Guido van Rossum (python.org/~guido)


More information about the Python-Dev mailing list