[3.12] gh-80527: Change support.requires_legacy_unicode_capi() (GH-108438) (#108446)

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 995f4c48e1)

Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
Miss Islington (bot) 2023-08-25 09:18:24 -07:00 committed by GitHub
parent 22621907ee
commit 0eb6d87304
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 17 additions and 16 deletions

View file

@ -21,11 +21,6 @@ import warnings
from .testresult import get_test_runner from .testresult import get_test_runner
try:
from _testcapi import unicode_legacy_string
except ImportError:
unicode_legacy_string = None
__all__ = [ __all__ = [
# globals # globals
"PIPE_MAX_SIZE", "verbose", "max_memuse", "use_resources", "failfast", "PIPE_MAX_SIZE", "verbose", "max_memuse", "use_resources", "failfast",
@ -507,7 +502,13 @@ def has_no_debug_ranges():
def requires_debug_ranges(reason='requires co_positions / debug_ranges'): def requires_debug_ranges(reason='requires co_positions / debug_ranges'):
return unittest.skipIf(has_no_debug_ranges(), reason) return unittest.skipIf(has_no_debug_ranges(), reason)
requires_legacy_unicode_capi = unittest.skipUnless(unicode_legacy_string, 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') 'requires legacy Unicode C API')
# Is not actually used in tests, but is kept for compatibility. # Is not actually used in tests, but is kept for compatibility.

View file

@ -1021,7 +1021,7 @@ class String_TestCase(unittest.TestCase):
buf = bytearray() buf = bytearray()
self.assertRaises(ValueError, getargs_et_hash, 'abc\xe9', 'latin1', buf) self.assertRaises(ValueError, getargs_et_hash, 'abc\xe9', 'latin1', buf)
@support.requires_legacy_unicode_capi @support.requires_legacy_unicode_capi()
def test_u(self): def test_u(self):
from _testcapi import getargs_u from _testcapi import getargs_u
with self.assertWarns(DeprecationWarning): with self.assertWarns(DeprecationWarning):
@ -1037,7 +1037,7 @@ class String_TestCase(unittest.TestCase):
with self.assertWarns(DeprecationWarning): with self.assertWarns(DeprecationWarning):
self.assertRaises(TypeError, getargs_u, None) self.assertRaises(TypeError, getargs_u, None)
@support.requires_legacy_unicode_capi @support.requires_legacy_unicode_capi()
def test_u_hash(self): def test_u_hash(self):
from _testcapi import getargs_u_hash from _testcapi import getargs_u_hash
with self.assertWarns(DeprecationWarning): with self.assertWarns(DeprecationWarning):
@ -1053,7 +1053,7 @@ class String_TestCase(unittest.TestCase):
with self.assertWarns(DeprecationWarning): with self.assertWarns(DeprecationWarning):
self.assertRaises(TypeError, getargs_u_hash, None) self.assertRaises(TypeError, getargs_u_hash, None)
@support.requires_legacy_unicode_capi @support.requires_legacy_unicode_capi()
def test_Z(self): def test_Z(self):
from _testcapi import getargs_Z from _testcapi import getargs_Z
with self.assertWarns(DeprecationWarning): with self.assertWarns(DeprecationWarning):
@ -1069,7 +1069,7 @@ class String_TestCase(unittest.TestCase):
with self.assertWarns(DeprecationWarning): with self.assertWarns(DeprecationWarning):
self.assertIsNone(getargs_Z(None)) self.assertIsNone(getargs_Z(None))
@support.requires_legacy_unicode_capi @support.requires_legacy_unicode_capi()
def test_Z_hash(self): def test_Z_hash(self):
from _testcapi import getargs_Z_hash from _testcapi import getargs_Z_hash
with self.assertWarns(DeprecationWarning): with self.assertWarns(DeprecationWarning):

View file

@ -282,7 +282,7 @@ class Test_Csv(unittest.TestCase):
self.assertRaises(OSError, writer.writerows, BadIterable()) self.assertRaises(OSError, writer.writerows, BadIterable())
@support.cpython_only @support.cpython_only
@support.requires_legacy_unicode_capi @support.requires_legacy_unicode_capi()
@warnings_helper.ignore_warnings(category=DeprecationWarning) @warnings_helper.ignore_warnings(category=DeprecationWarning)
def test_writerows_legacy_strings(self): def test_writerows_legacy_strings(self):
import _testcapi import _testcapi

View file

@ -587,7 +587,7 @@ class ExplicitConstructionTest:
self.assertRaises(InvalidOperation, Decimal, "1_2_\u00003") self.assertRaises(InvalidOperation, Decimal, "1_2_\u00003")
@cpython_only @cpython_only
@requires_legacy_unicode_capi @requires_legacy_unicode_capi()
@warnings_helper.ignore_warnings(category=DeprecationWarning) @warnings_helper.ignore_warnings(category=DeprecationWarning)
def test_from_legacy_strings(self): def test_from_legacy_strings(self):
import _testcapi import _testcapi
@ -2919,7 +2919,7 @@ class ContextAPItests:
Overflow]) Overflow])
@cpython_only @cpython_only
@requires_legacy_unicode_capi @requires_legacy_unicode_capi()
@warnings_helper.ignore_warnings(category=DeprecationWarning) @warnings_helper.ignore_warnings(category=DeprecationWarning)
def test_from_legacy_strings(self): def test_from_legacy_strings(self):
import _testcapi import _testcapi

View file

@ -814,7 +814,7 @@ class UnicodeTest(string_tests.CommonTest,
self.assertFalse("0".isidentifier()) self.assertFalse("0".isidentifier())
@support.cpython_only @support.cpython_only
@support.requires_legacy_unicode_capi @support.requires_legacy_unicode_capi()
@unittest.skipIf(_testcapi is None, 'need _testcapi module') @unittest.skipIf(_testcapi is None, 'need _testcapi module')
def test_isidentifier_legacy(self): def test_isidentifier_legacy(self):
u = '𝖀𝖓𝖎𝖈𝖔𝖉𝖊' u = '𝖀𝖓𝖎𝖈𝖔𝖉𝖊'
@ -2491,7 +2491,7 @@ class UnicodeTest(string_tests.CommonTest,
self.assertEqual(len(args), 1) self.assertEqual(len(args), 1)
@support.cpython_only @support.cpython_only
@support.requires_legacy_unicode_capi @support.requires_legacy_unicode_capi()
@unittest.skipIf(_testcapi is None, 'need _testcapi module') @unittest.skipIf(_testcapi is None, 'need _testcapi module')
def test_resize(self): def test_resize(self):
for length in range(1, 100, 7): for length in range(1, 100, 7):