report at bugs
May 2, 2012, 2:29 AM
New submission from Larry Hastings <larry [at] hastings>:
[issue14705] Add 'bool' format character to PyArg_ParseTuple*
Currently functions that parse their arguments with the PyArg_ParseTuple family which want to take a boolean-ish parameter face two choices:
* take an "int", then test whether or not the int is 0, or
* take an "object", then call PyObject_IsTrue themselves.
The former is foolish; though it works with ints and bools, it doesn't work with any other type (float, str, list, etc) which strictly speaking are valid for boolean fields. And this is common enough that the latter should not be necessary.
I propose to add support for a new format character to the PyArg_ParseTuple family: 'b', which specifies 'boolean'. Its
implementation would be much the same as that of 'd' except:
* the output type would be "int" instead of "double",
* it would check for a -1 instead of calling PyErr_Occured, and
* it would call PyObject_IsTrue instead of PyFloat_AsDouble.
If we cared, I could also add 'B', which would only accept
either Py_True or Py_False. But I see no reason why we'd ever want
to strictly enforce the type... do you? (I can see MvL's argument now:
"We've lived this long without it. YAGNI.")
If there's interest I'll knock out a patch. I expect it to be less than ten lines not counting documentation and test. (Less than twenty if
folks actually want 'B'.)
components: Interpreter Core
title: Add 'bool' format character to PyArg_ParseTuple*
versions: Python 3.3
Python tracker <report [at] bugs>
Python-bugs-list mailing list