Skip to content

Commit

Permalink
pythongh-116417: Move limited C API unicode.c tests to _testlimitedca…
Browse files Browse the repository at this point in the history
…pi (python#116993)

Split unicode.c tests of _testcapi into two parts: limited C API
tests in _testlimitedcapi and non-limited C API tests in _testcapi.

Update test_codecs.
  • Loading branch information
vstinner authored and diegorusso committed Apr 17, 2024
1 parent 7041920 commit 835d28e
Show file tree
Hide file tree
Showing 12 changed files with 2,082 additions and 2,032 deletions.
46 changes: 23 additions & 23 deletions Lib/test/test_capi/test_codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import sys
from test.support import import_helper

_testcapi = import_helper.import_module('_testcapi')
_testlimitedcapi = import_helper.import_module('_testlimitedcapi')

NULL = None

Expand All @@ -27,7 +27,7 @@ class CAPITest(unittest.TestCase):

def test_fromencodedobject(self):
"""Test PyUnicode_FromEncodedObject()"""
fromencodedobject = _testcapi.unicode_fromencodedobject
fromencodedobject = _testlimitedcapi.unicode_fromencodedobject

self.assertEqual(fromencodedobject(b'abc', NULL), 'abc')
self.assertEqual(fromencodedobject(b'abc', 'ascii'), 'abc')
Expand All @@ -52,7 +52,7 @@ def test_fromencodedobject(self):

def test_decode(self):
"""Test PyUnicode_Decode()"""
decode = _testcapi.unicode_decode
decode = _testlimitedcapi.unicode_decode

self.assertEqual(decode(b'[\xe2\x82\xac]', 'utf-8'), '[\u20ac]')
self.assertEqual(decode(b'[\xa4]', 'iso8859-15'), '[\u20ac]')
Expand All @@ -70,7 +70,7 @@ def test_decode(self):

def test_asencodedstring(self):
"""Test PyUnicode_AsEncodedString()"""
asencodedstring = _testcapi.unicode_asencodedstring
asencodedstring = _testlimitedcapi.unicode_asencodedstring

self.assertEqual(asencodedstring('abc', NULL), b'abc')
self.assertEqual(asencodedstring('abc', 'ascii'), b'abc')
Expand All @@ -93,7 +93,7 @@ def test_asencodedstring(self):

def test_decodeutf8(self):
"""Test PyUnicode_DecodeUTF8()"""
decodeutf8 = _testcapi.unicode_decodeutf8
decodeutf8 = _testlimitedcapi.unicode_decodeutf8

for s in ['abc', '\xa1\xa2', '\u4f60\u597d', 'a\U0001f600']:
b = s.encode('utf-8')
Expand All @@ -113,7 +113,7 @@ def test_decodeutf8(self):

def test_decodeutf8stateful(self):
"""Test PyUnicode_DecodeUTF8Stateful()"""
decodeutf8stateful = _testcapi.unicode_decodeutf8stateful
decodeutf8stateful = _testlimitedcapi.unicode_decodeutf8stateful

for s in ['abc', '\xa1\xa2', '\u4f60\u597d', 'a\U0001f600']:
b = s.encode('utf-8')
Expand All @@ -136,7 +136,7 @@ def test_decodeutf8stateful(self):

def test_asutf8string(self):
"""Test PyUnicode_AsUTF8String()"""
asutf8string = _testcapi.unicode_asutf8string
asutf8string = _testlimitedcapi.unicode_asutf8string

for s in ['abc', '\xa1\xa2', '\u4f60\u597d', 'a\U0001f600']:
self.assertEqual(asutf8string(s), s.encode('utf-8'))
Expand All @@ -148,7 +148,7 @@ def test_asutf8string(self):

def test_decodeutf16(self):
"""Test PyUnicode_DecodeUTF16()"""
decodeutf16 = _testcapi.unicode_decodeutf16
decodeutf16 = _testlimitedcapi.unicode_decodeutf16

naturalbyteorder = -1 if sys.byteorder == 'little' else 1
for s in ['abc', '\xa1\xa2', '\u4f60\u597d', 'a\U0001f600']:
Expand Down Expand Up @@ -192,7 +192,7 @@ def test_decodeutf16(self):

def test_decodeutf16stateful(self):
"""Test PyUnicode_DecodeUTF16Stateful()"""
decodeutf16stateful = _testcapi.unicode_decodeutf16stateful
decodeutf16stateful = _testlimitedcapi.unicode_decodeutf16stateful

naturalbyteorder = -1 if sys.byteorder == 'little' else 1
for s in ['abc', '\xa1\xa2', '\u4f60\u597d', 'a\U0001f600']:
Expand Down Expand Up @@ -238,7 +238,7 @@ def test_decodeutf16stateful(self):

def test_asutf16string(self):
"""Test PyUnicode_AsUTF16String()"""
asutf16string = _testcapi.unicode_asutf16string
asutf16string = _testlimitedcapi.unicode_asutf16string

for s in ['abc', '\xa1\xa2', '\u4f60\u597d', 'a\U0001f600']:
self.assertEqual(asutf16string(s), s.encode('utf-16'))
Expand All @@ -250,7 +250,7 @@ def test_asutf16string(self):

def test_decodeutf32(self):
"""Test PyUnicode_DecodeUTF8()"""
decodeutf32 = _testcapi.unicode_decodeutf32
decodeutf32 = _testlimitedcapi.unicode_decodeutf32

naturalbyteorder = -1 if sys.byteorder == 'little' else 1
for s in ['abc', '\xa1\xa2', '\u4f60\u597d', 'a\U0001f600']:
Expand Down Expand Up @@ -290,7 +290,7 @@ def test_decodeutf32(self):

def test_decodeutf32stateful(self):
"""Test PyUnicode_DecodeUTF32Stateful()"""
decodeutf32stateful = _testcapi.unicode_decodeutf32stateful
decodeutf32stateful = _testlimitedcapi.unicode_decodeutf32stateful

naturalbyteorder = -1 if sys.byteorder == 'little' else 1
for s in ['abc', '\xa1\xa2', '\u4f60\u597d', 'a\U0001f600']:
Expand Down Expand Up @@ -342,7 +342,7 @@ def test_decodeutf32stateful(self):

def test_asutf32string(self):
"""Test PyUnicode_AsUTF32String()"""
asutf32string = _testcapi.unicode_asutf32string
asutf32string = _testlimitedcapi.unicode_asutf32string

for s in ['abc', '\xa1\xa2', '\u4f60\u597d', 'a\U0001f600']:
self.assertEqual(asutf32string(s), s.encode('utf-32'))
Expand All @@ -354,7 +354,7 @@ def test_asutf32string(self):

def test_decodelatin1(self):
"""Test PyUnicode_DecodeLatin1()"""
decodelatin1 = _testcapi.unicode_decodelatin1
decodelatin1 = _testlimitedcapi.unicode_decodelatin1

self.assertEqual(decodelatin1(b'abc'), 'abc')
self.assertEqual(decodelatin1(b'abc', 'strict'), 'abc')
Expand All @@ -365,7 +365,7 @@ def test_decodelatin1(self):

def test_aslatin1string(self):
"""Test PyUnicode_AsLatin1String()"""
aslatin1string = _testcapi.unicode_aslatin1string
aslatin1string = _testlimitedcapi.unicode_aslatin1string

self.assertEqual(aslatin1string('abc'), b'abc')
self.assertEqual(aslatin1string('\xa1\xa2'), b'\xa1\xa2')
Expand All @@ -377,7 +377,7 @@ def test_aslatin1string(self):

def test_decodeascii(self):
"""Test PyUnicode_DecodeASCII()"""
decodeascii = _testcapi.unicode_decodeascii
decodeascii = _testlimitedcapi.unicode_decodeascii

self.assertEqual(decodeascii(b'abc'), 'abc')
self.assertEqual(decodeascii(b'abc', 'strict'), 'abc')
Expand All @@ -392,7 +392,7 @@ def test_decodeascii(self):

def test_asasciistring(self):
"""Test PyUnicode_AsASCIIString()"""
asasciistring = _testcapi.unicode_asasciistring
asasciistring = _testlimitedcapi.unicode_asasciistring

self.assertEqual(asasciistring('abc'), b'abc')

Expand All @@ -403,7 +403,7 @@ def test_asasciistring(self):

def test_decodecharmap(self):
"""Test PyUnicode_DecodeCharmap()"""
decodecharmap = _testcapi.unicode_decodecharmap
decodecharmap = _testlimitedcapi.unicode_decodecharmap

self.assertEqual(decodecharmap(b'\3\0\7', {0: 'a', 3: 'b', 7: 'c'}), 'bac')
self.assertEqual(decodecharmap(b'\1\0\2', ['a', 'b', 'c']), 'bac')
Expand All @@ -426,7 +426,7 @@ def test_decodecharmap(self):

def test_ascharmapstring(self):
"""Test PyUnicode_AsCharmapString()"""
ascharmapstring = _testcapi.unicode_ascharmapstring
ascharmapstring = _testlimitedcapi.unicode_ascharmapstring

self.assertEqual(ascharmapstring('abc', {97: 3, 98: 0, 99: 7}), b'\3\0\7')
self.assertEqual(ascharmapstring('\xa1\xa2\xa3', {0xa1: 3, 0xa2: 0, 0xa3: 7}), b'\3\0\7')
Expand All @@ -443,7 +443,7 @@ def test_ascharmapstring(self):

def test_decodeunicodeescape(self):
"""Test PyUnicode_DecodeUnicodeEscape()"""
decodeunicodeescape = _testcapi.unicode_decodeunicodeescape
decodeunicodeescape = _testlimitedcapi.unicode_decodeunicodeescape

self.assertEqual(decodeunicodeescape(b'abc'), 'abc')
self.assertEqual(decodeunicodeescape(br'\t\n\r\x0b\x0c\x00\\'), '\t\n\r\v\f\0\\')
Expand All @@ -467,7 +467,7 @@ def test_decodeunicodeescape(self):

def test_asunicodeescapestring(self):
"""Test PyUnicode_AsUnicodeEscapeString()"""
asunicodeescapestring = _testcapi.unicode_asunicodeescapestring
asunicodeescapestring = _testlimitedcapi.unicode_asunicodeescapestring

self.assertEqual(asunicodeescapestring('abc'), b'abc')
self.assertEqual(asunicodeescapestring('\t\n\r\v\f\0\\'), br'\t\n\r\x0b\x0c\x00\\')
Expand All @@ -481,7 +481,7 @@ def test_asunicodeescapestring(self):

def test_decoderawunicodeescape(self):
"""Test PyUnicode_DecodeRawUnicodeEscape()"""
decoderawunicodeescape = _testcapi.unicode_decoderawunicodeescape
decoderawunicodeescape = _testlimitedcapi.unicode_decoderawunicodeescape

self.assertEqual(decoderawunicodeescape(b'abc'), 'abc')
self.assertEqual(decoderawunicodeescape(b'\t\n\r\v\f\0\\'), '\t\n\r\v\f\0\\')
Expand All @@ -503,7 +503,7 @@ def test_decoderawunicodeescape(self):

def test_asrawunicodeescapestring(self):
"""Test PyUnicode_AsRawUnicodeEscapeString()"""
asrawunicodeescapestring = _testcapi.unicode_asrawunicodeescapestring
asrawunicodeescapestring = _testlimitedcapi.unicode_asrawunicodeescapestring

self.assertEqual(asrawunicodeescapestring('abc'), b'abc')
self.assertEqual(asrawunicodeescapestring('\t\n\r\v\f\0\\'), b'\t\n\r\v\f\0\\')
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_capi/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2338,7 +2338,7 @@ class Test_testcapi(unittest.TestCase):
# Suppress warning from PyUnicode_FromUnicode().
@warnings_helper.ignore_warnings(category=DeprecationWarning)
def test_widechar(self):
_testcapi.test_widechar()
_testlimitedcapi.test_widechar()

def test_version_api_data(self):
self.assertEqual(_testcapi.Py_Version, sys.hexversion)
Expand Down
Loading

0 comments on commit 835d28e

Please sign in to comment.