Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/ invest-robot-contest_TinkoffBotTwitch-main/venv/lib/python3.8/site-packages/attr/_compat.py
7762 views
1
# SPDX-License-Identifier: MIT
2
3
from __future__ import absolute_import, division, print_function
4
5
import platform
6
import sys
7
import threading
8
import types
9
import warnings
10
11
12
PY2 = sys.version_info[0] == 2
13
PYPY = platform.python_implementation() == "PyPy"
14
PY36 = sys.version_info[:2] >= (3, 6)
15
HAS_F_STRINGS = PY36
16
PY310 = sys.version_info[:2] >= (3, 10)
17
18
19
if PYPY or PY36:
20
ordered_dict = dict
21
else:
22
from collections import OrderedDict
23
24
ordered_dict = OrderedDict
25
26
27
if PY2:
28
from collections import Mapping, Sequence
29
30
from UserDict import IterableUserDict
31
32
# We 'bundle' isclass instead of using inspect as importing inspect is
33
# fairly expensive (order of 10-15 ms for a modern machine in 2016)
34
def isclass(klass):
35
return isinstance(klass, (type, types.ClassType))
36
37
def new_class(name, bases, kwds, exec_body):
38
"""
39
A minimal stub of types.new_class that we need for make_class.
40
"""
41
ns = {}
42
exec_body(ns)
43
44
return type(name, bases, ns)
45
46
# TYPE is used in exceptions, repr(int) is different on Python 2 and 3.
47
TYPE = "type"
48
49
def iteritems(d):
50
return d.iteritems()
51
52
# Python 2 is bereft of a read-only dict proxy, so we make one!
53
class ReadOnlyDict(IterableUserDict):
54
"""
55
Best-effort read-only dict wrapper.
56
"""
57
58
def __setitem__(self, key, val):
59
# We gently pretend we're a Python 3 mappingproxy.
60
raise TypeError(
61
"'mappingproxy' object does not support item assignment"
62
)
63
64
def update(self, _):
65
# We gently pretend we're a Python 3 mappingproxy.
66
raise AttributeError(
67
"'mappingproxy' object has no attribute 'update'"
68
)
69
70
def __delitem__(self, _):
71
# We gently pretend we're a Python 3 mappingproxy.
72
raise TypeError(
73
"'mappingproxy' object does not support item deletion"
74
)
75
76
def clear(self):
77
# We gently pretend we're a Python 3 mappingproxy.
78
raise AttributeError(
79
"'mappingproxy' object has no attribute 'clear'"
80
)
81
82
def pop(self, key, default=None):
83
# We gently pretend we're a Python 3 mappingproxy.
84
raise AttributeError(
85
"'mappingproxy' object has no attribute 'pop'"
86
)
87
88
def popitem(self):
89
# We gently pretend we're a Python 3 mappingproxy.
90
raise AttributeError(
91
"'mappingproxy' object has no attribute 'popitem'"
92
)
93
94
def setdefault(self, key, default=None):
95
# We gently pretend we're a Python 3 mappingproxy.
96
raise AttributeError(
97
"'mappingproxy' object has no attribute 'setdefault'"
98
)
99
100
def __repr__(self):
101
# Override to be identical to the Python 3 version.
102
return "mappingproxy(" + repr(self.data) + ")"
103
104
def metadata_proxy(d):
105
res = ReadOnlyDict()
106
res.data.update(d) # We blocked update, so we have to do it like this.
107
return res
108
109
def just_warn(*args, **kw): # pragma: no cover
110
"""
111
We only warn on Python 3 because we are not aware of any concrete
112
consequences of not setting the cell on Python 2.
113
"""
114
115
else: # Python 3 and later.
116
from collections.abc import Mapping, Sequence # noqa
117
118
def just_warn(*args, **kw):
119
"""
120
We only warn on Python 3 because we are not aware of any concrete
121
consequences of not setting the cell on Python 2.
122
"""
123
warnings.warn(
124
"Running interpreter doesn't sufficiently support code object "
125
"introspection. Some features like bare super() or accessing "
126
"__class__ will not work with slotted classes.",
127
RuntimeWarning,
128
stacklevel=2,
129
)
130
131
def isclass(klass):
132
return isinstance(klass, type)
133
134
TYPE = "class"
135
136
def iteritems(d):
137
return d.items()
138
139
new_class = types.new_class
140
141
def metadata_proxy(d):
142
return types.MappingProxyType(dict(d))
143
144
145
def make_set_closure_cell():
146
"""Return a function of two arguments (cell, value) which sets
147
the value stored in the closure cell `cell` to `value`.
148
"""
149
# pypy makes this easy. (It also supports the logic below, but
150
# why not do the easy/fast thing?)
151
if PYPY:
152
153
def set_closure_cell(cell, value):
154
cell.__setstate__((value,))
155
156
return set_closure_cell
157
158
# Otherwise gotta do it the hard way.
159
160
# Create a function that will set its first cellvar to `value`.
161
def set_first_cellvar_to(value):
162
x = value
163
return
164
165
# This function will be eliminated as dead code, but
166
# not before its reference to `x` forces `x` to be
167
# represented as a closure cell rather than a local.
168
def force_x_to_be_a_cell(): # pragma: no cover
169
return x
170
171
try:
172
# Extract the code object and make sure our assumptions about
173
# the closure behavior are correct.
174
if PY2:
175
co = set_first_cellvar_to.func_code
176
else:
177
co = set_first_cellvar_to.__code__
178
if co.co_cellvars != ("x",) or co.co_freevars != ():
179
raise AssertionError # pragma: no cover
180
181
# Convert this code object to a code object that sets the
182
# function's first _freevar_ (not cellvar) to the argument.
183
if sys.version_info >= (3, 8):
184
# CPython 3.8+ has an incompatible CodeType signature
185
# (added a posonlyargcount argument) but also added
186
# CodeType.replace() to do this without counting parameters.
187
set_first_freevar_code = co.replace(
188
co_cellvars=co.co_freevars, co_freevars=co.co_cellvars
189
)
190
else:
191
args = [co.co_argcount]
192
if not PY2:
193
args.append(co.co_kwonlyargcount)
194
args.extend(
195
[
196
co.co_nlocals,
197
co.co_stacksize,
198
co.co_flags,
199
co.co_code,
200
co.co_consts,
201
co.co_names,
202
co.co_varnames,
203
co.co_filename,
204
co.co_name,
205
co.co_firstlineno,
206
co.co_lnotab,
207
# These two arguments are reversed:
208
co.co_cellvars,
209
co.co_freevars,
210
]
211
)
212
set_first_freevar_code = types.CodeType(*args)
213
214
def set_closure_cell(cell, value):
215
# Create a function using the set_first_freevar_code,
216
# whose first closure cell is `cell`. Calling it will
217
# change the value of that cell.
218
setter = types.FunctionType(
219
set_first_freevar_code, {}, "setter", (), (cell,)
220
)
221
# And call it to set the cell.
222
setter(value)
223
224
# Make sure it works on this interpreter:
225
def make_func_with_cell():
226
x = None
227
228
def func():
229
return x # pragma: no cover
230
231
return func
232
233
if PY2:
234
cell = make_func_with_cell().func_closure[0]
235
else:
236
cell = make_func_with_cell().__closure__[0]
237
set_closure_cell(cell, 100)
238
if cell.cell_contents != 100:
239
raise AssertionError # pragma: no cover
240
241
except Exception:
242
return just_warn
243
else:
244
return set_closure_cell
245
246
247
set_closure_cell = make_set_closure_cell()
248
249
# Thread-local global to track attrs instances which are already being repr'd.
250
# This is needed because there is no other (thread-safe) way to pass info
251
# about the instances that are already being repr'd through the call stack
252
# in order to ensure we don't perform infinite recursion.
253
#
254
# For instance, if an instance contains a dict which contains that instance,
255
# we need to know that we're already repr'ing the outside instance from within
256
# the dict's repr() call.
257
#
258
# This lives here rather than in _make.py so that the functions in _make.py
259
# don't have a direct reference to the thread-local in their globals dict.
260
# If they have such a reference, it breaks cloudpickle.
261
repr_context = threading.local()
262
263