]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-43413: Fix handling keyword arguments in subclasses of some buitin classes (GH...
authorSerhiy Storchaka <storchaka@gmail.com>
Sun, 12 Sep 2021 10:27:50 +0000 (13:27 +0300)
committerGitHub <noreply@github.com>
Sun, 12 Sep 2021 10:27:50 +0000 (13:27 +0300)
* Constructors of subclasses of some buitin classes (e.g. tuple, list,
  frozenset) no longer accept arbitrary keyword arguments.
* Subclass of set can now define a __new__() method with additional
  keyword parameters without overriding also __init__().

26 files changed:
Lib/test/seq_tests.py
Lib/test/test_float.py
Lib/test/test_itertools.py
Lib/test/test_list.py
Lib/test/test_set.py
Lib/test/test_tuple.py
Misc/NEWS.d/next/Core and Builtins/2021-05-30-16-37-47.bpo-43413.vYFPPC.rst [new file with mode: 0644]
Modules/_io/clinic/bufferedio.c.h
Modules/_randommodule.c
Modules/_sqlite/clinic/cursor.c.h
Modules/_sqlite/clinic/row.c.h
Modules/arraymodule.c
Modules/clinic/_collectionsmodule.c.h
Modules/clinic/_queuemodule.c.h
Modules/clinic/_ssl.c.h
Modules/clinic/itertoolsmodule.c.h
Modules/clinic/selectmodule.c.h
Modules/itertoolsmodule.c
Objects/clinic/codeobject.c.h
Objects/clinic/enumobject.c.h
Objects/clinic/floatobject.c.h
Objects/clinic/listobject.c.h
Objects/clinic/tupleobject.c.h
Objects/setobject.c
Python/bltinmodule.c
Tools/clinic/clinic.py

index 1d9ad588987f9769bd98048558164fbdbe834027..a41970d8f3f55acbea2195dac64a28cbbf936f47 100644 (file)
@@ -145,6 +145,9 @@ class CommonTest(unittest.TestCase):
         self.assertEqual(self.type2test(LyingTuple((2,))), self.type2test((1,)))
         self.assertEqual(self.type2test(LyingList([2])), self.type2test([1]))
 
+        with self.assertRaises(TypeError):
+            self.type2test(unsupported_arg=[])
+
     def test_truth(self):
         self.assertFalse(self.type2test())
         self.assertTrue(self.type2test([42]))
index 29f775644dd4a82dc6caec2130b918e41716478c..aba15839f13781a403c30d26622c208102f8acc5 100644 (file)
@@ -245,6 +245,33 @@ class GeneralFloatCases(unittest.TestCase):
         with self.assertRaisesRegex(TypeError, 'keyword argument'):
             float(x='3.14')
 
+    def test_keywords_in_subclass(self):
+        class subclass(float):
+            pass
+        u = subclass(2.5)
+        self.assertIs(type(u), subclass)
+        self.assertEqual(float(u), 2.5)
+        with self.assertRaises(TypeError):
+            subclass(x=0)
+
+        class subclass_with_init(float):
+            def __init__(self, arg, newarg=None):
+                self.newarg = newarg
+        u = subclass_with_init(2.5, newarg=3)
+        self.assertIs(type(u), subclass_with_init)
+        self.assertEqual(float(u), 2.5)
+        self.assertEqual(u.newarg, 3)
+
+        class subclass_with_new(float):
+            def __new__(cls, arg, newarg=None):
+                self = super().__new__(cls, arg)
+                self.newarg = newarg
+                return self
+        u = subclass_with_new(2.5, newarg=3)
+        self.assertIs(type(u), subclass_with_new)
+        self.assertEqual(float(u), 2.5)
+        self.assertEqual(u.newarg, 3)
+
     def test_is_integer(self):
         self.assertFalse((1.1).is_integer())
         self.assertTrue((1.).is_integer())
index aa81076f32d001b0b8e9eb1b974c957fb18bf453..db6a98351bbd9bd79b20a8999e83b4f0b4f06a6b 100644 (file)
@@ -2229,16 +2229,57 @@ class RegressionTests(unittest.TestCase):
 class SubclassWithKwargsTest(unittest.TestCase):
     def test_keywords_in_subclass(self):
         # count is not subclassable...
-        for cls in (repeat, zip, filter, filterfalse, chain, map,
-                    starmap, islice, takewhile, dropwhile, cycle, compress):
-            class Subclass(cls):
-                def __init__(self, newarg=None, *args):
-                    cls.__init__(self, *args)
-            try:
-                Subclass(newarg=1)
-            except TypeError as err:
-                # we expect type errors because of wrong argument count
-                self.assertNotIn("keyword arguments", err.args[0])
+        testcases = [
+            (repeat, (1, 2), [1, 1]),
+            (zip, ([1, 2], 'ab'), [(1, 'a'), (2, 'b')]),
+            (filter, (None, [0, 1]), [1]),
+            (filterfalse, (None, [0, 1]), [0]),
+            (chain, ([1, 2], [3, 4]), [1, 2, 3]),
+            (map, (str, [1, 2]), ['1', '2']),
+            (starmap, (operator.pow, ((2, 3), (3, 2))), [8, 9]),
+            (islice, ([1, 2, 3, 4], 1, 3), [2, 3]),
+            (takewhile, (isEven, [2, 3, 4]), [2]),
+            (dropwhile, (isEven, [2, 3, 4]), [3, 4]),
+            (cycle, ([1, 2],), [1, 2, 1]),
+            (compress, ('ABC', [1, 0, 1]), ['A', 'C']),
+        ]
+        for cls, args, result in testcases:
+            with self.subTest(cls):
+                class subclass(cls):
+                    pass
+                u = subclass(*args)
+                self.assertIs(type(u), subclass)
+                self.assertEqual(list(islice(u, 0, 3)), result)
+                with self.assertRaises(TypeError):
+                    subclass(*args, newarg=3)
+
+        for cls, args, result in testcases:
+            # Constructors of repeat, zip, compress accept keyword arguments.
+            # Their subclasses need overriding __new__ to support new
+            # keyword arguments.
+            if cls in [repeat, zip, compress]:
+                continue
+            with self.subTest(cls):
+                class subclass_with_init(cls):
+                    def __init__(self, *args, newarg=None):
+                        self.newarg = newarg
+                u = subclass_with_init(*args, newarg=3)
+                self.assertIs(type(u), subclass_with_init)
+                self.assertEqual(list(islice(u, 0, 3)), result)
+                self.assertEqual(u.newarg, 3)
+
+        for cls, args, result in testcases:
+            with self.subTest(cls):
+                class subclass_with_new(cls):
+                    def __new__(cls, *args, newarg=None):
+                        self = super().__new__(cls, *args)
+                        self.newarg = newarg
+                        return self
+                u = subclass_with_new(*args, newarg=3)
+                self.assertIs(type(u), subclass_with_new)
+                self.assertEqual(list(islice(u, 0, 3)), result)
+                self.assertEqual(u.newarg, 3)
+
 
 @support.cpython_only
 class SizeofTest(unittest.TestCase):
index 3c8d82958fd7c88c00acbbb961f2bdad77b1483e..d3da05ba84ac0ffc22d7adddf8a622760fdc8ac1 100644 (file)
@@ -46,6 +46,34 @@ class ListTest(list_tests.CommonTest):
         with self.assertRaisesRegex(TypeError, 'keyword argument'):
             list(sequence=[])
 
+    def test_keywords_in_subclass(self):
+        class subclass(list):
+            pass
+        u = subclass([1, 2])
+        self.assertIs(type(u), subclass)
+        self.assertEqual(list(u), [1, 2])
+        with self.assertRaises(TypeError):
+            subclass(sequence=())
+
+        class subclass_with_init(list):
+            def __init__(self, seq, newarg=None):
+                super().__init__(seq)
+                self.newarg = newarg
+        u = subclass_with_init([1, 2], newarg=3)
+        self.assertIs(type(u), subclass_with_init)
+        self.assertEqual(list(u), [1, 2])
+        self.assertEqual(u.newarg, 3)
+
+        class subclass_with_new(list):
+            def __new__(cls, seq, newarg=None):
+                self = super().__new__(cls, seq)
+                self.newarg = newarg
+                return self
+        u = subclass_with_new([1, 2], newarg=3)
+        self.assertIs(type(u), subclass_with_new)
+        self.assertEqual(list(u), [1, 2])
+        self.assertEqual(u.newarg, 3)
+
     def test_truth(self):
         super().test_truth()
         self.assertTrue(not [])
index 29bb39df76c8a56929bae7960bd8e76b613cdf7f..65fda9e469bccccc61d0244371cbf5c27f91c849 100644 (file)
@@ -644,15 +644,34 @@ class TestSetSubclass(TestSet):
     thetype = SetSubclass
     basetype = set
 
-class SetSubclassWithKeywordArgs(set):
-    def __init__(self, iterable=[], newarg=None):
-        set.__init__(self, iterable)
-
-class TestSetSubclassWithKeywordArgs(TestSet):
-
     def test_keywords_in_subclass(self):
-        'SF bug #1486663 -- this used to erroneously raise a TypeError'
-        SetSubclassWithKeywordArgs(newarg=1)
+        class subclass(set):
+            pass
+        u = subclass([1, 2])
+        self.assertIs(type(u), subclass)
+        self.assertEqual(set(u), {1, 2})
+        with self.assertRaises(TypeError):
+            subclass(sequence=())
+
+        class subclass_with_init(set):
+            def __init__(self, arg, newarg=None):
+                super().__init__(arg)
+                self.newarg = newarg
+        u = subclass_with_init([1, 2], newarg=3)
+        self.assertIs(type(u), subclass_with_init)
+        self.assertEqual(set(u), {1, 2})
+        self.assertEqual(u.newarg, 3)
+
+        class subclass_with_new(set):
+            def __new__(cls, arg, newarg=None):
+                self = super().__new__(cls, arg)
+                self.newarg = newarg
+                return self
+        u = subclass_with_new([1, 2], newarg=3)
+        self.assertIs(type(u), subclass_with_new)
+        self.assertEqual(set(u), {1, 2})
+        self.assertEqual(u.newarg, 3)
+
 
 class TestFrozenSet(TestJointOps, unittest.TestCase):
     thetype = frozenset
@@ -734,6 +753,33 @@ class TestFrozenSetSubclass(TestFrozenSet):
     thetype = FrozenSetSubclass
     basetype = frozenset
 
+    def test_keywords_in_subclass(self):
+        class subclass(frozenset):
+            pass
+        u = subclass([1, 2])
+        self.assertIs(type(u), subclass)
+        self.assertEqual(set(u), {1, 2})
+        with self.assertRaises(TypeError):
+            subclass(sequence=())
+
+        class subclass_with_init(frozenset):
+            def __init__(self, arg, newarg=None):
+                self.newarg = newarg
+        u = subclass_with_init([1, 2], newarg=3)
+        self.assertIs(type(u), subclass_with_init)
+        self.assertEqual(set(u), {1, 2})
+        self.assertEqual(u.newarg, 3)
+
+        class subclass_with_new(frozenset):
+            def __new__(cls, arg, newarg=None):
+                self = super().__new__(cls, arg)
+                self.newarg = newarg
+                return self
+        u = subclass_with_new([1, 2], newarg=3)
+        self.assertIs(type(u), subclass_with_new)
+        self.assertEqual(set(u), {1, 2})
+        self.assertEqual(u.newarg, 3)
+
     def test_constructor_identity(self):
         s = self.thetype(range(3))
         t = self.thetype(s)
index d2a2ed310b05d79deaddd520925f772c46657f92..9ce80c5e8ea0093e00176c3c7078e0ad3123d4c3 100644 (file)
@@ -42,6 +42,33 @@ class TupleTest(seq_tests.CommonTest):
         with self.assertRaisesRegex(TypeError, 'keyword argument'):
             tuple(sequence=())
 
+    def test_keywords_in_subclass(self):
+        class subclass(tuple):
+            pass
+        u = subclass([1, 2])
+        self.assertIs(type(u), subclass)
+        self.assertEqual(list(u), [1, 2])
+        with self.assertRaises(TypeError):
+            subclass(sequence=())
+
+        class subclass_with_init(tuple):
+            def __init__(self, arg, newarg=None):
+                self.newarg = newarg
+        u = subclass_with_init([1, 2], newarg=3)
+        self.assertIs(type(u), subclass_with_init)
+        self.assertEqual(list(u), [1, 2])
+        self.assertEqual(u.newarg, 3)
+
+        class subclass_with_new(tuple):
+            def __new__(cls, arg, newarg=None):
+                self = super().__new__(cls, arg)
+                self.newarg = newarg
+                return self
+        u = subclass_with_new([1, 2], newarg=3)
+        self.assertIs(type(u), subclass_with_new)
+        self.assertEqual(list(u), [1, 2])
+        self.assertEqual(u.newarg, 3)
+
     def test_truth(self):
         super().test_truth()
         self.assertTrue(not ())
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-05-30-16-37-47.bpo-43413.vYFPPC.rst b/Misc/NEWS.d/next/Core and Builtins/2021-05-30-16-37-47.bpo-43413.vYFPPC.rst
new file mode 100644 (file)
index 0000000..579b57e
--- /dev/null
@@ -0,0 +1,4 @@
+Constructors of subclasses of some buitin classes (e.g. :class:`tuple`,
+:class:`list`, :class:`frozenset`) no longer accept arbitrary keyword
+arguments. Subclass of :class:`set` can now define a ``__new__()`` method
+with additional keyword parameters without overriding also ``__init__()``.
index 19543fdf79d58cdb17a9efd82369e4daed2079e1..2fd02329dc3e7e2580489bded397ac5c9ecf3662 100644 (file)
@@ -553,7 +553,8 @@ _io_BufferedRWPair___init__(PyObject *self, PyObject *args, PyObject *kwargs)
     PyObject *writer;
     Py_ssize_t buffer_size = DEFAULT_BUFFER_SIZE;
 
-    if (Py_IS_TYPE(self, &PyBufferedRWPair_Type) &&
+    if ((Py_IS_TYPE(self, &PyBufferedRWPair_Type) ||
+         Py_TYPE(self)->tp_new == PyBufferedRWPair_Type.tp_new) &&
         !_PyArg_NoKeywords("BufferedRWPair", kwargs)) {
         goto exit;
     }
@@ -637,4 +638,4 @@ skip_optional_pos:
 exit:
     return return_value;
 }
-/*[clinic end generated code: output=98ccf7610c0e82ba input=a9049054013a1b77]*/
+/*[clinic end generated code: output=79138a088729b5ee input=a9049054013a1b77]*/
index 7ad4299bc321b3c50fe04460b2c2df8ebc6b9855..65d41f4e8e80c7d02128251466ae433fcb64ba52 100644 (file)
@@ -523,8 +523,9 @@ random_init(RandomObject *self, PyObject *args, PyObject *kwds)
     PyObject *arg = NULL;
     _randomstate *state = _randomstate_type(Py_TYPE(self));
 
-    if (Py_IS_TYPE(self, (PyTypeObject *)state->Random_Type) &&
-        !_PyArg_NoKeywords("Random()", kwds)) {
+    if ((Py_IS_TYPE(self, (PyTypeObject *)state->Random_Type) ||
+         Py_TYPE(self)->tp_init == ((PyTypeObject*)state->Random_Type)->tp_init) &&
+        !_PyArg_NoKeywords("Random", kwds)) {
         return -1;
     }
 
index 07e15870146cf742c122fb161f0d715dd4b5791e..eb3e7ce31ada30554eac0f366bfd5e282102126f 100644 (file)
@@ -12,7 +12,8 @@ pysqlite_cursor_init(PyObject *self, PyObject *args, PyObject *kwargs)
     int return_value = -1;
     pysqlite_Connection *connection;
 
-    if (Py_IS_TYPE(self, clinic_state()->CursorType) &&
+    if ((Py_IS_TYPE(self, clinic_state()->CursorType) ||
+         Py_TYPE(self)->tp_new == clinic_state()->CursorType->tp_new) &&
         !_PyArg_NoKeywords("Cursor", kwargs)) {
         goto exit;
     }
@@ -299,4 +300,4 @@ pysqlite_cursor_close(pysqlite_Cursor *self, PyTypeObject *cls, PyObject *const
 exit:
     return return_value;
 }
-/*[clinic end generated code: output=ace31a7481aa3f41 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=3b5328c1619b7626 input=a9049054013a1b77]*/
index 310d62b8a7f49ac77ca4390bd363aedb566c3f1f..c936ef75fdeaa464d0d90a2a09cd1192405d4413 100644 (file)
@@ -13,7 +13,8 @@ pysqlite_row_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
     pysqlite_Cursor *cursor;
     PyObject *data;
 
-    if ((type == clinic_state()->RowType) &&
+    if ((type == clinic_state()->RowType ||
+         type->tp_init == clinic_state()->RowType->tp_init) &&
         !_PyArg_NoKeywords("Row", kwargs)) {
         goto exit;
     }
@@ -53,4 +54,4 @@ pysqlite_row_keys(pysqlite_Row *self, PyObject *Py_UNUSED(ignored))
 {
     return pysqlite_row_keys_impl(self);
 }
-/*[clinic end generated code: output=0382771b4fc85f36 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=9d54919dbb4ba5f1 input=a9049054013a1b77]*/
index 1d9d4cd591afa61489e8f9233ad040201045b543..9a3203c7caaa8ac9c49aee3464cfbf90d4c21b49 100644 (file)
@@ -2617,7 +2617,9 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
     PyObject *initial = NULL, *it = NULL;
     const struct arraydescr *descr;
 
-    if (type == state->ArrayType && !_PyArg_NoKeywords("array.array", kwds))
+    if ((type == state->ArrayType ||
+         type->tp_init == state->ArrayType->tp_init) &&
+        !_PyArg_NoKeywords("array.array", kwds))
         return NULL;
 
     if (!PyArg_ParseTuple(args, "C|O:array", &c, &initial))
index 7e18aeb312c4a09a42d919cce07d60fe86279e35..2875b3cd26628b72b92c2a96dc0f4325b0b6d236 100644 (file)
@@ -43,7 +43,8 @@ tuplegetter_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
     Py_ssize_t index;
     PyObject *doc;
 
-    if ((type == &tuplegetter_type) &&
+    if ((type == &tuplegetter_type ||
+         type->tp_init == tuplegetter_type.tp_init) &&
         !_PyArg_NoKeywords("_tuplegetter", kwargs)) {
         goto exit;
     }
@@ -68,4 +69,4 @@ tuplegetter_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
 exit:
     return return_value;
 }
-/*[clinic end generated code: output=947186d369f50f1e input=a9049054013a1b77]*/
+/*[clinic end generated code: output=3dfa12a35e655844 input=a9049054013a1b77]*/
index 8741f7d9aff881096a2184dcd2bdede576e8e001..d9522562359e64ccccaeba11511d07480c678f07 100644 (file)
@@ -16,11 +16,13 @@ simplequeue_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
 {
     PyObject *return_value = NULL;
 
-    if ((type == simplequeue_get_state_by_type(type)->SimpleQueueType) &&
+    if ((type == simplequeue_get_state_by_type(type)->SimpleQueueType ||
+         type->tp_init == simplequeue_get_state_by_type(type)->SimpleQueueType->tp_init) &&
         !_PyArg_NoPositional("SimpleQueue", args)) {
         goto exit;
     }
-    if ((type == simplequeue_get_state_by_type(type)->SimpleQueueType) &&
+    if ((type == simplequeue_get_state_by_type(type)->SimpleQueueType ||
+         type->tp_init == simplequeue_get_state_by_type(type)->SimpleQueueType->tp_init) &&
         !_PyArg_NoKeywords("SimpleQueue", kwargs)) {
         goto exit;
     }
@@ -246,4 +248,4 @@ _queue_SimpleQueue_qsize(simplequeueobject *self, PyObject *Py_UNUSED(ignored))
 exit:
     return return_value;
 }
-/*[clinic end generated code: output=ce56b46fac150909 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=96cc57168d72aab1 input=a9049054013a1b77]*/
index b59b129af8a09570790993e2f22ae0240a71d880..67eaf3f9609fab6b8fe8d275610fb99432d8828f 100644 (file)
@@ -408,7 +408,8 @@ _ssl__SSLContext(PyTypeObject *type, PyObject *args, PyObject *kwargs)
     PyObject *return_value = NULL;
     int proto_version;
 
-    if ((type == get_state_type(type)->PySSLContext_Type) &&
+    if ((type == get_state_type(type)->PySSLContext_Type ||
+         type->tp_init == get_state_type(type)->PySSLContext_Type->tp_init) &&
         !_PyArg_NoKeywords("_SSLContext", kwargs)) {
         goto exit;
     }
@@ -884,11 +885,13 @@ _ssl_MemoryBIO(PyTypeObject *type, PyObject *args, PyObject *kwargs)
 {
     PyObject *return_value = NULL;
 
-    if ((type == get_state_type(type)->PySSLMemoryBIO_Type) &&
+    if ((type == get_state_type(type)->PySSLMemoryBIO_Type ||
+         type->tp_init == get_state_type(type)->PySSLMemoryBIO_Type->tp_init) &&
         !_PyArg_NoPositional("MemoryBIO", args)) {
         goto exit;
     }
-    if ((type == get_state_type(type)->PySSLMemoryBIO_Type) &&
+    if ((type == get_state_type(type)->PySSLMemoryBIO_Type ||
+         type->tp_init == get_state_type(type)->PySSLMemoryBIO_Type->tp_init) &&
         !_PyArg_NoKeywords("MemoryBIO", kwargs)) {
         goto exit;
     }
@@ -1358,4 +1361,4 @@ exit:
 #ifndef _SSL_ENUM_CRLS_METHODDEF
     #define _SSL_ENUM_CRLS_METHODDEF
 #endif /* !defined(_SSL_ENUM_CRLS_METHODDEF) */
-/*[clinic end generated code: output=5a7d7bf5cf8ee092 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=cd2a53c26eda295e input=a9049054013a1b77]*/
index 82729eeb56bce9dfa1a02eed2fb708379f987875..7f5abe648af8874bcfb5a913f444d38448927187 100644 (file)
@@ -19,7 +19,8 @@ pairwise_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
     PyObject *return_value = NULL;
     PyObject *iterable;
 
-    if ((type == &pairwise_type) &&
+    if ((type == &pairwise_type ||
+         type->tp_init == pairwise_type.tp_init) &&
         !_PyArg_NoKeywords("pairwise", kwargs)) {
         goto exit;
     }
@@ -89,7 +90,8 @@ itertools__grouper(PyTypeObject *type, PyObject *args, PyObject *kwargs)
     PyObject *parent;
     PyObject *tgtkey;
 
-    if ((type == &_grouper_type) &&
+    if ((type == &_grouper_type ||
+         type->tp_init == _grouper_type.tp_init) &&
         !_PyArg_NoKeywords("_grouper", kwargs)) {
         goto exit;
     }
@@ -126,7 +128,8 @@ itertools_teedataobject(PyTypeObject *type, PyObject *args, PyObject *kwargs)
     PyObject *values;
     PyObject *next;
 
-    if ((type == &teedataobject_type) &&
+    if ((type == &teedataobject_type ||
+         type->tp_init == teedataobject_type.tp_init) &&
         !_PyArg_NoKeywords("teedataobject", kwargs)) {
         goto exit;
     }
@@ -161,7 +164,8 @@ itertools__tee(PyTypeObject *type, PyObject *args, PyObject *kwargs)
     PyObject *return_value = NULL;
     PyObject *iterable;
 
-    if ((type == &tee_type) &&
+    if ((type == &tee_type ||
+         type->tp_init == tee_type.tp_init) &&
         !_PyArg_NoKeywords("_tee", kwargs)) {
         goto exit;
     }
@@ -235,7 +239,8 @@ itertools_cycle(PyTypeObject *type, PyObject *args, PyObject *kwargs)
     PyObject *return_value = NULL;
     PyObject *iterable;
 
-    if ((type == &cycle_type) &&
+    if ((type == &cycle_type ||
+         type->tp_init == cycle_type.tp_init) &&
         !_PyArg_NoKeywords("cycle", kwargs)) {
         goto exit;
     }
@@ -267,7 +272,8 @@ itertools_dropwhile(PyTypeObject *type, PyObject *args, PyObject *kwargs)
     PyObject *func;
     PyObject *seq;
 
-    if ((type == &dropwhile_type) &&
+    if ((type == &dropwhile_type ||
+         type->tp_init == dropwhile_type.tp_init) &&
         !_PyArg_NoKeywords("dropwhile", kwargs)) {
         goto exit;
     }
@@ -298,7 +304,8 @@ itertools_takewhile(PyTypeObject *type, PyObject *args, PyObject *kwargs)
     PyObject *func;
     PyObject *seq;
 
-    if ((type == &takewhile_type) &&
+    if ((type == &takewhile_type ||
+         type->tp_init == takewhile_type.tp_init) &&
         !_PyArg_NoKeywords("takewhile", kwargs)) {
         goto exit;
     }
@@ -329,7 +336,8 @@ itertools_starmap(PyTypeObject *type, PyObject *args, PyObject *kwargs)
     PyObject *func;
     PyObject *seq;
 
-    if ((type == &starmap_type) &&
+    if ((type == &starmap_type ||
+         type->tp_init == starmap_type.tp_init) &&
         !_PyArg_NoKeywords("starmap", kwargs)) {
         goto exit;
     }
@@ -593,7 +601,8 @@ itertools_filterfalse(PyTypeObject *type, PyObject *args, PyObject *kwargs)
     PyObject *func;
     PyObject *seq;
 
-    if ((type == &filterfalse_type) &&
+    if ((type == &filterfalse_type ||
+         type->tp_init == filterfalse_type.tp_init) &&
         !_PyArg_NoKeywords("filterfalse", kwargs)) {
         goto exit;
     }
@@ -658,4 +667,4 @@ skip_optional_pos:
 exit:
     return return_value;
 }
-/*[clinic end generated code: output=889c4afc3b13574f input=a9049054013a1b77]*/
+/*[clinic end generated code: output=5364de2e143609b9 input=a9049054013a1b77]*/
index d7095dfb00ead9e7fead2a5c6f22609c7397fad0..a1695f2390a9c958ce5ae0c31e1b0796186be28b 100644 (file)
@@ -933,11 +933,13 @@ select_kqueue(PyTypeObject *type, PyObject *args, PyObject *kwargs)
 {
     PyObject *return_value = NULL;
 
-    if ((type == _selectstate_by_type(type)->kqueue_queue_Type) &&
+    if ((type == _selectstate_by_type(type)->kqueue_queue_Type ||
+         type->tp_init == _selectstate_by_type(type)->kqueue_queue_Type->tp_init) &&
         !_PyArg_NoPositional("kqueue", args)) {
         goto exit;
     }
-    if ((type == _selectstate_by_type(type)->kqueue_queue_Type) &&
+    if ((type == _selectstate_by_type(type)->kqueue_queue_Type ||
+         type->tp_init == _selectstate_by_type(type)->kqueue_queue_Type->tp_init) &&
         !_PyArg_NoKeywords("kqueue", kwargs)) {
         goto exit;
     }
@@ -1179,4 +1181,4 @@ exit:
 #ifndef SELECT_KQUEUE_CONTROL_METHODDEF
     #define SELECT_KQUEUE_CONTROL_METHODDEF
 #endif /* !defined(SELECT_KQUEUE_CONTROL_METHODDEF) */
-/*[clinic end generated code: output=cd2062a787e13b35 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=ed1e5a658863244c input=a9049054013a1b77]*/
index 643f47b045046cbfe4fa2ef2743350016200f182..ec0aa76fa5794c2518cf48b8b500b9942b4d9550 100644 (file)
@@ -1614,7 +1614,8 @@ islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
     Py_ssize_t numargs;
     isliceobject *lz;
 
-    if (type == &islice_type && !_PyArg_NoKeywords("islice", kwds))
+    if ((type == &islice_type || type->tp_init == islice_type.tp_init) &&
+        !_PyArg_NoKeywords("islice", kwds))
         return NULL;
 
     if (!PyArg_UnpackTuple(args, "islice", 2, 4, &seq, &a1, &a2, &a3))
@@ -2021,7 +2022,8 @@ chain_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
     PyObject *source;
 
-    if (type == &chain_type && !_PyArg_NoKeywords("chain", kwds))
+    if ((type == &chain_type || type->tp_init == chain_type.tp_init) &&
+        !_PyArg_NoKeywords("chain", kwds))
         return NULL;
 
     source = PyObject_GetIter(args);
index ac844b133c4878a8624eb2b6ae01b53e65540714..ee425f61bb113d58c7d9624f966776ac800ae486 100644 (file)
@@ -46,7 +46,8 @@ code_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
     PyObject *freevars = NULL;
     PyObject *cellvars = NULL;
 
-    if ((type == &PyCode_Type) &&
+    if ((type == &PyCode_Type ||
+         type->tp_init == PyCode_Type.tp_init) &&
         !_PyArg_NoKeywords("code", kwargs)) {
         goto exit;
     }
@@ -455,4 +456,4 @@ code__varname_from_oparg(PyCodeObject *self, PyObject *const *args, Py_ssize_t n
 exit:
     return return_value;
 }
-/*[clinic end generated code: output=18b9ddc86714e56e input=a9049054013a1b77]*/
+/*[clinic end generated code: output=9e8c4a19474ec520 input=a9049054013a1b77]*/
index 09d4c87e155f47229d3b9382aeaabff64cdfc66c..7513c9526ac50fc02ddb78f5165165838b895d67 100644 (file)
@@ -64,7 +64,8 @@ reversed_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
     PyObject *return_value = NULL;
     PyObject *seq;
 
-    if ((type == &PyReversed_Type) &&
+    if ((type == &PyReversed_Type ||
+         type->tp_init == PyReversed_Type.tp_init) &&
         !_PyArg_NoKeywords("reversed", kwargs)) {
         goto exit;
     }
@@ -77,4 +78,4 @@ reversed_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
 exit:
     return return_value;
 }
-/*[clinic end generated code: output=e18c3fefcf914ec7 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=a3937b6b33499560 input=a9049054013a1b77]*/
index 5643f0e3ac6501786efe29f11e827eb5f13c6d45..88dc482e32e52d6618825078bcc4eb2a294de4ca 100644 (file)
@@ -208,7 +208,8 @@ float_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
     PyObject *return_value = NULL;
     PyObject *x = NULL;
 
-    if ((type == &PyFloat_Type) &&
+    if ((type == &PyFloat_Type ||
+         type->tp_init == PyFloat_Type.tp_init) &&
         !_PyArg_NoKeywords("float", kwargs)) {
         goto exit;
     }
@@ -387,4 +388,4 @@ float___format__(PyObject *self, PyObject *arg)
 exit:
     return return_value;
 }
-/*[clinic end generated code: output=bb079c3e130e4ce6 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=122a73f4c9d25806 input=a9049054013a1b77]*/
index 01e31d76cfa17503289af9c75688db83e2ce4d3d..75ed9f9df8599eb96dfb3e5f9abdb02ea5af961e 100644 (file)
@@ -299,7 +299,8 @@ list___init__(PyObject *self, PyObject *args, PyObject *kwargs)
     int return_value = -1;
     PyObject *iterable = NULL;
 
-    if (Py_IS_TYPE(self, &PyList_Type) &&
+    if ((Py_IS_TYPE(self, &PyList_Type) ||
+         Py_TYPE(self)->tp_new == PyList_Type.tp_new) &&
         !_PyArg_NoKeywords("list", kwargs)) {
         goto exit;
     }
@@ -352,4 +353,4 @@ list___reversed__(PyListObject *self, PyObject *Py_UNUSED(ignored))
 {
     return list___reversed___impl(self);
 }
-/*[clinic end generated code: output=0063aad535edf62d input=a9049054013a1b77]*/
+/*[clinic end generated code: output=acb2f87736311930 input=a9049054013a1b77]*/
index fe2fae42eeafc0c78069bbce602ca01f19dd580d..f65e5fa14ebd7d76e5b1e4597399dd30f56cfa1c 100644 (file)
@@ -77,7 +77,8 @@ tuple_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
     PyObject *return_value = NULL;
     PyObject *iterable = NULL;
 
-    if ((type == &PyTuple_Type) &&
+    if ((type == &PyTuple_Type ||
+         type->tp_init == PyTuple_Type.tp_init) &&
         !_PyArg_NoKeywords("tuple", kwargs)) {
         goto exit;
     }
@@ -111,4 +112,4 @@ tuple___getnewargs__(PyTupleObject *self, PyObject *Py_UNUSED(ignored))
 {
     return tuple___getnewargs___impl(self);
 }
-/*[clinic end generated code: output=56fab9b7368aba49 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=72cc0bc4f7358116 input=a9049054013a1b77]*/
index caff85c9e38939e56ae62012b3798b4cb754095e..af521b2baca41fc1a9bf5978437be439f80b4f41 100644 (file)
@@ -1009,7 +1009,9 @@ frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
     PyObject *iterable = NULL;
 
-    if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset", kwds)) {
+    if ((type == &PyFrozenSet_Type ||
+         type->tp_init == PyFrozenSet_Type.tp_init) &&
+        !_PyArg_NoKeywords("frozenset", kwds)) {
         return NULL;
     }
 
@@ -1944,7 +1946,9 @@ set_init(PySetObject *self, PyObject *args, PyObject *kwds)
 {
     PyObject *iterable = NULL;
 
-    if (!_PyArg_NoKeywords("set", kwds))
+     if ((Py_IS_TYPE(self, &PySet_Type) ||
+          Py_TYPE(self)->tp_new == PySet_Type.tp_new) &&
+         !_PyArg_NoKeywords("set", kwds))
         return -1;
     if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
         return -1;
index 73541835d6c8cc8d983d8d4b862cf31d5f62e7e6..76c9f87fdf18a8ffe0c5ac8145d589a4e04ce852 100644 (file)
@@ -507,7 +507,8 @@ filter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
     PyObject *it;
     filterobject *lz;
 
-    if (type == &PyFilter_Type && !_PyArg_NoKeywords("filter", kwds))
+    if ((type == &PyFilter_Type || type->tp_init == PyFilter_Type.tp_init) &&
+        !_PyArg_NoKeywords("filter", kwds))
         return NULL;
 
     if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq))
@@ -1218,7 +1219,8 @@ map_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
     mapobject *lz;
     Py_ssize_t numargs, i;
 
-    if (type == &PyMap_Type && !_PyArg_NoKeywords("map", kwds))
+    if ((type == &PyMap_Type || type->tp_init == PyMap_Type.tp_init) &&
+        !_PyArg_NoKeywords("map", kwds))
         return NULL;
 
     numargs = PyTuple_Size(args);
index d6a820d828178fb57b4f9e80d64aa8ec3e9a291c..f20fab61c4de8e9e126d6787189992b74950ff41 100755 (executable)
@@ -3735,10 +3735,16 @@ class self_converter(CConverter):
 
         if ((kind in (METHOD_NEW, METHOD_INIT)) and cls and cls.typedef):
             type_object = self.function.cls.type_object
+            prefix = (type_object[1:] + '.' if type_object[0] == '&' else
+                      type_object + '->')
             if kind == METHOD_NEW:
-                type_check = '({} == {})'.format(self.name, type_object)
+                type_check = ('({0} == {1} ||\n        '
+                              ' {0}->tp_init == {2}tp_init)'
+                             ).format(self.name, type_object, prefix)
             else:
-                type_check = 'Py_IS_TYPE({}, {})'.format(self.name, type_object)
+                type_check = ('(Py_IS_TYPE({0}, {1}) ||\n        '
+                              ' Py_TYPE({0})->tp_new == {2}tp_new)'
+                             ).format(self.name, type_object, prefix)
 
             line = '{} &&\n        '.format(type_check)
             template_dict['self_type_check'] = line