diff -r 95da47ddebe0 Lib/test/test_array.py --- a/Lib/test/test_array.py Mon Aug 06 00:46:05 2012 +0200 +++ b/Lib/test/test_array.py Mon Aug 06 01:00:55 2012 +0200 @@ -1051,6 +1051,20 @@ class UnicodeTest(StringTest): self.assertRaises(TypeError, a.fromunicode) + def test_buffer_format(self): + import ctypes + a = array.array('u', 'xyz') + view = memoryview(a) + self.assertEqual(a, view) + self.assertEqual(view, a) + sizeof_wchar = ctypes.sizeof(ctypes.c_wchar) + self.assertEqual(struct.calcsize(view.format), sizeof_wchar) + if sizeof_wchar == 2: + self.assertEqual(view.format, 'H') + else: + self.assertEqual(view.format, 'I') + + tests.append(UnicodeTest) class NumberTest(BaseTest): diff -r 95da47ddebe0 Modules/arraymodule.c --- a/Modules/arraymodule.c Mon Aug 06 00:46:05 2012 +0200 +++ b/Modules/arraymodule.c Mon Aug 06 01:00:55 2012 +0200 @@ -2417,12 +2417,17 @@ array_buffer_getbuf(arrayobject *self, P view->format = NULL; view->internal = NULL; if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) { - view->format = self->ob_descr->formats; + if (self->ob_descr->typecode == 'u') { #ifdef Py_UNICODE_WIDE - if (self->ob_descr->typecode == 'u') { - view->format = "w"; + assert(sizeof(Py_UNICODE) == sizeof(unsigned int)); + view->format = "I"; +#else + assert(sizeof(Py_UNICODE) == sizeof(unsigned short)); + view->format = "H"; +#endif } -#endif + else + view->format = self->ob_descr->formats; } finish: