diff options
26 files changed, 285 insertions, 67 deletions
diff --git a/Lib/test/seq_tests.py b/Lib/test/seq_tests.py index 1d9ad58..a41970d 100644 --- a/Lib/test/seq_tests.py +++ b/Lib/test/seq_tests.py @@ -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])) diff --git a/Lib/test/test_float.py b/Lib/test/test_float.py index 29f7756..aba1583 100644 --- a/Lib/test/test_float.py +++ b/Lib/test/test_float.py @@ -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()) diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py index aa81076..db6a983 100644 --- a/Lib/test/test_itertools.py +++ b/Lib/test/test_itertools.py @@ -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): diff --git a/Lib/test/test_list.py b/Lib/test/test_list.py index 3c8d829..d3da05b 100644 --- a/Lib/test/test_list.py +++ b/Lib/test/test_list.py @@ -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 []) diff --git a/Lib/test/test_set.py b/Lib/test/test_set.py index 29bb39d..65fda9e 100644 --- a/Lib/test/test_set.py +++ b/Lib/test/test_set.py @@ -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) diff --git a/Lib/test/test_tuple.py b/Lib/test/test_tuple.py index d2a2ed3..9ce80c5 100644 --- a/Lib/test/test_tuple.py +++ b/Lib/test/test_tuple.py @@ -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 index 0000000..579b57e --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2021-05-30-16-37-47.bpo-43413.vYFPPC.rst @@ -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__()``. diff --git a/Modules/_io/clinic/bufferedio.c.h b/Modules/_io/clinic/bufferedio.c.h index 19543fd..2fd0232 100644 --- a/Modules/_io/clinic/bufferedio.c.h +++ b/Modules/_io/clinic/bufferedio.c.h @@ -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]*/ diff --git a/Modules/_randommodule.c b/Modules/_randommodule.c index 7ad4299..65d41f4 100644 --- a/Modules/_randommodule.c +++ b/Modules/_randommodule.c @@ -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; } diff --git a/Modules/_sqlite/clinic/cursor.c.h b/Modules/_sqlite/clinic/cursor.c.h index 07e1587..eb3e7ce 100644 --- a/Modules/_sqlite/clinic/cursor.c.h +++ b/Modules/_sqlite/clinic/cursor.c.h @@ -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]*/ diff --git a/Modules/_sqlite/clinic/row.c.h b/Modules/_sqlite/clinic/row.c.h index 310d62b..c936ef7 100644 --- a/Modules/_sqlite/clinic/row.c.h +++ b/Modules/_sqlite/clinic/row.c.h @@ -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]*/ diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index 1d9d4cd..9a3203c 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -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)) diff --git a/Modules/clinic/_collectionsmodule.c.h b/Modules/clinic/_collectionsmodule.c.h index 7e18aeb..2875b3c 100644 --- a/Modules/clinic/_collectionsmodule.c.h +++ b/Modules/clinic/_collectionsmodule.c.h @@ -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]*/ diff --git a/Modules/clinic/_queuemodule.c.h b/Modules/clinic/_queuemodule.c.h index 8741f7d..d952256 100644 --- a/Modules/clinic/_queuemodule.c.h +++ b/Modules/clinic/_queuemodule.c.h @@ -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]*/ diff --git a/Modules/clinic/_ssl.c.h b/Modules/clinic/_ssl.c.h index b59b129..67eaf3f 100644 --- a/Modules/clinic/_ssl.c.h +++ b/Modules/clinic/_ssl.c.h @@ -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]*/ diff --git a/Modules/clinic/itertoolsmodule.c.h b/Modules/clinic/itertoolsmodule.c.h index 82729ee..7f5abe6 100644 --- a/Modules/clinic/itertoolsmodule.c.h +++ b/Modules/clinic/itertoolsmodule.c.h @@ -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]*/ diff --git a/Modules/clinic/selectmodule.c.h b/Modules/clinic/selectmodule.c.h index d7095df..a1695f2 100644 --- a/Modules/clinic/selectmodule.c.h +++ b/Modules/clinic/selectmodule.c.h @@ -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]*/ diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index 643f47b..ec0aa76 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -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); diff --git a/Objects/clinic/codeobject.c.h b/Objects/clinic/codeobject.c.h index ac844b1..ee425f6 100644 --- a/Objects/clinic/codeobject.c.h +++ b/Objects/clinic/codeobject.c.h @@ -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]*/ diff --git a/Objects/clinic/enumobject.c.h b/Objects/clinic/enumobject.c.h index 09d4c87..7513c95 100644 --- a/Objects/clinic/enumobject.c.h +++ b/Objects/clinic/enumobject.c.h @@ -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]*/ diff --git a/Objects/clinic/floatobject.c.h b/Objects/clinic/floatobject.c.h index 5643f0e..88dc482 100644 --- a/Objects/clinic/floatobject.c.h +++ b/Objects/clinic/floatobject.c.h @@ -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]*/ diff --git a/Objects/clinic/listobject.c.h b/Objects/clinic/listobject.c.h index 01e31d7..75ed9f9 100644 --- a/Objects/clinic/listobject.c.h +++ b/Objects/clinic/listobject.c.h @@ -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]*/ diff --git a/Objects/clinic/tupleobject.c.h b/Objects/clinic/tupleobject.c.h index fe2fae4..f65e5fa 100644 --- a/Objects/clinic/tupleobject.c.h +++ b/Objects/clinic/tupleobject.c.h @@ -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]*/ diff --git a/Objects/setobject.c b/Objects/setobject.c index caff85c..af521b2 100644 --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -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; diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 7354183..76c9f87 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -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); diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index d6a820d..f20fab6 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -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 |