Skip to content

bpo-43988: Add test.support.check_disallow_instantiation #25757

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

Merged
merged 1 commit into from
May 26, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
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 @@ -1982,3 +1983,13 @@ def skip_if_broken_multiprocessing_synchronize():
synchronize.Lock(ctx=None)
except OSError as exc:
raise unittest.SkipTest(f"broken multiprocessing SemLock: {exc!r}")


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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use f"xxx\.xxx" or fr"xxx.xxx". Or maybe use re.escape().

Copy link
Contributor Author

@erlend-aasland erlend-aasland May 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since these tests are decorated with cpython_only, it should be ok to match the exact string, as produced by Objects/typeobject.c? Just matching the type module/name could in theory generate wrong result; maybe not in practice though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about setting the match pattern depending on what test.support.check_impl_detail() says?

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