Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
[3.10] bpo-43988: Add test.support.check_disallow_instantiation() (GH…
…-25757).

(cherry picked from commit 4f72526)

Co-authored-by: Erlend Egeberg Aasland <erlend.aasland@innova.no>
  • Loading branch information
Erlend Egeberg Aasland committed Jun 23, 2021
commit 9122e878f0254b4ff419960ba94f2d27843e33cf
11 changes: 11 additions & 0 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"requires_IEEE_754", "requires_zlib",
"anticipate_failure", "load_package_tests", "detect_api_mismatch",
"check__all__", "skip_if_buggy_ucrt_strfptime",
"check_disallow_instantiation",
# sys
"is_jython", "is_android", "check_impl_detail", "unix_shell",
"setswitchinterval",
Expand Down Expand Up @@ -1992,3 +1993,13 @@ def infinite_recursion(max_depth=75):
yield
finally:
sys.setrecursionlimit(original_depth)


def check_disallow_instantiation(testcase, tp, *args, **kwds):
"""
Helper for testing types with the Py_TPFLAGS_DISALLOW_INSTANTIATION flag.

See bpo-43916.
"""
msg = f"cannot create '{tp.__module__}\.{tp.__name__}' instances"
testcase.assertRaisesRegex(TypeError, msg, tp, *args, **kwds)
6 changes: 3 additions & 3 deletions Lib/test/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ def test_bad_constructor(self):

@support.cpython_only
def test_disallow_instantiation(self):
# Ensure that the type disallows instantiation (bpo-43916)
tp = type(iter(array.array('I')))
self.assertRaises(TypeError, tp)
my_array = array.array("I")
tp = type(iter(my_array))
support.check_disallow_instantiation(self, tp, my_array)

@support.cpython_only
def test_immutable(self):
Expand Down