Patch #1720595: add T_BOOL to the range of structmember types.

Patch by Angelo Mottola, reviewed by MvL, tests by me.
This commit is contained in:
Georg Brandl 2008-01-21 21:23:15 +00:00
parent f00b38e08c
commit 32a3fb5ec9
5 changed files with 32 additions and 3 deletions

View File

@ -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. */

View File

@ -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

View File

@ -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.

View File

@ -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,

View File

@ -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())