From ae69f9b8e4a8987ff814006f9b3731e0185d320b Mon Sep 17 00:00:00 2001 From: Radislav Chugunov Date: Sun, 7 May 2023 15:26:14 +0300 Subject: [PATCH 01/12] Disallow instantiation and subtyping of `_csv.Reader` and `_csv.Writer` types Set `Py_TPFLAGS_DISALLOW_INSTANTIATION` and unset `Py_TPFLAGS_BASETYPE` flags on `Reader` and `Writer` types to prevent their instantiation and subtyping --- Modules/_csv.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Modules/_csv.c b/Modules/_csv.c index 0cde5c5a8bdc68..370738cad1c413 100644 --- a/Modules/_csv.c +++ b/Modules/_csv.c @@ -999,8 +999,8 @@ static PyType_Slot Reader_Type_slots[] = { PyType_Spec Reader_Type_spec = { .name = "_csv.reader", .basicsize = sizeof(ReaderObj), - .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | - Py_TPFLAGS_IMMUTABLETYPE), + .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | + Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_DISALLOW_INSTANTIATION), .slots = Reader_Type_slots }; @@ -1430,8 +1430,8 @@ static PyType_Slot Writer_Type_slots[] = { PyType_Spec Writer_Type_spec = { .name = "_csv.writer", .basicsize = sizeof(WriterObj), - .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | - Py_TPFLAGS_IMMUTABLETYPE), + .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | + Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_DISALLOW_INSTANTIATION), .slots = Writer_Type_slots, }; From 7d1fcd809040aeb0ff3bd8efd52a52feb8678cb1 Mon Sep 17 00:00:00 2001 From: Radislav Chugunov Date: Sun, 7 May 2023 17:14:39 +0300 Subject: [PATCH 02/12] added test --- Lib/test/test_csv.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Lib/test/test_csv.py b/Lib/test/test_csv.py index 8fb97bc0c1a1a7..ae893a11d04570 100644 --- a/Lib/test/test_csv.py +++ b/Lib/test/test_csv.py @@ -7,6 +7,7 @@ from io import StringIO from tempfile import TemporaryFile import csv +import _csv import gc import pickle from test import support @@ -1430,5 +1431,15 @@ def test_subclassable(self): # issue 44089 class Foo(csv.Error): ... + def test_issue104265(self): + with self.assertRaisesRegex(TypeError, "cannot create '_csv.reader' instances"): + _csv.Reader() + with self.assertRaisesRegex(TypeError, "cannot create '_csv.writer' instances"): + _csv.Writer() + with self.assertRaisesRegex(TypeError, "type '_csv.reader' is not an acceptable base type"): + class Foo(_csv.Reader): pass + with self.assertRaisesRegex(TypeError, "type '_csv.writer' is not an acceptable base type"): + class Foo(_csv.Writer): pass + if __name__ == '__main__': unittest.main() From aa34fae64b55982bb0abb8a306f4075fe17292c1 Mon Sep 17 00:00:00 2001 From: Radislav Chugunov Date: Sun, 7 May 2023 17:30:52 +0300 Subject: [PATCH 03/12] changed tests to use check_disallow_instantiation, separated tests --- Lib/test/test_csv.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_csv.py b/Lib/test/test_csv.py index ae893a11d04570..4bf945ec63df80 100644 --- a/Lib/test/test_csv.py +++ b/Lib/test/test_csv.py @@ -11,7 +11,7 @@ import gc import pickle from test import support -from test.support import warnings_helper +from test.support import warnings_helper, check_disallow_instantiation from itertools import permutations from textwrap import dedent from collections import OrderedDict @@ -1431,13 +1431,17 @@ def test_subclassable(self): # issue 44089 class Foo(csv.Error): ... - def test_issue104265(self): - with self.assertRaisesRegex(TypeError, "cannot create '_csv.reader' instances"): - _csv.Reader() - with self.assertRaisesRegex(TypeError, "cannot create '_csv.writer' instances"): - _csv.Writer() + def test_reader_disallow_instantiation(self): + check_disallow_instantiation(self, _csv.Reader) + + def test_writer_disallow_instantiation(self): + check_disallow_instantiation(self, _csv.Writer) + + def test_reader_not_basetype(self): with self.assertRaisesRegex(TypeError, "type '_csv.reader' is not an acceptable base type"): class Foo(_csv.Reader): pass + + def test_writer_not_basetype(self): with self.assertRaisesRegex(TypeError, "type '_csv.writer' is not an acceptable base type"): class Foo(_csv.Writer): pass From 6c45ff87210c6e26633e24f9a45b11adf0b4bd71 Mon Sep 17 00:00:00 2001 From: Radislav Chugunov Date: Sun, 7 May 2023 18:42:42 +0300 Subject: [PATCH 04/12] reverted Py_TPFLAGS_BASETYPE flag deletion, removed corresponding tests --- Lib/test/test_csv.py | 8 -------- Modules/_csv.c | 4 ++-- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/Lib/test/test_csv.py b/Lib/test/test_csv.py index 4bf945ec63df80..b1b8263ed8c2ea 100644 --- a/Lib/test/test_csv.py +++ b/Lib/test/test_csv.py @@ -1437,13 +1437,5 @@ def test_reader_disallow_instantiation(self): def test_writer_disallow_instantiation(self): check_disallow_instantiation(self, _csv.Writer) - def test_reader_not_basetype(self): - with self.assertRaisesRegex(TypeError, "type '_csv.reader' is not an acceptable base type"): - class Foo(_csv.Reader): pass - - def test_writer_not_basetype(self): - with self.assertRaisesRegex(TypeError, "type '_csv.writer' is not an acceptable base type"): - class Foo(_csv.Writer): pass - if __name__ == '__main__': unittest.main() diff --git a/Modules/_csv.c b/Modules/_csv.c index 370738cad1c413..9ab2ad266c2739 100644 --- a/Modules/_csv.c +++ b/Modules/_csv.c @@ -999,7 +999,7 @@ static PyType_Slot Reader_Type_slots[] = { PyType_Spec Reader_Type_spec = { .name = "_csv.reader", .basicsize = sizeof(ReaderObj), - .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | + .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_DISALLOW_INSTANTIATION), .slots = Reader_Type_slots }; @@ -1430,7 +1430,7 @@ static PyType_Slot Writer_Type_slots[] = { PyType_Spec Writer_Type_spec = { .name = "_csv.writer", .basicsize = sizeof(WriterObj), - .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | + .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_DISALLOW_INSTANTIATION), .slots = Writer_Type_slots, }; From 7d9ec2a140c846dcaa2e13b16106047fab4d3a93 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sun, 7 May 2023 16:13:10 +0000 Subject: [PATCH 05/12] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20b?= =?UTF-8?q?lurb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2023-05-07-16-13-07.gh-issue-104265.fU60bZ.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-05-07-16-13-07.gh-issue-104265.fU60bZ.rst diff --git a/Misc/NEWS.d/next/Library/2023-05-07-16-13-07.gh-issue-104265.fU60bZ.rst b/Misc/NEWS.d/next/Library/2023-05-07-16-13-07.gh-issue-104265.fU60bZ.rst new file mode 100644 index 00000000000000..6786cc03030c41 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-05-07-16-13-07.gh-issue-104265.fU60bZ.rst @@ -0,0 +1 @@ +Fix a :mod:`_csv` module regression where :class:`~_csv.Reader` and :class:`~_csv.Writer` types became directly instantiable which led to their improper initialization and subsequent crashes. The regression was introduced in 3.10.0a4 with :gh:`23224`. From 19bddd06eb3eec41561c0356c97ce38797b92af9 Mon Sep 17 00:00:00 2001 From: chgnrdv <52372310+chgnrdv@users.noreply.github.com> Date: Sun, 7 May 2023 19:24:55 +0300 Subject: [PATCH 06/12] Update 2023-05-07-16-13-07.gh-issue-104265.fU60bZ.rst --- .../next/Library/2023-05-07-16-13-07.gh-issue-104265.fU60bZ.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2023-05-07-16-13-07.gh-issue-104265.fU60bZ.rst b/Misc/NEWS.d/next/Library/2023-05-07-16-13-07.gh-issue-104265.fU60bZ.rst index 6786cc03030c41..87787454b3f5d4 100644 --- a/Misc/NEWS.d/next/Library/2023-05-07-16-13-07.gh-issue-104265.fU60bZ.rst +++ b/Misc/NEWS.d/next/Library/2023-05-07-16-13-07.gh-issue-104265.fU60bZ.rst @@ -1 +1 @@ -Fix a :mod:`_csv` module regression where :class:`~_csv.Reader` and :class:`~_csv.Writer` types became directly instantiable which led to their improper initialization and subsequent crashes. The regression was introduced in 3.10.0a4 with :gh:`23224`. +Fix a :mod:`_csv` module regression where :class:`~_csv.Reader` and :class:`~_csv.Writer` types became directly instantiable which led to their improper initialization and subsequent crashes. The regression was introduced in 3.10.0a4 with PR 23224 (:bpo:`14935`). From b3f4a65c5d6b49ce6a59b6abd84bedfabc2f3460 Mon Sep 17 00:00:00 2001 From: chgnrdv <52372310+chgnrdv@users.noreply.github.com> Date: Sun, 7 May 2023 19:28:50 +0300 Subject: [PATCH 07/12] Update 2023-05-07-16-13-07.gh-issue-104265.fU60bZ.rst --- .../next/Library/2023-05-07-16-13-07.gh-issue-104265.fU60bZ.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2023-05-07-16-13-07.gh-issue-104265.fU60bZ.rst b/Misc/NEWS.d/next/Library/2023-05-07-16-13-07.gh-issue-104265.fU60bZ.rst index 87787454b3f5d4..95741ea7ba3b4f 100644 --- a/Misc/NEWS.d/next/Library/2023-05-07-16-13-07.gh-issue-104265.fU60bZ.rst +++ b/Misc/NEWS.d/next/Library/2023-05-07-16-13-07.gh-issue-104265.fU60bZ.rst @@ -1 +1 @@ -Fix a :mod:`_csv` module regression where :class:`~_csv.Reader` and :class:`~_csv.Writer` types became directly instantiable which led to their improper initialization and subsequent crashes. The regression was introduced in 3.10.0a4 with PR 23224 (:bpo:`14935`). +Fix a :mod:`_csv` module regression where :class:`~_csv.Reader` and :class:`~_csv.Writer` types became directly instantiable which led to their improper initialization and subsequent crashes. The regression was introduced in 3.10.0a4 with PR 23224 (:issue:`14935`). From a022e91600542a25e011649873a2c7af7adb080f Mon Sep 17 00:00:00 2001 From: chgnrdv <52372310+chgnrdv@users.noreply.github.com> Date: Sun, 7 May 2023 19:39:31 +0300 Subject: [PATCH 08/12] Update 2023-05-07-16-13-07.gh-issue-104265.fU60bZ.rst --- .../next/Library/2023-05-07-16-13-07.gh-issue-104265.fU60bZ.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2023-05-07-16-13-07.gh-issue-104265.fU60bZ.rst b/Misc/NEWS.d/next/Library/2023-05-07-16-13-07.gh-issue-104265.fU60bZ.rst index 95741ea7ba3b4f..e6b3e9329d098a 100644 --- a/Misc/NEWS.d/next/Library/2023-05-07-16-13-07.gh-issue-104265.fU60bZ.rst +++ b/Misc/NEWS.d/next/Library/2023-05-07-16-13-07.gh-issue-104265.fU60bZ.rst @@ -1 +1 @@ -Fix a :mod:`_csv` module regression where :class:`~_csv.Reader` and :class:`~_csv.Writer` types became directly instantiable which led to their improper initialization and subsequent crashes. The regression was introduced in 3.10.0a4 with PR 23224 (:issue:`14935`). +Fix a :mod:`!_csv` module regression where :class:`!_csv.Reader` and :class:`!_csv.Writer` types became directly instantiable which led to their improper initialization and subsequent crashes. The regression was introduced in 3.10.0a4 with PR 23224 (:issue:`14935`). From 52aacfd58f25b44b112ce8e437c0bb895a982d1b Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sun, 7 May 2023 19:56:47 +0000 Subject: [PATCH 09/12] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20b?= =?UTF-8?q?lurb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2023-05-07-19-56-45.gh-issue-104265.fVblry.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-05-07-19-56-45.gh-issue-104265.fVblry.rst diff --git a/Misc/NEWS.d/next/Library/2023-05-07-19-56-45.gh-issue-104265.fVblry.rst b/Misc/NEWS.d/next/Library/2023-05-07-19-56-45.gh-issue-104265.fVblry.rst new file mode 100644 index 00000000000000..6a31feba22759e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-05-07-19-56-45.gh-issue-104265.fVblry.rst @@ -0,0 +1 @@ +Prevent possible crash by disallow instantiation of the :class:`!_csv.Reader` and :class:`!_csv.Writer` types. The regression was introduced in 3.10.0a4 with PR 23224 (:issue:`14935`). Patch by Radislav Chugunov. From d31d2140b1356cc89b0e4b5a5df47f84de290bb9 Mon Sep 17 00:00:00 2001 From: chgnrdv <52372310+chgnrdv@users.noreply.github.com> Date: Sun, 7 May 2023 22:57:15 +0300 Subject: [PATCH 10/12] Delete 2023-05-07-16-13-07.gh-issue-104265.fU60bZ.rst --- .../next/Library/2023-05-07-16-13-07.gh-issue-104265.fU60bZ.rst | 1 - 1 file changed, 1 deletion(-) delete mode 100644 Misc/NEWS.d/next/Library/2023-05-07-16-13-07.gh-issue-104265.fU60bZ.rst diff --git a/Misc/NEWS.d/next/Library/2023-05-07-16-13-07.gh-issue-104265.fU60bZ.rst b/Misc/NEWS.d/next/Library/2023-05-07-16-13-07.gh-issue-104265.fU60bZ.rst deleted file mode 100644 index e6b3e9329d098a..00000000000000 --- a/Misc/NEWS.d/next/Library/2023-05-07-16-13-07.gh-issue-104265.fU60bZ.rst +++ /dev/null @@ -1 +0,0 @@ -Fix a :mod:`!_csv` module regression where :class:`!_csv.Reader` and :class:`!_csv.Writer` types became directly instantiable which led to their improper initialization and subsequent crashes. The regression was introduced in 3.10.0a4 with PR 23224 (:issue:`14935`). From 28047448a58d9e4525b710e417a91a3ad60f7a7d Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Sun, 7 May 2023 22:27:22 +0200 Subject: [PATCH 11/12] Update Misc/NEWS.d/next/Library/2023-05-07-19-56-45.gh-issue-104265.fVblry.rst --- .../Library/2023-05-07-19-56-45.gh-issue-104265.fVblry.rst | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2023-05-07-19-56-45.gh-issue-104265.fVblry.rst b/Misc/NEWS.d/next/Library/2023-05-07-19-56-45.gh-issue-104265.fVblry.rst index 6a31feba22759e..9c582844bf909b 100644 --- a/Misc/NEWS.d/next/Library/2023-05-07-19-56-45.gh-issue-104265.fVblry.rst +++ b/Misc/NEWS.d/next/Library/2023-05-07-19-56-45.gh-issue-104265.fVblry.rst @@ -1 +1,4 @@ -Prevent possible crash by disallow instantiation of the :class:`!_csv.Reader` and :class:`!_csv.Writer` types. The regression was introduced in 3.10.0a4 with PR 23224 (:issue:`14935`). Patch by Radislav Chugunov. +Prevent possible crash by disallowing instantiation of the +:class:`!_csv.Reader` and :class:`!_csv.Writer` types. +The regression was introduced in 3.10.0a4 with PR 23224 (:issue:`14935`). +Patch by Radislav Chugunov. From 8255467fac91c691367f1b38a1a89a28f68dd5bc Mon Sep 17 00:00:00 2001 From: Radislav Chugunov Date: Sun, 7 May 2023 23:49:19 +0300 Subject: [PATCH 12/12] applied suggestion by erlend-aasland --- Lib/test/test_csv.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Lib/test/test_csv.py b/Lib/test/test_csv.py index b1b8263ed8c2ea..de7ac97d72cb8e 100644 --- a/Lib/test/test_csv.py +++ b/Lib/test/test_csv.py @@ -7,11 +7,10 @@ from io import StringIO from tempfile import TemporaryFile import csv -import _csv import gc import pickle from test import support -from test.support import warnings_helper, check_disallow_instantiation +from test.support import warnings_helper, import_helper, check_disallow_instantiation from itertools import permutations from textwrap import dedent from collections import OrderedDict @@ -1431,11 +1430,12 @@ def test_subclassable(self): # issue 44089 class Foo(csv.Error): ... - def test_reader_disallow_instantiation(self): - check_disallow_instantiation(self, _csv.Reader) - - def test_writer_disallow_instantiation(self): - check_disallow_instantiation(self, _csv.Writer) + @support.cpython_only + def test_disallow_instantiation(self): + _csv = import_helper.import_module("_csv") + for tp in _csv.Reader, _csv.Writer: + with self.subTest(tp=tp): + check_disallow_instantiation(self, tp) if __name__ == '__main__': unittest.main()