[Python-Dev] Re: [Python-checkins] python/dist/src/Include object.h, 2.121, 2.122 boolobject.h, 1.4, 1.5

Brett C. bac at OCF.Berkeley.EDU
Sun Oct 19 17:40:36 EDT 2003


Skip Montanaro wrote:

>     bcannon> Defined macros Py_RETURN_(TRUE|FALSE|NONE) as helper functions
>     bcannon> for returning the specified value.  All three Py_INCREF the
>     bcannon> singleton and then return it.
> 
>     ...
>     bcannon> + /* Macro for returning Py_None from a function */
>     bcannon> + #define Py_RETURN_NONE Py_INCREF(Py_None); return Py_None;
>     ...  
>     bcannon> + /* Macros for returning Py_True or Py_False, respectively */
>     bcannon> + #define Py_RETURN_TRUE Py_INCREF(Py_True); return Py_True;
>     bcannon> + #define Py_RETURN_FALSE Py_INCREF(Py_False); return Py_False;
> 
> These don't look right to me.  First, you have no protection against them
> being called like this:
> 
>        if (!error)
>            Py_RETURN_TRUE;
> 

Realized that after my first commit.  Already fixed.

> Second, any time you expect to use a macro in a statement context, I don't
> think you want to terminate it with a semicolon (the programmer will do
> that).  I would have coded them as
> 
>   #define Py_RETURN_NONE do {Py_INCREF(Py_None); return Py_None;} while (0)
>   #define Py_RETURN_TRUE do {Py_INCREF(Py_True); return Py_True;} while (0)
>   #define Py_RETURN_FALSE do {Py_INCREF(Py_False); return Py_False;} while (0)
> 

Isn't {Py_INCREF(Py_None); return Py_None} enough?  I thought ending a 
curly brace with a semi-colon is harmless (equivalent of a NO-OP).  Why 
bother with the do/while loop?

-Brett





More information about the Python-Dev mailing list