comparison Modules/arraymodule.c @ 42112:ebc87b94e6ea

"Fix" the array module test -- by ripping out the 'c' typecode. (We already have 'b' for bytes and 'u' for unicode.)
author Guido van Rossum <guido@python.org>
date Mon, 18 Jun 2007 18:44:28 +0000
parents e4e8b12770e4
children 79c2656056f6
comparison
equal deleted inserted replaced
42111:32fe2b9e9f11 42112:ebc87b94e6ea
100 Note that the basic Get and Set functions do NOT check that the index is 100 Note that the basic Get and Set functions do NOT check that the index is
101 in bounds; that's the responsibility of the caller. 101 in bounds; that's the responsibility of the caller.
102 ****************************************************************************/ 102 ****************************************************************************/
103 103
104 static PyObject * 104 static PyObject *
105 c_getitem(arrayobject *ap, Py_ssize_t i)
106 {
107 Py_UNICODE buf[1];
108 buf[0] = ((unsigned char *)ap->ob_item)[i];
109 return PyUnicode_FromUnicode(buf, 1);
110 }
111
112 static int
113 c_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v)
114 {
115 char x;
116 if (!PyArg_Parse(v, "c;array item must be char", &x))
117 return -1;
118 if (i >= 0)
119 ((char *)ap->ob_item)[i] = x;
120 return 0;
121 }
122
123 static PyObject *
124 b_getitem(arrayobject *ap, Py_ssize_t i) 105 b_getitem(arrayobject *ap, Py_ssize_t i)
125 { 106 {
126 long x = ((char *)ap->ob_item)[i]; 107 long x = ((char *)ap->ob_item)[i];
127 if (x >= 128) 108 if (x >= 128)
128 x -= 256; 109 x -= 256;
389 return 0; 370 return 0;
390 } 371 }
391 372
392 /* Description of types */ 373 /* Description of types */
393 static struct arraydescr descriptors[] = { 374 static struct arraydescr descriptors[] = {
394 {'c', sizeof(char), c_getitem, c_setitem},
395 {'b', sizeof(char), b_getitem, b_setitem}, 375 {'b', sizeof(char), b_getitem, b_setitem},
396 {'B', sizeof(char), BB_getitem, BB_setitem}, 376 {'B', sizeof(char), BB_getitem, BB_setitem},
397 {'u', sizeof(Py_UNICODE), u_getitem, u_setitem}, 377 {'u', sizeof(Py_UNICODE), u_getitem, u_setitem},
398 {'h', sizeof(short), h_getitem, h_setitem}, 378 {'h', sizeof(short), h_getitem, h_setitem},
399 {'H', sizeof(short), HH_getitem, HH_setitem}, 379 {'H', sizeof(short), HH_getitem, HH_setitem},
1401 1381
1402 1382
1403 static PyObject * 1383 static PyObject *
1404 array_tostring(arrayobject *self, PyObject *unused) 1384 array_tostring(arrayobject *self, PyObject *unused)
1405 { 1385 {
1406 return PyString_FromStringAndSize(self->ob_item, 1386 return PyBytes_FromStringAndSize(self->ob_item,
1407 self->ob_size * self->ob_descr->itemsize); 1387 self->ob_size * self->ob_descr->itemsize);
1408 } 1388 }
1409 1389
1410 PyDoc_STRVAR(tostring_doc, 1390 PyDoc_STRVAR(tostring_doc,
1411 "tostring() -> string\n\ 1391 "tostring() -> string\n\
1412 \n\ 1392 \n\
1560 len = a->ob_size; 1540 len = a->ob_size;
1561 typecode = a->ob_descr->typecode; 1541 typecode = a->ob_descr->typecode;
1562 if (len == 0) { 1542 if (len == 0) {
1563 return PyUnicode_FromFormat("array('%c')", typecode); 1543 return PyUnicode_FromFormat("array('%c')", typecode);
1564 } 1544 }
1565 if (typecode == 'c') 1545 if (typecode == 'u')
1566 v = array_tostring(a, NULL);
1567 else if (typecode == 'u')
1568 v = array_tounicode(a, NULL); 1546 v = array_tounicode(a, NULL);
1569 else 1547 else
1570 v = array_tolist(a, NULL); 1548 v = array_tolist(a, NULL);
1571 1549
1572 s = PyUnicode_FromFormat("array('%c', %R)", typecode, v); 1550 s = PyUnicode_FromFormat("array('%c', %R)", typecode, v);
1897 } 1875 }
1898 return a; 1876 return a;
1899 } 1877 }
1900 } 1878 }
1901 PyErr_SetString(PyExc_ValueError, 1879 PyErr_SetString(PyExc_ValueError,
1902 "bad typecode (must be c, b, B, u, h, H, i, I, l, L, f or d)"); 1880 "bad typecode (must be b, B, u, h, H, i, I, l, L, f or d)");
1903 return NULL; 1881 return NULL;
1904 } 1882 }
1905 1883
1906 1884
1907 PyDoc_STRVAR(module_doc, 1885 PyDoc_STRVAR(module_doc,
1911 except that the type of objects stored in them is constrained. The\n\ 1889 except that the type of objects stored in them is constrained. The\n\
1912 type is specified at object creation time by using a type code, which\n\ 1890 type is specified at object creation time by using a type code, which\n\
1913 is a single character. The following type codes are defined:\n\ 1891 is a single character. The following type codes are defined:\n\
1914 \n\ 1892 \n\
1915 Type code C Type Minimum size in bytes \n\ 1893 Type code C Type Minimum size in bytes \n\
1916 'c' character 1 \n\
1917 'b' signed integer 1 \n\ 1894 'b' signed integer 1 \n\
1918 'B' unsigned integer 1 \n\ 1895 'B' unsigned integer 1 \n\
1919 'u' Unicode character 2 \n\ 1896 'u' Unicode character 2 \n\
1920 'h' signed integer 2 \n\ 1897 'h' signed integer 2 \n\
1921 'H' unsigned integer 2 \n\ 1898 'H' unsigned integer 2 \n\