Fix the array tests. Only a minor change to the C code was required.

This commit is contained in:
Guido van Rossum 2007-05-10 18:41:30 +00:00
parent bce56a6c5b
commit 1f05a3b7fb
2 changed files with 15 additions and 17 deletions

View file

@ -755,32 +755,28 @@ tests.append(CharacterTest)
if test_support.have_unicode: if test_support.have_unicode:
class UnicodeTest(StringTest): class UnicodeTest(StringTest):
typecode = 'u' typecode = 'u'
example = str(r'\x01\u263a\x00\ufeff', 'unicode-escape') example = '\x01\u263a\x00\ufeff'
smallerexample = str(r'\x01\u263a\x00\ufefe', 'unicode-escape') smallerexample = '\x01\u263a\x00\ufefe'
biggerexample = str(r'\x01\u263a\x01\ufeff', 'unicode-escape') biggerexample = '\x01\u263a\x01\ufeff'
outside = str('\x33') outside = str('\x33')
minitemsize = 2 minitemsize = 2
def test_unicode(self): def test_unicode(self):
self.assertRaises(TypeError, array.array, 'b', str('foo', 'ascii')) self.assertRaises(TypeError, array.array, 'b', 'foo')
a = array.array('u', str(r'\xa0\xc2\u1234', 'unicode-escape')) a = array.array('u', '\xa0\xc2\u1234')
a.fromunicode(str(' ', 'ascii')) a.fromunicode(' ')
a.fromunicode(str('', 'ascii')) a.fromunicode('')
a.fromunicode(str('', 'ascii')) a.fromunicode('')
a.fromunicode(str(r'\x11abc\xff\u1234', 'unicode-escape')) a.fromunicode('\x11abc\xff\u1234')
s = a.tounicode() s = a.tounicode()
self.assertEqual( self.assertEqual(s, '\xa0\xc2\u1234 \x11abc\xff\u1234')
s,
str(r'\xa0\xc2\u1234 \x11abc\xff\u1234', 'unicode-escape')
)
s = str(r'\x00="\'a\\b\x80\xff\u0000\u0001\u1234', 'unicode-escape') s = '\x00="\'a\\b\x80\xff\u0000\u0001\u1234'
a = array.array('u', s) a = array.array('u', s)
self.assertEqual( self.assertEqual(
repr(a), repr(a),
r"""array('u', u'\x00="\'a\\b\x80\xff\x00\x01\u1234')""" "array('u', '\\x00=\"\\'a\\\\b\\x80\\xff\\x00\\x01\\u1234')")
)
self.assertRaises(TypeError, a.fromunicode) self.assertRaises(TypeError, a.fromunicode)

View file

@ -104,7 +104,9 @@ in bounds; that's the responsibility of the caller.
static PyObject * static PyObject *
c_getitem(arrayobject *ap, Py_ssize_t i) c_getitem(arrayobject *ap, Py_ssize_t i)
{ {
return PyString_FromStringAndSize(&((char *)ap->ob_item)[i], 1); Py_UNICODE buf[1];
buf[0] = ((unsigned char *)ap->ob_item)[i];
return PyUnicode_FromUnicode(buf, 1);
} }
static int static int