Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4dfcb1a

Browse files
committed
merge heads
2 parents 48deae1 + ab5fcc0 commit 4dfcb1a

3 files changed

Lines changed: 85 additions & 54 deletions

File tree

Lib/test/test_multibytecodec_support.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,16 @@ def test_errorhandle(self):
5858
result = func(source, scheme)[0]
5959
if func is self.decode:
6060
self.assertTrue(type(result) is str, type(result))
61+
self.assertEqual(result, expected,
62+
'%a.decode(%r, %r)=%a != %a'
63+
% (source, self.encoding, scheme, result,
64+
expected))
6165
else:
6266
self.assertTrue(type(result) is bytes, type(result))
63-
self.assertEqual(result, expected,
64-
'%a.decode(%r)=%a != %a'
65-
% (source, self.encoding, result, expected))
67+
self.assertEqual(result, expected,
68+
'%a.encode(%r, %r)=%a != %a'
69+
% (source, self.encoding, scheme, result,
70+
expected))
6671
else:
6772
self.assertRaises(UnicodeError, func, source, scheme)
6873

@@ -279,6 +284,7 @@ class TestBase_Mapping(unittest.TestCase):
279284
pass_enctest = []
280285
pass_dectest = []
281286
supmaps = []
287+
codectests = []
282288

283289
def __init__(self, *args, **kw):
284290
unittest.TestCase.__init__(self, *args, **kw)
@@ -348,6 +354,30 @@ def _testpoint(self, csetch, unich):
348354
if (csetch, unich) not in self.pass_dectest:
349355
self.assertEqual(str(csetch, self.encoding), unich)
350356

357+
def test_errorhandle(self):
358+
for source, scheme, expected in self.codectests:
359+
if isinstance(source, bytes):
360+
func = source.decode
361+
else:
362+
func = source.encode
363+
if expected:
364+
if isinstance(source, bytes):
365+
result = func(self.encoding, scheme)
366+
self.assertTrue(type(result) is str, type(result))
367+
self.assertEqual(result, expected,
368+
'%a.decode(%r, %r)=%a != %a'
369+
% (source, self.encoding, scheme, result,
370+
expected))
371+
else:
372+
result = func(self.encoding, scheme)
373+
self.assertTrue(type(result) is bytes, type(result))
374+
self.assertEqual(result, expected,
375+
'%a.encode(%r, %r)=%a != %a'
376+
% (source, self.encoding, scheme, result,
377+
expected))
378+
else:
379+
self.assertRaises(UnicodeError, func, self.encoding, scheme)
380+
351381
def load_teststring(name):
352382
dir = os.path.join(os.path.dirname(__file__), 'cjkencodings')
353383
with open(os.path.join(dir, name + '.txt'), 'rb') as f:

Modules/cjkcodecs/_codecs_hk.c

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -115,55 +115,56 @@ DECODER(big5hkscs)
115115

116116
REQUIRE_INBUF(2)
117117

118-
if (0xc6 <= c && c <= 0xc8 && (c >= 0xc7 || IN2 >= 0xa1))
119-
goto hkscsdec;
118+
if (0xc6 > c || c > 0xc8 || (c < 0xc7 && IN2 < 0xa1)) {
119+
TRYMAP_DEC(big5, **outbuf, c, IN2) {
120+
NEXT(2, 1)
121+
continue;
122+
}
123+
}
124+
125+
TRYMAP_DEC(big5hkscs, decoded, c, IN2)
126+
{
127+
int s = BH2S(c, IN2);
128+
const unsigned char *hintbase;
129+
130+
assert(0x87 <= c && c <= 0xfe);
131+
assert(0x40 <= IN2 && IN2 <= 0xfe);
132+
133+
if (BH2S(0x87, 0x40) <= s && s <= BH2S(0xa0, 0xfe)) {
134+
hintbase = big5hkscs_phint_0;
135+
s -= BH2S(0x87, 0x40);
136+
}
137+
else if (BH2S(0xc6,0xa1) <= s && s <= BH2S(0xc8,0xfe)){
138+
hintbase = big5hkscs_phint_12130;
139+
s -= BH2S(0xc6, 0xa1);
140+
}
141+
else if (BH2S(0xf9,0xd6) <= s && s <= BH2S(0xfe,0xfe)){
142+
hintbase = big5hkscs_phint_21924;
143+
s -= BH2S(0xf9, 0xd6);
144+
}
145+
else
146+
return MBERR_INTERNAL;
120147

121-
TRYMAP_DEC(big5, **outbuf, c, IN2) {
122-
NEXT(2, 1)
148+
if (hintbase[s >> 3] & (1 << (s & 7))) {
149+
WRITEUCS4(decoded | 0x20000)
150+
NEXT_IN(2)
151+
}
152+
else {
153+
OUT1(decoded)
154+
NEXT(2, 1)
155+
}
156+
continue;
123157
}
124-
else
125-
hkscsdec: TRYMAP_DEC(big5hkscs, decoded, c, IN2) {
126-
int s = BH2S(c, IN2);
127-
const unsigned char *hintbase;
128-
129-
assert(0x87 <= c && c <= 0xfe);
130-
assert(0x40 <= IN2 && IN2 <= 0xfe);
131-
132-
if (BH2S(0x87, 0x40) <= s && s <= BH2S(0xa0, 0xfe)) {
133-
hintbase = big5hkscs_phint_0;
134-
s -= BH2S(0x87, 0x40);
135-
}
136-
else if (BH2S(0xc6,0xa1) <= s && s <= BH2S(0xc8,0xfe)){
137-
hintbase = big5hkscs_phint_12130;
138-
s -= BH2S(0xc6, 0xa1);
139-
}
140-
else if (BH2S(0xf9,0xd6) <= s && s <= BH2S(0xfe,0xfe)){
141-
hintbase = big5hkscs_phint_21924;
142-
s -= BH2S(0xf9, 0xd6);
143-
}
144-
else
145-
return MBERR_INTERNAL;
146-
147-
if (hintbase[s >> 3] & (1 << (s & 7))) {
148-
WRITEUCS4(decoded | 0x20000)
149-
NEXT_IN(2)
150-
}
151-
else {
152-
OUT1(decoded)
153-
NEXT(2, 1)
154-
}
155-
}
156-
else {
157-
switch ((c << 8) | IN2) {
158-
case 0x8862: WRITE2(0x00ca, 0x0304); break;
159-
case 0x8864: WRITE2(0x00ca, 0x030c); break;
160-
case 0x88a3: WRITE2(0x00ea, 0x0304); break;
161-
case 0x88a5: WRITE2(0x00ea, 0x030c); break;
162-
default: return 2;
163-
}
164-
165-
NEXT(2, 2) /* all decoded codepoints are pairs, above. */
158+
159+
switch ((c << 8) | IN2) {
160+
case 0x8862: WRITE2(0x00ca, 0x0304); break;
161+
case 0x8864: WRITE2(0x00ca, 0x030c); break;
162+
case 0x88a3: WRITE2(0x00ea, 0x0304); break;
163+
case 0x88a5: WRITE2(0x00ea, 0x030c); break;
164+
default: return 2;
166165
}
166+
167+
NEXT(2, 2) /* all decoded codepoints are pairs, above. */
167168
}
168169

169170
return 0;

Modules/cjkcodecs/_codecs_jp.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -371,11 +371,11 @@ DECODER(euc_jp)
371371

372372
REQUIRE_OUTBUF(1)
373373

374-
if (c < 0x80) {
375-
OUT1(c)
376-
NEXT(1, 1)
377-
continue;
378-
}
374+
if (c < 0x80) {
375+
OUT1(c)
376+
NEXT(1, 1)
377+
continue;
378+
}
379379

380380
if (c == 0x8e) {
381381
/* JIS X 0201 half-width katakana */

0 commit comments

Comments
 (0)