cython

Форк
0
/
bytearray_iter.py 
105 строк · 2.2 Кб
1
# mode: run
2
# tag: pure3, pure2
3

4
import cython
5

6
@cython.test_assert_path_exists("//ForFromStatNode")
7
@cython.test_fail_if_path_exists("//ForInStatNode")
8
@cython.locals(x=bytearray)
9
def basic_bytearray_iter(x):
10
    """
11
    >>> basic_bytearray_iter(bytearray(b"hello"))
12
    h
13
    e
14
    l
15
    l
16
    o
17
    """
18
    for a in x:
19
        print(chr(a))
20

21
@cython.test_assert_path_exists("//ForFromStatNode")
22
@cython.test_fail_if_path_exists("//ForInStatNode")
23
@cython.locals(x=bytearray)
24
def reversed_bytearray_iter(x):
25
    """
26
    >>> reversed_bytearray_iter(bytearray(b"hello"))
27
    o
28
    l
29
    l
30
    e
31
    h
32
    """
33
    for a in reversed(x):
34
        print(chr(a))
35

36
@cython.test_assert_path_exists("//ForFromStatNode")
37
@cython.test_fail_if_path_exists("//ForInStatNode")
38
@cython.locals(x=bytearray)
39
def modifying_bytearray_iter1(x):
40
    """
41
    >>> modifying_bytearray_iter1(bytearray(b"abcdef"))
42
    a
43
    b
44
    c
45
    3
46
    """
47
    count = 0
48
    for a in x:
49
        print(chr(a))
50
        del x[-1]
51
        count += 1
52
    print(count)
53

54
@cython.test_assert_path_exists("//ForFromStatNode")
55
@cython.test_fail_if_path_exists("//ForInStatNode")
56
@cython.locals(x=bytearray)
57
def modifying_bytearray_iter2(x):
58
    """
59
    >>> modifying_bytearray_iter2(bytearray(b"abcdef"))
60
    a
61
    c
62
    e
63
    3
64
    """
65
    count = 0
66
    for a in x:
67
        print(chr(a))
68
        del x[0]
69
        count += 1
70
    print(count)
71

72
@cython.test_assert_path_exists("//ForFromStatNode")
73
@cython.test_fail_if_path_exists("//ForInStatNode")
74
@cython.locals(x=bytearray)
75
def modifying_reversed_bytearray_iter(x):
76
    """
77
    NOTE - I'm not 100% sure how well-defined this behaviour is in Python.
78
    However, for the moment Python and Cython seem to do the same thing.
79
    Testing that it doesn't crash is probably more important than the exact output!
80
    >>> modifying_reversed_bytearray_iter(bytearray(b"abcdef"))
81
    f
82
    f
83
    f
84
    f
85
    f
86
    f
87
    """
88
    for a in reversed(x):
89
        print(chr(a))
90
        del x[0]
91

92
# ticket: 3473
93

94
def test_bytearray_iteration(src):
95
    """
96
    >>> src = b'123'
97
    >>> test_bytearray_iteration(src)
98
    49
99
    50
100
    51
101
    """
102

103
    data = bytearray(src)
104
    for elem in data:
105
        print(elem)
106

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.