summaryrefslogtreecommitdiff
path: root/tests/run/builtin_memory_view.pyx
blob: 34dd32ba701e365b6b278c2a6fb2b54236dd1109 (plain)
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
# mode: run

# Tests Python's builtin memoryview.

from __future__ import print_function

import sys

cimport cython
#from cpython.memoryview cimport PyMemoryView_GET_BUFFER

@cython.test_fail_if_path_exists("//SimpleCallNode")
def test_convert_from_obj(o):
    """
    >>> abc = b'abc'
    >>> all(x == y for x, y in zip(test_convert_from_obj(abc), abc))
    True
    """
    return memoryview(o)

# TODO - this currently doesn't work because the buffer fails a
# "can coerce to python object" test earlier. But it'd be nice to support
'''
def test_create_from_buffer():
    """
    memoryview from Py_buffer exists and is special-cased
    >>> mview = test_create_from_buffer()
    >>> >>> all(x == y for x, y in zip(mview, b'argh!'))
    True
    """
    other_view = memoryview(b'argh!')
    cdef Py_buffer *buf = PyMemoryView_GET_BUFFER(other_view)
    return memoryview(buf)
'''

@cython.test_fail_if_path_exists("//AttributeNode")
def test_optimized_attributes(memoryview view):
    """
    >>> test_optimized_attributes(memoryview(b'zzzzz'))
    1 1 True
    """
    print(view.itemsize, view.ndim, view.readonly)

def test_isinstance(x):
    """
    >>> test_isinstance(b"abc")
    False
    >>> test_isinstance(memoryview(b"abc"))
    True
    """
    return isinstance(x, memoryview)

def test_in_with(x):
    """
    This is really just a compile test. An optimization was being
    applied in a way that generated invalid code
    >>> test_in_with(b"abc")
    98
    """
    if sys.version_info[0] < 3:
        # Python 2 doesn't support memoryviews as context-managers
        # so just skip the test
        print(98)
        return
    with memoryview(x) as xv:
        print(xv[1])