comparison Modules/arraymodule.c @ 78447:95da47ddebe0

Close #13072: Restore code before the PEP 393 for the array module 'u' format of the array module uses again Py_UNICODE type for backward compatibility with Python 3.2. The only change from Python 3.2 is that PyUnicode_AsUnicode() result is now checked for NULL value.
author Victor Stinner <victor.stinner@gmail.com>
date Mon, 06 Aug 2012 00:46:05 +0200
parents fb907ee4fdfa
children 7d82a60850fd
comparison
equal deleted inserted replaced
78446:099639507383 78447:95da47ddebe0
172 } 172 }
173 173
174 static PyObject * 174 static PyObject *
175 u_getitem(arrayobject *ap, Py_ssize_t i) 175 u_getitem(arrayobject *ap, Py_ssize_t i)
176 { 176 {
177 return PyUnicode_FromOrdinal(((Py_UCS4 *) ap->ob_item)[i]); 177 return PyUnicode_FromUnicode(&((Py_UNICODE *) ap->ob_item)[i], 1);
178 } 178 }
179 179
180 static int 180 static int
181 u_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) 181 u_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
182 { 182 {
183 PyObject *p; 183 Py_UNICODE *p;
184 184 Py_ssize_t len;
185 if (!PyArg_Parse(v, "U;array item must be unicode character", &p)) 185
186 return -1; 186 if (!PyArg_Parse(v, "u#;array item must be unicode character", &p, &len))
187 if (PyUnicode_READY(p)) 187 return -1;
188 return -1; 188 if (len != 1) {
189 if (PyUnicode_GET_LENGTH(p) != 1) {
190 PyErr_SetString(PyExc_TypeError, 189 PyErr_SetString(PyExc_TypeError,
191 "array item must be unicode character"); 190 "array item must be unicode character");
192 return -1; 191 return -1;
193 } 192 }
194 if (i >= 0) 193 if (i >= 0)
195 ((Py_UCS4 *)ap->ob_item)[i] = PyUnicode_READ_CHAR(p, 0); 194 ((Py_UNICODE *)ap->ob_item)[i] = p[0];
196 return 0; 195 return 0;
197 } 196 }
198 197
199 198
200 static PyObject * 199 static PyObject *
442 if (i >= 0) 441 if (i >= 0)
443 ((double *)ap->ob_item)[i] = x; 442 ((double *)ap->ob_item)[i] = x;
444 return 0; 443 return 0;
445 } 444 }
446 445
447 #if SIZEOF_INT == 4
448 # define STRUCT_LONG_FORMAT "I"
449 #elif SIZEOF_LONG == 4
450 # define STRUCT_LONG_FORMAT "L"
451 #else
452 # error "Unable to get struct format for Py_UCS4"
453 #endif
454 446
455 /* Description of types. 447 /* Description of types.
456 * 448 *
457 * Don't forget to update typecode_to_mformat_code() if you add a new 449 * Don't forget to update typecode_to_mformat_code() if you add a new
458 * typecode. 450 * typecode.
459 */ 451 */
460 static struct arraydescr descriptors[] = { 452 static struct arraydescr descriptors[] = {
461 {'b', 1, b_getitem, b_setitem, "b", 1, 1}, 453 {'b', 1, b_getitem, b_setitem, "b", 1, 1},
462 {'B', 1, BB_getitem, BB_setitem, "B", 1, 0}, 454 {'B', 1, BB_getitem, BB_setitem, "B", 1, 0},
463 {'u', sizeof(Py_UCS4), u_getitem, u_setitem, STRUCT_LONG_FORMAT, 0, 0}, 455 {'u', sizeof(Py_UNICODE), u_getitem, u_setitem, "u", 0, 0},
464 {'h', sizeof(short), h_getitem, h_setitem, "h", 1, 1}, 456 {'h', sizeof(short), h_getitem, h_setitem, "h", 1, 1},
465 {'H', sizeof(short), HH_getitem, HH_setitem, "H", 1, 0}, 457 {'H', sizeof(short), HH_getitem, HH_setitem, "H", 1, 0},
466 {'i', sizeof(int), i_getitem, i_setitem, "i", 1, 1}, 458 {'i', sizeof(int), i_getitem, i_setitem, "i", 1, 1},
467 {'I', sizeof(int), II_getitem, II_setitem, "I", 1, 0}, 459 {'I', sizeof(int), II_getitem, II_setitem, "I", 1, 0},
468 {'l', sizeof(long), l_getitem, l_setitem, "l", 1, 1}, 460 {'l', sizeof(long), l_getitem, l_setitem, "l", 1, 1},
1517 1509
1518 1510
1519 static PyObject * 1511 static PyObject *
1520 array_fromunicode(arrayobject *self, PyObject *args) 1512 array_fromunicode(arrayobject *self, PyObject *args)
1521 { 1513 {
1522 PyObject *ustr; 1514 Py_UNICODE *ustr;
1523 Py_ssize_t n; 1515 Py_ssize_t n;
1524 1516 char typecode;
1525 if (!PyArg_ParseTuple(args, "U:fromunicode", &ustr)) 1517
1526 return NULL; 1518 if (!PyArg_ParseTuple(args, "u#:fromunicode", &ustr, &n))
1527 if (self->ob_descr->typecode != 'u') { 1519 return NULL;
1520 typecode = self->ob_descr->typecode;
1521 if ((typecode != 'u')) {
1528 PyErr_SetString(PyExc_ValueError, 1522 PyErr_SetString(PyExc_ValueError,
1529 "fromunicode() may only be called on " 1523 "fromunicode() may only be called on "
1530 "unicode type arrays"); 1524 "unicode type arrays");
1531 return NULL; 1525 return NULL;
1532 } 1526 }
1533 if (PyUnicode_READY(ustr))
1534 return NULL;
1535 n = PyUnicode_GET_LENGTH(ustr);
1536 if (n > 0) { 1527 if (n > 0) {
1537 Py_ssize_t old_size = Py_SIZE(self); 1528 Py_ssize_t old_size = Py_SIZE(self);
1538 if (array_resize(self, old_size + n) == -1) 1529 if (array_resize(self, old_size + n) == -1)
1539 return NULL; 1530 return NULL;
1540 if (!PyUnicode_AsUCS4(ustr, (Py_UCS4 *)self->ob_item + old_size, n, 0)) 1531 memcpy(self->ob_item + old_size * sizeof(Py_UNICODE),
1541 return NULL; 1532 ustr, n * sizeof(Py_UNICODE));
1542 } 1533 }
1543 1534
1544 Py_INCREF(Py_None); 1535 Py_INCREF(Py_None);
1545 return Py_None; 1536 return Py_None;
1546 } 1537 }
1555 1546
1556 1547
1557 static PyObject * 1548 static PyObject *
1558 array_tounicode(arrayobject *self, PyObject *unused) 1549 array_tounicode(arrayobject *self, PyObject *unused)
1559 { 1550 {
1560 if (self->ob_descr->typecode != 'u') { 1551 char typecode;
1552 typecode = self->ob_descr->typecode;
1553 if ((typecode != 'u')) {
1561 PyErr_SetString(PyExc_ValueError, 1554 PyErr_SetString(PyExc_ValueError,
1562 "tounicode() may only be called on unicode type arrays"); 1555 "tounicode() may only be called on unicode type arrays");
1563 return NULL; 1556 return NULL;
1564 } 1557 }
1565 return PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, 1558 return PyUnicode_FromUnicode((Py_UNICODE *) self->ob_item, Py_SIZE(self));
1566 (Py_UCS4 *) self->ob_item,
1567 Py_SIZE(self));
1568 } 1559 }
1569 1560
1570 PyDoc_STRVAR(tounicode_doc, 1561 PyDoc_STRVAR(tounicode_doc,
1571 "tounicode() -> unicode\n\ 1562 "tounicode() -> unicode\n\
1572 \n\ 1563 \n\
1669 return SIGNED_INT8; 1660 return SIGNED_INT8;
1670 case 'B': 1661 case 'B':
1671 return UNSIGNED_INT8; 1662 return UNSIGNED_INT8;
1672 1663
1673 case 'u': 1664 case 'u':
1674 return UTF32_LE + is_big_endian; 1665 if (sizeof(Py_UNICODE) == 2) {
1666 return UTF16_LE + is_big_endian;
1667 }
1668 if (sizeof(Py_UNICODE) == 4) {
1669 return UTF32_LE + is_big_endian;
1670 }
1671 return UNKNOWN_FORMAT;
1675 1672
1676 case 'f': 1673 case 'f':
1677 if (sizeof(float) == 4) { 1674 if (sizeof(float) == 4) {
1678 const float y = 16711938.0; 1675 const float y = 16711938.0;
1679 if (memcmp(&y, "\x4b\x7f\x01\x02", 4) == 0) 1676 if (memcmp(&y, "\x4b\x7f\x01\x02", 4) == 0)
2417 view->strides = NULL; 2414 view->strides = NULL;
2418 if ((flags & PyBUF_STRIDES)==PyBUF_STRIDES) 2415 if ((flags & PyBUF_STRIDES)==PyBUF_STRIDES)
2419 view->strides = &(view->itemsize); 2416 view->strides = &(view->itemsize);
2420 view->format = NULL; 2417 view->format = NULL;
2421 view->internal = NULL; 2418 view->internal = NULL;
2422 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) 2419 if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) {
2423 view->format = self->ob_descr->formats; 2420 view->format = self->ob_descr->formats;
2421 #ifdef Py_UNICODE_WIDE
2422 if (self->ob_descr->typecode == 'u') {
2423 view->format = "w";
2424 }
2425 #endif
2426 }
2424 2427
2425 finish: 2428 finish:
2426 self->ob_exports++; 2429 self->ob_exports++;
2427 return 0; 2430 return 0;
2428 } 2431 }
2532 return NULL; 2535 return NULL;
2533 } 2536 }
2534 Py_DECREF(v); 2537 Py_DECREF(v);
2535 } 2538 }
2536 else if (initial != NULL && PyUnicode_Check(initial)) { 2539 else if (initial != NULL && PyUnicode_Check(initial)) {
2540 Py_UNICODE *ustr;
2537 Py_ssize_t n; 2541 Py_ssize_t n;
2538 if (PyUnicode_READY(initial)) { 2542
2543 ustr = PyUnicode_AsUnicode(initial);
2544 if (ustr == NULL) {
2545 PyErr_NoMemory();
2539 Py_DECREF(a); 2546 Py_DECREF(a);
2540 return NULL; 2547 return NULL;
2541 } 2548 }
2542 n = PyUnicode_GET_LENGTH(initial); 2549
2550 n = PyUnicode_GET_DATA_SIZE(initial);
2543 if (n > 0) { 2551 if (n > 0) {
2544 arrayobject *self = (arrayobject *)a; 2552 arrayobject *self = (arrayobject *)a;
2545 Py_UCS4 *item = (Py_UCS4 *)self->ob_item; 2553 char *item = self->ob_item;
2546 item = (Py_UCS4 *)PyMem_Realloc(item, n * sizeof(Py_UCS4)); 2554 item = (char *)PyMem_Realloc(item, n);
2547 if (item == NULL) { 2555 if (item == NULL) {
2548 PyErr_NoMemory(); 2556 PyErr_NoMemory();
2549 Py_DECREF(a); 2557 Py_DECREF(a);
2550 return NULL; 2558 return NULL;
2551 } 2559 }
2552 self->ob_item = (char*)item; 2560 self->ob_item = item;
2553 Py_SIZE(self) = n; 2561 Py_SIZE(self) = n / sizeof(Py_UNICODE);
2554 if (!PyUnicode_AsUCS4(initial, item, n, 0)) 2562 memcpy(item, ustr, n);
2555 return NULL;
2556 self->allocated = Py_SIZE(self); 2563 self->allocated = Py_SIZE(self);
2557 } 2564 }
2558 } 2565 }
2559 else if (initial != NULL && array_Check(initial)) { 2566 else if (initial != NULL && array_Check(initial)) {
2560 arrayobject *self = (arrayobject *)a; 2567 arrayobject *self = (arrayobject *)a;
2591 is a single character. The following type codes are defined:\n\ 2598 is a single character. The following type codes are defined:\n\
2592 \n\ 2599 \n\
2593 Type code C Type Minimum size in bytes \n\ 2600 Type code C Type Minimum size in bytes \n\
2594 'b' signed integer 1 \n\ 2601 'b' signed integer 1 \n\
2595 'B' unsigned integer 1 \n\ 2602 'B' unsigned integer 1 \n\
2596 'u' Unicode character 4 \n\ 2603 'u' Unicode character 2 (see note) \n\
2597 'h' signed integer 2 \n\ 2604 'h' signed integer 2 \n\
2598 'H' unsigned integer 2 \n\ 2605 'H' unsigned integer 2 \n\
2599 'i' signed integer 2 \n\ 2606 'i' signed integer 2 \n\
2600 'I' unsigned integer 2 \n\ 2607 'I' unsigned integer 2 \n\
2601 'l' signed integer 4 \n\ 2608 'l' signed integer 4 \n\
2602 'L' unsigned integer 4 \n\ 2609 'L' unsigned integer 4 \n\
2603 'q' signed integer 8 (see note) \n\ 2610 'q' signed integer 8 (see note) \n\
2604 'Q' unsigned integer 8 (see note) \n\ 2611 'Q' unsigned integer 8 (see note) \n\
2605 'f' floating point 4 \n\ 2612 'f' floating point 4 \n\
2606 'd' floating point 8 \n\ 2613 'd' floating point 8 \n\
2614 \n\
2615 NOTE: The 'u' typecode corresponds to Python's unicode character. On \n\
2616 narrow builds this is 2-bytes on wide builds this is 4-bytes.\n\
2607 \n\ 2617 \n\
2608 NOTE: The 'q' and 'Q' type codes are only available if the platform \n\ 2618 NOTE: The 'q' and 'Q' type codes are only available if the platform \n\
2609 C compiler used to build Python supports 'long long', or, on Windows, \n\ 2619 C compiler used to build Python supports 'long long', or, on Windows, \n\
2610 '__int64'.\n\ 2620 '__int64'.\n\
2611 \n\ 2621 \n\