forked from microsoft/debugpy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompat.py
More file actions
215 lines (162 loc) · 5.71 KB
/
compat.py
File metadata and controls
215 lines (162 loc) · 5.71 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# Copyright (c) Microsoft Corporation. All rights reserved.
# # Licensed under the MIT License. See LICENSE in the project root
# for license information.
from __future__ import absolute_import, division, print_function, unicode_literals
"""Python 2/3 compatibility helpers.
"""
import functools
import inspect
import itertools
import sys
from debugpy.common import fmt
try:
import __builtin__ as builtins
except ImportError:
import builtins
try:
unicode = builtins.unicode
bytes = builtins.str
except AttributeError:
unicode = builtins.str
bytes = builtins.bytes
try:
xrange = builtins.xrange
except AttributeError:
xrange = builtins.range
try:
izip = itertools.izip
except AttributeError:
izip = builtins.zip
try:
reload = builtins.reload
except AttributeError:
from importlib import reload # noqa
try:
import queue
except ImportError:
import Queue as queue # noqa
def force_unicode(s, encoding, errors="strict"):
"""Converts s to Unicode, using the provided encoding. If s is already Unicode,
it is returned as is.
"""
return s.decode(encoding, errors) if isinstance(s, bytes) else unicode(s)
def force_bytes(s, encoding, errors="strict"):
"""Converts s to bytes, using the provided encoding. If s is already bytes,
it is returned as is.
If errors="strict" and s is bytes, its encoding is verified by decoding it;
UnicodeError is raised if it cannot be decoded.
"""
if isinstance(s, unicode):
return s.encode(encoding, errors)
else:
s = bytes(s)
if errors == "strict":
# Return value ignored - invoked solely for verification.
s.decode(encoding, errors)
return s
def force_str(s, encoding="ascii", errors="strict"):
"""Converts s to str (which is bytes on Python 2, and unicode on Python 3), using
the provided encoding if necessary. If s is already str, it is returned as is.
If errors="strict", str is bytes, and s is str, its encoding is verified by decoding
it; UnicodeError is raised if it cannot be decoded.
"""
return (force_bytes if str is bytes else force_unicode)(s, encoding, errors)
def force_ascii(s, errors="strict"):
"""Same as force_bytes(s, "ascii", errors)
"""
return force_bytes(s, "ascii", errors)
def force_utf8(s, errors="strict"):
"""Same as force_bytes(s, "utf8", errors)
"""
return force_bytes(s, "utf8", errors)
def filename(s, errors="strict"):
"""Same as force_unicode(s, sys.getfilesystemencoding(), errors)
"""
return force_unicode(s, sys.getfilesystemencoding(), errors)
def filename_bytes(s, errors="strict"):
"""Same as force_bytes(s, sys.getfilesystemencoding(), errors)
"""
return force_bytes(s, sys.getfilesystemencoding(), errors)
def filename_str(s, errors="strict"):
"""Same as force_str(s, sys.getfilesystemencoding(), errors)
"""
return force_str(s, sys.getfilesystemencoding(), errors)
def nameof(obj, quote=False):
"""Returns the most descriptive name of a Python module, class, or function,
as a Unicode string
If quote=True, name is quoted with repr().
Best-effort, but guaranteed to not fail - always returns something.
"""
try:
name = obj.__qualname__
except Exception:
try:
name = obj.__name__
except Exception:
# Fall back to raw repr(), and skip quoting.
try:
name = repr(obj)
except Exception:
return "<unknown>"
else:
quote = False
if quote:
try:
name = repr(name)
except Exception:
pass
return force_unicode(name, "utf-8", "replace")
def unicode_repr(obj):
"""Like repr(), but guarantees that the result is Unicode even on Python 2.
"""
return force_unicode(repr(obj), "ascii")
def srcnameof(obj):
"""Returns the most descriptive name of a Python module, class, or function,
including source information (filename and linenumber), if available.
Best-effort, but guaranteed to not fail - always returns something.
"""
name = nameof(obj, quote=True)
# Get the source information if possible.
try:
src_file = filename(inspect.getsourcefile(obj), "replace")
except Exception:
pass
else:
name += fmt(" (file {0!r}", src_file)
try:
_, src_lineno = inspect.getsourcelines(obj)
except Exception:
pass
else:
name += fmt(", line {0}", src_lineno)
name += ")"
return name
def kwonly(f):
"""Makes all arguments with default values keyword-only.
If the default value is kwonly.required, then the argument must be specified.
"""
try:
inspect.getfullargspec
except AttributeError:
arg_names, args_name, kwargs_name, arg_defaults = inspect.getargspec(f)
else:
arg_names, args_name, kwargs_name, arg_defaults, _, _, _ = inspect.getfullargspec(f)
assert args_name is None and kwargs_name is None
argc = len(arg_names)
pos_argc = argc - len(arg_defaults)
required_names = {
name
for name, val in zip(arg_names[pos_argc:], arg_defaults)
if val is kwonly.required
}
@functools.wraps(f)
def kwonly_f(*args, **kwargs):
if len(args) > pos_argc:
raise TypeError("too many positional arguments")
if not required_names.issubset(kwargs):
missing_names = required_names.difference(kwargs)
missing_names = ", ".join(repr(s) for s in missing_names)
raise TypeError("missing required keyword-only arguments: " + missing_names)
return f(*args, **kwargs)
return kwonly_f
kwonly.required = object()