| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 1 /* Array object implementation */ | 1 /* Array object implementation */ |
| 2 | 2 |
| 3 /* An array is a uniform list -- all items have the same type. | 3 /* An array is a uniform list -- all items have the same type. |
| 4 The item type is restricted to simple C types like int or float */ | 4 The item type is restricted to simple C types like int or float */ |
| 5 | 5 |
| 6 #define PY_SSIZE_T_CLEAN | 6 #define PY_SSIZE_T_CLEAN |
| 7 #include "Python.h" | 7 #include "Python.h" |
| 8 #include "structmember.h" | 8 #include "structmember.h" |
| 9 | 9 |
| 10 #ifdef STDC_HEADERS | 10 #ifdef STDC_HEADERS |
| (...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 346 | 346 |
| 347 } | 347 } |
| 348 if (x > ULONG_MAX) { | 348 if (x > ULONG_MAX) { |
| 349 PyErr_SetString(PyExc_OverflowError, | 349 PyErr_SetString(PyExc_OverflowError, |
| 350 "unsigned long is greater than maximum"); | 350 "unsigned long is greater than maximum"); |
| 351 return -1; | 351 return -1; |
| 352 } | 352 } |
| 353 | 353 |
| 354 if (i >= 0) | 354 if (i >= 0) |
| 355 ((unsigned long *)ap->ob_item)[i] = x; | 355 ((unsigned long *)ap->ob_item)[i] = x; |
| 356 return 0; | |
| 357 } | |
| 358 | |
| 359 #ifdef HAVE_LONG_LONG | |
| 360 | |
| 361 static PyObject * | |
| 362 q_getitem(arrayobject *ap, int i) | |
|
skrah
2011/09/11 10:53:59
'int i' should be 'Py_ssize_t i' (also in other pl
meadori
2011/09/11 16:43:38
Fixed.
| |
| 363 { | |
| 364 return PyLong_FromLongLong(((PY_LONG_LONG *)ap->ob_item)[i]); | |
| 365 } | |
| 366 | |
| 367 static int | |
| 368 q_setitem(arrayobject *ap, int i, PyObject *v) | |
| 369 { | |
| 370 PY_LONG_LONG x; | |
| 371 if (!PyArg_Parse(v, "L;array item must be integer", &x)) | |
|
skrah
2011/09/11 10:53:59
Why not use PyLong_AsLongLong() directly. I see th
meadori
2011/09/11 16:43:38
The only reason that I can see is that 'PyArg_Pars
| |
| 372 return -1; | |
| 373 if (i >= 0) | |
| 374 ((PY_LONG_LONG *)ap->ob_item)[i] = x; | |
| 356 return 0; | 375 return 0; |
| 357 } | 376 } |
| 358 | 377 |
| 359 static PyObject * | 378 static PyObject * |
| 379 QQ_getitem(arrayobject *ap, int i) | |
| 380 { | |
| 381 return PyLong_FromUnsignedLongLong( | |
| 382 ((unsigned PY_LONG_LONG *)ap->ob_item)[i]); | |
| 383 } | |
| 384 | |
| 385 static int | |
| 386 QQ_setitem(arrayobject *ap, int i, PyObject *v) | |
| 387 { | |
| 388 unsigned PY_LONG_LONG x; | |
| 389 if (PyLong_Check(v)) { | |
| 390 x = PyLong_AsUnsignedLongLong(v); | |
| 391 if (x == (unsigned PY_LONG_LONG) -1 && PyErr_Occurred()) | |
| 392 return -1; | |
| 393 } | |
| 394 else { | |
|
skrah
2011/09/11 10:53:59
All PyArg_Parse() does in the test suite is raise
meadori
2011/09/11 16:43:38
Maybe the tests needed extending. Replacing this
| |
| 395 PY_LONG_LONG y; | |
| 396 if (!PyArg_Parse(v, "L;array item must be integer", &y)) | |
| 397 return -1; | |
| 398 if (y < 0) { | |
| 399 PyErr_SetString(PyExc_OverflowError, | |
| 400 "unsigned long long is less than minimum"); | |
| 401 return -1; | |
| 402 } | |
| 403 x = (unsigned PY_LONG_LONG)y; | |
| 404 } | |
| 405 | |
| 406 if (i >= 0) | |
| 407 ((unsigned PY_LONG_LONG *)ap->ob_item)[i] = x; | |
| 408 return 0; | |
| 409 } | |
| 410 #endif | |
| 411 | |
| 412 static PyObject * | |
| 360 f_getitem(arrayobject *ap, Py_ssize_t i) | 413 f_getitem(arrayobject *ap, Py_ssize_t i) |
| 361 { | 414 { |
| 362 return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]); | 415 return PyFloat_FromDouble((double) ((float *)ap->ob_item)[i]); |
| 363 } | 416 } |
| 364 | 417 |
| 365 static int | 418 static int |
| 366 f_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) | 419 f_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) |
| 367 { | 420 { |
| 368 float x; | 421 float x; |
| 369 if (!PyArg_Parse(v, "f;array item must be float", &x)) | 422 if (!PyArg_Parse(v, "f;array item must be float", &x)) |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 399 static struct arraydescr descriptors[] = { | 452 static struct arraydescr descriptors[] = { |
| 400 {'b', 1, b_getitem, b_setitem, "b", 1, 1}, | 453 {'b', 1, b_getitem, b_setitem, "b", 1, 1}, |
| 401 {'B', 1, BB_getitem, BB_setitem, "B", 1, 0}, | 454 {'B', 1, BB_getitem, BB_setitem, "B", 1, 0}, |
| 402 {'u', sizeof(Py_UNICODE), u_getitem, u_setitem, "u", 0, 0}, | 455 {'u', sizeof(Py_UNICODE), u_getitem, u_setitem, "u", 0, 0}, |
| 403 {'h', sizeof(short), h_getitem, h_setitem, "h", 1, 1}, | 456 {'h', sizeof(short), h_getitem, h_setitem, "h", 1, 1}, |
| 404 {'H', sizeof(short), HH_getitem, HH_setitem, "H", 1, 0}, | 457 {'H', sizeof(short), HH_getitem, HH_setitem, "H", 1, 0}, |
| 405 {'i', sizeof(int), i_getitem, i_setitem, "i", 1, 1}, | 458 {'i', sizeof(int), i_getitem, i_setitem, "i", 1, 1}, |
| 406 {'I', sizeof(int), II_getitem, II_setitem, "I", 1, 0}, | 459 {'I', sizeof(int), II_getitem, II_setitem, "I", 1, 0}, |
| 407 {'l', sizeof(long), l_getitem, l_setitem, "l", 1, 1}, | 460 {'l', sizeof(long), l_getitem, l_setitem, "l", 1, 1}, |
| 408 {'L', sizeof(long), LL_getitem, LL_setitem, "L", 1, 0}, | 461 {'L', sizeof(long), LL_getitem, LL_setitem, "L", 1, 0}, |
| 462 #ifdef HAVE_LONG_LONG | |
| 463 {'q', sizeof(PY_LONG_LONG), q_getitem, q_setitem, "q", 1, 1}, | |
| 464 {'Q', sizeof(PY_LONG_LONG), QQ_getitem, QQ_setitem, "Q", 1, 0}, | |
| 465 #endif | |
| 409 {'f', sizeof(float), f_getitem, f_setitem, "f", 0, 0}, | 466 {'f', sizeof(float), f_getitem, f_setitem, "f", 0, 0}, |
| 410 {'d', sizeof(double), d_getitem, d_setitem, "d", 0, 0}, | 467 {'d', sizeof(double), d_getitem, d_setitem, "d", 0, 0}, |
| 411 {'\0', 0, 0, 0, 0, 0, 0} /* Sentinel */ | 468 {'\0', 0, 0, 0, 0, 0, 0} /* Sentinel */ |
| 412 }; | 469 }; |
| 413 | 470 |
| 414 /**************************************************************************** | 471 /**************************************************************************** |
| 415 Implementations of array object methods. | 472 Implementations of array object methods. |
| 416 ****************************************************************************/ | 473 ****************************************************************************/ |
| 417 | 474 |
| 418 static PyObject * | 475 static PyObject * |
| (...skipping 1229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1648 is_signed = 0; | 1705 is_signed = 0; |
| 1649 break; | 1706 break; |
| 1650 case 'l': | 1707 case 'l': |
| 1651 intsize = sizeof(long); | 1708 intsize = sizeof(long); |
| 1652 is_signed = 1; | 1709 is_signed = 1; |
| 1653 break; | 1710 break; |
| 1654 case 'L': | 1711 case 'L': |
| 1655 intsize = sizeof(long); | 1712 intsize = sizeof(long); |
| 1656 is_signed = 0; | 1713 is_signed = 0; |
| 1657 break; | 1714 break; |
| 1715 #if HAVE_LONG_LONG | |
| 1716 case 'q': | |
| 1717 intsize = sizeof(PY_LONG_LONG); | |
| 1718 is_signed = 1; | |
| 1719 break; | |
| 1720 case 'Q': | |
| 1721 intsize = sizeof(PY_LONG_LONG); | |
| 1722 is_signed = 0; | |
| 1723 break; | |
| 1724 #endif | |
| 1658 default: | 1725 default: |
| 1659 return UNKNOWN_FORMAT; | 1726 return UNKNOWN_FORMAT; |
| 1660 } | 1727 } |
| 1661 switch (intsize) { | 1728 switch (intsize) { |
| 1662 case 2: | 1729 case 2: |
| 1663 return UNSIGNED_INT16_LE + is_big_endian + (2 * is_signed); | 1730 return UNSIGNED_INT16_LE + is_big_endian + (2 * is_signed); |
| 1664 case 4: | 1731 case 4: |
| 1665 return UNSIGNED_INT32_LE + is_big_endian + (2 * is_signed); | 1732 return UNSIGNED_INT32_LE + is_big_endian + (2 * is_signed); |
| 1666 case 8: | 1733 case 8: |
| 1667 return UNSIGNED_INT64_LE + is_big_endian + (2 * is_signed); | 1734 return UNSIGNED_INT64_LE + is_big_endian + (2 * is_signed); |
| (...skipping 826 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2494 Py_DECREF(it); | 2561 Py_DECREF(it); |
| 2495 Py_DECREF(a); | 2562 Py_DECREF(a); |
| 2496 return NULL; | 2563 return NULL; |
| 2497 } | 2564 } |
| 2498 Py_DECREF(it); | 2565 Py_DECREF(it); |
| 2499 } | 2566 } |
| 2500 return a; | 2567 return a; |
| 2501 } | 2568 } |
| 2502 } | 2569 } |
| 2503 PyErr_SetString(PyExc_ValueError, | 2570 PyErr_SetString(PyExc_ValueError, |
| 2571 #ifdef HAVE_LONG_LONG | |
| 2572 "bad typecode (must be b, B, u, h, H, i, I, l, L, q, Q, f or d)"); | |
| 2573 #else | |
| 2504 "bad typecode (must be b, B, u, h, H, i, I, l, L, f or d)"); | 2574 "bad typecode (must be b, B, u, h, H, i, I, l, L, f or d)"); |
| 2575 #endif | |
| 2505 return NULL; | 2576 return NULL; |
| 2506 } | 2577 } |
| 2507 | 2578 |
| 2508 | 2579 |
| 2509 PyDoc_STRVAR(module_doc, | 2580 PyDoc_STRVAR(module_doc, |
| 2510 "This module defines an object type which can efficiently represent\n\ | 2581 "This module defines an object type which can efficiently represent\n\ |
| 2511 an array of basic values: characters, integers, floating point\n\ | 2582 an array of basic values: characters, integers, floating point\n\ |
| 2512 numbers. Arrays are sequence types and behave very much like lists,\n\ | 2583 numbers. Arrays are sequence types and behave very much like lists,\n\ |
| 2513 except that the type of objects stored in them is constrained. The\n\ | 2584 except that the type of objects stored in them is constrained. The\n\ |
| 2514 type is specified at object creation time by using a type code, which\n\ | 2585 type is specified at object creation time by using a type code, which\n\ |
| 2515 is a single character. The following type codes are defined:\n\ | 2586 is a single character. The following type codes are defined:\n\ |
| 2516 \n\ | 2587 \n\ |
| 2517 Type code C Type Minimum size in bytes \n\ | 2588 Type code C Type Minimum size in bytes \n\ |
| 2518 'b' signed integer 1 \n\ | 2589 'b' signed integer 1 \n\ |
| 2519 'B' unsigned integer 1 \n\ | 2590 'B' unsigned integer 1 \n\ |
| 2520 'u' Unicode character 2 (see note) \n\ | 2591 'u' Unicode character 2 (see note) \n\ |
| 2521 'h' signed integer 2 \n\ | 2592 'h' signed integer 2 \n\ |
| 2522 'H' unsigned integer 2 \n\ | 2593 'H' unsigned integer 2 \n\ |
| 2523 'i' signed integer 2 \n\ | 2594 'i' signed integer 2 \n\ |
| 2524 'I' unsigned integer 2 \n\ | 2595 'I' unsigned integer 2 \n\ |
| 2525 'l' signed integer 4 \n\ | 2596 'l' signed integer 4 \n\ |
| 2526 'L' unsigned integer 4 \n\ | 2597 'L' unsigned integer 4 \n\ |
| 2598 'q' signed integer 8 (see note) \n\ | |
| 2599 'Q' unsigned integer 8 (see note) \n\ | |
| 2527 'f' floating point 4 \n\ | 2600 'f' floating point 4 \n\ |
| 2528 'd' floating point 8 \n\ | 2601 'd' floating point 8 \n\ |
| 2529 \n\ | 2602 \n\ |
| 2530 NOTE: The 'u' typecode corresponds to Python's unicode character. On \n\ | 2603 NOTE: The 'u' type code corresponds to Python's unicode character. On \n\ |
| 2531 narrow builds this is 2-bytes on wide builds this is 4-bytes.\n\ | 2604 narrow builds this is 2-bytes on wide builds this is 4-bytes.\n\ |
| 2605 \n\ | |
| 2606 NOTE: The 'q' and 'Q' type codes are only available if the platform \n\ | |
| 2607 C compiler used to build Python supports 'long long', or, on Windows, \n\ | |
| 2608 '__int64'.\n\ | |
| 2532 \n\ | 2609 \n\ |
| 2533 The constructor is:\n\ | 2610 The constructor is:\n\ |
| 2534 \n\ | 2611 \n\ |
| 2535 array(typecode [, initializer]) -- create a new array\n\ | 2612 array(typecode [, initializer]) -- create a new array\n\ |
| 2536 "); | 2613 "); |
| 2537 | 2614 |
| 2538 PyDoc_STRVAR(arraytype_doc, | 2615 PyDoc_STRVAR(arraytype_doc, |
| 2539 "array(typecode [, initializer]) -> array\n\ | 2616 "array(typecode [, initializer]) -> array\n\ |
| 2540 \n\ | 2617 \n\ |
| 2541 Return a new array whose items are restricted by typecode, and\n\ | 2618 Return a new array whose items are restricted by typecode, and\n\ |
| (...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2761 } | 2838 } |
| 2762 | 2839 |
| 2763 PyModule_AddObject(m, "typecodes", (PyObject *)typecodes); | 2840 PyModule_AddObject(m, "typecodes", (PyObject *)typecodes); |
| 2764 | 2841 |
| 2765 if (PyErr_Occurred()) { | 2842 if (PyErr_Occurred()) { |
| 2766 Py_DECREF(m); | 2843 Py_DECREF(m); |
| 2767 m = NULL; | 2844 m = NULL; |
| 2768 } | 2845 } |
| 2769 return m; | 2846 return m; |
| 2770 } | 2847 } |
| OLD | NEW |