|
3 | 3 | Roger E. Masse |
4 | 4 | """ |
5 | 5 | import array |
6 | | -from test_support import verbose, TESTFN, unlink, TestFailed |
| 6 | +from test_support import verbose, TESTFN, unlink, TestFailed, have_unicode |
7 | 7 |
|
8 | 8 | def main(): |
9 | 9 | testtype('c', 'c') |
10 | | - testtype('u', u'\u263a') |
| 10 | + if have_unicode: |
| 11 | + testtype('u', unicode(r'\u263a', 'unicode-escape')) |
11 | 12 | for type in (['b', 'h', 'i', 'l', 'f', 'd']): |
12 | 13 | testtype(type, 1) |
13 | | - testunicode() |
| 14 | + if have_unicode: |
| 15 | + testunicode() |
14 | 16 | testsubclassing() |
15 | 17 | unlink(TESTFN) |
16 | 18 |
|
17 | 19 | def testunicode(): |
18 | 20 | try: |
19 | | - array.array('b', u'foo') |
| 21 | + array.array('b', unicode('foo', 'ascii')) |
20 | 22 | except TypeError: |
21 | 23 | pass |
22 | 24 | else: |
23 | 25 | raise TestFailed("creating a non-unicode array from " |
24 | 26 | "a Unicode string should fail") |
25 | 27 |
|
26 | | - x = array.array('u', u'\xa0\xc2\u1234') |
27 | | - x.fromunicode(u' ') |
28 | | - x.fromunicode(u'') |
29 | | - x.fromunicode(u'') |
30 | | - x.fromunicode(u'\x11abc\xff\u1234') |
| 28 | + x = array.array('u', unicode(r'\xa0\xc2\u1234', 'unicode-escape')) |
| 29 | + x.fromunicode(unicode(' ', 'ascii')) |
| 30 | + x.fromunicode(unicode('', 'ascii')) |
| 31 | + x.fromunicode(unicode('', 'ascii')) |
| 32 | + x.fromunicode(unicode(r'\x11abc\xff\u1234', 'unicode-escape')) |
31 | 33 | s = x.tounicode() |
32 | | - if s != u'\xa0\xc2\u1234 \x11abc\xff\u1234': |
| 34 | + if s != unicode(r'\xa0\xc2\u1234 \x11abc\xff\u1234', 'unicode-escape'): |
33 | 35 | raise TestFailed("fromunicode()/tounicode()") |
34 | 36 |
|
35 | | - s = u'\x00="\'a\\b\x80\xff\u0000\u0001\u1234' |
| 37 | + s = unicode(r'\x00="\'a\\b\x80\xff\u0000\u0001\u1234', 'unicode-escape') |
36 | 38 | a = array.array('u', s) |
37 | 39 | if verbose: |
38 | 40 | print "repr of type 'u' array:", repr(a) |
@@ -235,42 +237,42 @@ def testtype(type, example): |
235 | 237 | if a != array.array(type, "dca"): |
236 | 238 | raise TestFailed, "array(%s) reverse-test" % `type` |
237 | 239 | elif type == 'u': |
238 | | - a = array.array(type, u"abcde") |
| 240 | + a = array.array(type, unicode("abcde", 'ascii')) |
239 | 241 | a[:-1] = a |
240 | | - if a != array.array(type, u"abcdee"): |
| 242 | + if a != array.array(type, unicode("abcdee", 'ascii')): |
241 | 243 | raise TestFailed, "array(%s) self-slice-assign (head)" % `type` |
242 | | - a = array.array(type, u"abcde") |
| 244 | + a = array.array(type, unicode("abcde", 'ascii')) |
243 | 245 | a[1:] = a |
244 | | - if a != array.array(type, u"aabcde"): |
| 246 | + if a != array.array(type, unicode("aabcde", 'ascii')): |
245 | 247 | raise TestFailed, "array(%s) self-slice-assign (tail)" % `type` |
246 | | - a = array.array(type, u"abcde") |
| 248 | + a = array.array(type, unicode("abcde", 'ascii')) |
247 | 249 | a[1:-1] = a |
248 | | - if a != array.array(type, u"aabcdee"): |
| 250 | + if a != array.array(type, unicode("aabcdee", 'ascii')): |
249 | 251 | raise TestFailed, "array(%s) self-slice-assign (cntr)" % `type` |
250 | | - if a.index(u"e") != 5: |
| 252 | + if a.index(unicode("e", 'ascii')) != 5: |
251 | 253 | raise TestFailed, "array(%s) index-test" % `type` |
252 | | - if a.count(u"a") != 2: |
| 254 | + if a.count(unicode("a", 'ascii')) != 2: |
253 | 255 | raise TestFailed, "array(%s) count-test" % `type` |
254 | | - a.remove(u"e") |
255 | | - if a != array.array(type, u"aabcde"): |
| 256 | + a.remove(unicode("e", 'ascii')) |
| 257 | + if a != array.array(type, unicode("aabcde", 'ascii')): |
256 | 258 | raise TestFailed, "array(%s) remove-test" % `type` |
257 | | - if a.pop(0) != u"a": |
| 259 | + if a.pop(0) != unicode("a", 'ascii'): |
258 | 260 | raise TestFailed, "array(%s) pop-test" % `type` |
259 | | - if a.pop(1) != u"b": |
| 261 | + if a.pop(1) != unicode("b", 'ascii'): |
260 | 262 | raise TestFailed, "array(%s) pop-test" % `type` |
261 | | - a.extend(array.array(type, u"xyz")) |
262 | | - if a != array.array(type, u"acdexyz"): |
| 263 | + a.extend(array.array(type, unicode("xyz", 'ascii'))) |
| 264 | + if a != array.array(type, unicode("acdexyz", 'ascii')): |
263 | 265 | raise TestFailed, "array(%s) extend-test" % `type` |
264 | 266 | a.pop() |
265 | 267 | a.pop() |
266 | 268 | a.pop() |
267 | 269 | x = a.pop() |
268 | | - if x != u'e': |
| 270 | + if x != unicode('e', 'ascii'): |
269 | 271 | raise TestFailed, "array(%s) pop-test" % `type` |
270 | | - if a != array.array(type, u"acd"): |
| 272 | + if a != array.array(type, unicode("acd", 'ascii')): |
271 | 273 | raise TestFailed, "array(%s) pop-test" % `type` |
272 | 274 | a.reverse() |
273 | | - if a != array.array(type, u"dca"): |
| 275 | + if a != array.array(type, unicode("dca", 'ascii')): |
274 | 276 | raise TestFailed, "array(%s) reverse-test" % `type` |
275 | 277 | else: |
276 | 278 | a = array.array(type, [1, 2, 3, 4, 5]) |
|
0 commit comments