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
|
# 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])
def test_returned_type():
"""
This is really just a compile test. An optimization was being
applied in a way that generated invalid code
>>> test_returned_type()
98
"""
def foo() -> memoryview:
rv = memoryview(b"abc")[:]
return rv
# Python 2 prints 'n' instead of 98. We're only really testing the
# type check for the return type, so skip the test.
if sys.version_info[0] < 3:
print(98)
else:
print(foo()[1])
|