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 3 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
17 changes: 16 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,19 @@ 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

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()
8 changes: 4 additions & 4 deletions Modules/_csv.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

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

Expand Down