This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Allow T_BOOL in PyMemberDef definitions
Type: enhancement Stage:
Components: Interpreter Core Versions: Python 2.6
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: Nosy List: christian.heimes, georg.brandl, lillo, loewis, pitrou
Priority: normal Keywords: patch

Created on 2007-05-17 09:50 by lillo, last changed 2022-04-11 14:56 by admin. This issue is now closed.

File name Uploaded Description Edit
Python-2.5_T_BOOL.diff lillo, 2007-05-17 09:50 The patch built against a plain Python 2.5 installation
Messages (8)
msg52637 - (view) Author: Angelo Mottola (lillo) Date: 2007-05-17 09:50

The attached patch allows to use T_BOOL alongside other common types in the PyMemberDef definitions of your custom types.
The boolean value is assumed to be held in a straight C char variable, and PyMember_SetOne() sets it to 1 or 0 depending on the result of calling PyObject_IsTrue() on the passed value object.

Usage example:

typedef struct ButtonObject
	char		fToggled;
} ButtonObject;

static PyMemberDef eb_ButtonEventMembers[] = {

	{ "toggled", T_BOOL, offsetof(ButtonObject, fToggled), RO, "True if the button is currently toggled" },
	{ NULL, 0, 0, 0, NULL }

The patch has been built against a plain Python 2.5 installation.
msg52638 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2007-05-17 15:58
I'd rather not allow any object to be assigned to the attribute, but only a bool. T_INT, for example, doesn't automatically call int() on its values too.
msg59795 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2008-01-12 05:16
allow or reject it for 2.6, Georg?
msg59807 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-01-12 10:20
The question is also what C type to assume for boolean fields -- char or
msg61362 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2008-01-20 21:50
It seems to me that, when a boolean is to be represented, most C
programmers would expect a char rather than an int. Also an int would
make the object larger without any benefit.

As for type checking, Angelo's patch already does a PyBool_Check against
the argument. Is anything more required?
msg61413 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-01-21 16:55
Martin - is a char struct member for T_BOOL fine with you?
msg61448 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2008-01-21 21:03
I can't see anything wrong with the feature. Of course, the patch itself
lacks changes to the test suite.

Ideally, there would also be changes to the documentation, but as
PyMemberDef seems to be undocumented currently, it's unfair to ask for
documentation with this patch. If documentation was made, it should list
for each T_ constant what the corresponding C type should be.

I think the cast to long (in PyBool_FromLong) is redundant.
msg61450 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-01-21 21:24
Okay, added tests, removed the cast and committed in r60181. If I find
time, I'll add docs for PyMemberDef in the next few days.
Date User Action Args
2022-04-11 14:56:24adminsetgithub: 44962
2008-01-21 21:24:22georg.brandlsetstatus: open -> closed
resolution: accepted
messages: + msg61450
2008-01-21 21:03:28loewissetassignee: loewis ->
messages: + msg61448
2008-01-21 16:55:18georg.brandlsetassignee: loewis
messages: + msg61413
nosy: + loewis
2008-01-20 21:50:39pitrousetnosy: + pitrou
messages: + msg61362
2008-01-12 10:20:14georg.brandlsetmessages: + msg59807
2008-01-12 05:16:58christian.heimessetnosy: + christian.heimes
type: enhancement
messages: + msg59795
versions: + Python 2.6, - Python 2.5
2007-05-17 09:50:16lillocreate