From 628f162cd47f3252e49c8b1f1e55c09bced292a4 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 24 Aug 2023 20:09:23 +0200 Subject: [PATCH] gh-80527: Change support.requires_legacy_unicode_capi() (GH-108438) The decorator now requires to be called with parenthesis: @support.requires_legacy_unicode_capi() instead of: @support.requires_legacy_unicode_capi The implementation now only imports _testcapi when the decorator is called, so "import test.support" no longer imports the _testcapi extension. (cherry picked from commit 995f4c48e11349fbfb9233e02b732d4534d3008e) Co-authored-by: Victor Stinner --- Lib/test/support/__init__.py | 15 ++++++++------- Lib/test/test_capi/test_getargs.py | 8 ++++---- Lib/test/test_csv.py | 2 +- Lib/test/test_decimal.py | 4 ++-- Lib/test/test_unicode.py | 4 ++-- 5 files changed, 17 insertions(+), 16 deletions(-) diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index c3c3cf0a71596c..ef7b5c814d5d47 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -21,11 +21,6 @@ from .testresult import get_test_runner -try: - from _testcapi import unicode_legacy_string -except ImportError: - unicode_legacy_string = None - __all__ = [ # globals "PIPE_MAX_SIZE", "verbose", "max_memuse", "use_resources", "failfast", @@ -507,8 +502,14 @@ def has_no_debug_ranges(): def requires_debug_ranges(reason='requires co_positions / debug_ranges'): return unittest.skipIf(has_no_debug_ranges(), reason) -requires_legacy_unicode_capi = unittest.skipUnless(unicode_legacy_string, - 'requires legacy Unicode C API') +def requires_legacy_unicode_capi(): + try: + from _testcapi import unicode_legacy_string + except ImportError: + unicode_legacy_string = None + + return unittest.skipUnless(unicode_legacy_string, + 'requires legacy Unicode C API') # Is not actually used in tests, but is kept for compatibility. is_jython = sys.platform.startswith('java') diff --git a/Lib/test/test_capi/test_getargs.py b/Lib/test/test_capi/test_getargs.py index 3792d1a6515b44..ec4100e976e50d 100644 --- a/Lib/test/test_capi/test_getargs.py +++ b/Lib/test/test_capi/test_getargs.py @@ -1021,7 +1021,7 @@ def test_et_hash(self): buf = bytearray() self.assertRaises(ValueError, getargs_et_hash, 'abc\xe9', 'latin1', buf) - @support.requires_legacy_unicode_capi + @support.requires_legacy_unicode_capi() def test_u(self): from _testcapi import getargs_u with self.assertWarns(DeprecationWarning): @@ -1037,7 +1037,7 @@ def test_u(self): with self.assertWarns(DeprecationWarning): self.assertRaises(TypeError, getargs_u, None) - @support.requires_legacy_unicode_capi + @support.requires_legacy_unicode_capi() def test_u_hash(self): from _testcapi import getargs_u_hash with self.assertWarns(DeprecationWarning): @@ -1053,7 +1053,7 @@ def test_u_hash(self): with self.assertWarns(DeprecationWarning): self.assertRaises(TypeError, getargs_u_hash, None) - @support.requires_legacy_unicode_capi + @support.requires_legacy_unicode_capi() def test_Z(self): from _testcapi import getargs_Z with self.assertWarns(DeprecationWarning): @@ -1069,7 +1069,7 @@ def test_Z(self): with self.assertWarns(DeprecationWarning): self.assertIsNone(getargs_Z(None)) - @support.requires_legacy_unicode_capi + @support.requires_legacy_unicode_capi() def test_Z_hash(self): from _testcapi import getargs_Z_hash with self.assertWarns(DeprecationWarning): diff --git a/Lib/test/test_csv.py b/Lib/test/test_csv.py index de7ac97d72cb8e..bc9961e0831f0e 100644 --- a/Lib/test/test_csv.py +++ b/Lib/test/test_csv.py @@ -282,7 +282,7 @@ def test_writerows_errors(self): self.assertRaises(OSError, writer.writerows, BadIterable()) @support.cpython_only - @support.requires_legacy_unicode_capi + @support.requires_legacy_unicode_capi() @warnings_helper.ignore_warnings(category=DeprecationWarning) def test_writerows_legacy_strings(self): import _testcapi diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py index d0ba34803c21e6..4d3ea732212905 100644 --- a/Lib/test/test_decimal.py +++ b/Lib/test/test_decimal.py @@ -587,7 +587,7 @@ def test_explicit_from_string(self): self.assertRaises(InvalidOperation, Decimal, "1_2_\u00003") @cpython_only - @requires_legacy_unicode_capi + @requires_legacy_unicode_capi() @warnings_helper.ignore_warnings(category=DeprecationWarning) def test_from_legacy_strings(self): import _testcapi @@ -2919,7 +2919,7 @@ def test_none_args(self): Overflow]) @cpython_only - @requires_legacy_unicode_capi + @requires_legacy_unicode_capi() @warnings_helper.ignore_warnings(category=DeprecationWarning) def test_from_legacy_strings(self): import _testcapi diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py index 4ebbb9d32a3d75..2fd66c93f9f319 100644 --- a/Lib/test/test_unicode.py +++ b/Lib/test/test_unicode.py @@ -814,7 +814,7 @@ def test_isidentifier(self): self.assertFalse("0".isidentifier()) @support.cpython_only - @support.requires_legacy_unicode_capi + @support.requires_legacy_unicode_capi() @unittest.skipIf(_testcapi is None, 'need _testcapi module') def test_isidentifier_legacy(self): u = '𝖀𝖓𝖎𝖈𝖔𝖉𝖊' @@ -2491,7 +2491,7 @@ def test_getnewargs(self): self.assertEqual(len(args), 1) @support.cpython_only - @support.requires_legacy_unicode_capi + @support.requires_legacy_unicode_capi() @unittest.skipIf(_testcapi is None, 'need _testcapi module') def test_resize(self): for length in range(1, 100, 7):