Skip to content

Commit 06c2a48

Browse files
authoredMay 7, 2023
gh-104265 Disallow instantiation of _csv.Reader and _csv.Writer (#104266)
1 parent c0ece3d commit 06c2a48

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed
 

‎Lib/test/test_csv.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import gc
1111
import pickle
1212
from test import support
13-
from test.support import warnings_helper
13+
from test.support import warnings_helper, import_helper, check_disallow_instantiation
1414
from itertools import permutations
1515
from textwrap import dedent
1616
from collections import OrderedDict
@@ -1430,5 +1430,12 @@ def test_subclassable(self):
14301430
# issue 44089
14311431
class Foo(csv.Error): ...
14321432

1433+
@support.cpython_only
1434+
def test_disallow_instantiation(self):
1435+
_csv = import_helper.import_module("_csv")
1436+
for tp in _csv.Reader, _csv.Writer:
1437+
with self.subTest(tp=tp):
1438+
check_disallow_instantiation(self, tp)
1439+
14331440
if __name__ == '__main__':
14341441
unittest.main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Prevent possible crash by disallowing instantiation of the
2+
:class:`!_csv.Reader` and :class:`!_csv.Writer` types.
3+
The regression was introduced in 3.10.0a4 with PR 23224 (:issue:`14935`).
4+
Patch by Radislav Chugunov.

‎Modules/_csv.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,7 @@ PyType_Spec Reader_Type_spec = {
10001000
.name = "_csv.reader",
10011001
.basicsize = sizeof(ReaderObj),
10021002
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
1003-
Py_TPFLAGS_IMMUTABLETYPE),
1003+
Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_DISALLOW_INSTANTIATION),
10041004
.slots = Reader_Type_slots
10051005
};
10061006

@@ -1431,7 +1431,7 @@ PyType_Spec Writer_Type_spec = {
14311431
.name = "_csv.writer",
14321432
.basicsize = sizeof(WriterObj),
14331433
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
1434-
Py_TPFLAGS_IMMUTABLETYPE),
1434+
Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_DISALLOW_INSTANTIATION),
14351435
.slots = Writer_Type_slots,
14361436
};
14371437

0 commit comments

Comments
 (0)
Please sign in to comment.