]> granicus.if.org Git - python/commitdiff
Raise a TypeError instead of a ValueError when too many initializers
authorThomas Heller <theller@ctypes.org>
Wed, 16 Jan 2008 19:45:51 +0000 (19:45 +0000)
committerThomas Heller <theller@ctypes.org>
Wed, 16 Jan 2008 19:45:51 +0000 (19:45 +0000)
are used in a Structure or Union constructor.

Lib/ctypes/test/test_structures.py
Misc/NEWS
Modules/_ctypes/_ctypes.c

index 3005e829b3abf65e8fde90e90acfd5f4dd67c856..0c5a3477630e5f826b0d01ff264250397bba9996 100644 (file)
@@ -222,8 +222,8 @@ class StructureTestCase(unittest.TestCase):
         self.assertRaises(TypeError, POINT, 2, 3, x=4)
         self.assertRaises(TypeError, POINT, 2, 3, y=4)
 
-        # Should this raise TypeError instead?
-        self.assertRaises(ValueError, POINT, 2, 3, 4)
+        # too many initializers
+        self.assertRaises(TypeError, POINT, 2, 3, 4)
 
     def test_keyword_initializers(self):
         class POINT(Structure):
@@ -320,9 +320,9 @@ class StructureTestCase(unittest.TestCase):
         self.failUnlessEqual(cls, RuntimeError)
         if issubclass(Exception, object):
             self.failUnlessEqual(msg,
-                                 "(Phone) <type 'exceptions.ValueError'>: too many initializers")
+                                 "(Phone) <type 'exceptions.TypeError'>: too many initializers")
         else:
-            self.failUnlessEqual(msg, "(Phone) exceptions.ValueError: too many initializers")
+            self.failUnlessEqual(msg, "(Phone) exceptions.TypeError: too many initializers")
 
 
     def get_except(self, func, *args):
index 29bd3dd939751581fdb5bc5bd33e269634d305bf..497889be92ca3cb8510d4e7a7f96a23cf204a228 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -366,6 +366,8 @@ Library
 
 - Issue #1831: ctypes now raises a TypeError if conflicting positional
   and named arguments are passed to a Structure or Union initializer.
+  When too many positional arguments are passed, also a TypeError is
+  raised instead of a ValueError.
 
 - Convert the internal ctypes array type cache to a WeakValueDict so
   that array types do not live longer than needed.
index 8c6619489f7d24d27d1e63e8614f1d6a7a40d9c2..24d3c2fd51ce21b93a8fba30d99e930400b89525 100644 (file)
@@ -3557,7 +3557,7 @@ Struct_init(PyObject *self, PyObject *args, PyObject *kwds)
 
                if (PyTuple_GET_SIZE(args) > PySequence_Length(fields)) {
                        Py_DECREF(fields);
-                       PyErr_SetString(PyExc_ValueError,
+                       PyErr_SetString(PyExc_TypeError,
                                        "too many initializers");
                        return -1;
                }