New submission from Larry Hastings <la...@hastings.org>:

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'.)

----------
assignee: larry
components: Interpreter Core
messages: 159781
nosy: larry
priority: normal
severity: normal
status: open
title: Add 'bool' format character to PyArg_ParseTuple*
type: enhancement
versions: Python 3.3

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue14705>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to