cython

Форк
0
/
unicodeencode.pyx 
109 строк · 2.6 Кб
1
# -*- coding: utf-8 -*-
2

3
__doc__ = u"""
4
>>> len(u)
5
15
6
"""
7

8
cimport cython
9

10
_bytes = bytes
11

12
cdef unicode text = u'abcäöüöéèâÁÀABC'
13

14
u = text
15

16
def default():
17
    """
18
    >>> default() == 'abcdefg'.encode()
19
    True
20
    """
21
    return u'abcdefg'.encode()
22

23
def encode_non_constant(encoding):
24
    """
25
    >>> isinstance(encode_non_constant('utf8'), _bytes)
26
    True
27
    >>> encode_non_constant('utf8') == u.encode('UTF-8')
28
    True
29
    """
30
    return text.encode(encoding)
31

32
@cython.test_assert_path_exists('//PythonCapiFunctionNode[@cname = "PyUnicode_AsUTF8String"]')
33
def utf8():
34
    """
35
    >>> isinstance(utf8(), _bytes)
36
    True
37
    >>> utf8() == u.encode('UTF-8')
38
    True
39
    """
40
    return text.encode(u'UTF-8')
41

42
@cython.test_assert_path_exists('//PythonCapiFunctionNode[@cname = "PyUnicode_AsUTF8String"]')
43
def utf8_strict():
44
    """
45
    >>> isinstance(utf8_strict(), _bytes)
46
    True
47
    >>> utf8_strict() == u.encode('UTF-8', 'strict')
48
    True
49
    """
50
    return text.encode(u'UTF-8', u'strict')
51

52
@cython.test_assert_path_exists('//PythonCapiFunctionNode[@cname = "PyUnicode_AsUTF8String"]')
53
def utf8_str_strict():
54
    """
55
    >>> isinstance(utf8_str_strict(), _bytes)
56
    True
57
    >>> utf8_str_strict() == u.encode('UTF-8', 'strict')
58
    True
59
    """
60
    return text.encode('UTF-8', 'strict')
61

62
@cython.test_assert_path_exists('//PythonCapiFunctionNode[@cname = "PyUnicode_AsUTF8String"]')
63
def utf8_bytes_strict():
64
    """
65
    >>> isinstance(utf8_bytes_strict(), _bytes)
66
    True
67
    >>> utf8_bytes_strict() == u.encode('UTF-8', 'strict')
68
    True
69
    """
70
    return text.encode(b'UTF-8', b'strict')
71

72
@cython.test_assert_path_exists('//PythonCapiFunctionNode[@cname = "PyUnicode_AsEncodedString"]')
73
def ascii_replace():
74
    """
75
    >>> isinstance(ascii_replace(), _bytes)
76
    True
77
    >>> ascii_replace() == u.encode('ASCII', 'replace')
78
    True
79
    """
80
    return text.encode(u'ASCII', u'replace')
81

82
def cp850_strict():
83
    """
84
    >>> isinstance(cp850_strict(), _bytes)
85
    True
86
    >>> cp850_strict() == u.encode('cp850', 'strict')
87
    True
88
    """
89
    return text.encode(u'cp850', u'strict')
90

91
@cython.test_assert_path_exists('//PythonCapiFunctionNode[@cname = "PyUnicode_AsLatin1String"]')
92
def latin1():
93
    """
94
    >>> isinstance(latin1(), _bytes)
95
    True
96
    >>> latin1() == u.encode('latin-1')
97
    True
98
    """
99
    return text.encode(u'latin-1')
100

101
@cython.test_fail_if_path_exists('//PythonCapiFunctionNode', '//SimpleCallNode')
102
def latin1_constant():
103
    """
104
    >>> isinstance(latin1_constant(), _bytes)
105
    True
106
    >>> latin1_constant() == latin1()
107
    True
108
    """
109
    return u'abcäöüöéèâÁÀABC'.encode('latin1')
110

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

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

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

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