diff options
author | Georg Brandl <georg@python.org> | 2008-01-21 21:23:15 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2008-01-21 21:23:15 (GMT) |
commit | 32a3fb5ec95c9417c43344a80d9e09c8042ac66d (patch) | |
tree | 7ecc7556d326a3e4e873f52e7611ca41a122b680 | |
parent | f00b38e08ccdcb48e409df951454f91cec428c0e (diff) | |
download | cpython-32a3fb5ec95c9417c43344a80d9e09c8042ac66d.zip cpython-32a3fb5ec95c9417c43344a80d9e09c8042ac66d.tar.gz cpython-32a3fb5ec95c9417c43344a80d9e09c8042ac66d.tar.bz2 |
Patch #1720595: add T_BOOL to the range of structmember types.
Patch by Angelo Mottola, reviewed by MvL, tests by me.
-rw-r--r-- | Include/structmember.h | 3 | ||||
-rw-r--r-- | Lib/test/test_structmembers.py | 8 | ||||
-rw-r--r-- | Misc/NEWS | 2 | ||||
-rw-r--r-- | Modules/_testcapimodule.c | 7 | ||||
-rw-r--r-- | Python/structmember.c | 15 |
5 files changed, 32 insertions, 3 deletions
diff --git a/Include/structmember.h b/Include/structmember.h index 589c47c..1f26b42 100644 --- a/Include/structmember.h +++ b/Include/structmember.h @@ -62,6 +62,9 @@ typedef struct PyMemberDef { /* Added by Jack: strings contained in the structure */ #define T_STRING_INPLACE 13 +/* Added by Lillo: bools contained in the structure (assumed char) */ +#define T_BOOL 14 + #define T_OBJECT_EX 16 /* Like T_OBJECT, but raises AttributeError when the value is NULL, instead of converting to None. */ diff --git a/Lib/test/test_structmembers.py b/Lib/test/test_structmembers.py index 08829c0..f9cff7f 100644 --- a/Lib/test/test_structmembers.py +++ b/Lib/test/test_structmembers.py @@ -8,10 +8,16 @@ from _testcapi import test_structmembersType, \ import warnings, exceptions, unittest, sys from test import test_support -ts=test_structmembersType(1,2,3,4,5,6,7,8,9.99999,10.1010101010) +ts=test_structmembersType(False,1,2,3,4,5,6,7,8,9.99999,10.1010101010) class ReadWriteTests(unittest.TestCase): def test_types(self): + ts.T_BOOL=True + self.assertEquals(ts.T_BOOL, True) + ts.T_BOOL=False + self.assertEquals(ts.T_BOOL, False) + self.assertRaises(TypeError, setattr, ts, 'T_BOOL', 1) + ts.T_BYTE=CHAR_MAX self.assertEquals(ts.T_BYTE, CHAR_MAX) ts.T_BYTE=CHAR_MIN @@ -12,6 +12,8 @@ What's New in Python 2.6 alpha 1? Core and builtins ----------------- +- Patch #1720595: add T_BOOL to the range of structmember types. + - Issue #1882: when compiling code from a string, encoding cookies in the second line of code were not always recognized correctly. diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index 4bc0bbf..b852473 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -762,6 +762,7 @@ static PyMethodDef TestMethods[] = { #define AddSym(d, n, f, v) {PyObject *o = f(v); PyDict_SetItemString(d, n, o); Py_DECREF(o);} typedef struct { + char bool_member; char byte_member; unsigned char ubyte_member; short short_member; @@ -784,6 +785,7 @@ typedef struct { } test_structmembers; static struct PyMemberDef test_members[] = { + {"T_BOOL", T_BOOL, offsetof(test_structmembers, structmembers.bool_member), 0, NULL}, {"T_BYTE", T_BYTE, offsetof(test_structmembers, structmembers.byte_member), 0, NULL}, {"T_UBYTE", T_UBYTE, offsetof(test_structmembers, structmembers.ubyte_member), 0, NULL}, {"T_SHORT", T_SHORT, offsetof(test_structmembers, structmembers.short_member), 0, NULL}, @@ -803,13 +805,13 @@ static struct PyMemberDef test_members[] = { static PyObject *test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs){ - static char *keywords[]={"T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT", "T_INT", "T_UINT", + static char *keywords[]={"T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT", "T_INT", "T_UINT", "T_LONG", "T_ULONG", "T_FLOAT", "T_DOUBLE", #ifdef HAVE_LONG_LONG "T_LONGLONG", "T_ULONGLONG", #endif NULL}; - static char *fmt="|bBhHiIlkfd" + static char *fmt="|bbBhHiIlkfd" #ifdef HAVE_LONG_LONG "LK" #endif @@ -819,6 +821,7 @@ static PyObject *test_structmembers_new(PyTypeObject *type, PyObject *args, PyOb return NULL; memset(&ob->structmembers, 0, sizeof(all_structmembers)); if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, + &ob->structmembers.bool_member, &ob->structmembers.byte_member, &ob->structmembers.ubyte_member, &ob->structmembers.short_member, &ob->structmembers.ushort_member, &ob->structmembers.int_member, &ob->structmembers.uint_member, diff --git a/Python/structmember.c b/Python/structmember.c index 38fc1be..d230590 100644 --- a/Python/structmember.c +++ b/Python/structmember.c @@ -61,6 +61,9 @@ PyMember_GetOne(const char *addr, PyMemberDef *l) } addr += l->offset; switch (l->type) { + case T_BOOL: + v = PyBool_FromLong(*(char*)addr); + break; case T_BYTE: v = PyInt_FromLong(*(char*)addr); break; @@ -183,6 +186,18 @@ PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v) } addr += l->offset; switch (l->type) { + case T_BOOL:{ + if (!PyBool_Check(v)) { + PyErr_SetString(PyExc_TypeError, + "attribute value type must be bool"); + return -1; + } + if (v == Py_True) + *(char*)addr = (char) 1; + else + *(char*)addr = (char) 0; + break; + } case T_BYTE:{ long long_val = PyInt_AsLong(v); if ((long_val == -1) && PyErr_Occurred()) |