1
# cython: language_level=3str, binding=True
3
# tag: python3, str_is_str
5
print(end='') # test that language_level 3 applies immediately at the module start, for the first token.
10
>>> items = sorted(locals_function(1).items())
12
... print('%s = %r' % item)
19
def locals_function(a, b=2):
36
def truediv_int(int x):
48
def print_function(*args):
50
>>> print_function(1,2,3)
53
print(*args) # this isn't valid Py2 syntax
58
def no_unicode_literals():
60
>>> print( no_unicode_literals() )
64
Testing non-ASCII docstrings: Πυθαγόρας
66
print(isinstance(str_string, str) or type(str_string))
72
>>> s = 'ø\\x20\\u0020'
73
>>> isinstance(s, str)
75
>>> len(s) == 3 or len(s)
78
>>> s = non_ascii_str()
79
>>> isinstance(s, str)
81
>>> len(s) == 3 or len(s)
85
assert isinstance(s, str)
86
assert isinstance(s, unicode)
90
def non_ascii_raw_str():
92
>>> s = r'ø\\x20\\u0020'
93
>>> len(s) == 11 or len(s)
96
>>> s = non_ascii_raw_str()
97
>>> isinstance(s, str)
99
>>> len(s) == 11 or len(s)
103
assert isinstance(s, str)
104
assert isinstance(s, unicode)
108
def non_ascii_raw_unicode():
110
>>> s = non_ascii_raw_unicode()
111
>>> isinstance(s, bytes)
117
assert isinstance(s, unicode)
121
def str_type_is_str():
123
>>> str_type, s = str_type_is_str()
124
>>> isinstance(s, type(str_string)) or (s, str_type)
126
>>> isinstance(s, str_type) or (s, str_type)
128
>>> isinstance(str_string, str_type) or str_type
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
142
@cython.annotation_typing(False)
143
def annotation_syntax(a: "test new test", b : "other" = 2, *args: "ARGS", **kwargs: "KWARGS") -> "ret":
145
>>> annotation_syntax(1)
147
>>> annotation_syntax(1,3)
150
>>> len(annotation_syntax.__annotations__)
152
>>> strip_wrapped_string(annotation_syntax.__annotations__['a'])
154
>>> strip_wrapped_string(annotation_syntax.__annotations__['b'])
156
>>> strip_wrapped_string(annotation_syntax.__annotations__['args'])
158
>>> strip_wrapped_string(annotation_syntax.__annotations__['kwargs'])
160
>>> strip_wrapped_string(annotation_syntax.__annotations__['return'])
168
@cython.annotation_typing(True)
169
def repr_returns_str(x) -> str:
171
>>> repr_returns_str(123)