memslice.pyx 56.5 KB
Newer Older
Mark Florisson's avatar
Mark Florisson committed
1 2
# mode: run

3
# Note: see also bufaccess.pyx
4 5 6

from __future__ import unicode_literals

7 8 9
from cpython.object cimport PyObject
from cpython.ref cimport Py_INCREF, Py_DECREF

10
cimport cython
11
from cython cimport view
12
from cython.view cimport array
13
from cython.parallel cimport prange, parallel
14

15
from functools import wraps
16
import gc
17
import sys
Mark Florisson's avatar
Mark Florisson committed
18

19 20 21 22 23 24
if sys.version_info[0] < 3:
    import __builtin__ as builtins
else:
    import builtins


25
def testcase(func):
26
    @wraps(func)
27 28 29 30 31 32 33
    def wrapper(*args, **kwargs):
        gc.collect()
        result = func(*args, **kwargs)
        gc.collect()
        return result

    return wrapper
34 35


36
include "../buffers/mockbuffers.pxi"
37
include "cythonarrayutil.pxi"
38

39 40 41 42 43 44 45 46
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])

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
#
# 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
65
    released A
66 67
    released B
    >>> acquire_release(None, B)
68 69
    acquired B
    released B
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
    """
    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):
    """
152
    >>> acquire_nonbuffer1(3)  # doctest: +ELLIPSIS
153 154
    Traceback (most recent call last):
      ...
155 156
    TypeError:... 'int'...
    >>> acquire_nonbuffer1(type)  # doctest: +ELLIPSIS
157 158
    Traceback (most recent call last):
      ...
159 160
    TypeError:... 'type'...
    >>> acquire_nonbuffer1(None, 2)  # doctest: +ELLIPSIS
161 162
    Traceback (most recent call last):
      ...
163 164
    TypeError:... 'int'...
    >>> acquire_nonbuffer1(4, object())  # doctest: +ELLIPSIS
165 166
    Traceback (most recent call last):
      ...
167
    TypeError:... 'int'...
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
    """
    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
245
    released A
246 247
    2
    acquired A
248
    released B
249 250
    2
    acquired A
251
    released A
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
    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):
    """
273 274
    >>> A = IntMockBuffer("A", range(6))  # , writable=False)
    >>> B = IntMockBuffer("B", range(6))  # , writable=False)
275 276 277 278 279 280 281 282 283 284 285 286
    >>> 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):
    """
287 288
    >>> A = IntMockBuffer("A", range(6))  # , writable=False)
    >>> B = IntMockBuffer("B", range(6))  # , writable=False)
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
    >>> 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
    """
306
    cdef int[:] x = IntMockBuffer("A", range(10))  # , writable=False)
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
    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:
352
    >>> C = IntMockBuffer("C", range(6), (2,3))  # , writable=False)
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
    >>> 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)

416 417 418 419
    >>> 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
420 421 422
    """
    buf[i, j] = value

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

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


583 584 585
@testcase
def list_comprehension(int[:] buf, len):
    """
586
    >>> list_comprehension(IntMockBuffer(None, [1,2,3]), 3)  # , writable=False), 3)
587 588 589
    1|2|3
    """
    cdef int i
590
    print "|".join([str(buf[i]) for i in range(len)])
591 592 593 594 595 596 597

@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.

598
    >>> A = IntMockBuffer(None, range(4))  # , writable=False)
599 600 601 602 603 604 605 606 607 608 609 610 611 612
    >>> 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


#
613
# Test all kinds of indexing and flags
614 615 616 617 618 619 620 621 622
#

@testcase
def writable(obj):
    """
    >>> R = UnsignedShortMockBuffer("R", range(27), shape=(3, 3, 3))
    >>> writable(R)
    acquired R
    released R
623
    >>> [str(x) for x in R.received_flags] # Py2/3
624 625 626 627 628 629
    ['FORMAT', 'ND', 'STRIDES', 'WRITABLE']
    """
    cdef unsigned short int[:, :, :] buf = obj
    buf[2, 2, 1] = 23

@testcase
630
def strided(const int[:] buf):
631
    """
632
    >>> A = IntMockBuffer("A", range(4), writable=False)
633 634 635 636
    >>> strided(A)
    acquired A
    released A
    2
637 638
    >>> [str(x) for x in A.received_flags] # Py2/3
    ['FORMAT', 'ND', 'STRIDES']
639 640 641 642 643 644 645 646

    Check that the suboffsets were patched back prior to release.
    >>> A.release_ok
    True
    """
    return buf[2]

@testcase
647
def c_contig(const int[::1] buf):
648
    """
649
    >>> A = IntMockBuffer(None, range(4), writable=False)
650 651
    >>> c_contig(A)
    2
652 653
    >>> [str(x) for x in A.received_flags]
    ['FORMAT', 'ND', 'STRIDES', 'C_CONTIGUOUS']
654 655 656 657 658 659
    """
    return buf[2]

@testcase
def c_contig_2d(int[:, ::1] buf):
    """
Unknown's avatar
Unknown committed
660
    Multi-dim has separate implementation
661

662
    >>> A = IntMockBuffer(None, range(12), shape=(3,4))  # , writable=False)
663 664
    >>> c_contig_2d(A)
    7
665
    >>> [str(x) for x in A.received_flags]
666
    ['FORMAT', 'ND', 'STRIDES', 'C_CONTIGUOUS', 'WRITABLE']
667 668 669 670 671 672
    """
    return buf[1, 3]

@testcase
def f_contig(int[::1, :] buf):
    """
673
    >>> A = IntMockBuffer(None, range(4), shape=(2, 2), strides=(1, 2))  # , writable=False)
674 675
    >>> f_contig(A)
    2
676
    >>> [str(x) for x in A.received_flags]
677
    ['FORMAT', 'ND', 'STRIDES', 'F_CONTIGUOUS', 'WRITABLE']
678 679 680 681
    """
    return buf[0, 1]

@testcase
682
def f_contig_2d(int[::1, :] buf):
683 684 685
    """
    Must set up strides manually to ensure Fortran ordering.

686
    >>> A = IntMockBuffer(None, range(12), shape=(4,3), strides=(1, 4))  # , writable=False)
687 688
    >>> f_contig_2d(A)
    7
689
    >>> [str(x) for x in A.received_flags]
690
    ['FORMAT', 'ND', 'STRIDES', 'F_CONTIGUOUS', 'WRITABLE']
691 692 693
    """
    return buf[3, 1]

694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
@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
709
    >>> [str(x) for x in A.received_flags]
710
    ['FORMAT', 'INDIRECT', 'ND', 'STRIDES', 'WRITABLE']
711
    >>> [str(x) for x in B.received_flags]
712 713 714 715 716 717 718 719 720 721 722
    ['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]

723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
# 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
739
#     >>> [str(x) for x in A.received_flags]
740
#     ['FORMAT', 'INDIRECT', 'ND', 'STRIDES', 'WRITABLE']
741
#     >>> [str(x) for x in B.received_flags]
742 743 744 745 746 747 748 749 750 751
#     ['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]
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768

@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
769
    >>> [str(x) for x in A.received_flags]
770
    ['FORMAT', 'INDIRECT', 'ND', 'STRIDES', 'WRITABLE']
771
    >>> [str(x) for x in B.received_flags]
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
    ['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
800
    >>> [str(x) for x in A.received_flags]
801
    ['FORMAT', 'INDIRECT', 'ND', 'STRIDES', 'WRITABLE']
802
    >>> [str(x) for x in B.received_flags]
803 804 805 806 807 808 809 810 811 812 813 814 815
    ['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]



816 817 818 819 820 821 822 823 824
#
# 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):
    """
825
    >>> A = IntMockBuffer(None, range(10), shape=(3,), offset=5)  # , writable=False)
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

    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.
855
    >>> A = IntMockBuffer(None, range(10), shape=(3,), offset=5)  # , writable=False)
856 857 858 859 860 861 862 863 864 865 866 867
    >>> 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):
    """
868
    >>> A = IntMockBuffer(None, range(10), shape=(3,), offset=5)  # , writable=False)
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
    >>> 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:

900
    >>> printbuf_int_2d(IntMockBuffer("A", range(6), (2,3), writable=False), (2,3))
901 902 903 904
    acquired A
    0 1 2 END
    3 4 5 END
    released A
905
    >>> printbuf_int_2d(IntMockBuffer("A", range(100), (3,3), strides=(20,5), writable=False), (3,3))
906 907 908 909 910 911 912
    acquired A
    0 5 10 END
    20 25 30 END
    40 45 50 END
    released A

    Indirect:
913
    >>> printbuf_int_2d(IntMockBuffer("A", [[1,2],[3,4]], writable=False), (2,2))
914 915 916 917 918 919
    acquired A
    1 2 END
    3 4 END
    released A
    """
    # should make shape builtin
920
    cdef const int[::view.generic, ::view.generic] buf
921 922 923 924 925 926 927 928 929 930
    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):
    """
931
    >>> printbuf_float(FloatMockBuffer("F", [1.0, 1.25, 0.75, 1.0], writable=False), (4,))
932 933 934 935 936 937
    acquired F
    1.0 1.25 0.75 1.0 END
    released F
    """

    # should make shape builtin
938
    cdef const float[:] buf
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
    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):
    """
980
    >>> printbuf_td_cy_int(IntMockBuffer(None, range(3)), (3,))  # , writable=False), (3,))
981
    0 1 2 END
982
    >>> printbuf_td_cy_int(ShortMockBuffer(None, range(3)), (3,))  # , writable=False), (3,))
983 984 985 986 987 988 989 990 991 992
    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
993
def printbuf_td_h_short(td_h_short[:] buf, shape):
994
    """
995
    >>> printbuf_td_h_short(ShortMockBuffer(None, range(3)), (3,))  # , writable=False), (3,))
996
    0 1 2 END
997
    >>> printbuf_td_h_short(IntMockBuffer(None, range(3)), (3,))  # , writable=False), (3,))
998 999 1000 1001 1002 1003 1004 1005 1006 1007
    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
1008
def printbuf_td_h_cy_short(const td_h_cy_short[:] buf, shape):
1009
    """
1010
    >>> printbuf_td_h_cy_short(ShortMockBuffer(None, range(3), writable=False), (3,))
1011
    0 1 2 END
1012
    >>> printbuf_td_h_cy_short(IntMockBuffer(None, range(3), writable=False), (3,))
1013 1014
    Traceback (most recent call last):
       ...
1015
    ValueError: Buffer dtype mismatch, expected 'const td_h_cy_short' but got 'int'
1016 1017 1018 1019 1020 1021 1022
    """
    cdef int i
    for i in range(shape[0]):
        print buf[i],
    print 'END'

@testcase
1023
def printbuf_td_h_ushort(const td_h_ushort[:] buf, shape):
1024
    """
1025
    >>> printbuf_td_h_ushort(UnsignedShortMockBuffer(None, range(3), writable=False), (3,))
1026
    0 1 2 END
1027
    >>> printbuf_td_h_ushort(ShortMockBuffer(None, range(3), writable=False), (3,))
1028 1029
    Traceback (most recent call last):
       ...
1030
    ValueError: Buffer dtype mismatch, expected 'const td_h_ushort' but got 'short'
1031 1032 1033 1034 1035 1036 1037
    """
    cdef int i
    for i in range(shape[0]):
        print buf[i],
    print 'END'

@testcase
1038
def printbuf_td_h_double(const td_h_double[:] buf, shape):
1039
    """
1040
    >>> printbuf_td_h_double(DoubleMockBuffer(None, [0.25, 1, 3.125], writable=False), (3,))
1041
    0.25 1.0 3.125 END
1042
    >>> printbuf_td_h_double(FloatMockBuffer(None, [0.25, 1, 3.125], writable=False), (3,))
1043 1044
    Traceback (most recent call last):
       ...
1045
    ValueError: Buffer dtype mismatch, expected 'const td_h_double' but got 'float'
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
    """
    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)

def get_refcount(x):
    return (<PyObject*>x).ob_refcnt

@testcase
1065
def printbuf_object(object[:] buf, shape):
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
    """
    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.

    >>> a, b, c = "globally_unique_string_23234123", {4:23}, [34,3]
    >>> get_refcount(a), get_refcount(b), get_refcount(c)
    (2, 2, 2)
1077
    >>> A = ObjectMockBuffer(None, [a, b, c])  # , writable=False)
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
    >>> 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
1088
def assign_to_object(object[:] buf, int idx, obj):
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
    """
    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
1107
def assign_temporary_to_object(object[:] buf):
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
    """
    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}

#
# Test __cythonbufferdefaults__
#
@testcase
1137
def bufdefaults1(int[:] buf):
1138 1139 1140 1141 1142
    """
    For IntStridedMockBuffer, mode should be
    "strided" by defaults which should show
    up in the flags.

1143
    >>> A = IntStridedMockBuffer("A", range(10))  # , writable=False)
1144 1145 1146
    >>> bufdefaults1(A)
    acquired A
    released A
1147
    >>> [str(x) for x in A.received_flags]
1148
    ['FORMAT', 'ND', 'STRIDES', 'WRITABLE']
1149 1150 1151 1152 1153
    """
    pass


@testcase
1154
def basic_struct(MyStruct[:] buf):
1155 1156 1157
    """
    See also buffmt.pyx

1158
    >>> basic_struct(MyStructMockBuffer(None, [(1, 2, 3, 4, 5)]))  # , writable=False))
1159
    1 2 3 4 5
1160
    >>> basic_struct(MyStructMockBuffer(None, [(1, 2, 3, 4, 5)], format="ccqii"))  # , writable=False))
1161 1162 1163 1164 1165
    1 2 3 4 5
    """
    print buf[0].a, buf[0].b, buf[0].c, buf[0].d, buf[0].e

@testcase
1166
def nested_struct(NestedStruct[:] buf):
1167 1168 1169
    """
    See also buffmt.pyx

1170
    >>> nested_struct(NestedStructMockBuffer(None, [(1, 2, 3, 4, 5)]))  # , writable=False))
1171
    1 2 3 4 5
1172
    >>> nested_struct(NestedStructMockBuffer(None, [(1, 2, 3, 4, 5)], format="T{ii}T{2i}i"))  # , writable=False))
1173 1174 1175 1176 1177
    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
1178
def packed_struct(PackedStruct[:] buf):
1179 1180 1181
    """
    See also buffmt.pyx

1182
    >>> packed_struct(PackedStructMockBuffer(None, [(1, 2)]))  # , writable=False))
1183
    1 2
1184
    >>> packed_struct(PackedStructMockBuffer(None, [(1, 2)], format="T{c^i}"))  # , writable=False))
1185
    1 2
1186
    >>> packed_struct(PackedStructMockBuffer(None, [(1, 2)], format="T{c=i}"))  # , writable=False))
1187 1188 1189 1190 1191 1192
    1 2

    """
    print buf[0].a, buf[0].b

@testcase
1193
def nested_packed_struct(NestedPackedStruct[:] buf):
1194 1195 1196
    """
    See also buffmt.pyx

1197
    >>> nested_packed_struct(NestedPackedStructMockBuffer(None, [(1, 2, 3, 4, 5)]))  # , writable=False))
1198
    1 2 3 4 5
1199
    >>> nested_packed_struct(NestedPackedStructMockBuffer(None, [(1, 2, 3, 4, 5)], format="ci^ci@i"))  # , writable=False))
1200
    1 2 3 4 5
1201
    >>> nested_packed_struct(NestedPackedStructMockBuffer(None, [(1, 2, 3, 4, 5)], format="^c@i^ci@i"))  # , writable=False))
1202 1203 1204 1205 1206 1207
    1 2 3 4 5
    """
    print buf[0].a, buf[0].b, buf[0].sub.a, buf[0].sub.b, buf[0].c


@testcase
1208
def complex_dtype(long double complex[:] buf):
1209
    """
1210
    >>> complex_dtype(LongComplexMockBuffer(None, [(0, -1)]))  # , writable=False))
1211 1212 1213 1214 1215
    -1j
    """
    print buf[0]

@testcase
1216
def complex_inplace(long double complex[:] buf):
1217 1218 1219 1220 1221 1222 1223 1224
    """
    >>> complex_inplace(LongComplexMockBuffer(None, [(0, -1)]))
    (1+1j)
    """
    buf[0] = buf[0] + 1 + 2j
    print buf[0]

@testcase
1225
def complex_struct_dtype(LongComplex[:] buf):
1226 1227 1228
    """
    Note that the format string is "Zg" rather than "2g", yet a struct
    is accessed.
1229
    >>> complex_struct_dtype(LongComplexMockBuffer(None, [(0, -1)]))  # , writable=False))
1230 1231 1232 1233 1234
    0.0 -1.0
    """
    print buf[0].real, buf[0].imag

@testcase
1235
def complex_struct_inplace(LongComplex[:] buf):
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
    """
    >>> 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
#
Mark Florisson's avatar
Mark Florisson committed
1247

1248 1249 1250 1251 1252
@testcase
@cython.boundscheck(False)
def buffer_nogil():
    """
    >>> buffer_nogil()
1253
    (10, 10)
1254 1255
    """
    cdef int[:] buf = IntMockBuffer(None, [1,2,3])
1256 1257
    cdef int[:] buf2 = IntMockBuffer(None, [4,5,6])

1258 1259
    with nogil:
        buf[1] = 10
1260 1261 1262 1263 1264 1265 1266
        buf2 = buf

    return buf[1], buf2[1]

#
### Test cdef functions
#
Mark Florisson's avatar
Mark Florisson committed
1267 1268 1269
class UniqueObject(object):
    def __init__(self, value):
        self.value = value
1270

Mark Florisson's avatar
Mark Florisson committed
1271 1272 1273 1274
    def __repr__(self):
        return self.value

objs = [[UniqueObject("spam")], [UniqueObject("ham")], [UniqueObject("eggs")]]
1275
addref(*[obj for L in objs for obj in L])
1276
cdef cdef_function(int[:] buf1, object[::view.indirect, :] buf2 = ObjectMockBuffer(None, objs)):
1277 1278
    print 'cdef called'
    print buf1[6], buf2[1, 0]
Mark Florisson's avatar
Mark Florisson committed
1279
    buf2[1, 0] = UniqueObject("eggs")
1280

1281
@testcase
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
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
Mark Florisson's avatar
Mark Florisson committed
1294

1295 1296 1297
    >>> L = [[x] for x in range(25)]
    >>> addref(*[obj for mylist in L for obj in mylist])
    >>> B = ObjectMockBuffer("B", L, shape=(5, 5))
Mark Florisson's avatar
Mark Florisson committed
1298

1299
    >>> test_cdef_function(A, B)
1300 1301 1302 1303 1304 1305 1306 1307
    acquired A
    cdef called
    6 eggs
    released A
    acquired A
    cdef called
    6 eggs
    released A
1308 1309 1310 1311 1312
    acquired A
    acquired B
    cdef called
    6 1
    released A
1313 1314 1315 1316 1317 1318 1319 1320
    released B
    """
    cdef_function(o1)
    cdef_function(o1)

    if o2:
        cdef_function(o1, o2)

1321
cdef int[:] global_A = IntMockBuffer("Global_A", range(10))
Mark Florisson's avatar
Mark Florisson committed
1322

1323
addref(*[obj for L in objs for obj in L])
1324
cdef object[::view.indirect, :] global_B = ObjectMockBuffer(None, objs)
1325 1326 1327 1328

cdef cdef_function2(int[:] buf1, object[::view.indirect, :] buf2 = global_B):
    print 'cdef2 called'
    print buf1[6], buf2[1, 0]
Mark Florisson's avatar
Mark Florisson committed
1329
    buf2[1, 0] = UniqueObject("eggs")
1330

1331
@testcase
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
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)
1352 1353

@testcase
Mark Florisson's avatar
Mark Florisson committed
1354
def test_generic_slicing(arg, indirect=False):
1355 1356
    """
    Test simple slicing
1357
    >>> test_generic_slicing(IntMockBuffer("A", range(8 * 14 * 11), shape=(8, 14, 11)))  # , writable=False))
1358 1359
    acquired A
    3 9 2
1360
    308 -11 1
1361 1362 1363 1364
    -1 -1 -1
    released A

    Test direct slicing, negative slice oob in dim 2
1365
    >>> test_generic_slicing(IntMockBuffer("A", range(1 * 2 * 3), shape=(1, 2, 3)))  # , writable=False))
1366 1367
    acquired A
    0 0 2
1368
    12 -3 1
1369 1370 1371 1372
    -1 -1 -1
    released A

    Test indirect slicing
1373
    >>> test_generic_slicing(IntMockBuffer("A", shape_5_3_4_list, shape=(5, 3, 4)), indirect=True)  # , writable=False), indirect=True)
1374 1375
    acquired A
    2 0 2
1376 1377 1378
    0 1 -1
    released A

1379
    >>> test_generic_slicing(IntMockBuffer("A", shape_9_14_21_list, shape=(9, 14, 21)), indirect=True)  # , writable=False), indirect=True)
1380 1381
    acquired A
    3 9 2
Mark Florisson's avatar
Mark Florisson committed
1382
    10 1 -1
1383
    released A
1384

1385 1386 1387 1388 1389
    """
    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]
Mark Florisson's avatar
Mark Florisson committed
1390 1391

    if indirect:
Mark Florisson's avatar
Mark Florisson committed
1392 1393
        print b.suboffsets[0] // sizeof(int *),
        print b.suboffsets[1] // sizeof(int),
Mark Florisson's avatar
Mark Florisson committed
1394 1395
        print b.suboffsets[2]
    else:
1396
        print_int_offsets(b.strides[0], b.strides[1], b.strides[2])
Mark Florisson's avatar
Mark Florisson committed
1397
        print_int_offsets(b.suboffsets[0], b.suboffsets[1], b.suboffsets[2])
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410

    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
1411
    >>> test_indirect_slicing(IntMockBuffer("A", shape_5_3_4_list, shape=(5, 3, 4)))  # , writable=False))
1412 1413 1414 1415 1416
    acquired A
    5 3 2
    0 0 -1
    58
    56
1417 1418 1419 1420 1421 1422 1423
    58
    index away indirect
    58
    58
    index away generic
    58
    58
1424 1425
    released A

1426
    >>> test_indirect_slicing(IntMockBuffer("A", shape_9_14_21_list, shape=(9, 14, 21)))  # , writable=False))
1427 1428 1429 1430 1431
    acquired A
    5 14 3
    0 16 -1
    2412
    2410
1432 1433 1434 1435 1436 1437 1438
    2412
    index away indirect
    2412
    2412
    index away generic
    2412
    2412
1439 1440 1441 1442
    released A
    """
    cdef int[::view.indirect, ::view.indirect, :] a = arg
    cdef int[::view.indirect, ::view.indirect, :] b = a[-5:, ..., -5:100:2]
1443
    cdef int[::view.generic , :: view.generic, :] generic_b = a[-5:, ..., -5:100:2]
1444 1445
    cdef int[::view.indirect, ::view.indirect] c = b[..., 0]

1446 1447 1448 1449 1450 1451 1452
    # 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]

1453
    print b.shape[0], b.shape[1], b.shape[2]
Mark Florisson's avatar
Mark Florisson committed
1454 1455
    print b.suboffsets[0] // sizeof(int *),
    print b.suboffsets[1] // sizeof(int),
Mark Florisson's avatar
Mark Florisson committed
1456
    print b.suboffsets[2]
1457 1458 1459

    print b[4, 2, 1]
    print c[4, 2]
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
    # 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

Robert Bradshaw's avatar
Robert Bradshaw committed
1476
    cdef int[5] c_array
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
    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()
    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]
1544 1545 1546 1547 1548 1549 1550

@testcase
def test_direct_slicing(arg):
    """
    Fused types would be convenient to test this stuff!

    Test simple slicing
1551
    >>> test_direct_slicing(IntMockBuffer("A", range(8 * 14 * 11), shape=(8, 14, 11)))  # , writable=False))
1552 1553 1554 1555 1556 1557 1558
    acquired A
    3 9 2
    308 -11 1
    -1 -1 -1
    released A

    Test direct slicing, negative slice oob in dim 2
1559
    >>> test_direct_slicing(IntMockBuffer("A", range(1 * 2 * 3), shape=(1, 2, 3)))  # , writable=False))
1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571
    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])
1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583

    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):
    """
1584
    >>> a = IntStridedMockBuffer("A", range(10 * 3 * 5), shape=(10, 3, 5))  # , writable=False)
1585 1586 1587
    >>> test_slicing_and_indexing(a)
    acquired A
    5 2
1588
    15 2
1589 1590 1591 1592 1593 1594 1595 1596 1597 1598
    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]
1599
    print_int_offsets(b.strides[0], b.strides[1])
1600 1601 1602 1603 1604 1605 1606 1607 1608

    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]
1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619
    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)
    """
1620
    cdef int[:, :] a = IntMockBuffer("A", range(4 * 9), shape=(4, 9))  # , writable=False)
1621
    print a[:, 20]
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


cdef int nogil_oob(int[:, :] a) nogil except 0:
    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:
1650 1651
        with nogil:
            nogil_oob(a)
1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662
    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)
    """
1663
    cdef int[:, :] a = IntMockBuffer("A", range(4 * 9), shape=(4, 9))  # , writable=False)
1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674
    with nogil:
        a[100, 9:]

@cython.boundscheck(False)
cdef int cdef_nogil(int[:, :] a) nogil except 0:
    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]

1675
    return len(a)
1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687

@testcase
def test_nogil():
    """
    >>> test_nogil()
    acquired A
    released A
    acquired A
    -25
    released A
    """
    _a = IntMockBuffer("A", range(4 * 9), shape=(4, 9))
1688
    assert cdef_nogil(_a) == 4
1689 1690
    cdef int[:, :] a = _a
    print a[2, 7]
1691

1692 1693 1694 1695 1696
    cdef int length
    with nogil:
        length = cdef_nogil(a)
    assert length == 4

1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708
@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
    """
1709
    cdef int[:] a = IntMockBuffer("A", range(10), shape=(10,))  # , writable=False)
1710 1711 1712 1713
    with nogil:
        a = a[2:4]
    print a[0]

1714 1715 1716 1717 1718
@testcase
@cython.boundscheck(False)
@cython.wraparound(False)
def test_memslice_prange(arg):
    """
1719
    >>> test_memslice_prange(IntMockBuffer("A", range(400), shape=(20, 4, 5)))  # FIXME: , writable=False))
1720 1721
    acquired A
    released A
1722
    >>> test_memslice_prange(IntMockBuffer("A", range(200), shape=(100, 2, 1)))  # FIXME: , writable=False))
1723 1724 1725 1726 1727 1728 1729
    acquired A
    released A
    """
    cdef int[:, :, :] src, dst

    src = arg

1730
    dst = array((<object> src).shape, sizeof(int), format="i")
1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742

    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])
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
@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


1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805
# 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
    """
Robert Bradshaw's avatar
Robert Bradshaw committed
1806 1807
    cdef ArrayStruct[10] a1
    cdef PackedArrayStruct[10] a2
1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823

    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

Stefan Behnel's avatar
Stefan Behnel committed
1824
    if (2, 7) <= sys.version_info[:2] < (3, 3):
1825
        size1 = <Py_ssize_t>sizeof(FusedStruct)
1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843
        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')

1844 1845 1846 1847 1848 1849 1850 1851
cdef struct TestAttrs:
    int int_attrib
    char char_attrib

@testcase
def test_struct_attributes_format():
    """
    >>> test_struct_attributes_format()
Robert Bradshaw's avatar
Robert Bradshaw committed
1852
    T{i:int_attrib:c:char_attrib:}
1853 1854 1855
    """
    cdef TestAttrs[10] array
    cdef TestAttrs[:] struct_memview = array
1856
    print builtins.memoryview(struct_memview).format
1857 1858


1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877
# 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

1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899
# 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

1900 1901 1902 1903 1904
cdef fused FusedPadded:
    ArrayStruct
    PackedArrayStruct
    AlignedNested
    AlignedNestedNormal
1905 1906 1907 1908
    A
    B
    C
    D
1909 1910 1911 1912 1913 1914

@testcase
def test_padded_structs():
    """
    >>> test_padded_structs()
    """
Robert Bradshaw's avatar
Robert Bradshaw committed
1915 1916 1917 1918 1919 1920 1921 1922
    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
1923

1924
    _test_padded(a1)
1925 1926 1927 1928 1929 1930 1931 1932 1933
    _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)
1934 1935 1936 1937 1938 1939 1940

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

1941 1942 1943 1944 1945 1946 1947 1948
@testcase
def test_object_indices():
    """
    >>> test_object_indices()
    0
    1
    2
    """
Robert Bradshaw's avatar
Robert Bradshaw committed
1949
    cdef int[3] array
1950 1951 1952 1953 1954 1955 1956 1957
    cdef int[:] myslice = array
    cdef int j

    for i in range(3):
        myslice[i] = i

    for j in range(3):
        print myslice[j]
1958

1959 1960 1961 1962 1963 1964 1965 1966
cdef fused slice_1d:
    object
    int[:]

cdef fused slice_2d:
    object
    int[:, :]

1967 1968 1969 1970 1971
@testcase
def test_ellipsis_expr():
    """
    >>> test_ellipsis_expr()
    8
1972
    8
1973 1974 1975
    """
    cdef int[10] a
    cdef int[:] m = a
1976 1977 1978 1979 1980

    _test_ellipsis_expr(m)
    _test_ellipsis_expr(<object> m)

cdef _test_ellipsis_expr(slice_1d m):
1981 1982 1983 1984 1985 1986 1987 1988 1989
    m[4] = 8
    m[...] = m[...]
    print m[4]

@testcase
def test_slice_assignment():
    """
    >>> test_slice_assignment()
    """
Robert Bradshaw's avatar
Robert Bradshaw committed
1990
    cdef int[10][100] carray
1991 1992 1993 1994
    cdef int i, j

    for i in range(10):
        for j in range(100):
1995
            carray[i][j] = i * 100 + j
1996 1997 1998 1999

    cdef int[:, :] m = carray
    cdef int[:, :] copy = m[-6:-1, 60:65].copy()

2000 2001 2002 2003 2004 2005
    _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

2006 2007 2008 2009 2010 2011
    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):
2012
            assert copy[i, j] == m[-5 + i, -5 + j], (copy[i, j], m[-5 + i, -5 + j])
2013 2014

@testcase
2015
def test_slice_assignment_broadcast_leading():
2016
    """
2017
    >>> test_slice_assignment_broadcast_leading()
2018
    """
Robert Bradshaw's avatar
Robert Bradshaw committed
2019 2020
    cdef int[1][10] array1
    cdef int[10] array2
2021 2022 2023 2024 2025 2026 2027 2028
    cdef int i

    for i in range(10):
        array1[0][i] = i

    cdef int[:, :] a = array1
    cdef int[:] b = array2

2029 2030 2031 2032 2033 2034 2035 2036 2037 2038
    _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

2039 2040 2041 2042 2043
    b[:] = a[:, :]
    b = b[::-1]
    a[:, :] = b[:]

    for i in range(10):
2044
        assert a[0, i] == b[i] == 10 - 1 - i, (a[0, i], b[i], 10 - 1 - i)
2045 2046 2047 2048 2049 2050

@testcase
def test_slice_assignment_broadcast_strides():
    """
    >>> test_slice_assignment_broadcast_strides()
    """
Robert Bradshaw's avatar
Robert Bradshaw committed
2051 2052
    cdef int[10] src_array
    cdef int[10][5] dst_array
2053 2054 2055 2056 2057 2058 2059 2060 2061
    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()

2062 2063 2064 2065 2066 2067
    _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

2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082
    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)

2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093
@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
    """
2094 2095 2096
    cdef int i
    cdef int[10] carray
    carray[:] = range(10)
2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113
    _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
2114 2115 2116 2117 2118 2119 2120 2121

class SingleObject(object):
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return str(self.value)

2122 2123 2124
    def __eq__(self, other):
        return self.value == getattr(other, 'value', None) or self.value == other

2125
cdef _get_empty_object_slice(fill=None):
2126
    cdef array a = array((10,), sizeof(PyObject *), 'O')
2127 2128 2129
    assert a.dtype_is_object
    return a

2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144
@testcase
def test_object_dtype_copying():
    """
    >>> test_object_dtype_copying()
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    2 5
2145
    1 5
2146 2147 2148
    """
    cdef int i

2149 2150
    unique = object()
    unique_refcount = get_refcount(unique)
2151

2152 2153
    cdef object[:] m1 = _get_empty_object_slice()
    cdef object[:] m2 = _get_empty_object_slice()
2154 2155 2156 2157 2158

    for i in range(10):
        m1[i] = SingleObject(i)

    m2[...] = m1
2159
    del m1
2160 2161 2162 2163 2164 2165 2166 2167 2168 2169

    for i in range(10):
        print m2[i]

    obj = m2[5]
    print get_refcount(obj), obj

    del m2
    print get_refcount(obj), obj

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
    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

Robert Bradshaw's avatar
Robert Bradshaw committed
2201
    cdef int[5][10] a2
2202 2203 2204
    cdef int[:, ::1] m2 = a2

    _test_scalar_slice_assignment(m, m2)
2205
    print
2206
    _test_scalar_slice_assignment(<object> m, <object> m2)
2207

2208 2209
cdef _test_scalar_slice_assignment(slice_1d m, slice_2d m2):
    cdef int i, j
2210 2211 2212 2213 2214 2215
    for i in range(10):
        m[i] = i

    m[-2:0:-2] = 6
    for i in range(10):
        print m[i]
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

    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
    20 20 20 20
    """
Robert Bradshaw's avatar
Robert Bradshaw committed
2257
    cdef int[5][10] a
2258 2259 2260 2261 2262 2263 2264
    cdef int[:, ::1] m = a

    m[...] = 14
    print m[0, 0], m[-1, -1], m[3, 2], m[4, 9]

    m[:, :] = 20
    print m[0, 0], m[-1, -1], m[3, 2], m[4, 9]
2265 2266 2267 2268 2269 2270

@testcase
def test_dtype_object_scalar_assignment():
    """
    >>> test_dtype_object_scalar_assignment()
    """
2271
    cdef object[:] m = array((10,), sizeof(PyObject *), 'O')
2272 2273 2274 2275 2276
    m[:] = SingleObject(2)
    assert m[0] == m[4] == m[-1] == 2

    (<object> m)[:] = SingleObject(3)
    assert m[0] == m[4] == m[-1] == 3
2277

2278 2279 2280
#
### Test slices that are set to None
#
2281 2282 2283 2284

# for none memoryview slice attribute testing, slicing, indexing, etc, see
# nonecheck.pyx

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
@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
Mark Florisson's avatar
Mark Florisson committed
2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340

@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

2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355
@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

2356 2357 2358 2359 2360 2361 2362 2363
@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
    """

2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377
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]
2378 2379 2380 2381

@testcase
def test_newaxis(int[:] one_D):
    """
2382
    >>> A = IntMockBuffer("A", range(6))  # , writable=False)
2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403
    >>> 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):
    """
2404
    >>> A = IntMockBuffer("A", range(6), shape=(3, 2))  # , writable=False)
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
    >>> 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)


2438
@testcase
2439
def test_const_buffer(const int[:] a):
2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450
    """
    >>> 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])