File: builtin_len.pyx

package info (click to toggle)
cython 3.0.11%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 19,092 kB
  • sloc: python: 83,539; ansic: 18,831; cpp: 1,402; xml: 1,031; javascript: 511; makefile: 403; sh: 204; sed: 11
file content (134 lines) | stat: -rw-r--r-- 2,876 bytes parent folder | download | duplicates (11)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# -*- coding: utf-8 -*-

cimport cython

unicode_str = u'ab jd  üöä ôñ ÄÖ'
bytes_str   = b'ab jd  sdflk as sa  sadas asdas fsdf '

_frozenset = frozenset
_set = set

@cython.test_assert_path_exists(
    "//CoerceToPyTypeNode",
    "//PythonCapiCallNode")
def len_unicode(unicode s):
    """
    >>> len(unicode_str)
    16
    >>> len_unicode(unicode_str)
    16
    >>> len_unicode(None)
    Traceback (most recent call last):
    TypeError: object of type 'NoneType' has no len()
    """
    return len(s)

@cython.test_assert_path_exists(
    "//CoerceToPyTypeNode",
    "//PythonCapiCallNode")
def len_bytes(bytes s):
    """
    >>> len(bytes_str)
    37
    >>> len_bytes(bytes_str)
    37
    >>> len_bytes(None)
    Traceback (most recent call last):
    TypeError: object of type 'NoneType' has no len()
    """
    return len(s)

#@cython.test_assert_path_exists(
#    "//CoerceToPyTypeNode",
#    "//PythonCapiCallNode")
def len_str(str s):
    """
    >>> len('abcdefg')
    7
    >>> len_str('abcdefg')
    7
    >>> len_unicode(None)
    Traceback (most recent call last):
    TypeError: object of type 'NoneType' has no len()
    """
    return len(s)

@cython.test_assert_path_exists(
    "//CoerceToPyTypeNode",
    "//PythonCapiCallNode")
def len_list(list s):
    """
    >>> l = [1,2,3,4]
    >>> len(l)
    4
    >>> len_list(l)
    4
    >>> len_list(None)
    Traceback (most recent call last):
    TypeError: object of type 'NoneType' has no len()
    """
    return len(s)

@cython.test_assert_path_exists(
    "//CoerceToPyTypeNode",
    "//PythonCapiCallNode")
def len_tuple(tuple s):
    """
    >>> t = (1,2,3,4)
    >>> len(t)
    4
    >>> len_tuple(t)
    4
    >>> len_tuple(None)
    Traceback (most recent call last):
    TypeError: object of type 'NoneType' has no len()
    """
    return len(s)

@cython.test_assert_path_exists(
    "//CoerceToPyTypeNode",
    "//PythonCapiCallNode")
def len_dict(dict s):
    """
    >>> d = dict(a=1, b=2, c=3, d=4)
    >>> len(d)
    4
    >>> len_dict(d)
    4
    >>> len_dict(None)
    Traceback (most recent call last):
    TypeError: object of type 'NoneType' has no len()
    """
    return len(s)

@cython.test_assert_path_exists(
    "//CoerceToPyTypeNode",
    "//PythonCapiCallNode")
def len_set(set s):
    """
    >>> s = _set((1,2,3,4))
    >>> len(s)
    4
    >>> len_set(s)
    4
    >>> len_set(None)
    Traceback (most recent call last):
    TypeError: object of type 'NoneType' has no len()
    """
    return len(s)

@cython.test_assert_path_exists(
    "//CoerceToPyTypeNode",
    "//PythonCapiCallNode")
def len_frozenset(frozenset s):
    """
    >>> s = _frozenset((1,2,3,4))
    >>> len(s)
    4
    >>> len_frozenset(s)
    4
    >>> len_set(None)
    Traceback (most recent call last):
    TypeError: object of type 'NoneType' has no len()
    """
    return len(s)