Skip to content

Commit a25f05e

Browse files
author
travis.oliphant
committed
Fix Issue 1268 with the array module by backing-out the 'w' addition. All builds will continue to use 'u' for unicode.
git-svn-id: http://svn.python.org/projects/python/branches/py3k@58441 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent d83d7ca commit a25f05e

1 file changed

Lines changed: 36 additions & 18 deletions

File tree

Modules/arraymodule.c

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,6 @@ typedef struct arrayobject {
4040

4141
static PyTypeObject Arraytype;
4242

43-
#ifdef Py_UNICODE_WIDE
44-
#define PyArr_UNI 'w'
45-
#define PyArr_UNISTR "w"
46-
#else
47-
#define PyArr_UNI 'u'
48-
#define PyArr_UNISTR "u"
49-
#endif
50-
5143
#define array_Check(op) PyObject_TypeCheck(op, &Arraytype)
5244
#define array_CheckExact(op) (Py_Type(op) == &Arraytype)
5345

@@ -193,12 +185,14 @@ u_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
193185
return 0;
194186
}
195187

188+
196189
static PyObject *
197190
h_getitem(arrayobject *ap, Py_ssize_t i)
198191
{
199192
return PyInt_FromLong((long) ((short *)ap->ob_item)[i]);
200193
}
201194

195+
202196
static int
203197
h_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
204198
{
@@ -389,9 +383,9 @@ d_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
389383

390384
/* Description of types */
391385
static struct arraydescr descriptors[] = {
392-
{'b', sizeof(char), b_getitem, b_setitem, "b"},
393-
{'B', sizeof(char), BB_getitem, BB_setitem, "B"},
394-
{PyArr_UNI, sizeof(Py_UNICODE), u_getitem, u_setitem, PyArr_UNISTR},
386+
{'b', 1, b_getitem, b_setitem, "b"},
387+
{'B', 1, BB_getitem, BB_setitem, "B"},
388+
{'u', sizeof(Py_UNICODE), u_getitem, u_setitem, "U"},
395389
{'h', sizeof(short), h_getitem, h_setitem, "h"},
396390
{'H', sizeof(short), HH_getitem, HH_setitem, "H"},
397391
{'i', sizeof(int), i_getitem, i_setitem, "i"},
@@ -1418,11 +1412,13 @@ array_fromunicode(arrayobject *self, PyObject *args)
14181412
{
14191413
Py_UNICODE *ustr;
14201414
Py_ssize_t n;
1415+
char typecode;
14211416

14221417
if (!PyArg_ParseTuple(args, "u#:fromunicode", &ustr, &n))
14231418
return NULL;
1424-
if (self->ob_descr->typecode != PyArr_UNI) {
1425-
PyErr_SetString(PyExc_ValueError,
1419+
typecode = self->ob_descr->typecode;
1420+
if ((typecode != 'u')) {
1421+
PyErr_SetString(PyExc_ValueError,
14261422
"fromunicode() may only be called on "
14271423
"unicode type arrays");
14281424
return NULL;
@@ -1457,9 +1453,11 @@ append Unicode data to an array of some other type.");
14571453
static PyObject *
14581454
array_tounicode(arrayobject *self, PyObject *unused)
14591455
{
1460-
if (self->ob_descr->typecode != PyArr_UNI) {
1456+
char typecode;
1457+
typecode = self->ob_descr->typecode;
1458+
if ((typecode != 'u')) {
14611459
PyErr_SetString(PyExc_ValueError,
1462-
"tounicode() may only be called on unicode type arrays");
1460+
"tounicode() may only be called on unicode type arrays");
14631461
return NULL;
14641462
}
14651463
return PyUnicode_FromUnicode((Py_UNICODE *) self->ob_item, Py_Size(self));
@@ -1560,7 +1558,7 @@ array_repr(arrayobject *a)
15601558
if (len == 0) {
15611559
return PyUnicode_FromFormat("array('%c')", typecode);
15621560
}
1563-
if (typecode == PyArr_UNI)
1561+
if ((typecode == 'u'))
15641562
v = array_tounicode(a, NULL);
15651563
else
15661564
v = array_tolist(a, NULL);
@@ -1864,7 +1862,7 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
18641862
if (!(initial == NULL || PyList_Check(initial)
18651863
|| PyBytes_Check(initial)
18661864
|| PyTuple_Check(initial)
1867-
|| (c == PyArr_UNI && PyUnicode_Check(initial)))) {
1865+
|| ((c=='u') && PyUnicode_Check(initial)))) {
18681866
it = PyObject_GetIter(initial);
18691867
if (it == NULL)
18701868
return NULL;
@@ -1966,7 +1964,7 @@ is a single character. The following type codes are defined:\n\
19661964
Type code C Type Minimum size in bytes \n\
19671965
'b' signed integer 1 \n\
19681966
'B' unsigned integer 1 \n\
1969-
'u' Unicode character 2 \n\
1967+
'u' Unicode character 2 (see note) \n\
19701968
'h' signed integer 2 \n\
19711969
'H' unsigned integer 2 \n\
19721970
'i' signed integer 2 \n\
@@ -1977,6 +1975,9 @@ is a single character. The following type codes are defined:\n\
19771975
'f' floating point 4 \n\
19781976
'd' floating point 8 \n\
19791977
\n\
1978+
NOTE: The 'u' typecode corresponds to Python's unicode character. On \n\
1979+
narrow builds this is 2-bytes on wide builds this is 4-bytes.\n\
1980+
\n\
19801981
The constructor is:\n\
19811982
\n\
19821983
array(typecode [, initializer]) -- create a new array\n\
@@ -2168,6 +2169,10 @@ PyMODINIT_FUNC
21682169
initarray(void)
21692170
{
21702171
PyObject *m;
2172+
PyObject *typecodes;
2173+
Py_ssize_t size = 0;
2174+
register Py_UNICODE *p;
2175+
struct arraydescr *descr;
21712176

21722177
if (PyType_Ready(&Arraytype) < 0)
21732178
return;
@@ -2180,5 +2185,18 @@ initarray(void)
21802185
PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype);
21812186
Py_INCREF((PyObject *)&Arraytype);
21822187
PyModule_AddObject(m, "array", (PyObject *)&Arraytype);
2188+
2189+
for (descr=descriptors; descr->typecode != '\0'; descr++) {
2190+
size++;
2191+
}
2192+
2193+
typecodes = PyUnicode_FromStringAndSize(NULL, size);
2194+
p = PyUnicode_AS_UNICODE(typecodes);
2195+
for (descr = descriptors; descr->typecode != '\0'; descr++) {
2196+
*p++ = (char)descr->typecode;
2197+
}
2198+
2199+
PyModule_AddObject(m, "typecodes", (PyObject *)typecodes);
2200+
21832201
/* No need to check the error here, the caller will do that */
21842202
}

0 commit comments

Comments
 (0)