array module stores the typecode in a char, instead of Py_UNICODE

This commit is contained in:
Victor Stinner 2011-09-30 00:03:59 +02:00
parent c806fdcd8b
commit f8bb7d02f6
1 changed files with 16 additions and 19 deletions

View File

@ -22,7 +22,7 @@ struct arrayobject; /* Forward */
* functions aren't visible yet. * functions aren't visible yet.
*/ */
struct arraydescr { struct arraydescr {
Py_UNICODE typecode; char typecode;
int itemsize; int itemsize;
PyObject * (*getitem)(struct arrayobject *, Py_ssize_t); PyObject * (*getitem)(struct arrayobject *, Py_ssize_t);
int (*setitem)(struct arrayobject *, Py_ssize_t, PyObject *); int (*setitem)(struct arrayobject *, Py_ssize_t, PyObject *);
@ -1510,7 +1510,7 @@ array_fromunicode(arrayobject *self, PyObject *args)
{ {
Py_UNICODE *ustr; Py_UNICODE *ustr;
Py_ssize_t n; Py_ssize_t n;
Py_UNICODE typecode; char typecode;
if (!PyArg_ParseTuple(args, "u#:fromunicode", &ustr, &n)) if (!PyArg_ParseTuple(args, "u#:fromunicode", &ustr, &n))
return NULL; return NULL;
@ -1545,7 +1545,7 @@ append Unicode data to an array of some other type.");
static PyObject * static PyObject *
array_tounicode(arrayobject *self, PyObject *unused) array_tounicode(arrayobject *self, PyObject *unused)
{ {
Py_UNICODE typecode; char typecode;
typecode = self->ob_descr->typecode; typecode = self->ob_descr->typecode;
if ((typecode != 'u')) { if ((typecode != 'u')) {
PyErr_SetString(PyExc_ValueError, PyErr_SetString(PyExc_ValueError,
@ -1642,7 +1642,7 @@ static const struct mformatdescr {
* be found. * be found.
*/ */
static enum machine_format_code static enum machine_format_code
typecode_to_mformat_code(int typecode) typecode_to_mformat_code(char typecode)
{ {
#ifdef WORDS_BIGENDIAN #ifdef WORDS_BIGENDIAN
const int is_big_endian = 1; const int is_big_endian = 1;
@ -1721,7 +1721,7 @@ typecode_to_mformat_code(int typecode)
intsize = sizeof(PY_LONG_LONG); intsize = sizeof(PY_LONG_LONG);
is_signed = 0; is_signed = 0;
break; break;
#endif #endif
default: default:
return UNKNOWN_FORMAT; return UNKNOWN_FORMAT;
} }
@ -1752,7 +1752,7 @@ static PyObject *array_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
* NULL is returned to indicate a failure. * NULL is returned to indicate a failure.
*/ */
static PyObject * static PyObject *
make_array(PyTypeObject *arraytype, Py_UNICODE typecode, PyObject *items) make_array(PyTypeObject *arraytype, char typecode, PyObject *items)
{ {
PyObject *new_args; PyObject *new_args;
PyObject *array_obj; PyObject *array_obj;
@ -1761,7 +1761,7 @@ make_array(PyTypeObject *arraytype, Py_UNICODE typecode, PyObject *items)
assert(arraytype != NULL); assert(arraytype != NULL);
assert(items != NULL); assert(items != NULL);
typecode_obj = PyUnicode_FromUnicode(&typecode, 1); typecode_obj = PyUnicode_FromOrdinal(typecode);
if (typecode_obj == NULL) if (typecode_obj == NULL)
return NULL; return NULL;
@ -1791,17 +1791,14 @@ array_reconstructor(PyObject *self, PyObject *args)
PyObject *items; PyObject *items;
PyObject *converted_items; PyObject *converted_items;
PyObject *result; PyObject *result;
int typecode_int; int typecode;
Py_UNICODE typecode;
enum machine_format_code mformat_code; enum machine_format_code mformat_code;
struct arraydescr *descr; struct arraydescr *descr;
if (!PyArg_ParseTuple(args, "OCiO:array._array_reconstructor", if (!PyArg_ParseTuple(args, "OCiO:array._array_reconstructor",
&arraytype, &typecode_int, &mformat_code, &items)) &arraytype, &typecode, &mformat_code, &items))
return NULL; return NULL;
typecode = (Py_UNICODE)typecode_int;
if (!PyType_Check(arraytype)) { if (!PyType_Check(arraytype)) {
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"first argument must a type object, not %.200s", "first argument must a type object, not %.200s",
@ -1815,7 +1812,7 @@ array_reconstructor(PyObject *self, PyObject *args)
return NULL; return NULL;
} }
for (descr = descriptors; descr->typecode != '\0'; descr++) { for (descr = descriptors; descr->typecode != '\0'; descr++) {
if (descr->typecode == typecode) if ((int)descr->typecode == typecode)
break; break;
} }
if (descr->typecode == '\0') { if (descr->typecode == '\0') {
@ -1837,9 +1834,9 @@ array_reconstructor(PyObject *self, PyObject *args)
} }
/* Fast path: No decoding has to be done. */ /* Fast path: No decoding has to be done. */
if (mformat_code == typecode_to_mformat_code(typecode) || if (mformat_code == typecode_to_mformat_code((char)typecode) ||
mformat_code == UNKNOWN_FORMAT) { mformat_code == UNKNOWN_FORMAT) {
return make_array(arraytype, typecode, items); return make_array(arraytype, (char)typecode, items);
} }
/* Slow path: Decode the byte string according to the given machine /* Slow path: Decode the byte string according to the given machine
@ -1985,7 +1982,7 @@ array_reconstructor(PyObject *self, PyObject *args)
return NULL; return NULL;
} }
result = make_array(arraytype, typecode, converted_items); result = make_array(arraytype, (char)typecode, converted_items);
Py_DECREF(converted_items); Py_DECREF(converted_items);
return result; return result;
} }
@ -2074,8 +2071,8 @@ PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
static PyObject * static PyObject *
array_get_typecode(arrayobject *a, void *closure) array_get_typecode(arrayobject *a, void *closure)
{ {
Py_UNICODE tc = a->ob_descr->typecode; char typecode = a->ob_descr->typecode;
return PyUnicode_FromUnicode(&tc, 1); return PyUnicode_FromOrdinal(typecode);
} }
static PyObject * static PyObject *
@ -2147,7 +2144,7 @@ static PyMethodDef array_methods[] = {
static PyObject * static PyObject *
array_repr(arrayobject *a) array_repr(arrayobject *a)
{ {
Py_UNICODE typecode; char typecode;
PyObject *s, *v = NULL; PyObject *s, *v = NULL;
Py_ssize_t len; Py_ssize_t len;