cython

Форк
0
/
cython3_no_unicode_literals.pyx 
174 строки · 3.4 Кб
1
# cython: language_level=3str, binding=True
2
# mode: run
3
# tag: python3, str_is_str
4

5
print(end='')  # test that language_level 3 applies immediately at the module start, for the first token.
6

7
import cython
8

9
__doc__ = """
10
>>> items = sorted(locals_function(1).items())
11
>>> for item in items:
12
...     print('%s = %r' % item)
13
a = 1
14
b = 2
15
x = 'abc'
16
"""
17

18

19
def locals_function(a, b=2):
20
    x = 'abc'
21
    return locals()
22

23

24
### true division
25

26
def truediv(x):
27
    """
28
    >>> truediv(4)
29
    2.0
30
    >>> truediv(3)
31
    1.5
32
    """
33
    return x / 2
34

35

36
def truediv_int(int x):
37
    """
38
    >>> truediv_int(4)
39
    2.0
40
    >>> truediv_int(3)
41
    1.5
42
    """
43
    return x / 2
44

45

46
### Py3 feature tests
47

48
def print_function(*args):
49
    """
50
    >>> print_function(1,2,3)
51
    1 2 3
52
    """
53
    print(*args) # this isn't valid Py2 syntax
54

55

56
str_string = "abcdefg"
57

58
def no_unicode_literals():
59
    """
60
    >>> print( no_unicode_literals() )
61
    True
62
    abcdefg
63

64
    Testing non-ASCII docstrings: Πυθαγόρας
65
    """
66
    print(isinstance(str_string, str) or type(str_string))
67
    return str_string
68

69

70
def non_ascii_str():
71
    u"""
72
    >>> s = 'ø\\x20\\u0020'
73
    >>> isinstance(s, str)
74
    True
75
    >>> len(s) == 3  or  len(s)
76
    True
77

78
    >>> s = non_ascii_str()
79
    >>> isinstance(s, str)
80
    True
81
    >>> len(s) == 3  or  len(s)
82
    True
83
    """
84
    s = 'ø\x20\u0020'
85
    assert isinstance(s, str)
86
    assert isinstance(s, unicode)
87
    return s
88

89

90
def non_ascii_raw_str():
91
    u"""
92
    >>> s = r'ø\\x20\\u0020'
93
    >>> len(s) == 11  or  len(s)
94
    True
95

96
    >>> s = non_ascii_raw_str()
97
    >>> isinstance(s, str)
98
    True
99
    >>> len(s) == 11  or  len(s)
100
    True
101
    """
102
    s = r'ø\x20\u0020'
103
    assert isinstance(s, str)
104
    assert isinstance(s, unicode)
105
    return s
106

107

108
def non_ascii_raw_unicode():
109
    u"""
110
    >>> s = non_ascii_raw_unicode()
111
    >>> isinstance(s, bytes)
112
    False
113
    >>> len(s)
114
    11
115
    """
116
    s = ru'ø\x20\u0020'
117
    assert isinstance(s, unicode)
118
    return s
119

120

121
def str_type_is_str():
122
    """
123
    >>> str_type, s = str_type_is_str()
124
    >>> isinstance(s, type(str_string)) or (s, str_type)
125
    True
126
    >>> isinstance(s, str_type) or (s, str_type)
127
    True
128
    >>> isinstance(str_string, str_type) or str_type
129
    True
130
    """
131
    cdef str s = 'abc'
132
    return str, s
133

134
def strip_wrapped_string(s):
135
    # PEP 563 translates an annotation of "test new test" to '"test new test"'
136
    # but choice of string delimiters is a bit arbitrary
137
    #  this function handles that
138
    assert s[0] == s[-1] # delimiters on either end are the same
139
    return s[1:-1] # strip them
140

141

142
@cython.annotation_typing(False)
143
def annotation_syntax(a: "test new test", b : "other" = 2, *args: "ARGS", **kwargs: "KWARGS") -> "ret":
144
    """
145
    >>> annotation_syntax(1)
146
    3
147
    >>> annotation_syntax(1,3)
148
    4
149

150
    >>> len(annotation_syntax.__annotations__)
151
    5
152
    >>> strip_wrapped_string(annotation_syntax.__annotations__['a'])
153
    'test new test'
154
    >>> strip_wrapped_string(annotation_syntax.__annotations__['b'])
155
    'other'
156
    >>> strip_wrapped_string(annotation_syntax.__annotations__['args'])
157
    'ARGS'
158
    >>> strip_wrapped_string(annotation_syntax.__annotations__['kwargs'])
159
    'KWARGS'
160
    >>> strip_wrapped_string(annotation_syntax.__annotations__['return'])
161
    'ret'
162
    """
163
    result : int = a + b
164

165
    return result
166

167

168
@cython.annotation_typing(True)
169
def repr_returns_str(x) -> str:
170
    """
171
    >>> repr_returns_str(123)
172
    '123'
173
    """
174
    return repr(x)
175

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

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

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

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