comparison env/lib/python3.7/site-packages/rdflib/py3compat.py @ 5:9b1c78e6ba9c draft default tip

"planemo upload commit 6c0a8142489327ece472c84e558c47da711a9142"
author shellac
date Mon, 01 Jun 2020 08:59:25 -0400
parents 79f47841a781
children
comparison
equal deleted inserted replaced
4:79f47841a781 5:9b1c78e6ba9c
1 """
2 Utility functions and objects to ease Python 3 compatibility.
3 """
4 import sys
5 import re
6 import codecs
7 import warnings
8
9 try:
10 from functools import wraps
11 assert wraps
12 except ImportError:
13 # No-op wraps decorator
14 def wraps(f):
15 def dec(newf):
16 return newf
17 return dec
18
19
20 def cast_bytes(s, enc='utf-8'):
21 if isinstance(s, str):
22 return s.encode(enc)
23 return s
24
25 PY3 = (sys.version_info[0] >= 3)
26
27
28 def _modify_str_or_docstring(str_change_func):
29 @wraps(str_change_func)
30 def wrapper(func_or_str):
31 if isinstance(func_or_str, str):
32 func = None
33 doc = func_or_str
34 else:
35 func = func_or_str
36 doc = func.__doc__
37
38 doc = str_change_func(doc)
39
40 if func:
41 func.__doc__ = doc
42 return func
43 return doc
44 return wrapper
45
46
47 if PY3:
48 # Python 3:
49 # ---------
50 def b(s):
51 return s.encode('ascii')
52
53 def ascii(stream):
54 return codecs.getreader('ascii')(stream)
55
56 def bopen(*args, **kwargs):
57 return open(*args, mode = 'rb', **kwargs)
58
59 bytestype = bytes
60
61 # Abstract u'abc' syntax:
62 @_modify_str_or_docstring
63 def format_doctest_out(s):
64 """Python 2 version
65 "%(u)s'abc'" --> "'abc'"
66 "%(b)s'abc'" --> "b'abc'"
67 "55%(L)s" --> "55"
68 "unicode(x)" --> "str(x)"
69
70 Accepts a string or a function, so it can be used as a decorator."""
71 # s may be None if processed by Py2exe
72 if s is None:
73 return ''
74 return s % {'u': '', 'b': 'b', 'L': '', 'unicode': 'str'}
75
76 def type_cmp(a, b):
77 """Python 2 style comparison based on type"""
78 ta, tb = type(a).__name__, type(b).__name__
79 # Ugly hack: some tests rely on tuple sorting before unicode, and I
80 # don't know if that's important. Better retain it for now.
81 if ta == 'str':
82 ta = 'unicode'
83 if tb == 'str':
84 tb = 'unicode'
85 # return 1 if ta > tb else -1 if ta < tb else 0
86 if ta > tb:
87 return 1
88 elif ta < tb:
89 return -1
90 else:
91 return 0
92
93 def sign(n):
94 if n < 0:
95 return -1
96 if n > 0:
97 return 1
98 return 0
99
100 else:
101 # Python 2
102 # --------
103 def b(s):
104 return s
105
106 def ascii(stream):
107 return stream
108
109 bopen = open
110
111 bytestype = str
112
113 # Abstract u'abc' syntax:
114 @_modify_str_or_docstring
115 def format_doctest_out(s):
116 """Python 2 version
117 "%(u)s'abc'" --> "u'abc'"
118 "%(b)s'abc'" --> "'abc'"
119 "55%(L)s" --> "55L"
120
121 Accepts a string or a function, so it can be used as a decorator."""
122 # s may be None if processed by Py2exe
123 if s is None:
124 return ''
125 return s % {'u': 'u', 'b': '', 'L': 'L', 'unicode': 'unicode'}
126
127 def type_cmp(a, b):
128 # return 1 if a > b else -1 if a < b else 0
129 if a > b:
130 return 1
131 elif a < b:
132 return -1
133 else:
134 return 0
135
136 def sign(n):
137 return cmp(n, 0)
138
139 r_unicodeEscape = re.compile(r'(\\u[0-9A-Fa-f]{4}|\\U[0-9A-Fa-f]{8})')
140
141 def _unicodeExpand(s):
142 return r_unicodeEscape.sub(lambda m: chr(int(m.group(0)[2:], 16)), s)
143
144 narrow_build = False
145 try:
146 chr(0x10FFFF)
147 except ValueError:
148 narrow_build = True
149
150 if narrow_build:
151 def _unicodeExpand(s):
152 try:
153 return r_unicodeEscape.sub(
154 lambda m: chr(int(m.group(0)[2:], 16)), s)
155 except ValueError:
156 warnings.warn(
157 'Encountered a unicode char > 0xFFFF in a narrow python build. '
158 'Trying to degrade gracefully, but this can cause problems '
159 'later when working with the string:\n%s' % s)
160 return r_unicodeEscape.sub(
161 lambda m: codecs.decode(m.group(0), 'unicode_escape'), s)
162
163
164 def decodeStringEscape(s):
165
166 """
167 s is byte-string - replace \ escapes in string
168 """
169
170 if not PY3:
171 s = s.decode('string-escape')
172 else:
173 s = s.replace('\\t', '\t')
174 s = s.replace('\\n', '\n')
175 s = s.replace('\\r', '\r')
176 s = s.replace('\\b', '\b')
177 s = s.replace('\\f', '\f')
178 s = s.replace('\\"', '"')
179 s = s.replace("\\'", "'")
180 s = s.replace('\\\\', '\\')
181
182 return s
183 #return _unicodeExpand(s) # hmm - string escape doesn't do unicode escaping
184
185 def decodeUnicodeEscape(s):
186 """
187 s is a unicode string
188 replace \n and \\u00AC unicode escapes
189 """
190 if not PY3:
191 s = s.encode('utf-8').decode('string-escape')
192 s = _unicodeExpand(s)
193 else:
194 s = s.replace('\\t', '\t')
195 s = s.replace('\\n', '\n')
196 s = s.replace('\\r', '\r')
197 s = s.replace('\\b', '\b')
198 s = s.replace('\\f', '\f')
199 s = s.replace('\\"', '"')
200 s = s.replace("\\'", "'")
201 s = s.replace('\\\\', '\\')
202
203 s = _unicodeExpand(s) # hmm - string escape doesn't do unicode escaping
204
205 return s