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

Skip Montanaro skip at pobox.com
Sun Oct 19 17:32:06 EDT 2003

    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)

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)


More information about the Python-Dev mailing list