]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] gh-104265 Disallow instantiation of `_csv.Reader` and `_csv.Writer` (GH-104266...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 8 May 2023 12:04:43 +0000 (05:04 -0700)
committerGitHub <noreply@github.com>
Mon, 8 May 2023 12:04:43 +0000 (17:34 +0530)
gh-104265 Disallow instantiation of `_csv.Reader` and `_csv.Writer` (GH-104266)
(cherry picked from commit 06c2a4858b8806abc700a0471434067910db54ec)

Co-authored-by: chgnrdv <52372310+chgnrdv@users.noreply.github.com>
Lib/test/test_csv.py
Misc/NEWS.d/next/Library/2023-05-07-19-56-45.gh-issue-104265.fVblry.rst [new file with mode: 0644]
Modules/_csv.c

index 834217bf6030dc9a426711e229c4a8739eb2f610..05653a268417990f70f92149847b4a4d55623741 100644 (file)
@@ -10,7 +10,7 @@ import csv
 import gc
 import pickle
 from test import support
-from test.support import warnings_helper
+from test.support import warnings_helper, import_helper, check_disallow_instantiation
 from itertools import permutations
 from textwrap import dedent
 from collections import OrderedDict
@@ -1390,5 +1390,12 @@ class MiscTestCase(unittest.TestCase):
         # issue 44089
         class Foo(csv.Error): ...
 
+    @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()
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 (file)
index 0000000..9c58284
--- /dev/null
@@ -0,0 +1,4 @@
+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.
index 7519da6807fe3e59cedc391dd355d4f8bc5d53c5..7314d9c705377857c5d78ee4013ab0efd11e554f 100644 (file)
@@ -1000,7 +1000,7 @@ 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),
+              Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_DISALLOW_INSTANTIATION),
     .slots = Reader_Type_slots
 };
 
@@ -1425,7 +1425,7 @@ 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),
+              Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_DISALLOW_INSTANTIATION),
     .slots = Writer_Type_slots,
 };