Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-104265 Disallow instantiation of _csv.Reader and _csv.Writer #104266

Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Lib/test/test_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
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
from test.support import warnings_helper, check_disallow_instantiation
from itertools import permutations
from textwrap import dedent
from collections import OrderedDict
Expand Down Expand Up @@ -1430,5 +1431,11 @@ 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)
erlend-aasland marked this conversation as resolved.
Show resolved Hide resolved

if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 2 additions & 2 deletions Modules/_csv.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down Expand Up @@ -1431,7 +1431,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,
};

Expand Down