From c51a8e907227591f74015eaeed5ddb37e7883a66 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 3 Jul 2017 23:01:57 +0300 Subject: [2.7] Rename test_ file that is really a support file to remove test_ prefix. (#2548) I thought I had run the full test suite before the last checkin, but obviously I didn't. test_multibytecodec_support.py isn't really a test file, it is a support file that contains a base test class. Rename it to multibytecodec_support so that regrtest test discovery doesn't think it is a test file that should be run.. (cherry picked from commit 75d9aca97ad3c3d823d2f2211f579454b3216f51) --- Lib/test/multibytecodec_support.py | 372 ++++++++++++++++++++++++++++++++ Lib/test/test_codecencodings_cn.py | 18 +- Lib/test/test_codecencodings_hk.py | 6 +- Lib/test/test_codecencodings_iso2022.py | 14 +- Lib/test/test_codecencodings_jp.py | 22 +- Lib/test/test_codecencodings_kr.py | 14 +- Lib/test/test_codecencodings_tw.py | 6 +- Lib/test/test_codecmaps_cn.py | 8 +- Lib/test/test_codecmaps_hk.py | 4 +- Lib/test/test_codecmaps_jp.py | 12 +- Lib/test/test_codecmaps_kr.py | 8 +- Lib/test/test_codecmaps_tw.py | 6 +- Lib/test/test_multibytecodec_support.py | 372 -------------------------------- 13 files changed, 431 insertions(+), 431 deletions(-) create mode 100644 Lib/test/multibytecodec_support.py delete mode 100644 Lib/test/test_multibytecodec_support.py diff --git a/Lib/test/multibytecodec_support.py b/Lib/test/multibytecodec_support.py new file mode 100644 index 0000000..5b2329b --- /dev/null +++ b/Lib/test/multibytecodec_support.py @@ -0,0 +1,372 @@ +# multibytecodec_support.py +# Common Unittest Routines for CJK codecs +# + +import codecs +import os +import re +import sys +import unittest +from httplib import HTTPException +from test import test_support +from StringIO import StringIO + +class TestBase: + encoding = '' # codec name + codec = None # codec tuple (with 4 elements) + tstring = '' # string to test StreamReader + + codectests = None # must set. codec test tuple + roundtriptest = 1 # set if roundtrip is possible with unicode + has_iso10646 = 0 # set if this encoding contains whole iso10646 map + xmlcharnametest = None # string to test xmlcharrefreplace + unmappedunicode = u'\udeee' # a unicode code point that is not mapped. + + def setUp(self): + if self.codec is None: + self.codec = codecs.lookup(self.encoding) + self.encode = self.codec.encode + self.decode = self.codec.decode + self.reader = self.codec.streamreader + self.writer = self.codec.streamwriter + self.incrementalencoder = self.codec.incrementalencoder + self.incrementaldecoder = self.codec.incrementaldecoder + + def test_chunkcoding(self): + for native, utf8 in zip(*[StringIO(f).readlines() + for f in self.tstring]): + u = self.decode(native)[0] + self.assertEqual(u, utf8.decode('utf-8')) + if self.roundtriptest: + self.assertEqual(native, self.encode(u)[0]) + + def test_errorhandle(self): + for source, scheme, expected in self.codectests: + if isinstance(source, bytes): + func = self.decode + else: + func = self.encode + if expected: + result = func(source, scheme)[0] + if func is self.decode: + self.assertTrue(type(result) is unicode, type(result)) + self.assertEqual(result, expected, + '%r.decode(%r, %r)=%r != %r' + % (source, self.encoding, scheme, result, + expected)) + else: + self.assertTrue(type(result) is bytes, type(result)) + self.assertEqual(result, expected, + '%r.encode(%r, %r)=%r != %r' + % (source, self.encoding, scheme, result, + expected)) + else: + self.assertRaises(UnicodeError, func, source, scheme) + + def test_xmlcharrefreplace(self): + if self.has_iso10646: + self.skipTest('encoding contains full ISO 10646 map') + + s = u"\u0b13\u0b23\u0b60 nd eggs" + self.assertEqual( + self.encode(s, "xmlcharrefreplace")[0], + "ଓଣୠ nd eggs" + ) + + def test_customreplace_encode(self): + if self.has_iso10646: + self.skipTest('encoding contains full ISO 10646 map') + + from htmlentitydefs import codepoint2name + + def xmlcharnamereplace(exc): + if not isinstance(exc, UnicodeEncodeError): + raise TypeError("don't know how to handle %r" % exc) + l = [] + for c in exc.object[exc.start:exc.end]: + if ord(c) in codepoint2name: + l.append(u"&%s;" % codepoint2name[ord(c)]) + else: + l.append(u"&#%d;" % ord(c)) + return (u"".join(l), exc.end) + + codecs.register_error("test.xmlcharnamereplace", xmlcharnamereplace) + + if self.xmlcharnametest: + sin, sout = self.xmlcharnametest + else: + sin = u"\xab\u211c\xbb = \u2329\u1234\u232a" + sout = "«ℜ» = ⟨ሴ⟩" + self.assertEqual(self.encode(sin, + "test.xmlcharnamereplace")[0], sout) + + def test_callback_wrong_objects(self): + def myreplace(exc): + return (ret, exc.end) + codecs.register_error("test.cjktest", myreplace) + + for ret in ([1, 2, 3], [], None, object(), 'string', ''): + self.assertRaises(TypeError, self.encode, self.unmappedunicode, + 'test.cjktest') + + def test_callback_long_index(self): + def myreplace(exc): + return (u'x', long(exc.end)) + codecs.register_error("test.cjktest", myreplace) + self.assertEqual(self.encode(u'abcd' + self.unmappedunicode + u'efgh', + 'test.cjktest'), ('abcdxefgh', 9)) + + def myreplace(exc): + return (u'x', sys.maxint + 1) + codecs.register_error("test.cjktest", myreplace) + self.assertRaises(IndexError, self.encode, self.unmappedunicode, + 'test.cjktest') + + def test_callback_None_index(self): + def myreplace(exc): + return (u'x', None) + codecs.register_error("test.cjktest", myreplace) + self.assertRaises(TypeError, self.encode, self.unmappedunicode, + 'test.cjktest') + + def test_callback_backward_index(self): + def myreplace(exc): + if myreplace.limit > 0: + myreplace.limit -= 1 + return (u'REPLACED', 0) + else: + return (u'TERMINAL', exc.end) + myreplace.limit = 3 + codecs.register_error("test.cjktest", myreplace) + self.assertEqual(self.encode(u'abcd' + self.unmappedunicode + u'efgh', + 'test.cjktest'), + ('abcdREPLACEDabcdREPLACEDabcdREPLACEDabcdTERMINALefgh', 9)) + + def test_callback_forward_index(self): + def myreplace(exc): + return (u'REPLACED', exc.end + 2) + codecs.register_error("test.cjktest", myreplace) + self.assertEqual(self.encode(u'abcd' + self.unmappedunicode + u'efgh', + 'test.cjktest'), ('abcdREPLACEDgh', 9)) + + def test_callback_index_outofbound(self): + def myreplace(exc): + return (u'TERM', 100) + codecs.register_error("test.cjktest", myreplace) + self.assertRaises(IndexError, self.encode, self.unmappedunicode, + 'test.cjktest') + + def test_incrementalencoder(self): + UTF8Reader = codecs.getreader('utf-8') + for sizehint in [None] + range(1, 33) + \ + [64, 128, 256, 512, 1024]: + istream = UTF8Reader(StringIO(self.tstring[1])) + ostream = StringIO() + encoder = self.incrementalencoder() + while 1: + if sizehint is not None: + data = istream.read(sizehint) + else: + data = istream.read() + + if not data: + break + e = encoder.encode(data) + ostream.write(e) + + self.assertEqual(ostream.getvalue(), self.tstring[0]) + + def test_incrementaldecoder(self): + UTF8Writer = codecs.getwriter('utf-8') + for sizehint in [None, -1] + range(1, 33) + \ + [64, 128, 256, 512, 1024]: + istream = StringIO(self.tstring[0]) + ostream = UTF8Writer(StringIO()) + decoder = self.incrementaldecoder() + while 1: + data = istream.read(sizehint) + if not data: + break + else: + u = decoder.decode(data) + ostream.write(u) + + self.assertEqual(ostream.getvalue(), self.tstring[1]) + + def test_incrementalencoder_error_callback(self): + inv = self.unmappedunicode + + e = self.incrementalencoder() + self.assertRaises(UnicodeEncodeError, e.encode, inv, True) + + e.errors = 'ignore' + self.assertEqual(e.encode(inv, True), '') + + e.reset() + def tempreplace(exc): + return (u'called', exc.end) + codecs.register_error('test.incremental_error_callback', tempreplace) + e.errors = 'test.incremental_error_callback' + self.assertEqual(e.encode(inv, True), 'called') + + # again + e.errors = 'ignore' + self.assertEqual(e.encode(inv, True), '') + + def test_streamreader(self): + UTF8Writer = codecs.getwriter('utf-8') + for name in ["read", "readline", "readlines"]: + for sizehint in [None, -1] + range(1, 33) + \ + [64, 128, 256, 512, 1024]: + istream = self.reader(StringIO(self.tstring[0])) + ostream = UTF8Writer(StringIO()) + func = getattr(istream, name) + while 1: + data = func(sizehint) + if not data: + break + if name == "readlines": + ostream.writelines(data) + else: + ostream.write(data) + + self.assertEqual(ostream.getvalue(), self.tstring[1]) + + def test_streamwriter(self): + readfuncs = ('read', 'readline', 'readlines') + UTF8Reader = codecs.getreader('utf-8') + for name in readfuncs: + for sizehint in [None] + range(1, 33) + \ + [64, 128, 256, 512, 1024]: + istream = UTF8Reader(StringIO(self.tstring[1])) + ostream = self.writer(StringIO()) + func = getattr(istream, name) + while 1: + if sizehint is not None: + data = func(sizehint) + else: + data = func() + + if not data: + break + if name == "readlines": + ostream.writelines(data) + else: + ostream.write(data) + + self.assertEqual(ostream.getvalue(), self.tstring[0]) + +class TestBase_Mapping(unittest.TestCase): + pass_enctest = [] + pass_dectest = [] + supmaps = [] + codectests = [] + + def __init__(self, *args, **kw): + unittest.TestCase.__init__(self, *args, **kw) + try: + self.open_mapping_file().close() # test it to report the error early + except (IOError, HTTPException): + self.skipTest("Could not retrieve "+self.mapfileurl) + + def open_mapping_file(self): + return test_support.open_urlresource(self.mapfileurl) + + def test_mapping_file(self): + if self.mapfileurl.endswith('.xml'): + self._test_mapping_file_ucm() + else: + self._test_mapping_file_plain() + + def _test_mapping_file_plain(self): + _unichr = lambda c: eval("u'\\U%08x'" % int(c, 16)) + unichrs = lambda s: u''.join(_unichr(c) for c in s.split('+')) + urt_wa = {} + + with self.open_mapping_file() as f: + for line in f: + if not line: + break + data = line.split('#')[0].strip().split() + if len(data) != 2: + continue + + csetval = eval(data[0]) + if csetval <= 0x7F: + csetch = chr(csetval & 0xff) + elif csetval >= 0x1000000: + csetch = chr(csetval >> 24) + chr((csetval >> 16) & 0xff) + \ + chr((csetval >> 8) & 0xff) + chr(csetval & 0xff) + elif csetval >= 0x10000: + csetch = chr(csetval >> 16) + \ + chr((csetval >> 8) & 0xff) + chr(csetval & 0xff) + elif csetval >= 0x100: + csetch = chr(csetval >> 8) + chr(csetval & 0xff) + else: + continue + + unich = unichrs(data[1]) + if unich == u'\ufffd' or unich in urt_wa: + continue + urt_wa[unich] = csetch + + self._testpoint(csetch, unich) + + def _test_mapping_file_ucm(self): + with self.open_mapping_file() as f: + ucmdata = f.read() + uc = re.findall('', ucmdata) + for uni, coded in uc: + unich = unichr(int(uni, 16)) + codech = ''.join(chr(int(c, 16)) for c in coded.split()) + self._testpoint(codech, unich) + + def test_mapping_supplemental(self): + for mapping in self.supmaps: + self._testpoint(*mapping) + + def _testpoint(self, csetch, unich): + if (csetch, unich) not in self.pass_enctest: + try: + self.assertEqual(unich.encode(self.encoding), csetch) + except UnicodeError, exc: + self.fail('Encoding failed while testing %s -> %s: %s' % ( + repr(unich), repr(csetch), exc.reason)) + if (csetch, unich) not in self.pass_dectest: + try: + self.assertEqual(csetch.decode(self.encoding), unich) + except UnicodeError, exc: + self.fail('Decoding failed while testing %s -> %s: %s' % ( + repr(csetch), repr(unich), exc.reason)) + + def test_errorhandle(self): + for source, scheme, expected in self.codectests: + if isinstance(source, bytes): + func = source.decode + else: + func = source.encode + if expected: + if isinstance(source, bytes): + result = func(self.encoding, scheme) + self.assertTrue(type(result) is unicode, type(result)) + self.assertEqual(result, expected, + '%r.decode(%r, %r)=%r != %r' + % (source, self.encoding, scheme, result, + expected)) + else: + result = func(self.encoding, scheme) + self.assertTrue(type(result) is bytes, type(result)) + self.assertEqual(result, expected, + '%r.encode(%r, %r)=%r != %r' + % (source, self.encoding, scheme, result, + expected)) + else: + self.assertRaises(UnicodeError, func, self.encoding, scheme) + +def load_teststring(name): + dir = os.path.join(os.path.dirname(__file__), 'cjkencodings') + with open(os.path.join(dir, name + '.txt'), 'rb') as f: + encoded = f.read() + with open(os.path.join(dir, name + '-utf8.txt'), 'rb') as f: + utf8 = f.read() + return encoded, utf8 diff --git a/Lib/test/test_codecencodings_cn.py b/Lib/test/test_codecencodings_cn.py index a104937..a1bbcb7 100644 --- a/Lib/test/test_codecencodings_cn.py +++ b/Lib/test/test_codecencodings_cn.py @@ -4,12 +4,12 @@ # from test import test_support -from test import test_multibytecodec_support +from test import multibytecodec_support import unittest -class Test_GB2312(test_multibytecodec_support.TestBase, unittest.TestCase): +class Test_GB2312(multibytecodec_support.TestBase, unittest.TestCase): encoding = 'gb2312' - tstring = test_multibytecodec_support.load_teststring('gb2312') + tstring = multibytecodec_support.load_teststring('gb2312') codectests = ( # invalid bytes ("abc\x81\x81\xc1\xc4", "strict", None), @@ -20,9 +20,9 @@ class Test_GB2312(test_multibytecodec_support.TestBase, unittest.TestCase): ("\xc1\x64", "strict", None), ) -class Test_GBK(test_multibytecodec_support.TestBase, unittest.TestCase): +class Test_GBK(multibytecodec_support.TestBase, unittest.TestCase): encoding = 'gbk' - tstring = test_multibytecodec_support.load_teststring('gbk') + tstring = multibytecodec_support.load_teststring('gbk') codectests = ( # invalid bytes ("abc\x80\x80\xc1\xc4", "strict", None), @@ -34,9 +34,9 @@ class Test_GBK(test_multibytecodec_support.TestBase, unittest.TestCase): (u"\u30fb", "strict", None), ) -class Test_GB18030(test_multibytecodec_support.TestBase, unittest.TestCase): +class Test_GB18030(multibytecodec_support.TestBase, unittest.TestCase): encoding = 'gb18030' - tstring = test_multibytecodec_support.load_teststring('gb18030') + tstring = multibytecodec_support.load_teststring('gb18030') codectests = ( # invalid bytes ("abc\x80\x80\xc1\xc4", "strict", None), @@ -55,9 +55,9 @@ class Test_GB18030(test_multibytecodec_support.TestBase, unittest.TestCase): ) has_iso10646 = True -class Test_HZ(test_multibytecodec_support.TestBase, unittest.TestCase): +class Test_HZ(multibytecodec_support.TestBase, unittest.TestCase): encoding = 'hz' - tstring = test_multibytecodec_support.load_teststring('hz') + tstring = multibytecodec_support.load_teststring('hz') codectests = ( # test '~\n' (3 lines) (b'This sentence is in ASCII.\n' diff --git a/Lib/test/test_codecencodings_hk.py b/Lib/test/test_codecencodings_hk.py index 391c316..00774ab 100644 --- a/Lib/test/test_codecencodings_hk.py +++ b/Lib/test/test_codecencodings_hk.py @@ -4,12 +4,12 @@ # from test import test_support -from test import test_multibytecodec_support +from test import multibytecodec_support import unittest -class Test_Big5HKSCS(test_multibytecodec_support.TestBase, unittest.TestCase): +class Test_Big5HKSCS(multibytecodec_support.TestBase, unittest.TestCase): encoding = 'big5hkscs' - tstring = test_multibytecodec_support.load_teststring('big5hkscs') + tstring = multibytecodec_support.load_teststring('big5hkscs') codectests = ( # invalid bytes ("abc\x80\x80\xc1\xc4", "strict", None), diff --git a/Lib/test/test_codecencodings_iso2022.py b/Lib/test/test_codecencodings_iso2022.py index 9f23628..75f4cfc 100644 --- a/Lib/test/test_codecencodings_iso2022.py +++ b/Lib/test/test_codecencodings_iso2022.py @@ -1,7 +1,7 @@ # Codec encoding tests for ISO 2022 encodings. from test import test_support -from test import test_multibytecodec_support +from test import multibytecodec_support import unittest COMMON_CODEC_TESTS = ( @@ -11,23 +11,23 @@ COMMON_CODEC_TESTS = ( (b'ab\x1B$def', 'replace', u'ab\uFFFD'), ) -class Test_ISO2022_JP(test_multibytecodec_support.TestBase, unittest.TestCase): +class Test_ISO2022_JP(multibytecodec_support.TestBase, unittest.TestCase): encoding = 'iso2022_jp' - tstring = test_multibytecodec_support.load_teststring('iso2022_jp') + tstring = multibytecodec_support.load_teststring('iso2022_jp') codectests = COMMON_CODEC_TESTS + ( (b'ab\x1BNdef', 'replace', u'ab\x1BNdef'), ) -class Test_ISO2022_JP2(test_multibytecodec_support.TestBase, unittest.TestCase): +class Test_ISO2022_JP2(multibytecodec_support.TestBase, unittest.TestCase): encoding = 'iso2022_jp_2' - tstring = test_multibytecodec_support.load_teststring('iso2022_jp') + tstring = multibytecodec_support.load_teststring('iso2022_jp') codectests = COMMON_CODEC_TESTS + ( (b'ab\x1BNdef', 'replace', u'abdef'), ) -class Test_ISO2022_KR(test_multibytecodec_support.TestBase, unittest.TestCase): +class Test_ISO2022_KR(multibytecodec_support.TestBase, unittest.TestCase): encoding = 'iso2022_kr' - tstring = test_multibytecodec_support.load_teststring('iso2022_kr') + tstring = multibytecodec_support.load_teststring('iso2022_kr') codectests = COMMON_CODEC_TESTS + ( (b'ab\x1BNdef', 'replace', u'ab\x1BNdef'), ) diff --git a/Lib/test/test_codecencodings_jp.py b/Lib/test/test_codecencodings_jp.py index f3cf923..e0ad054 100644 --- a/Lib/test/test_codecencodings_jp.py +++ b/Lib/test/test_codecencodings_jp.py @@ -4,12 +4,12 @@ # from test import test_support -from test import test_multibytecodec_support +from test import multibytecodec_support import unittest -class Test_CP932(test_multibytecodec_support.TestBase, unittest.TestCase): +class Test_CP932(multibytecodec_support.TestBase, unittest.TestCase): encoding = 'cp932' - tstring = test_multibytecodec_support.load_teststring('shift_jis') + tstring = multibytecodec_support.load_teststring('shift_jis') codectests = ( # invalid bytes ("abc\x81\x00\x81\x00\x82\x84", "strict", None), @@ -22,10 +22,10 @@ class Test_CP932(test_multibytecodec_support.TestBase, unittest.TestCase): ("\x81\x5f\x81\x61\x81\x7c", "replace", u"\uff3c\u2225\uff0d"), ) -class Test_EUC_JISX0213(test_multibytecodec_support.TestBase, +class Test_EUC_JISX0213(multibytecodec_support.TestBase, unittest.TestCase): encoding = 'euc_jisx0213' - tstring = test_multibytecodec_support.load_teststring('euc_jisx0213') + tstring = multibytecodec_support.load_teststring('euc_jisx0213') codectests = ( # invalid bytes ("abc\x80\x80\xc1\xc4", "strict", None), @@ -52,10 +52,10 @@ eucjp_commontests = ( ("\xc1\x64", "strict", None), ) -class Test_EUC_JP_COMPAT(test_multibytecodec_support.TestBase, +class Test_EUC_JP_COMPAT(multibytecodec_support.TestBase, unittest.TestCase): encoding = 'euc_jp' - tstring = test_multibytecodec_support.load_teststring('euc_jp') + tstring = multibytecodec_support.load_teststring('euc_jp') codectests = eucjp_commontests + ( ("\xa1\xc0\\", "strict", u"\uff3c\\"), (u"\xa5", "strict", "\x5c"), @@ -70,17 +70,17 @@ shiftjis_commonenctests = ( ("abc\x80\x80\x82\x84def", "ignore", u"abc\uff44def"), ) -class Test_SJIS_COMPAT(test_multibytecodec_support.TestBase, unittest.TestCase): +class Test_SJIS_COMPAT(multibytecodec_support.TestBase, unittest.TestCase): encoding = 'shift_jis' - tstring = test_multibytecodec_support.load_teststring('shift_jis') + tstring = multibytecodec_support.load_teststring('shift_jis') codectests = shiftjis_commonenctests + ( ("\\\x7e", "strict", u"\\\x7e"), ("\x81\x5f\x81\x61\x81\x7c", "strict", u"\uff3c\u2016\u2212"), ) -class Test_SJISX0213(test_multibytecodec_support.TestBase, unittest.TestCase): +class Test_SJISX0213(multibytecodec_support.TestBase, unittest.TestCase): encoding = 'shift_jisx0213' - tstring = test_multibytecodec_support.load_teststring('shift_jisx0213') + tstring = multibytecodec_support.load_teststring('shift_jisx0213') codectests = ( # invalid bytes ("abc\x80\x80\x82\x84", "strict", None), diff --git a/Lib/test/test_codecencodings_kr.py b/Lib/test/test_codecencodings_kr.py index 45ea62b..7b2f232 100644 --- a/Lib/test/test_codecencodings_kr.py +++ b/Lib/test/test_codecencodings_kr.py @@ -4,12 +4,12 @@ # from test import test_support -from test import test_multibytecodec_support +from test import multibytecodec_support import unittest -class Test_CP949(test_multibytecodec_support.TestBase, unittest.TestCase): +class Test_CP949(multibytecodec_support.TestBase, unittest.TestCase): encoding = 'cp949' - tstring = test_multibytecodec_support.load_teststring('cp949') + tstring = multibytecodec_support.load_teststring('cp949') codectests = ( # invalid bytes ("abc\x80\x80\xc1\xc4", "strict", None), @@ -19,9 +19,9 @@ class Test_CP949(test_multibytecodec_support.TestBase, unittest.TestCase): ("abc\x80\x80\xc1\xc4", "ignore", u"abc\uc894"), ) -class Test_EUCKR(test_multibytecodec_support.TestBase, unittest.TestCase): +class Test_EUCKR(multibytecodec_support.TestBase, unittest.TestCase): encoding = 'euc_kr' - tstring = test_multibytecodec_support.load_teststring('euc_kr') + tstring = multibytecodec_support.load_teststring('euc_kr') codectests = ( # invalid bytes ("abc\x80\x80\xc1\xc4", "strict", None), @@ -49,9 +49,9 @@ class Test_EUCKR(test_multibytecodec_support.TestBase, unittest.TestCase): ("\xc1\xc4", "strict", u"\uc894"), ) -class Test_JOHAB(test_multibytecodec_support.TestBase, unittest.TestCase): +class Test_JOHAB(multibytecodec_support.TestBase, unittest.TestCase): encoding = 'johab' - tstring = test_multibytecodec_support.load_teststring('johab') + tstring = multibytecodec_support.load_teststring('johab') codectests = ( # invalid bytes ("abc\x80\x80\xc1\xc4", "strict", None), diff --git a/Lib/test/test_codecencodings_tw.py b/Lib/test/test_codecencodings_tw.py index c62d321..748840b 100644 --- a/Lib/test/test_codecencodings_tw.py +++ b/Lib/test/test_codecencodings_tw.py @@ -4,12 +4,12 @@ # from test import test_support -from test import test_multibytecodec_support +from test import multibytecodec_support import unittest -class Test_Big5(test_multibytecodec_support.TestBase, unittest.TestCase): +class Test_Big5(multibytecodec_support.TestBase, unittest.TestCase): encoding = 'big5' - tstring = test_multibytecodec_support.load_teststring('big5') + tstring = multibytecodec_support.load_teststring('big5') codectests = ( # invalid bytes ("abc\x80\x80\xc1\xc4", "strict", None), diff --git a/Lib/test/test_codecmaps_cn.py b/Lib/test/test_codecmaps_cn.py index b1d1eb4..73b10bc 100644 --- a/Lib/test/test_codecmaps_cn.py +++ b/Lib/test/test_codecmaps_cn.py @@ -4,20 +4,20 @@ # from test import test_support -from test import test_multibytecodec_support +from test import multibytecodec_support import unittest -class TestGB2312Map(test_multibytecodec_support.TestBase_Mapping, +class TestGB2312Map(multibytecodec_support.TestBase_Mapping, unittest.TestCase): encoding = 'gb2312' mapfileurl = 'http://www.pythontest.net/unicode/EUC-CN.TXT' -class TestGBKMap(test_multibytecodec_support.TestBase_Mapping, +class TestGBKMap(multibytecodec_support.TestBase_Mapping, unittest.TestCase): encoding = 'gbk' mapfileurl = 'http://www.pythontest.net/unicode/CP936.TXT' -class TestGB18030Map(test_multibytecodec_support.TestBase_Mapping, +class TestGB18030Map(multibytecodec_support.TestBase_Mapping, unittest.TestCase): encoding = 'gb18030' mapfileurl = 'http://www.pythontest.net/unicode/gb-18030-2000.xml' diff --git a/Lib/test/test_codecmaps_hk.py b/Lib/test/test_codecmaps_hk.py index 0a41b24..feda7a7 100644 --- a/Lib/test/test_codecmaps_hk.py +++ b/Lib/test/test_codecmaps_hk.py @@ -4,10 +4,10 @@ # from test import test_support -from test import test_multibytecodec_support +from test import multibytecodec_support import unittest -class TestBig5HKSCSMap(test_multibytecodec_support.TestBase_Mapping, +class TestBig5HKSCSMap(multibytecodec_support.TestBase_Mapping, unittest.TestCase): encoding = 'big5hkscs' mapfileurl = 'http://www.pythontest.net/unicode/BIG5HKSCS-2004.TXT' diff --git a/Lib/test/test_codecmaps_jp.py b/Lib/test/test_codecmaps_jp.py index 907645d..f37a81c 100644 --- a/Lib/test/test_codecmaps_jp.py +++ b/Lib/test/test_codecmaps_jp.py @@ -4,10 +4,10 @@ # from test import test_support -from test import test_multibytecodec_support +from test import multibytecodec_support import unittest -class TestCP932Map(test_multibytecodec_support.TestBase_Mapping, +class TestCP932Map(multibytecodec_support.TestBase_Mapping, unittest.TestCase): encoding = 'cp932' mapfileurl = 'http://www.pythontest.net/unicode/CP932.TXT' @@ -22,14 +22,14 @@ class TestCP932Map(test_multibytecodec_support.TestBase_Mapping, supmaps.append((chr(i), unichr(i+0xfec0))) -class TestEUCJPCOMPATMap(test_multibytecodec_support.TestBase_Mapping, +class TestEUCJPCOMPATMap(multibytecodec_support.TestBase_Mapping, unittest.TestCase): encoding = 'euc_jp' mapfilename = 'EUC-JP.TXT' mapfileurl = 'http://www.pythontest.net/unicode/EUC-JP.TXT' -class TestSJISCOMPATMap(test_multibytecodec_support.TestBase_Mapping, +class TestSJISCOMPATMap(multibytecodec_support.TestBase_Mapping, unittest.TestCase): encoding = 'shift_jis' mapfilename = 'SHIFTJIS.TXT' @@ -43,14 +43,14 @@ class TestSJISCOMPATMap(test_multibytecodec_support.TestBase_Mapping, ('\x81_', u'\\'), ] -class TestEUCJISX0213Map(test_multibytecodec_support.TestBase_Mapping, +class TestEUCJISX0213Map(multibytecodec_support.TestBase_Mapping, unittest.TestCase): encoding = 'euc_jisx0213' mapfilename = 'EUC-JISX0213.TXT' mapfileurl = 'http://www.pythontest.net/unicode/EUC-JISX0213.TXT' -class TestSJISX0213Map(test_multibytecodec_support.TestBase_Mapping, +class TestSJISX0213Map(multibytecodec_support.TestBase_Mapping, unittest.TestCase): encoding = 'shift_jisx0213' mapfilename = 'SHIFT_JISX0213.TXT' diff --git a/Lib/test/test_codecmaps_kr.py b/Lib/test/test_codecmaps_kr.py index 0ba71bf..9e1df5e 100644 --- a/Lib/test/test_codecmaps_kr.py +++ b/Lib/test/test_codecmaps_kr.py @@ -4,16 +4,16 @@ # from test import test_support -from test import test_multibytecodec_support +from test import multibytecodec_support import unittest -class TestCP949Map(test_multibytecodec_support.TestBase_Mapping, +class TestCP949Map(multibytecodec_support.TestBase_Mapping, unittest.TestCase): encoding = 'cp949' mapfileurl = 'http://www.pythontest.net/unicode/CP949.TXT' -class TestEUCKRMap(test_multibytecodec_support.TestBase_Mapping, +class TestEUCKRMap(multibytecodec_support.TestBase_Mapping, unittest.TestCase): encoding = 'euc_kr' mapfileurl = 'http://www.pythontest.net/unicode/EUC-KR.TXT' @@ -23,7 +23,7 @@ class TestEUCKRMap(test_multibytecodec_support.TestBase_Mapping, pass_dectest = [('\xa4\xd4', u'\u3164')] -class TestJOHABMap(test_multibytecodec_support.TestBase_Mapping, +class TestJOHABMap(multibytecodec_support.TestBase_Mapping, unittest.TestCase): encoding = 'johab' mapfileurl = 'http://www.pythontest.net/unicode/JOHAB.TXT' diff --git a/Lib/test/test_codecmaps_tw.py b/Lib/test/test_codecmaps_tw.py index 0d57343..5b500ff 100644 --- a/Lib/test/test_codecmaps_tw.py +++ b/Lib/test/test_codecmaps_tw.py @@ -4,15 +4,15 @@ # from test import test_support -from test import test_multibytecodec_support +from test import multibytecodec_support import unittest -class TestBIG5Map(test_multibytecodec_support.TestBase_Mapping, +class TestBIG5Map(multibytecodec_support.TestBase_Mapping, unittest.TestCase): encoding = 'big5' mapfileurl = 'http://www.pythontest.net/unicode/BIG5.TXT' -class TestCP950Map(test_multibytecodec_support.TestBase_Mapping, +class TestCP950Map(multibytecodec_support.TestBase_Mapping, unittest.TestCase): encoding = 'cp950' mapfileurl = 'http://www.pythontest.net/unicode/CP950.TXT' diff --git a/Lib/test/test_multibytecodec_support.py b/Lib/test/test_multibytecodec_support.py deleted file mode 100644 index 1631e46..0000000 --- a/Lib/test/test_multibytecodec_support.py +++ /dev/null @@ -1,372 +0,0 @@ -# test_multibytecodec_support.py -# Common Unittest Routines for CJK codecs -# - -import codecs -import os -import re -import sys -import unittest -from httplib import HTTPException -from test import test_support -from StringIO import StringIO - -class TestBase: - encoding = '' # codec name - codec = None # codec tuple (with 4 elements) - tstring = '' # string to test StreamReader - - codectests = None # must set. codec test tuple - roundtriptest = 1 # set if roundtrip is possible with unicode - has_iso10646 = 0 # set if this encoding contains whole iso10646 map - xmlcharnametest = None # string to test xmlcharrefreplace - unmappedunicode = u'\udeee' # a unicode code point that is not mapped. - - def setUp(self): - if self.codec is None: - self.codec = codecs.lookup(self.encoding) - self.encode = self.codec.encode - self.decode = self.codec.decode - self.reader = self.codec.streamreader - self.writer = self.codec.streamwriter - self.incrementalencoder = self.codec.incrementalencoder - self.incrementaldecoder = self.codec.incrementaldecoder - - def test_chunkcoding(self): - for native, utf8 in zip(*[StringIO(f).readlines() - for f in self.tstring]): - u = self.decode(native)[0] - self.assertEqual(u, utf8.decode('utf-8')) - if self.roundtriptest: - self.assertEqual(native, self.encode(u)[0]) - - def test_errorhandle(self): - for source, scheme, expected in self.codectests: - if isinstance(source, bytes): - func = self.decode - else: - func = self.encode - if expected: - result = func(source, scheme)[0] - if func is self.decode: - self.assertTrue(type(result) is unicode, type(result)) - self.assertEqual(result, expected, - '%r.decode(%r, %r)=%r != %r' - % (source, self.encoding, scheme, result, - expected)) - else: - self.assertTrue(type(result) is bytes, type(result)) - self.assertEqual(result, expected, - '%r.encode(%r, %r)=%r != %r' - % (source, self.encoding, scheme, result, - expected)) - else: - self.assertRaises(UnicodeError, func, source, scheme) - - def test_xmlcharrefreplace(self): - if self.has_iso10646: - self.skipTest('encoding contains full ISO 10646 map') - - s = u"\u0b13\u0b23\u0b60 nd eggs" - self.assertEqual( - self.encode(s, "xmlcharrefreplace")[0], - "ଓଣୠ nd eggs" - ) - - def test_customreplace_encode(self): - if self.has_iso10646: - self.skipTest('encoding contains full ISO 10646 map') - - from htmlentitydefs import codepoint2name - - def xmlcharnamereplace(exc): - if not isinstance(exc, UnicodeEncodeError): - raise TypeError("don't know how to handle %r" % exc) - l = [] - for c in exc.object[exc.start:exc.end]: - if ord(c) in codepoint2name: - l.append(u"&%s;" % codepoint2name[ord(c)]) - else: - l.append(u"&#%d;" % ord(c)) - return (u"".join(l), exc.end) - - codecs.register_error("test.xmlcharnamereplace", xmlcharnamereplace) - - if self.xmlcharnametest: - sin, sout = self.xmlcharnametest - else: - sin = u"\xab\u211c\xbb = \u2329\u1234\u232a" - sout = "«ℜ» = ⟨ሴ⟩" - self.assertEqual(self.encode(sin, - "test.xmlcharnamereplace")[0], sout) - - def test_callback_wrong_objects(self): - def myreplace(exc): - return (ret, exc.end) - codecs.register_error("test.cjktest", myreplace) - - for ret in ([1, 2, 3], [], None, object(), 'string', ''): - self.assertRaises(TypeError, self.encode, self.unmappedunicode, - 'test.cjktest') - - def test_callback_long_index(self): - def myreplace(exc): - return (u'x', long(exc.end)) - codecs.register_error("test.cjktest", myreplace) - self.assertEqual(self.encode(u'abcd' + self.unmappedunicode + u'efgh', - 'test.cjktest'), ('abcdxefgh', 9)) - - def myreplace(exc): - return (u'x', sys.maxint + 1) - codecs.register_error("test.cjktest", myreplace) - self.assertRaises(IndexError, self.encode, self.unmappedunicode, - 'test.cjktest') - - def test_callback_None_index(self): - def myreplace(exc): - return (u'x', None) - codecs.register_error("test.cjktest", myreplace) - self.assertRaises(TypeError, self.encode, self.unmappedunicode, - 'test.cjktest') - - def test_callback_backward_index(self): - def myreplace(exc): - if myreplace.limit > 0: - myreplace.limit -= 1 - return (u'REPLACED', 0) - else: - return (u'TERMINAL', exc.end) - myreplace.limit = 3 - codecs.register_error("test.cjktest", myreplace) - self.assertEqual(self.encode(u'abcd' + self.unmappedunicode + u'efgh', - 'test.cjktest'), - ('abcdREPLACEDabcdREPLACEDabcdREPLACEDabcdTERMINALefgh', 9)) - - def test_callback_forward_index(self): - def myreplace(exc): - return (u'REPLACED', exc.end + 2) - codecs.register_error("test.cjktest", myreplace) - self.assertEqual(self.encode(u'abcd' + self.unmappedunicode + u'efgh', - 'test.cjktest'), ('abcdREPLACEDgh', 9)) - - def test_callback_index_outofbound(self): - def myreplace(exc): - return (u'TERM', 100) - codecs.register_error("test.cjktest", myreplace) - self.assertRaises(IndexError, self.encode, self.unmappedunicode, - 'test.cjktest') - - def test_incrementalencoder(self): - UTF8Reader = codecs.getreader('utf-8') - for sizehint in [None] + range(1, 33) + \ - [64, 128, 256, 512, 1024]: - istream = UTF8Reader(StringIO(self.tstring[1])) - ostream = StringIO() - encoder = self.incrementalencoder() - while 1: - if sizehint is not None: - data = istream.read(sizehint) - else: - data = istream.read() - - if not data: - break - e = encoder.encode(data) - ostream.write(e) - - self.assertEqual(ostream.getvalue(), self.tstring[0]) - - def test_incrementaldecoder(self): - UTF8Writer = codecs.getwriter('utf-8') - for sizehint in [None, -1] + range(1, 33) + \ - [64, 128, 256, 512, 1024]: - istream = StringIO(self.tstring[0]) - ostream = UTF8Writer(StringIO()) - decoder = self.incrementaldecoder() - while 1: - data = istream.read(sizehint) - if not data: - break - else: - u = decoder.decode(data) - ostream.write(u) - - self.assertEqual(ostream.getvalue(), self.tstring[1]) - - def test_incrementalencoder_error_callback(self): - inv = self.unmappedunicode - - e = self.incrementalencoder() - self.assertRaises(UnicodeEncodeError, e.encode, inv, True) - - e.errors = 'ignore' - self.assertEqual(e.encode(inv, True), '') - - e.reset() - def tempreplace(exc): - return (u'called', exc.end) - codecs.register_error('test.incremental_error_callback', tempreplace) - e.errors = 'test.incremental_error_callback' - self.assertEqual(e.encode(inv, True), 'called') - - # again - e.errors = 'ignore' - self.assertEqual(e.encode(inv, True), '') - - def test_streamreader(self): - UTF8Writer = codecs.getwriter('utf-8') - for name in ["read", "readline", "readlines"]: - for sizehint in [None, -1] + range(1, 33) + \ - [64, 128, 256, 512, 1024]: - istream = self.reader(StringIO(self.tstring[0])) - ostream = UTF8Writer(StringIO()) - func = getattr(istream, name) - while 1: - data = func(sizehint) - if not data: - break - if name == "readlines": - ostream.writelines(data) - else: - ostream.write(data) - - self.assertEqual(ostream.getvalue(), self.tstring[1]) - - def test_streamwriter(self): - readfuncs = ('read', 'readline', 'readlines') - UTF8Reader = codecs.getreader('utf-8') - for name in readfuncs: - for sizehint in [None] + range(1, 33) + \ - [64, 128, 256, 512, 1024]: - istream = UTF8Reader(StringIO(self.tstring[1])) - ostream = self.writer(StringIO()) - func = getattr(istream, name) - while 1: - if sizehint is not None: - data = func(sizehint) - else: - data = func() - - if not data: - break - if name == "readlines": - ostream.writelines(data) - else: - ostream.write(data) - - self.assertEqual(ostream.getvalue(), self.tstring[0]) - -class TestBase_Mapping(unittest.TestCase): - pass_enctest = [] - pass_dectest = [] - supmaps = [] - codectests = [] - - def __init__(self, *args, **kw): - unittest.TestCase.__init__(self, *args, **kw) - try: - self.open_mapping_file().close() # test it to report the error early - except (IOError, HTTPException): - self.skipTest("Could not retrieve "+self.mapfileurl) - - def open_mapping_file(self): - return test_support.open_urlresource(self.mapfileurl) - - def test_mapping_file(self): - if self.mapfileurl.endswith('.xml'): - self._test_mapping_file_ucm() - else: - self._test_mapping_file_plain() - - def _test_mapping_file_plain(self): - _unichr = lambda c: eval("u'\\U%08x'" % int(c, 16)) - unichrs = lambda s: u''.join(_unichr(c) for c in s.split('+')) - urt_wa = {} - - with self.open_mapping_file() as f: - for line in f: - if not line: - break - data = line.split('#')[0].strip().split() - if len(data) != 2: - continue - - csetval = eval(data[0]) - if csetval <= 0x7F: - csetch = chr(csetval & 0xff) - elif csetval >= 0x1000000: - csetch = chr(csetval >> 24) + chr((csetval >> 16) & 0xff) + \ - chr((csetval >> 8) & 0xff) + chr(csetval & 0xff) - elif csetval >= 0x10000: - csetch = chr(csetval >> 16) + \ - chr((csetval >> 8) & 0xff) + chr(csetval & 0xff) - elif csetval >= 0x100: - csetch = chr(csetval >> 8) + chr(csetval & 0xff) - else: - continue - - unich = unichrs(data[1]) - if unich == u'\ufffd' or unich in urt_wa: - continue - urt_wa[unich] = csetch - - self._testpoint(csetch, unich) - - def _test_mapping_file_ucm(self): - with self.open_mapping_file() as f: - ucmdata = f.read() - uc = re.findall('', ucmdata) - for uni, coded in uc: - unich = unichr(int(uni, 16)) - codech = ''.join(chr(int(c, 16)) for c in coded.split()) - self._testpoint(codech, unich) - - def test_mapping_supplemental(self): - for mapping in self.supmaps: - self._testpoint(*mapping) - - def _testpoint(self, csetch, unich): - if (csetch, unich) not in self.pass_enctest: - try: - self.assertEqual(unich.encode(self.encoding), csetch) - except UnicodeError, exc: - self.fail('Encoding failed while testing %s -> %s: %s' % ( - repr(unich), repr(csetch), exc.reason)) - if (csetch, unich) not in self.pass_dectest: - try: - self.assertEqual(csetch.decode(self.encoding), unich) - except UnicodeError, exc: - self.fail('Decoding failed while testing %s -> %s: %s' % ( - repr(csetch), repr(unich), exc.reason)) - - def test_errorhandle(self): - for source, scheme, expected in self.codectests: - if isinstance(source, bytes): - func = source.decode - else: - func = source.encode - if expected: - if isinstance(source, bytes): - result = func(self.encoding, scheme) - self.assertTrue(type(result) is unicode, type(result)) - self.assertEqual(result, expected, - '%r.decode(%r, %r)=%r != %r' - % (source, self.encoding, scheme, result, - expected)) - else: - result = func(self.encoding, scheme) - self.assertTrue(type(result) is bytes, type(result)) - self.assertEqual(result, expected, - '%r.encode(%r, %r)=%r != %r' - % (source, self.encoding, scheme, result, - expected)) - else: - self.assertRaises(UnicodeError, func, self.encoding, scheme) - -def load_teststring(name): - dir = os.path.join(os.path.dirname(__file__), 'cjkencodings') - with open(os.path.join(dir, name + '.txt'), 'rb') as f: - encoded = f.read() - with open(os.path.join(dir, name + '-utf8.txt'), 'rb') as f: - utf8 = f.read() - return encoded, utf8 -- cgit v0.12