@@ -27,7 +27,7 @@ class ArraySubclassWithKwargs(array.array):
27
27
def __init__ (self , typecode , newarg = None ):
28
28
array .array .__init__ (self )
29
29
30
- typecodes = 'ubBhHiIlLfdqQ '
30
+ typecodes = 'uwbBhHiIlLfdqQ '
31
31
32
32
class MiscTest (unittest .TestCase ):
33
33
@@ -186,11 +186,12 @@ def test_unicode(self):
186
186
)
187
187
for testcase in testcases :
188
188
mformat_code , encoding = testcase
189
- a = array .array ('u' , teststr )
190
- b = array_reconstructor (
191
- array .array , 'u' , mformat_code , teststr .encode (encoding ))
192
- self .assertEqual (a , b ,
193
- msg = "{0!r} != {1!r}; testcase={2!r}" .format (a , b , testcase ))
189
+ for c in 'uw' :
190
+ a = array .array (c , teststr )
191
+ b = array_reconstructor (
192
+ array .array , c , mformat_code , teststr .encode (encoding ))
193
+ self .assertEqual (a , b ,
194
+ msg = "{0!r} != {1!r}; testcase={2!r}" .format (a , b , testcase ))
194
195
195
196
196
197
class BaseTest :
@@ -234,7 +235,7 @@ def test_buffer_info(self):
234
235
self .assertEqual (bi [1 ], len (a ))
235
236
236
237
def test_byteswap (self ):
237
- if self .typecode == 'u' :
238
+ if self .typecode in ( 'u' , 'w' ) :
238
239
example = '\U00100100 '
239
240
else :
240
241
example = self .example
@@ -1079,7 +1080,7 @@ def test_buffer(self):
1079
1080
self .assertEqual (m .tobytes (), expected )
1080
1081
self .assertRaises (BufferError , a .frombytes , a .tobytes ())
1081
1082
self .assertEqual (m .tobytes (), expected )
1082
- if self .typecode == 'u' :
1083
+ if self .typecode in ( 'u' , 'w' ) :
1083
1084
self .assertRaises (BufferError , a .fromunicode , a .tounicode ())
1084
1085
self .assertEqual (m .tobytes (), expected )
1085
1086
self .assertRaises (BufferError , operator .imul , a , 2 )
@@ -1135,16 +1136,17 @@ def test_sizeof_without_buffer(self):
1135
1136
support .check_sizeof (self , a , basesize )
1136
1137
1137
1138
def test_initialize_with_unicode (self ):
1138
- if self .typecode != 'u' :
1139
+ if self .typecode not in ( 'u' , 'w' ) :
1139
1140
with self .assertRaises (TypeError ) as cm :
1140
1141
a = array .array (self .typecode , 'foo' )
1141
1142
self .assertIn ("cannot use a str" , str (cm .exception ))
1142
1143
with self .assertRaises (TypeError ) as cm :
1143
- a = array .array (self .typecode , array .array ('u ' , 'foo' ))
1144
+ a = array .array (self .typecode , array .array ('w ' , 'foo' ))
1144
1145
self .assertIn ("cannot use a unicode array" , str (cm .exception ))
1145
1146
else :
1146
1147
a = array .array (self .typecode , "foo" )
1147
1148
a = array .array (self .typecode , array .array ('u' , 'foo' ))
1149
+ a = array .array (self .typecode , array .array ('w' , 'foo' ))
1148
1150
1149
1151
@support .cpython_only
1150
1152
def test_obsolete_write_lock (self ):
@@ -1171,40 +1173,45 @@ class UnicodeTest(StringTest, unittest.TestCase):
1171
1173
smallerexample = '\x01 \u263a \x00 \ufefe '
1172
1174
biggerexample = '\x01 \u263a \x01 \ufeff '
1173
1175
outside = str ('\x33 ' )
1174
- minitemsize = 2
1176
+ minitemsize = sizeof_wchar
1175
1177
1176
1178
def test_unicode (self ):
1177
1179
self .assertRaises (TypeError , array .array , 'b' , 'foo' )
1178
1180
1179
- a = array .array ('u' , '\xa0 \xc2 \u1234 ' )
1181
+ a = array .array (self . typecode , '\xa0 \xc2 \u1234 ' )
1180
1182
a .fromunicode (' ' )
1181
1183
a .fromunicode ('' )
1182
1184
a .fromunicode ('' )
1183
1185
a .fromunicode ('\x11 abc\xff \u1234 ' )
1184
1186
s = a .tounicode ()
1185
1187
self .assertEqual (s , '\xa0 \xc2 \u1234 \x11 abc\xff \u1234 ' )
1186
- self .assertEqual (a .itemsize , sizeof_wchar )
1188
+ self .assertEqual (a .itemsize , self . minitemsize )
1187
1189
1188
1190
s = '\x00 ="\' a\\ b\x80 \xff \u0000 \u0001 \u1234 '
1189
- a = array .array ('u' , s )
1191
+ a = array .array (self . typecode , s )
1190
1192
self .assertEqual (
1191
1193
repr (a ),
1192
- "array('u ', '\\ x00=\" \\ 'a\\ \\ b\\ x80\xff \\ x00\\ x01\u1234 ')" )
1194
+ f "array('{ self . typecode } ', '\\ x00=\" \\ 'a\\ \\ b\\ x80\xff \\ x00\\ x01\u1234 ')" )
1193
1195
1194
1196
self .assertRaises (TypeError , a .fromunicode )
1195
1197
1196
1198
def test_issue17223 (self ):
1197
- # this used to crash
1198
- if sizeof_wchar == 4 :
1199
- # U+FFFFFFFF is an invalid code point in Unicode 6.0
1200
- invalid_str = b'\xff \xff \xff \xff '
1201
- else :
1199
+ if self .typecode == 'u' and sizeof_wchar == 2 :
1202
1200
# PyUnicode_FromUnicode() cannot fail with 16-bit wchar_t
1203
1201
self .skipTest ("specific to 32-bit wchar_t" )
1204
- a = array .array ('u' , invalid_str )
1202
+
1203
+ # this used to crash
1204
+ # U+FFFFFFFF is an invalid code point in Unicode 6.0
1205
+ invalid_str = b'\xff \xff \xff \xff '
1206
+
1207
+ a = array .array (self .typecode , invalid_str )
1205
1208
self .assertRaises (ValueError , a .tounicode )
1206
1209
self .assertRaises (ValueError , str , a )
1207
1210
1211
+ class UCS4Test (UnicodeTest ):
1212
+ typecode = 'w'
1213
+ minitemsize = 4
1214
+
1208
1215
class NumberTest (BaseTest ):
1209
1216
1210
1217
def test_extslice (self ):
0 commit comments