Merged revisions 74917 via svnmerge from

svn+ssh://pythondev@svn.python.org/python/trunk

........
  r74917 | thomas.heller | 2009-09-18 20:55:17 +0200 (Fr, 18 Sep 2009) | 3 lines

  Issue #5042: Structure sub-subclass does now initialize correctly with
  base class positional arguments.
........
This commit is contained in:
Thomas Heller 2009-09-18 19:32:08 +00:00
parent 25e8dabab2
commit 820aefe29d
3 changed files with 95 additions and 58 deletions

View file

@ -355,6 +355,25 @@ class StructureTestCase(unittest.TestCase):
self.failUnless("from_address" in dir(type(Structure))) self.failUnless("from_address" in dir(type(Structure)))
self.failUnless("in_dll" in dir(type(Structure))) self.failUnless("in_dll" in dir(type(Structure)))
def test_positional_args(self):
# see also http://bugs.python.org/issue5042
class W(Structure):
_fields_ = [("a", c_int), ("b", c_int)]
class X(W):
_fields_ = [("c", c_int)]
class Y(X):
pass
class Z(Y):
_fields_ = [("d", c_int), ("e", c_int), ("f", c_int)]
z = Z(1, 2, 3, 4, 5, 6)
self.failUnlessEqual((z.a, z.b, z.c, z.d, z.e, z.f),
(1, 2, 3, 4, 5, 6))
z = Z(1)
self.failUnlessEqual((z.a, z.b, z.c, z.d, z.e, z.f),
(1, 0, 0, 0, 0, 0))
self.assertRaises(TypeError, lambda: Z(1, 2, 3, 4, 5, 6, 7))
class PointerMemberTestCase(unittest.TestCase): class PointerMemberTestCase(unittest.TestCase):
def test(self): def test(self):

View file

@ -82,6 +82,9 @@ Core and Builtins
Library Library
------- -------
- Issue #5042: Structure sub-subclass does now initialize correctly
with base class positional arguments.
- Issue #6938: Fix a TypeError in string formatting of a multiprocessing - Issue #6938: Fix a TypeError in string formatting of a multiprocessing
debug message. debug message.

View file

@ -4017,55 +4017,50 @@ IBUG(char *msg)
return -1; return -1;
} }
static int /*
Struct_init(PyObject *self, PyObject *args, PyObject *kwds) This function is called to initialize a Structure or Union with positional
{ arguments. It calls itself recursively for all Structure or Union base
int i; classes, then retrieves the _fields_ member to associate the argument
PyObject *fields; position with the correct field name.
/* Optimization possible: Store the attribute names _fields_[x][0] Returns -1 on error, or the index of next argument on success.
* in C accessible fields somewhere ?
*/ */
static int
_init_pos_args(PyObject *self, PyTypeObject *type,
PyObject *args, PyObject *kwds,
int index)
{
StgDictObject *dict;
PyObject *fields;
int i;
/* Check this code again for correctness! */ if (PyType_stgdict((PyObject *)type->tp_base)) {
index = _init_pos_args(self, type->tp_base,
if (!PyTuple_Check(args)) { args, kwds,
PyErr_SetString(PyExc_TypeError, index);
"args not a tuple?"); if (index == -1)
return -1;
}
if (PyTuple_GET_SIZE(args)) {
fields = PyObject_GetAttrString(self, "_fields_");
if (!fields) {
PyErr_Clear();
fields = PyTuple_New(0);
if (!fields)
return -1; return -1;
} }
if (PyTuple_GET_SIZE(args) > PySequence_Length(fields)) { dict = PyType_stgdict((PyObject *)type);
Py_DECREF(fields); fields = PyDict_GetItemString((PyObject *)dict, "_fields_");
PyErr_SetString(PyExc_TypeError, if (fields == NULL)
"too many initializers"); return index;
return -1;
}
for (i = 0; i < PyTuple_GET_SIZE(args); ++i) { for (i = 0;
i < dict->length && (i+index) < PyTuple_GET_SIZE(args);
++i) {
PyObject *pair = PySequence_GetItem(fields, i); PyObject *pair = PySequence_GetItem(fields, i);
PyObject *name; PyObject *name, *val;
PyObject *val; int res;
if (!pair) { if (!pair)
Py_DECREF(fields); return -1;
return IBUG("_fields_[i] failed");
}
name = PySequence_GetItem(pair, 0); name = PySequence_GetItem(pair, 0);
if (!name) { if (!name) {
Py_DECREF(pair); Py_DECREF(pair);
Py_DECREF(fields); return -1;
return IBUG("_fields_[i][0] failed");
} }
val = PyTuple_GET_ITEM(args, i + index);
if (kwds && PyDict_GetItem(kwds, name)) { if (kwds && PyDict_GetItem(kwds, name)) {
char *field = PyString_AsString(name); char *field = PyString_AsString(name);
if (field == NULL) { if (field == NULL) {
@ -4073,26 +4068,46 @@ Struct_init(PyObject *self, PyObject *args, PyObject *kwds)
field = "???"; field = "???";
} }
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"duplicate values for field %s", "duplicate values for field '%s'",
field); field);
Py_DECREF(pair); Py_DECREF(pair);
Py_DECREF(name); Py_DECREF(name);
Py_DECREF(fields);
return -1; return -1;
} }
val = PyTuple_GET_ITEM(args, i); res = PyObject_SetAttr(self, name, val);
if (-1 == PyObject_SetAttr(self, name, val)) {
Py_DECREF(pair); Py_DECREF(pair);
Py_DECREF(name); Py_DECREF(name);
Py_DECREF(fields); if (res == -1)
return -1; return -1;
} }
return index + dict->length;
Py_DECREF(name); }
Py_DECREF(pair);
static int
Struct_init(PyObject *self, PyObject *args, PyObject *kwds)
{
StgDictObject *stgdict = PyObject_stgdict(self);
/* Optimization possible: Store the attribute names _fields_[x][0]
* in C accessible fields somewhere ?
*/
if (!PyTuple_Check(args)) {
PyErr_SetString(PyExc_TypeError,
"args not a tuple?");
return -1;
}
if (PyTuple_GET_SIZE(args)) {
int res = _init_pos_args(self, Py_TYPE(self),
args, kwds, 0);
if (res == -1)
return -1;
if (res < PyTuple_GET_SIZE(args)) {
PyErr_SetString(PyExc_TypeError,
"too many initializers");
return -1;
} }
Py_DECREF(fields);
} }
if (kwds) { if (kwds) {