1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703
|
# mode: run
# Test the behaviour of memoryview slicing and indexing, contiguity, etc.
# Note: see also bufaccess.pyx
from __future__ import unicode_literals
from cpython.object cimport PyObject
from cpython.ref cimport Py_INCREF, Py_DECREF, Py_CLEAR
cimport cython
from cython cimport view
from cython.view cimport array
from cython.parallel cimport prange, parallel
from functools import wraps
import gc
import sys
if sys.version_info[0] < 3:
import __builtin__ as builtins
else:
import builtins
try:
from Cython.Tests.this_module_does_not_exist import *
except ImportError:
# Fails, but the existence of "import *" interacted badly with some utility code
pass
def testcase(func):
@wraps(func)
def wrapper(*args, **kwargs):
gc.collect()
result = func(*args, **kwargs)
gc.collect()
return result
return wrapper
include "../buffers/mockbuffers.pxi"
include "../testsupport/cythonarrayutil.pxi"
def _print_attributes(memview):
print "shape: " + " ".join(map(str, memview.shape))
print "strides: " + " ".join([str(stride // memview.itemsize)
for stride in memview.strides])
print "suboffsets: " + " ".join(
[str(suboffset if suboffset < 0 else suboffset // memview.itemsize)
for suboffset in memview.suboffsets])
#
# Buffer acquire and release tests
#
def nousage():
"""
The challenge here is just compilation.
"""
cdef int[:, :] buf
@testcase
def acquire_release(o1, o2):
"""
>>> A = IntMockBuffer("A", range(6))
>>> B = IntMockBuffer("B", range(6))
>>> acquire_release(A, B)
acquired A
acquired B
released A
released B
>>> acquire_release(None, B)
acquired B
released B
"""
cdef int[:] buf
buf = o1
buf = o2
@testcase
def acquire_raise(o):
"""
Apparently, doctest won't handle mixed exceptions and print
stats, so need to circumvent this.
>>> A = IntMockBuffer("A", range(6))
>>> A.resetlog()
>>> acquire_raise(A)
Traceback (most recent call last):
...
Exception: on purpose
>>> A.printlog()
acquired A
released A
"""
cdef int[:] buf
buf = o
raise Exception("on purpose")
@testcase
def acquire_failure1():
"""
>>> acquire_failure1()
acquired working
0 3
0 3
released working
"""
cdef int[:] buf
buf = IntMockBuffer("working", range(4))
print buf[0], buf[3]
try:
buf = ErrorBuffer()
assert False
except Exception:
print buf[0], buf[3]
@testcase
def acquire_failure2():
"""
>>> acquire_failure2()
acquired working
0 3
0 3
released working
"""
cdef int[:] buf = IntMockBuffer("working", range(4))
print buf[0], buf[3]
try:
buf = ErrorBuffer()
assert False
except Exception:
print buf[0], buf[3]
@testcase
def acquire_failure3():
"""
>>> acquire_failure3()
acquired working
0 3
0 3
released working
"""
cdef int[:] buf
buf = IntMockBuffer("working", range(4))
print buf[0], buf[3]
try:
buf = object()
assert False
except Exception:
print buf[0], buf[3]
@testcase
def acquire_nonbuffer1(first, second=None):
"""
>>> acquire_nonbuffer1(3) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
TypeError:... 'int'...
>>> acquire_nonbuffer1(type) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
TypeError:... 'type'...
>>> acquire_nonbuffer1(None, 2) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
TypeError:... 'int'...
>>> acquire_nonbuffer1(4, object()) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
TypeError:... 'int'...
"""
cdef int[:] buf
buf = first
buf = second
@testcase
def acquire_nonbuffer2():
"""
>>> acquire_nonbuffer2()
acquired working
0 3
0 3
released working
"""
cdef int[:] buf = IntMockBuffer("working", range(4))
print buf[0], buf[3]
try:
buf = ErrorBuffer
assert False
except Exception:
print buf[0], buf[3]
@testcase
def as_argument(int[:] bufarg, int n):
"""
>>> A = IntMockBuffer("A", range(6))
>>> as_argument(A, 6)
acquired A
0 1 2 3 4 5 END
released A
"""
cdef int i
for i in range(n):
print bufarg[i],
print 'END'
@testcase
def as_argument_defval(int[:] bufarg=IntMockBuffer('default', range(6)), int n=6):
"""
>>> as_argument_defval()
0 1 2 3 4 5 END
>>> A = IntMockBuffer("A", range(6))
>>> as_argument_defval(A, 6)
acquired A
0 1 2 3 4 5 END
released A
"""
cdef int i
for i in range(n):
print bufarg[i],
print 'END'
@testcase
def cdef_assignment(obj, n):
"""
>>> A = IntMockBuffer("A", range(6))
>>> cdef_assignment(A, 6)
acquired A
0 1 2 3 4 5 END
released A
"""
cdef int[:] buf = obj
cdef int i
for i in range(n):
print buf[i],
print 'END'
@testcase
def forin_assignment(objs, int pick):
"""
>>> A = IntMockBuffer("A", range(6))
>>> B = IntMockBuffer("B", range(6))
>>> forin_assignment([A, B, A, A], 2)
acquired A
2
acquired B
released A
2
acquired A
released B
2
acquired A
released A
2
released A
"""
cdef int[:] buf
for buf in objs:
print buf[pick]
@testcase
def cascaded_buffer_assignment(obj):
"""
>>> A = IntMockBuffer("A", range(6))
>>> cascaded_buffer_assignment(A)
acquired A
released A
"""
cdef int[:] a, b
a = b = obj
@testcase
def tuple_buffer_assignment1(a, b):
"""
>>> A = IntMockBuffer("A", range(6)) # , writable=False)
>>> B = IntMockBuffer("B", range(6)) # , writable=False)
>>> tuple_buffer_assignment1(A, B)
acquired A
acquired B
released A
released B
"""
cdef int[:] x, y
x, y = a, b
@testcase
def tuple_buffer_assignment2(tup):
"""
>>> A = IntMockBuffer("A", range(6)) # , writable=False)
>>> B = IntMockBuffer("B", range(6)) # , writable=False)
>>> tuple_buffer_assignment2((A, B))
acquired A
acquired B
released A
released B
"""
cdef int[:] x, y
x, y = tup
@testcase
def explicitly_release_buffer():
"""
>>> explicitly_release_buffer()
acquired A
released A
After release
"""
cdef int[:] x = IntMockBuffer("A", range(10)) # , writable=False)
del x
print "After release"
#
# Getting items and index bounds checking
#
@testcase
def get_int_2d(int[:, :] buf, int i, int j):
"""
>>> C = IntMockBuffer("C", range(6), (2,3))
>>> get_int_2d(C, 1, 1)
acquired C
released C
4
Check negative indexing:
>>> get_int_2d(C, -1, 0)
acquired C
released C
3
>>> get_int_2d(C, -1, -2)
acquired C
released C
4
>>> get_int_2d(C, -2, -3)
acquired C
released C
0
Out-of-bounds errors:
>>> get_int_2d(C, 2, 0)
Traceback (most recent call last):
...
IndexError: Out of bounds on buffer access (axis 0)
>>> get_int_2d(C, 0, -4)
Traceback (most recent call last):
...
IndexError: Out of bounds on buffer access (axis 1)
"""
return buf[i, j]
@testcase
def get_int_2d_uintindex(int[:, :] buf, unsigned int i, unsigned int j):
"""
Unsigned indexing:
>>> C = IntMockBuffer("C", range(6), (2,3)) # , writable=False)
>>> get_int_2d_uintindex(C, 0, 0)
acquired C
released C
0
>>> get_int_2d_uintindex(C, 1, 2)
acquired C
released C
5
"""
# This is most interesting with regards to the C code
# generated.
return buf[i, j]
@testcase
def set_int_2d(int[:, :] buf, int i, int j, int value):
"""
Uses get_int_2d to read back the value afterwards. For pure
unit test, one should support reading in MockBuffer instead.
>>> C = IntMockBuffer("C", range(6), (2,3))
>>> set_int_2d(C, 1, 1, 10)
acquired C
released C
>>> get_int_2d(C, 1, 1)
acquired C
released C
10
Check negative indexing:
>>> set_int_2d(C, -1, 0, 3)
acquired C
released C
>>> get_int_2d(C, -1, 0)
acquired C
released C
3
>>> set_int_2d(C, -1, -2, 8)
acquired C
released C
>>> get_int_2d(C, -1, -2)
acquired C
released C
8
>>> set_int_2d(C, -2, -3, 9)
acquired C
released C
>>> get_int_2d(C, -2, -3)
acquired C
released C
9
Out-of-bounds errors:
>>> set_int_2d(C, 2, 0, 19)
Traceback (most recent call last):
...
IndexError: Out of bounds on buffer access (axis 0)
>>> set_int_2d(C, 0, -4, 19)
Traceback (most recent call last):
...
IndexError: Out of bounds on buffer access (axis 1)
>>> C = IntMockBuffer("C", range(6), (2,3), writable=False)
>>> set_int_2d(C, -2, -3, 9)
Traceback (most recent call last):
BufferError: Writable buffer requested from read-only mock: FORMAT | ND | STRIDES | WRITABLE
"""
buf[i, j] = value
def _read_int2d(int[:, :] buf, int i, int j):
return buf[i, j]
@testcase
def schar_index_vars(int[:, :] buf, signed char i, signed char j, int value):
"""
>>> C = IntMockBuffer("C", range(300*300), (300, 300)) # > sizeof(char)
>>> schar_index_vars(C, 1, 1, 5)
acquired C
reading
writing
validated
released C
301
>>> _read_int2d(C, 1, 1) # validate with int indices
acquired C
released C
5
>>> schar_index_vars(C, -1, 1, 6)
acquired C
reading
writing
validated
released C
89701
>>> _read_int2d(C, -1, 1) # validate with int indices
acquired C
released C
6
>>> schar_index_vars(C, -1, -2, 7)
acquired C
reading
writing
validated
released C
89998
>>> _read_int2d(C, -1, -2) # validate with int indices
acquired C
released C
7
>>> schar_index_vars(C, -2, -3, 8)
acquired C
reading
writing
validated
released C
89697
>>> _read_int2d(C, -2, -3) # validate with int indices
acquired C
released C
8
>>> C = IntMockBuffer("C", range(6), (2, 3))
>>> schar_index_vars(C, 5, 1, 10)
Traceback (most recent call last):
IndexError: Out of bounds on buffer access (axis 0)
>>> schar_index_vars(C, 1, 5, 10)
Traceback (most recent call last):
IndexError: Out of bounds on buffer access (axis 1)
>>> schar_index_vars(C, -2, 1, 10)
acquired C
reading
writing
validated
released C
1
>>> schar_index_vars(C, -3, 1, 10)
Traceback (most recent call last):
IndexError: Out of bounds on buffer access (axis 0)
>>> schar_index_vars(C, 1, -3, 10)
acquired C
reading
writing
validated
released C
3
>>> schar_index_vars(C, 1, -4, 10)
Traceback (most recent call last):
IndexError: Out of bounds on buffer access (axis 1)
"""
print("reading")
old_value = buf[i, j]
print("writing")
buf[i, j] = value
if buf[i, j] == value:
print("validated")
return old_value
@testcase
def uchar_index_vars(int[:, :] buf, unsigned char i, unsigned char j, int value):
"""
>>> C = IntMockBuffer("C", range(300*300), (300, 300)) # > sizeof(char)
>>> uchar_index_vars(C, 1, 1, 5)
acquired C
reading
writing
validated
released C
301
>>> _read_int2d(C, 1, 1) # validate with int indices
acquired C
released C
5
>>> C = IntMockBuffer("C", range(6), (2, 3))
>>> uchar_index_vars(C, 5, 1, 10)
Traceback (most recent call last):
IndexError: Out of bounds on buffer access (axis 0)
>>> uchar_index_vars(C, 1, 5, 10)
Traceback (most recent call last):
IndexError: Out of bounds on buffer access (axis 1)
"""
print("reading")
old_value = buf[i, j]
print("writing")
buf[i, j] = value
if buf[i, j] == value:
print("validated")
return old_value
@testcase
def char_index_vars(int[:, :] buf, char i, char j, int value):
"""
>>> C = IntMockBuffer("C", range(300*300), (300, 300)) # > sizeof(char)
>>> char_index_vars(C, 1, 1, 5)
acquired C
reading
writing
validated
released C
301
>>> _read_int2d(C, 1, 1) # validate with int indices
acquired C
released C
5
>>> C = IntMockBuffer("C", range(6), (2, 3))
>>> char_index_vars(C, 5, 1, 10)
Traceback (most recent call last):
IndexError: Out of bounds on buffer access (axis 0)
>>> char_index_vars(C, 1, 5, 10)
Traceback (most recent call last):
IndexError: Out of bounds on buffer access (axis 1)
"""
print("reading")
old_value = buf[i, j]
print("writing")
buf[i, j] = value
if buf[i, j] == value:
print("validated")
return old_value
@testcase
def list_comprehension(int[:] buf, len):
"""
>>> list_comprehension(IntMockBuffer(None, [1,2,3]), 3) # , writable=False), 3)
1|2|3
"""
cdef int i
print "|".join([str(buf[i]) for i in range(len)])
@testcase
@cython.wraparound(False)
def wraparound_directive(int[:] buf, int pos_idx, int neg_idx):
"""
Again, the most interesting thing here is to inspect the C source.
>>> A = IntMockBuffer(None, range(4)) # , writable=False)
>>> wraparound_directive(A, 2, -1)
5
>>> wraparound_directive(A, -1, 2)
Traceback (most recent call last):
...
IndexError: Out of bounds on buffer access (axis 0)
"""
cdef int byneg
with cython.wraparound(True):
byneg = buf[neg_idx]
return buf[pos_idx] + byneg
#
# Test all kinds of indexing and flags
#
@testcase
def writable(obj):
"""
>>> R = UnsignedShortMockBuffer("R", range(27), shape=(3, 3, 3))
>>> writable(R)
acquired R
released R
>>> [str(x) for x in R.received_flags] # Py2/3
['FORMAT', 'ND', 'STRIDES', 'WRITABLE']
"""
cdef unsigned short int[:, :, :] buf = obj
buf[2, 2, 1] = 23
@testcase
def strided(const int[:] buf):
"""
>>> A = IntMockBuffer("A", range(4), writable=False)
>>> strided(A)
acquired A
released A
2
>>> [str(x) for x in A.received_flags] # Py2/3
['FORMAT', 'ND', 'STRIDES']
Check that the suboffsets were patched back prior to release.
>>> A.release_ok
True
"""
return buf[2]
@testcase
def c_contig(const int[::1] buf):
"""
>>> A = IntMockBuffer(None, range(4), writable=False)
>>> c_contig(A)
2
>>> [str(x) for x in A.received_flags]
['FORMAT', 'ND', 'STRIDES', 'C_CONTIGUOUS']
"""
return buf[2]
@testcase
def c_contig_2d(int[:, ::1] buf):
"""
Multi-dim has separate implementation
>>> A = IntMockBuffer(None, range(12), shape=(3,4)) # , writable=False)
>>> c_contig_2d(A)
7
>>> [str(x) for x in A.received_flags]
['FORMAT', 'ND', 'STRIDES', 'C_CONTIGUOUS', 'WRITABLE']
"""
return buf[1, 3]
@testcase
def f_contig(int[::1, :] buf):
"""
>>> A = IntMockBuffer(None, range(4), shape=(2, 2), strides=(1, 2)) # , writable=False)
>>> f_contig(A)
2
>>> [str(x) for x in A.received_flags]
['FORMAT', 'ND', 'STRIDES', 'F_CONTIGUOUS', 'WRITABLE']
"""
return buf[0, 1]
@testcase
def f_contig_2d(int[::1, :] buf):
"""
Must set up strides manually to ensure Fortran ordering.
>>> A = IntMockBuffer(None, range(12), shape=(4,3), strides=(1, 4)) # , writable=False)
>>> f_contig_2d(A)
7
>>> [str(x) for x in A.received_flags]
['FORMAT', 'ND', 'STRIDES', 'F_CONTIGUOUS', 'WRITABLE']
"""
return buf[3, 1]
@testcase
def generic(int[::view.generic, ::view.generic] buf1,
int[::view.generic, ::view.generic] buf2):
"""
>>> A = IntMockBuffer("A", [[0,1,2], [3,4,5], [6,7,8]])
>>> B = IntMockBuffer("B", [[0,1,2], [3,4,5], [6,7,8]], shape=(3, 3), strides=(1, 3))
>>> generic(A, B)
acquired A
acquired B
4
4
10
11
released A
released B
>>> [str(x) for x in A.received_flags]
['FORMAT', 'INDIRECT', 'ND', 'STRIDES', 'WRITABLE']
>>> [str(x) for x in B.received_flags]
['FORMAT', 'INDIRECT', 'ND', 'STRIDES', 'WRITABLE']
"""
print buf1[1, 1]
print buf2[1, 1]
buf1[2, -1] = 10
buf2[2, -1] = 11
print buf1[2, 2]
print buf2[2, 2]
# Note: disabled. generic_contiguous isn't very useful (you have to check suboffsets,
# might as well multiply with strides)
# def generic_contig(int[::view.generic_contiguous, :] buf1,
# int[::view.generic_contiguous, :] buf2):
# """
# >>> A = IntMockBuffer("A", [[0,1,2], [3,4,5], [6,7,8]])
# >>> B = IntMockBuffer("B", [[0,1,2], [3,4,5], [6,7,8]], shape=(3, 3), strides=(1, 3))
# >>> generic_contig(A, B)
# acquired A
# acquired B
# 4
# 4
# 10
# 11
# released A
# released B
# >>> [str(x) for x in A.received_flags]
# ['FORMAT', 'INDIRECT', 'ND', 'STRIDES', 'WRITABLE']
# >>> [str(x) for x in B.received_flags]
# ['FORMAT', 'INDIRECT', 'ND', 'STRIDES', 'WRITABLE']
# """
# print buf1[1, 1]
# print buf2[1, 1]
#
# buf1[2, -1] = 10
# buf2[2, -1] = 11
#
# print buf1[2, 2]
# print buf2[2, 2]
@testcase
def indirect_strided_and_contig(
int[::view.indirect, ::view.strided] buf1,
int[::view.indirect, ::view.contiguous] buf2):
"""
>>> A = IntMockBuffer("A", [[0,1,2], [3,4,5], [6,7,8]])
>>> B = IntMockBuffer("B", [[0,1,2], [3,4,5], [6,7,8]], shape=(3, 3), strides=(1, 3))
>>> indirect_strided_and_contig(A, B)
acquired A
acquired B
4
4
10
11
released A
released B
>>> [str(x) for x in A.received_flags]
['FORMAT', 'INDIRECT', 'ND', 'STRIDES', 'WRITABLE']
>>> [str(x) for x in B.received_flags]
['FORMAT', 'INDIRECT', 'ND', 'STRIDES', 'WRITABLE']
"""
print buf1[1, 1]
print buf2[1, 1]
buf1[2, -1] = 10
buf2[2, -1] = 11
print buf1[2, 2]
print buf2[2, 2]
@testcase
def indirect_contig(
int[::view.indirect_contiguous, ::view.contiguous] buf1,
int[::view.indirect_contiguous, ::view.generic] buf2):
"""
>>> A = IntMockBuffer("A", [[0,1,2], [3,4,5], [6,7,8]])
>>> B = IntMockBuffer("B", [[0,1,2], [3,4,5], [6,7,8]], shape=(3, 3), strides=(1, 3))
>>> indirect_contig(A, B)
acquired A
acquired B
4
4
10
11
released A
released B
>>> [str(x) for x in A.received_flags]
['FORMAT', 'INDIRECT', 'ND', 'STRIDES', 'WRITABLE']
>>> [str(x) for x in B.received_flags]
['FORMAT', 'INDIRECT', 'ND', 'STRIDES', 'WRITABLE']
"""
print buf1[1, 1]
print buf2[1, 1]
buf1[2, -1] = 10
buf2[2, -1] = 11
print buf1[2, 2]
print buf2[2, 2]
#
# Test compiler options for bounds checking. We create an array with a
# safe "boundary" (memory
# allocated outside of what it published) and then check whether we get back
# what we stored in the memory or an error.
@testcase
def safe_get(int[:] buf, int idx):
"""
>>> A = IntMockBuffer(None, range(10), shape=(3,), offset=5) # , writable=False)
Validate our testing buffer...
>>> safe_get(A, 0)
5
>>> safe_get(A, 2)
7
>>> safe_get(A, -3)
5
Access outside it. This is already done above for bounds check
testing but we include it to tell the story right.
>>> safe_get(A, -4)
Traceback (most recent call last):
...
IndexError: Out of bounds on buffer access (axis 0)
>>> safe_get(A, 3)
Traceback (most recent call last):
...
IndexError: Out of bounds on buffer access (axis 0)
"""
return buf[idx]
@testcase
@cython.boundscheck(False) # outer decorators should take precedence
@cython.boundscheck(True)
def unsafe_get(int[:] buf, int idx):
"""
Access outside of the area the buffer publishes.
>>> A = IntMockBuffer(None, range(10), shape=(3,), offset=5) # , writable=False)
>>> unsafe_get(A, -4)
4
>>> unsafe_get(A, -5)
3
>>> unsafe_get(A, 3)
8
"""
return buf[idx]
@testcase
def mixed_get(int[:] buf, int unsafe_idx, int safe_idx):
"""
>>> A = IntMockBuffer(None, range(10), shape=(3,), offset=5) # , writable=False)
>>> mixed_get(A, -4, 0)
(4, 5)
>>> mixed_get(A, 0, -4)
Traceback (most recent call last):
...
IndexError: Out of bounds on buffer access (axis 0)
"""
with cython.boundscheck(False):
one = buf[unsafe_idx]
with cython.boundscheck(True):
two = buf[safe_idx]
return (one, two)
#
# Testing that accessing data using various types of buffer access
# all works.
#
def printbuf_int(int[:] buf, shape):
# Utility func
cdef int i
for i in range(shape[0]):
print buf[i],
print 'END'
@testcase
def printbuf_int_2d(o, shape):
"""
Strided:
>>> printbuf_int_2d(IntMockBuffer("A", range(6), (2,3), writable=False), (2,3))
acquired A
0 1 2 END
3 4 5 END
released A
>>> printbuf_int_2d(IntMockBuffer("A", range(100), (3,3), strides=(20,5), writable=False), (3,3))
acquired A
0 5 10 END
20 25 30 END
40 45 50 END
released A
Indirect:
>>> printbuf_int_2d(IntMockBuffer("A", [[1,2],[3,4]], writable=False), (2,2))
acquired A
1 2 END
3 4 END
released A
"""
# should make shape builtin
cdef const int[::view.generic, ::view.generic] buf
buf = o
cdef int i, j
for i in range(shape[0]):
for j in range(shape[1]):
print buf[i, j],
print 'END'
@testcase
def printbuf_float(o, shape):
"""
>>> printbuf_float(FloatMockBuffer("F", [1.0, 1.25, 0.75, 1.0], writable=False), (4,))
acquired F
1.0 1.25 0.75 1.0 END
released F
"""
# should make shape builtin
cdef const float[:] buf
buf = o
cdef int i, j
for i in range(shape[0]):
print buf[i],
print "END"
#
# Test assignments
#
@testcase
def inplace_operators(int[:] buf):
"""
>>> buf = IntMockBuffer(None, [2, 2])
>>> inplace_operators(buf)
>>> printbuf_int(buf, (2,))
0 3 END
"""
cdef int j = 0
buf[1] += 1
buf[j] *= 2
buf[0] -= 4
#
# Typedefs
#
# Test three layers of typedefs going through a h file for plain int, and
# simply a header file typedef for floats and unsigned.
ctypedef int td_cy_int
cdef extern from "bufaccess.h":
ctypedef td_cy_int td_h_short # Defined as short, but Cython doesn't know this!
ctypedef float td_h_double # Defined as double
ctypedef unsigned int td_h_ushort # Defined as unsigned short
ctypedef td_h_short td_h_cy_short
@testcase
def printbuf_td_cy_int(td_cy_int[:] buf, shape):
"""
>>> printbuf_td_cy_int(IntMockBuffer(None, range(3)), (3,)) # , writable=False), (3,))
0 1 2 END
>>> printbuf_td_cy_int(ShortMockBuffer(None, range(3)), (3,)) # , writable=False), (3,))
Traceback (most recent call last):
...
ValueError: Buffer dtype mismatch, expected 'td_cy_int' but got 'short'
"""
cdef int i
for i in range(shape[0]):
print buf[i],
print 'END'
@testcase
def printbuf_td_h_short(td_h_short[:] buf, shape):
"""
>>> printbuf_td_h_short(ShortMockBuffer(None, range(3)), (3,)) # , writable=False), (3,))
0 1 2 END
>>> printbuf_td_h_short(IntMockBuffer(None, range(3)), (3,)) # , writable=False), (3,))
Traceback (most recent call last):
...
ValueError: Buffer dtype mismatch, expected 'td_h_short' but got 'int'
"""
cdef int i
for i in range(shape[0]):
print buf[i],
print 'END'
@testcase
def printbuf_td_h_cy_short(const td_h_cy_short[:] buf, shape):
"""
>>> printbuf_td_h_cy_short(ShortMockBuffer(None, range(3), writable=False), (3,))
0 1 2 END
>>> printbuf_td_h_cy_short(IntMockBuffer(None, range(3), writable=False), (3,))
Traceback (most recent call last):
...
ValueError: Buffer dtype mismatch, expected 'const td_h_cy_short' but got 'int'
"""
cdef int i
for i in range(shape[0]):
print buf[i],
print 'END'
@testcase
def printbuf_td_h_ushort(const td_h_ushort[:] buf, shape):
"""
>>> printbuf_td_h_ushort(UnsignedShortMockBuffer(None, range(3), writable=False), (3,))
0 1 2 END
>>> printbuf_td_h_ushort(ShortMockBuffer(None, range(3), writable=False), (3,))
Traceback (most recent call last):
...
ValueError: Buffer dtype mismatch, expected 'const td_h_ushort' but got 'short'
"""
cdef int i
for i in range(shape[0]):
print buf[i],
print 'END'
@testcase
def printbuf_td_h_double(const td_h_double[:] buf, shape):
"""
>>> printbuf_td_h_double(DoubleMockBuffer(None, [0.25, 1, 3.125], writable=False), (3,))
0.25 1.0 3.125 END
>>> printbuf_td_h_double(FloatMockBuffer(None, [0.25, 1, 3.125], writable=False), (3,))
Traceback (most recent call last):
...
ValueError: Buffer dtype mismatch, expected 'const td_h_double' but got 'float'
"""
cdef int i
for i in range(shape[0]):
print buf[i],
print 'END'
#
# Object access
#
def addref(*args):
for item in args: Py_INCREF(item)
def decref(*args):
for item in args: Py_DECREF(item)
@cython.binding(False)
@cython.always_allow_keywords(False)
def get_refcount(x):
return (<PyObject*>x).ob_refcnt
@testcase
def printbuf_object(object[:] buf, shape):
"""
Only play with unique objects, interned numbers etc. will have
unpredictable refcounts.
ObjectMockBuffer doesn't do anything about increfing/decrefing,
we to the "buffer implementor" refcounting directly in the
testcase.
>>> _x = 1
>>> a, b, c = "globally_unique_string_2323412" + "3" * _x, {4:23}, [34,3]
>>> get_refcount(a), get_refcount(b), get_refcount(c)
(2, 2, 2)
>>> A = ObjectMockBuffer(None, [a, b, c]) # , writable=False)
>>> printbuf_object(A, (3,))
'globally_unique_string_23234123' 2
{4: 23} 2
[34, 3] 2
"""
cdef int i
for i in range(shape[0]):
print repr(buf[i]), (<PyObject*>buf[i]).ob_refcnt
@testcase
def assign_to_object(object[:] buf, int idx, obj):
"""
See comments on printbuf_object above.
>>> a, b = [1, 2, 3], [4, 5, 6]
>>> get_refcount(a), get_refcount(b)
(2, 2)
>>> addref(a)
>>> A = ObjectMockBuffer(None, [1, a]) # 1, ...,otherwise it thinks nested lists...
>>> get_refcount(a), get_refcount(b)
(3, 2)
>>> assign_to_object(A, 1, b)
>>> get_refcount(a), get_refcount(b)
(2, 3)
>>> decref(b)
"""
buf[idx] = obj
@testcase
def assign_temporary_to_object(object[:] buf):
"""
See comments on printbuf_object above.
>>> a, b = [1, 2, 3], {4:23}
>>> get_refcount(a)
2
>>> addref(a)
>>> A = ObjectMockBuffer(None, [b, a])
>>> get_refcount(a)
3
>>> assign_temporary_to_object(A)
>>> get_refcount(a)
2
>>> printbuf_object(A, (2,))
{4: 23} 2
{1: 8} 2
To avoid leaking a reference in our testcase we need to
replace the temporary with something we can manually decref :-)
>>> assign_to_object(A, 1, a)
>>> decref(a)
"""
buf[1] = {3-2: 2+(2*4)-2}
@testcase
def check_object_nulled_1d(object[:] buf, int idx, obj):
"""
See comments on printbuf_object above.
>>> a = object()
>>> rc1 = get_refcount(a)
>>> A = ObjectMockBuffer(None, [a, a])
>>> check_object_nulled_1d(A, 0, a)
>>> check_object_nulled_1d(A, 1, a)
>>> A = ObjectMockBuffer(None, [a, a, a, a], strides=(2,))
>>> check_object_nulled_1d(A, 0, a) # only 0 due to stride
>>> get_refcount(a) == rc1
True
"""
cdef ObjectMockBuffer omb = buf.base
cdef PyObject **data = <PyObject**>(omb.buffer)
Py_CLEAR(data[idx])
res = buf[idx] # takes None
buf[idx] = obj
return res
@testcase
def check_object_nulled_2d(object[:, ::1] buf, int idx1, int idx2, obj):
"""
See comments on printbuf_object above.
>>> a = object()
>>> rc1 = get_refcount(a)
>>> A = ObjectMockBuffer(None, [a, a, a, a], shape=(2, 2))
>>> check_object_nulled_2d(A, 0, 0, a)
>>> check_object_nulled_2d(A, 1, 1, a)
>>> get_refcount(a) == rc1
True
"""
cdef ObjectMockBuffer omb = buf.base
cdef PyObject **data = <PyObject**>(omb.buffer)
Py_CLEAR(data[idx1 + 2*idx2])
res = buf[idx1, idx2] # takes None
buf[idx1, idx2] = obj
return res
#
# Test __cythonbufferdefaults__
#
@testcase
def bufdefaults1(int[:] buf):
"""
For IntStridedMockBuffer, mode should be
"strided" by defaults which should show
up in the flags.
>>> A = IntStridedMockBuffer("A", range(10)) # , writable=False)
>>> bufdefaults1(A)
acquired A
released A
>>> [str(x) for x in A.received_flags]
['FORMAT', 'ND', 'STRIDES', 'WRITABLE']
"""
pass
@testcase
def basic_struct(MyStruct[:] buf):
"""
See also buffmt.pyx
>>> basic_struct(MyStructMockBuffer(None, [(1, 2, 3, 4, 5)]))
1 2 3 4 5
>>> basic_struct(MyStructMockBuffer(None, [(1, 2, 3, 4, 5)], format="ccqii"))
1 2 3 4 5
"""
print buf[0].a, buf[0].b, buf[0].c, buf[0].d, buf[0].e
@testcase
def const_struct(const MyStruct[:] buf):
"""
See also buffmt.pyx
>>> const_struct(MyStructMockBuffer(None, [(1, 2, 3, 4, 5)], writable=False))
1 2 3 4 5
>>> const_struct(MyStructMockBuffer(None, [(1, 2, 3, 4, 5)], format="ccqii", writable=False))
1 2 3 4 5
"""
print buf[0].a, buf[0].b, buf[0].c, buf[0].d, buf[0].e
@testcase
def nested_struct(NestedStruct[:] buf):
"""
See also buffmt.pyx
>>> nested_struct(NestedStructMockBuffer(None, [(1, 2, 3, 4, 5)]))
1 2 3 4 5
>>> nested_struct(NestedStructMockBuffer(None, [(1, 2, 3, 4, 5)], format="T{ii}T{2i}i"))
1 2 3 4 5
"""
print buf[0].x.a, buf[0].x.b, buf[0].y.a, buf[0].y.b, buf[0].z
@testcase
def const_nested_struct(const NestedStruct[:] buf):
"""
See also buffmt.pyx
>>> const_nested_struct(NestedStructMockBuffer(None, [(1, 2, 3, 4, 5)], writable=False))
1 2 3 4 5
>>> const_nested_struct(NestedStructMockBuffer(None, [(1, 2, 3, 4, 5)], format="T{ii}T{2i}i", writable=False))
1 2 3 4 5
"""
print buf[0].x.a, buf[0].x.b, buf[0].y.a, buf[0].y.b, buf[0].z
@testcase
def packed_struct(PackedStruct[:] buf):
"""
See also buffmt.pyx
>>> packed_struct(PackedStructMockBuffer(None, [(1, 2)]))
1 2
>>> packed_struct(PackedStructMockBuffer(None, [(1, 2)], format="T{c^i}"))
1 2
>>> packed_struct(PackedStructMockBuffer(None, [(1, 2)], format="T{c=i}"))
1 2
"""
print buf[0].a, buf[0].b
@testcase
def const_packed_struct(const PackedStruct[:] buf):
"""
See also buffmt.pyx
>>> const_packed_struct(PackedStructMockBuffer(None, [(1, 2)], writable=False))
1 2
>>> const_packed_struct(PackedStructMockBuffer(None, [(1, 2)], format="T{c^i}", writable=False))
1 2
>>> const_packed_struct(PackedStructMockBuffer(None, [(1, 2)], format="T{c=i}", writable=False))
1 2
"""
print buf[0].a, buf[0].b
@testcase
def nested_packed_struct(NestedPackedStruct[:] buf):
"""
See also buffmt.pyx
>>> nested_packed_struct(NestedPackedStructMockBuffer(None, [(1, 2, 3, 4, 5)]))
1 2 3 4 5
>>> nested_packed_struct(NestedPackedStructMockBuffer(None, [(1, 2, 3, 4, 5)], format="ci^ci@i"))
1 2 3 4 5
>>> nested_packed_struct(NestedPackedStructMockBuffer(None, [(1, 2, 3, 4, 5)], format="^c@i^ci@i"))
1 2 3 4 5
"""
print buf[0].a, buf[0].b, buf[0].sub.a, buf[0].sub.b, buf[0].c
@testcase
def const_nested_packed_struct(const NestedPackedStruct[:] buf):
"""
See also buffmt.pyx
>>> const_nested_packed_struct(NestedPackedStructMockBuffer(None, [(1, 2, 3, 4, 5)], writable=False))
1 2 3 4 5
>>> const_nested_packed_struct(NestedPackedStructMockBuffer(None, [(1, 2, 3, 4, 5)], format="ci^ci@i", writable=False))
1 2 3 4 5
>>> const_nested_packed_struct(NestedPackedStructMockBuffer(None, [(1, 2, 3, 4, 5)], format="^c@i^ci@i", writable=False))
1 2 3 4 5
"""
print buf[0].a, buf[0].b, buf[0].sub.a, buf[0].sub.b, buf[0].c
@testcase
def complex_dtype(long double complex[:] buf):
"""
>>> complex_dtype(LongComplexMockBuffer(None, [(0, -1)])) # , writable=False))
-1j
"""
print buf[0]
@testcase
def complex_inplace(long double complex[:] buf):
"""
>>> complex_inplace(LongComplexMockBuffer(None, [(0, -1)]))
(1+1j)
"""
buf[0] = buf[0] + 1 + 2j
print buf[0]
@testcase
def complex_struct_dtype(LongComplex[:] buf):
"""
Note that the format string is "Zg" rather than "2g", yet a struct
is accessed.
>>> complex_struct_dtype(LongComplexMockBuffer(None, [(0, -1)])) # , writable=False))
0.0 -1.0
"""
print buf[0].real, buf[0].imag
@testcase
def complex_struct_inplace(LongComplex[:] buf):
"""
>>> complex_struct_inplace(LongComplexMockBuffer(None, [(0, -1)]))
1.0 1.0
"""
buf[0].real += 1
buf[0].imag += 2
print buf[0].real, buf[0].imag
#
# Nogil
#
@testcase
@cython.boundscheck(False)
def buffer_nogil():
"""
>>> buffer_nogil()
(10, 10)
"""
cdef int[:] buf = IntMockBuffer(None, [1,2,3])
cdef int[:] buf2 = IntMockBuffer(None, [4,5,6])
with nogil:
buf[1] = 10
buf2 = buf
return buf[1], buf2[1]
#
### Test cdef functions
#
class UniqueObject(object):
def __init__(self, value):
self.value = value
def __repr__(self):
return self.value
objs = [[UniqueObject("spam")], [UniqueObject("ham")], [UniqueObject("eggs")]]
addref(*[obj for L in objs for obj in L])
cdef cdef_function(int[:] buf1, object[::view.indirect, :] buf2 = ObjectMockBuffer(None, objs)):
print 'cdef called'
print buf1[6], buf2[1, 0]
buf2[1, 0] = UniqueObject("eggs")
@testcase
def test_cdef_function(o1, o2=None):
"""
>>> A = IntMockBuffer("A", range(10))
>>> test_cdef_function(A)
acquired A
cdef called
6 ham
released A
acquired A
cdef called
6 eggs
released A
>>> L = [[x] for x in range(25)]
>>> addref(*[obj for mylist in L for obj in mylist])
>>> B = ObjectMockBuffer("B", L, shape=(5, 5))
>>> test_cdef_function(A, B)
acquired A
cdef called
6 eggs
released A
acquired A
cdef called
6 eggs
released A
acquired A
acquired B
cdef called
6 1
released A
released B
"""
cdef_function(o1)
cdef_function(o1)
if o2:
cdef_function(o1, o2)
cdef int[:] global_A = IntMockBuffer("Global_A", range(10))
addref(*[obj for L in objs for obj in L])
cdef object[::view.indirect, :] global_B = ObjectMockBuffer(None, objs)
cdef cdef_function2(int[:] buf1, object[::view.indirect, :] buf2 = global_B):
print 'cdef2 called'
print buf1[6], buf2[1, 0]
buf2[1, 0] = UniqueObject("eggs")
@testcase
def test_cdef_function2():
"""
>>> test_cdef_function2()
cdef2 called
6 ham
eggs
cdef2 called
6 eggs
"""
cdef int[:] A = global_A
cdef object[::view.indirect, :] B = global_B
cdef_function2(A, B)
del A
del B
print global_B[1, 0]
cdef_function2(global_A, global_B)
@testcase
def test_generic_slicing(arg, indirect=False):
"""
Test simple slicing
>>> test_generic_slicing(IntMockBuffer("A", range(8 * 14 * 11), shape=(8, 14, 11))) # , writable=False))
acquired A
3 9 2
308 -11 1
-1 -1 -1
released A
Test direct slicing, negative slice oob in dim 2
>>> test_generic_slicing(IntMockBuffer("A", range(1 * 2 * 3), shape=(1, 2, 3))) # , writable=False))
acquired A
0 0 2
12 -3 1
-1 -1 -1
released A
Test indirect slicing
>>> test_generic_slicing(IntMockBuffer("A", shape_5_3_4_list, shape=(5, 3, 4)), indirect=True) # , writable=False), indirect=True)
acquired A
2 0 2
0 1 -1
released A
>>> test_generic_slicing(IntMockBuffer("A", shape_9_14_21_list, shape=(9, 14, 21)), indirect=True) # , writable=False), indirect=True)
acquired A
3 9 2
10 1 -1
released A
"""
cdef int[::view.generic, ::view.generic, :] a = arg
cdef int[::view.generic, ::view.generic, :] b = a[2:8:2, -4:1:-1, 1:3]
print b.shape[0], b.shape[1], b.shape[2]
if indirect:
print b.suboffsets[0] // sizeof(int *),
print b.suboffsets[1] // sizeof(int),
print b.suboffsets[2]
else:
print_int_offsets(b.strides[0], b.strides[1], b.strides[2])
print_int_offsets(b.suboffsets[0], b.suboffsets[1], b.suboffsets[2])
cdef int i, j, k
for i in range(b.shape[0]):
for j in range(b.shape[1]):
for k in range(b.shape[2]):
itemA = a[2 + 2 * i, -4 - j, 1 + k]
itemB = b[i, j, k]
assert itemA == itemB, (i, j, k, itemA, itemB)
@testcase
def test_indirect_slicing(arg):
"""
Test indirect slicing
>>> test_indirect_slicing(IntMockBuffer("A", shape_5_3_4_list, shape=(5, 3, 4))) # , writable=False))
acquired A
5 3 2
0 0 -1
58
56
58
index away indirect
58
58
index away generic
58
58
released A
>>> test_indirect_slicing(IntMockBuffer("A", shape_9_14_21_list, shape=(9, 14, 21))) # , writable=False))
acquired A
5 14 3
0 16 -1
2412
2410
2412
index away indirect
2412
2412
index away generic
2412
2412
released A
"""
cdef int[::view.indirect, ::view.indirect, :] a = arg
cdef int[::view.indirect, ::view.indirect, :] b = a[-5:, ..., -5:100:2]
cdef int[::view.generic , :: view.generic, :] generic_b = a[-5:, ..., -5:100:2]
cdef int[::view.indirect, ::view.indirect] c = b[..., 0]
# try indexing away leading indirect dimensions
cdef int[::view.indirect, :] d = b[4]
cdef int[:] e = b[4, 2]
cdef int[::view.generic, :] generic_d = generic_b[4]
cdef int[:] generic_e = generic_b[4, 2]
print b.shape[0], b.shape[1], b.shape[2]
print b.suboffsets[0] // sizeof(int *),
print b.suboffsets[1] // sizeof(int),
print b.suboffsets[2]
print b[4, 2, 1]
print c[4, 2]
# test adding offset from last dimension to suboffset
print b[..., 1][4, 2]
print "index away indirect"
print d[2, 1]
print e[1]
print "index away generic"
print generic_d[2, 1]
print generic_e[1]
cdef class TestIndexSlicingDirectIndirectDims(object):
"Test a int[:, ::view.indirect, :] slice"
cdef Py_ssize_t[3] shape, strides, suboffsets
cdef int[5] c_array
cdef int *myarray[5][5]
cdef bytes format
def __init__(self):
cdef int i
self.c_array[3] = 20
self.myarray[1][2] = self.c_array
for i in range(3):
self.shape[i] = 5
self.strides[0] = sizeof(int *) * 5
self.strides[1] = sizeof(int *)
self.strides[2] = sizeof(int)
self.suboffsets[0] = -1
self.suboffsets[1] = 0
self.suboffsets[2] = -1
self.format = b"i"
def __getbuffer__(self, Py_buffer *info, int flags):
info.buf = <void *> self.myarray
info.len = 5 * 5 * 5
info.ndim = 3
info.shape = self.shape
info.strides = self.strides
info.suboffsets = self.suboffsets
info.itemsize = sizeof(int)
info.readonly = 0
info.obj = self
info.format = self.format
@testcase
def test_index_slicing_away_direct_indirect():
"""
>>> test_index_slicing_away_direct_indirect()
20
20
20
20
<BLANKLINE>
20
20
20
20
All dimensions preceding dimension 1 must be indexed and not sliced
"""
cdef int[:, ::view.indirect, :] a = TestIndexSlicingDirectIndirectDims()
cdef object a_obj = a
print a[1][2][3]
print a[1, 2, 3]
print a[1, 2][3]
print a[..., 3][1, 2]
print
print a_obj[1][2][3]
print a_obj[1, 2, 3]
print a_obj[1, 2][3]
print a_obj[..., 3][1, 2]
try:
print a_obj[1:, 2][3]
except IndexError, e:
print e.args[0]
@testcase
def test_direct_slicing(arg):
"""
Fused types would be convenient to test this stuff!
Test simple slicing
>>> test_direct_slicing(IntMockBuffer("A", range(8 * 14 * 11), shape=(8, 14, 11))) # , writable=False))
acquired A
3 9 2
308 -11 1
-1 -1 -1
released A
Test direct slicing, negative slice oob in dim 2
>>> test_direct_slicing(IntMockBuffer("A", range(1 * 2 * 3), shape=(1, 2, 3))) # , writable=False))
acquired A
0 0 2
12 -3 1
-1 -1 -1
released A
"""
cdef int[:, :, ::1] a = arg
cdef int[:, :, :] b = a[2:8:2, -4:1:-1, 1:3]
print b.shape[0], b.shape[1], b.shape[2]
print_int_offsets(b.strides[0], b.strides[1], b.strides[2])
print_int_offsets(b.suboffsets[0], b.suboffsets[1], b.suboffsets[2])
cdef int i, j, k
for i in range(b.shape[0]):
for j in range(b.shape[1]):
for k in range(b.shape[2]):
itemA = a[2 + 2 * i, -4 - j, 1 + k]
itemB = b[i, j, k]
assert itemA == itemB, (i, j, k, itemA, itemB)
@testcase
def test_slicing_and_indexing(arg):
"""
>>> a = IntStridedMockBuffer("A", range(10 * 3 * 5), shape=(10, 3, 5)) # , writable=False)
>>> test_slicing_and_indexing(a)
acquired A
5 2
15 2
126 113
[111]
released A
"""
cdef int[:, :, :] a = arg
cdef int[:, :] b = a[-5:, 1, 1::2]
cdef int[:, :] c = b[4:1:-1, ::-1]
cdef int[:] d = c[2, 1:2]
print b.shape[0], b.shape[1]
print_int_offsets(b.strides[0], b.strides[1])
cdef int i, j
for i in range(b.shape[0]):
for j in range(b.shape[1]):
itemA = a[-5 + i, 1, 1 + 2 * j]
itemB = b[i, j]
assert itemA == itemB, (i, j, itemA, itemB)
print c[1, 1], c[2, 0]
print [d[i] for i in range(d.shape[0])]
@testcase
def test_oob():
"""
>>> test_oob()
Traceback (most recent call last):
...
IndexError: Index out of bounds (axis 1)
"""
cdef int[:, :] a = IntMockBuffer("A", range(4 * 9), shape=(4, 9)) # , writable=False)
print a[:, 20]
cdef int nogil_oob(int[:, :] a) except 0 nogil:
a[100, 9:]
return 1
@testcase
def test_nogil_oob1():
"""
A is acquired at the beginning of the function and released at the end.
B is acquired as a temporary and as such is immediately released in the
except clause.
>>> test_nogil_oob1()
acquired A
acquired B
released B
Index out of bounds (axis 0)
Index out of bounds (axis 0)
released A
"""
cdef int[:, :] a = IntMockBuffer("A", range(4 * 9), shape=(4, 9))
try:
nogil_oob(IntMockBuffer("B", range(4 * 9), shape=(4, 9)))
except IndexError, e:
print e.args[0]
try:
with nogil:
nogil_oob(a)
except IndexError, e:
print e.args[0]
@testcase
def test_nogil_oob2():
"""
>>> test_nogil_oob2()
Traceback (most recent call last):
...
IndexError: Index out of bounds (axis 0)
"""
cdef int[:, :] a = IntMockBuffer("A", range(4 * 9), shape=(4, 9)) # , writable=False)
with nogil:
a[100, 9:]
@cython.boundscheck(False)
cdef int cdef_nogil(int[:, :] a) except 0 nogil:
cdef int i, j
cdef int[:, :] b = a[::-1, 3:10:2]
for i in range(b.shape[0]):
for j in range(b.shape[1]):
b[i, j] = -b[i, j]
return len(a)
@testcase
def test_nogil():
"""
>>> test_nogil()
acquired A
released A
acquired A
-25
released A
"""
_a = IntMockBuffer("A", range(4 * 9), shape=(4, 9))
assert cdef_nogil(_a) == 4
cdef int[:, :] a = _a
print a[2, 7]
cdef int length
with nogil:
length = cdef_nogil(a)
assert length == 4
@testcase
def test_convert_slicenode_to_indexnode():
"""
When indexing with a[i:j] a SliceNode gets created instead of an IndexNode, which
forces coercion to object and back. This would not only be inefficient, but it would
also not compile in nogil mode. So instead we mutate it into an IndexNode.
>>> test_convert_slicenode_to_indexnode()
acquired A
2
released A
"""
cdef int[:] a = IntMockBuffer("A", range(10), shape=(10,)) # , writable=False)
with nogil:
a = a[2:4]
print a[0]
@testcase
@cython.boundscheck(False)
@cython.wraparound(False)
def test_memslice_prange(arg):
"""
>>> test_memslice_prange(IntMockBuffer("A", range(400), shape=(20, 4, 5))) # FIXME: , writable=False))
acquired A
released A
>>> test_memslice_prange(IntMockBuffer("A", range(200), shape=(100, 2, 1))) # FIXME: , writable=False))
acquired A
released A
"""
cdef int[:, :, :] src, dst
src = arg
dst = array((<object> src).shape, sizeof(int), format="i")
cdef int i, j, k
for i in prange(src.shape[0], nogil=True):
for j in range(src.shape[1]):
for k in range(src.shape[2]):
dst[i, j, k] = src[i, j, k]
for i in range(src.shape[0]):
for j in range(src.shape[1]):
for k in range(src.shape[2]):
assert src[i, j, k] == dst[i, j, k], (src[i, j, k] == dst[i, j, k])
@testcase
def test_clean_temps_prange(int[:, :] buf):
"""
Try to access a buffer out of bounds in a parallel section, and make sure any
temps used by the slicing processes are correctly counted.
>>> A = IntMockBuffer("A", range(100), (10, 10))
>>> test_clean_temps_prange(A)
acquired A
released A
"""
cdef int i
try:
for i in prange(buf.shape[0], nogil=True):
buf[1:10, 20] = 0
except IndexError:
pass
@testcase
def test_clean_temps_parallel(int[:, :] buf):
"""
Try to access a buffer out of bounds in a parallel section, and make sure any
temps used by the slicing processes are correctly counted.
>>> A = IntMockBuffer("A", range(100), (10, 10))
>>> test_clean_temps_parallel(A)
acquired A
released A
"""
cdef int i
try:
with nogil, parallel():
try:
with gil: pass
for i in prange(buf.shape[0]):
buf[1:10, 20] = 0
finally:
buf[1:10, 20] = 0
except IndexError:
pass
# Test arrays in structs
cdef struct ArrayStruct:
int ints[10]
char chars[3]
cdef packed struct PackedArrayStruct:
int ints[10]
char chars[3]
cdef fused FusedStruct:
ArrayStruct
PackedArrayStruct
@testcase
def test_memslice_struct_with_arrays():
"""
>>> test_memslice_struct_with_arrays()
abc
abc
"""
cdef ArrayStruct[10] a1
cdef PackedArrayStruct[10] a2
test_structs_with_arr(a1)
test_structs_with_arr(a2)
cdef test_structs_with_arr(FusedStruct array[10]):
cdef FusedStruct[:] myslice1, myslice2, myslice3, myslice4
cdef int i, j
myslice1 = <FusedStruct[:10]> array
for i in range(10):
for j in range(10):
myslice1[i].ints[j] = i
for j in range(3):
myslice1[i].chars[j] = 97 + j
if (2, 7) <= sys.version_info[:2] < (3, 3):
size1 = <Py_ssize_t>sizeof(FusedStruct)
size2 = len(builtins.memoryview(myslice1)[0])
assert size1 == size2, (size1, size2, builtins.memoryview(myslice1).format)
myslice2 = builtins.memoryview(myslice1)
for i in range(10):
assert myslice2[i].ints[i] == myslice1[i].ints[i]
assert myslice2[i].chars[i] == myslice1[i].chars[i]
myslice3 = <object> myslice1
myslice4 = myslice1
for i in range(10):
for j in range(10):
assert myslice3[i].ints[j] == myslice4[i].ints[j] == myslice1[i].ints[j]
for j in range(3):
assert myslice3[i].chars[j] == myslice4[i].chars[j] == myslice1[i].chars[j]
print myslice1[0].chars[:3].decode('ascii')
cdef struct TestAttrs:
int int_attrib
char char_attrib
@testcase
def test_struct_attributes_format():
"""
>>> test_struct_attributes_format()
T{i:int_attrib:c:char_attrib:}
"""
cdef TestAttrs[10] array
cdef TestAttrs[:] struct_memview = array
print builtins.memoryview(struct_memview).format
# Test padding at the end of structs in the buffer support
cdef struct PaddedAtEnd:
int a[3]
char b[3]
cdef struct AlignedNested:
PaddedAtEnd a
char chars[1]
cdef struct PaddedAtEndNormal:
int a
char b
char c
char d
cdef struct AlignedNestedNormal:
PaddedAtEndNormal a
char chars
# Test nested structs in a struct, make sure we compute padding each time
# accordingly. If the first struct member is a struct, align on the first
# member of that struct (recursively)
cdef struct A:
double d
char c
cdef struct B:
char c1
A a
char c2
cdef struct C:
A a
char c1
cdef struct D:
B b
C cstruct
int a[2]
char c
cdef fused FusedPadded:
ArrayStruct
PackedArrayStruct
AlignedNested
AlignedNestedNormal
A
B
C
D
@testcase
def test_padded_structs():
"""
>>> test_padded_structs()
"""
cdef ArrayStruct[10] a1
cdef PackedArrayStruct[10] a2
cdef AlignedNested[10] a3
cdef AlignedNestedNormal[10] a4
cdef A[10] a5
cdef B[10] a6
cdef C[10] a7
cdef D[10] a8
_test_padded(a1)
_test_padded(a2)
_test_padded(a3)
_test_padded(a4)
_test_padded(a5)
_test_padded(a6)
_test_padded(a7)
# There is a pre-existing bug that doesn't parse the format for this
# struct properly -- fix this
#_test_padded(a8)
cdef _test_padded(FusedPadded myarray[10]):
# test that the buffer format parser accepts our format string...
cdef FusedPadded[:] myslice = <FusedPadded[:10]> myarray
obj = myslice
cdef FusedPadded[:] myotherslice = obj
@testcase
def test_object_indices():
"""
>>> test_object_indices()
0
1
2
"""
cdef int[3] array
cdef int[:] myslice = array
cdef int j
for i in range(3):
myslice[i] = i
for j in range(3):
print myslice[j]
cdef fused slice_1d:
object
int[:]
cdef fused slice_2d:
object
int[:, :]
@testcase
def test_ellipsis_expr():
"""
>>> test_ellipsis_expr()
8
8
"""
cdef int[10] a
cdef int[:] m = a
_test_ellipsis_expr(m)
_test_ellipsis_expr(<object> m)
cdef _test_ellipsis_expr(slice_1d m):
m[4] = 8
m[...] = m[...]
print m[4]
@testcase
def test_slice_assignment():
"""
>>> test_slice_assignment()
"""
cdef int[10][100] carray
cdef int i, j
for i in range(10):
for j in range(100):
carray[i][j] = i * 100 + j
cdef int[:, :] m = carray
cdef int[:, :] copy = m[-6:-1, 60:65].copy()
_test_slice_assignment(m, copy)
_test_slice_assignment(<object> m, <object> copy)
cdef _test_slice_assignment(slice_2d m, slice_2d copy):
cdef int i, j
m[...] = m[::-1, ::-1]
m[:, :] = m[::-1, ::-1]
m[-5:, -5:] = m[-6:-1, 60:65]
for i in range(5):
for j in range(5):
assert copy[i, j] == m[-5 + i, -5 + j], (copy[i, j], m[-5 + i, -5 + j])
@testcase
def test_slice_assignment_broadcast_leading():
"""
>>> test_slice_assignment_broadcast_leading()
"""
cdef int[1][10] array1
cdef int[10] array2
cdef int i
for i in range(10):
array1[0][i] = i
cdef int[:, :] a = array1
cdef int[:] b = array2
_test_slice_assignment_broadcast_leading(a, b)
for i in range(10):
array1[0][i] = i
_test_slice_assignment_broadcast_leading(<object> a, <object> b)
cdef _test_slice_assignment_broadcast_leading(slice_2d a, slice_1d b):
cdef int i
b[:] = a[:, :]
b = b[::-1]
a[:, :] = b[:]
for i in range(10):
assert a[0, i] == b[i] == 10 - 1 - i, (a[0, i], b[i], 10 - 1 - i)
@testcase
def test_slice_assignment_broadcast_strides():
"""
>>> test_slice_assignment_broadcast_strides()
"""
cdef int[10] src_array
cdef int[10][5] dst_array
cdef int i, j
for i in range(10):
src_array[i] = 10 - 1 - i
cdef int[:] src = src_array
cdef int[:, :] dst = dst_array
cdef int[:, :] dst_f = dst.copy_fortran()
_test_slice_assignment_broadcast_strides(src, dst, dst_f)
_test_slice_assignment_broadcast_strides(<object> src, <object> dst, <object> dst_f)
cdef _test_slice_assignment_broadcast_strides(slice_1d src, slice_2d dst, slice_2d dst_f):
cdef int i, j
dst[1:] = src[-1:-6:-1]
dst_f[1:] = src[-1:-6:-1]
for i in range(1, 10):
for j in range(1, 5):
assert dst[i, j] == dst_f[i, j] == j, (dst[i, j], dst_f[i, j], j)
# test overlapping memory with broadcasting
dst[:, 1:4] = dst[1, :3]
dst_f[:, 1:4] = dst[1, 1:4]
for i in range(10):
for j in range(1, 3):
assert dst[i, j] == dst_f[i, j] == j - 1, (dst[i, j], dst_f[i, j], j - 1)
@testcase
def test_borrowed_slice():
"""
Test the difference between borrowed an non-borrowed slices. If you delete or assign
to a slice in a cdef function, it is not borrowed.
>>> test_borrowed_slice()
5
5
5
"""
cdef int i
cdef int[10] carray
carray[:] = range(10)
_borrowed(carray)
_not_borrowed(carray)
_not_borrowed2(carray)
cdef _borrowed(int[:] m):
print m[5]
cdef _not_borrowed(int[:] m):
print m[5]
if object():
del m
cdef _not_borrowed2(int[:] m):
cdef int[10] carray
print m[5]
if object():
m = carray
class SingleObject(object):
def __init__(self, value):
self.value = value
def __str__(self):
return str(self.value)
def __eq__(self, other):
return self.value == getattr(other, 'value', None) or self.value == other
cdef _get_empty_object_slice(fill=None):
cdef array a = array((10,), sizeof(PyObject *), 'O')
assert a.dtype_is_object
return a
@testcase
def test_object_dtype_copying():
"""
>>> test_object_dtype_copying()
0
1
2
3
4
5
6
7
8
9
5
1 5
"""
cdef int i
unique = object()
unique_refcount = get_refcount(unique)
cdef object[:] m1 = _get_empty_object_slice()
cdef object[:] m2 = _get_empty_object_slice()
for i in range(10):
m1[i] = SingleObject(i)
m2[...] = m1
del m1
for i in range(10):
print m2[i]
obj = m2[5]
refcount1 = get_refcount(obj)
print obj
del m2
refcount2 = get_refcount(obj)
print refcount1 - refcount2, obj
assert unique_refcount == get_refcount(unique), (unique_refcount, get_refcount(unique))
@testcase
def test_scalar_slice_assignment():
"""
>>> test_scalar_slice_assignment()
0
1
6
3
6
5
6
7
6
9
<BLANKLINE>
0
1
6
3
6
5
6
7
6
9
"""
cdef int[10] a
cdef int[:] m = a
cdef int[5][10] a2
cdef int[:, ::1] m2 = a2
_test_scalar_slice_assignment(m, m2)
print
_test_scalar_slice_assignment(<object> m, <object> m2)
cdef _test_scalar_slice_assignment(slice_1d m, slice_2d m2):
cdef int i, j
for i in range(10):
m[i] = i
m[-2:0:-2] = 6
for i in range(10):
print m[i]
for i in range(m2.shape[0]):
for j in range(m2.shape[1]):
m2[i, j] = i * m2.shape[1] + j
cdef int x = 2, y = -2
cdef long value = 1
m2[::2, ::-1] = value
m2[-2::-2, ::-1] = 2
m2[::2, -2::-2] = 0
m2[-2::-2, -2::-2] = 0
cdef int[:, :] s = m2[..., 1::2]
for i in range(s.shape[0]):
for j in range(s.shape[1]):
assert s[i, j] == i % 2 + 1, (s[i, j], i)
s = m2[::2, 1::2]
for i in range(s.shape[0]):
for j in range(s.shape[1]):
assert s[i, j] == 1, s[i, j]
s = m2[1::2, ::2]
for i in range(s.shape[0]):
for j in range(s.shape[1]):
assert s[i, j] == 0, s[i, j]
m2[...] = 3
for i in range(m2.shape[0]):
for j in range(m2.shape[1]):
assert m2[i, j] == 3, s[i, j]
@testcase
def test_contig_scalar_to_slice_assignment():
"""
>>> test_contig_scalar_to_slice_assignment()
14 14 14 14
30 14 30 14
20 20 20 20
30 30 20 20
"""
cdef int[5][10] a
cdef int[:, ::1] m = a
m[...] = 14
print m[0, 0], m[-1, -1], m[3, 2], m[4, 9]
m[:, :1] = 30
print m[0, 0], m[0, 1], m[1, 0], m[1, 1]
m[:, :] = 20
print m[0, 0], m[-1, -1], m[3, 2], m[4, 9]
m[:1, :] = 30
print m[0, 0], m[0, 1], m[1, 0], m[1, 1]
@testcase
def test_dtype_object_scalar_assignment():
"""
>>> test_dtype_object_scalar_assignment()
"""
cdef object[:] m = array((10,), sizeof(PyObject *), 'O')
m[:] = SingleObject(2)
assert m[0] == m[4] == m[-1] == 2
(<object> m)[:] = SingleObject(3)
assert m[0] == m[4] == m[-1] == 3
#
### Test slices that are set to None
#
# for none memoryview slice attribute testing, slicing, indexing, etc, see
# nonecheck.pyx
@testcase
def test_coerce_to_from_None(double[:] m1, double[:] m2 = None):
"""
>>> test_coerce_to_from_None(None)
(None, None)
>>> test_coerce_to_from_None(None, None)
(None, None)
"""
return m1, m2
@testcase
def test_noneslice_compare(double[:] m):
"""
>>> test_noneslice_compare(None)
(True, True)
"""
with cython.nonecheck(True):
result = m is None
return result, m is None
cdef class NoneSliceAttr(object):
cdef double[:] m
@testcase
def test_noneslice_ext_attr():
"""
>>> test_noneslice_ext_attr()
AttributeError Memoryview is not initialized
None
"""
cdef NoneSliceAttr obj = NoneSliceAttr()
with cython.nonecheck(True):
try: print obj.m
except Exception, e: print type(e).__name__, e.args[0]
obj.m = None
print obj.m
@testcase
def test_noneslice_del():
"""
>>> test_noneslice_del()
Traceback (most recent call last):
...
UnboundLocalError: local variable 'm' referenced before assignment
"""
cdef int[10] a
cdef int[:] m = a
with cython.nonecheck(True):
m = None
del m
print m
@testcase
def test_noneslice_nogil_check_none(double[:] m):
"""
>>> test_noneslice_nogil_check_none(None)
(True, False)
"""
cdef bint is_none = False
cdef bint not_none = True
with nogil:
is_none = m is None and None is m and m == None and None == m
not_none = m is not None and None is not m and m != None and None != m
return is_none, not_none
@testcase
def test_noneslice_not_none(double[:] m not None):
"""
>>> test_noneslice_not_none(None)
Traceback (most recent call last):
TypeError: Argument 'm' must not be None
"""
def get_int():
return 10
@testcase
def test_inplace_assignment():
"""
>>> test_inplace_assignment()
10
"""
cdef int[10] a
cdef int[:] m = a
m[0] = get_int()
print m[0]
@testcase
def test_newaxis(int[:] one_D):
"""
>>> A = IntMockBuffer("A", range(6)) # , writable=False)
>>> test_newaxis(A)
acquired A
3
3
3
3
released A
"""
cdef int[:, :] two_D_1 = one_D[None]
cdef int[:, :] two_D_2 = one_D[None, :]
cdef int[:, :] two_D_3 = one_D[:, None]
cdef int[:, :] two_D_4 = one_D[..., None]
print two_D_1[0, 3]
print two_D_2[0, 3]
print two_D_3[3, 0]
print two_D_4[3, 0]
@testcase
def test_newaxis2(int[:, :] two_D):
"""
>>> A = IntMockBuffer("A", range(6), shape=(3, 2)) # , writable=False)
>>> test_newaxis2(A)
acquired A
shape: 3 1 1
strides: 2 0 0
suboffsets: -1 -1 -1
<BLANKLINE>
shape: 1 2 1
strides: 0 1 0
suboffsets: -1 -1 -1
<BLANKLINE>
shape: 3 1 1 1
strides: 2 0 1 0
suboffsets: -1 -1 -1 -1
<BLANKLINE>
shape: 1 2 2 1
strides: 0 2 1 0
suboffsets: -1 -1 -1 -1
released A
"""
cdef int[:, :, :] a = two_D[..., None, 1, None]
cdef int[:, :, :] b = two_D[None, 1, ..., None]
cdef int[:, :, :, :] c = two_D[..., None, 1:, None]
cdef int[:, :, :, :] d = two_D[None, 1:, ..., None]
_print_attributes(a)
print
_print_attributes(b)
print
_print_attributes(c)
print
_print_attributes(d)
@testcase
def test_const_buffer(const int[:] a):
"""
>>> A = IntMockBuffer("A", range(6), shape=(6,), writable=False)
>>> test_const_buffer(A)
acquired A
0
5
released A
"""
cdef const int[:] c = a
print(a[0])
print(c[-1])
@testcase
def test_loop(int[:] a, throw_exception):
"""
>>> A = IntMockBuffer("A", range(6), shape=(6,))
>>> test_loop(A, False)
acquired A
15
released A
>>> try:
... test_loop(A, True)
... except ValueError:
... pass
acquired A
released A
"""
cdef int sum = 0
for ai in a:
sum += ai
if throw_exception:
raise ValueError()
print(sum)
@testcase
def test_loop_reassign(int[:] a):
"""
>>> A = IntMockBuffer("A", range(6), shape=(6,))
>>> test_loop_reassign(A)
acquired A
0
1
2
3
4
5
15
released A
"""
cdef int sum = 0
for ai in a:
sum += ai
print(ai)
a = None # this should not mess up the loop though!
print(sum)
# release happens in the wrapper function
@testcase
def test_arg_in_closure(int [:] a):
"""
>>> A = IntMockBuffer("A", range(6), shape=(6,))
>>> inner = test_arg_in_closure(A)
acquired A
>>> inner()
(0, 1)
The assignment below is just to avoid printing what was collected
>>> del inner; ignore_me = gc.collect()
released A
"""
def inner():
return (a[0], a[1])
return inner
cdef arg_in_closure_cdef(int [:] a):
def inner():
return (a[0], a[1])
return inner
def test_arg_in_closure_cdef(a):
"""
>>> A = IntMockBuffer("A", range(6), shape=(6,))
>>> inner = test_arg_in_closure_cdef(A)
acquired A
>>> inner()
(0, 1)
The assignment below is just to avoid printing what was collected
>>> del inner; ignore_me = gc.collect()
released A
"""
return arg_in_closure_cdef(a)
@testcase
def test_local_in_closure(a):
"""
>>> A = IntMockBuffer("A", range(6), shape=(6,))
>>> inner = test_local_in_closure(A)
acquired A
>>> inner()
(0, 1)
The assignment below is just to avoid printing what was collected
>>> del inner; ignore_me = gc.collect()
released A
"""
cdef int[:] a_view = a
def inner():
return (a_view[0], a_view[1])
return inner
@testcase
def test_local_in_generator_expression(a, initialize, execute_now):
"""
>>> A1 = IntMockBuffer("A1", range(6), shape=(6,))
>>> A2 = IntMockBuffer("A2", range(6), shape=(6,))
>>> test_local_in_generator_expression(A1, initialize=False, execute_now=False) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
UnboundLocalError...
>>> test_local_in_generator_expression(A1, initialize=True, execute_now=True)
acquired A1
released A1
True
>>> genexp = test_local_in_generator_expression(A2, initialize=True, execute_now=False)
acquired A2
>>> sum(genexp)
released A2
2
"""
cdef int[:] a_view
if initialize:
a_view = a
if execute_now:
return any(ai > 3 for ai in a_view)
else:
return (ai > 3 for ai in a_view)
|