Path: blob/master/ invest-robot-contest_TinkoffBotTwitch-main/venv/lib/python3.8/site-packages/attr/_compat.py
7762 views
# SPDX-License-Identifier: MIT12from __future__ import absolute_import, division, print_function34import platform5import sys6import threading7import types8import warnings91011PY2 = sys.version_info[0] == 212PYPY = platform.python_implementation() == "PyPy"13PY36 = sys.version_info[:2] >= (3, 6)14HAS_F_STRINGS = PY3615PY310 = sys.version_info[:2] >= (3, 10)161718if PYPY or PY36:19ordered_dict = dict20else:21from collections import OrderedDict2223ordered_dict = OrderedDict242526if PY2:27from collections import Mapping, Sequence2829from UserDict import IterableUserDict3031# We 'bundle' isclass instead of using inspect as importing inspect is32# fairly expensive (order of 10-15 ms for a modern machine in 2016)33def isclass(klass):34return isinstance(klass, (type, types.ClassType))3536def new_class(name, bases, kwds, exec_body):37"""38A minimal stub of types.new_class that we need for make_class.39"""40ns = {}41exec_body(ns)4243return type(name, bases, ns)4445# TYPE is used in exceptions, repr(int) is different on Python 2 and 3.46TYPE = "type"4748def iteritems(d):49return d.iteritems()5051# Python 2 is bereft of a read-only dict proxy, so we make one!52class ReadOnlyDict(IterableUserDict):53"""54Best-effort read-only dict wrapper.55"""5657def __setitem__(self, key, val):58# We gently pretend we're a Python 3 mappingproxy.59raise TypeError(60"'mappingproxy' object does not support item assignment"61)6263def update(self, _):64# We gently pretend we're a Python 3 mappingproxy.65raise AttributeError(66"'mappingproxy' object has no attribute 'update'"67)6869def __delitem__(self, _):70# We gently pretend we're a Python 3 mappingproxy.71raise TypeError(72"'mappingproxy' object does not support item deletion"73)7475def clear(self):76# We gently pretend we're a Python 3 mappingproxy.77raise AttributeError(78"'mappingproxy' object has no attribute 'clear'"79)8081def pop(self, key, default=None):82# We gently pretend we're a Python 3 mappingproxy.83raise AttributeError(84"'mappingproxy' object has no attribute 'pop'"85)8687def popitem(self):88# We gently pretend we're a Python 3 mappingproxy.89raise AttributeError(90"'mappingproxy' object has no attribute 'popitem'"91)9293def setdefault(self, key, default=None):94# We gently pretend we're a Python 3 mappingproxy.95raise AttributeError(96"'mappingproxy' object has no attribute 'setdefault'"97)9899def __repr__(self):100# Override to be identical to the Python 3 version.101return "mappingproxy(" + repr(self.data) + ")"102103def metadata_proxy(d):104res = ReadOnlyDict()105res.data.update(d) # We blocked update, so we have to do it like this.106return res107108def just_warn(*args, **kw): # pragma: no cover109"""110We only warn on Python 3 because we are not aware of any concrete111consequences of not setting the cell on Python 2.112"""113114else: # Python 3 and later.115from collections.abc import Mapping, Sequence # noqa116117def just_warn(*args, **kw):118"""119We only warn on Python 3 because we are not aware of any concrete120consequences of not setting the cell on Python 2.121"""122warnings.warn(123"Running interpreter doesn't sufficiently support code object "124"introspection. Some features like bare super() or accessing "125"__class__ will not work with slotted classes.",126RuntimeWarning,127stacklevel=2,128)129130def isclass(klass):131return isinstance(klass, type)132133TYPE = "class"134135def iteritems(d):136return d.items()137138new_class = types.new_class139140def metadata_proxy(d):141return types.MappingProxyType(dict(d))142143144def make_set_closure_cell():145"""Return a function of two arguments (cell, value) which sets146the value stored in the closure cell `cell` to `value`.147"""148# pypy makes this easy. (It also supports the logic below, but149# why not do the easy/fast thing?)150if PYPY:151152def set_closure_cell(cell, value):153cell.__setstate__((value,))154155return set_closure_cell156157# Otherwise gotta do it the hard way.158159# Create a function that will set its first cellvar to `value`.160def set_first_cellvar_to(value):161x = value162return163164# This function will be eliminated as dead code, but165# not before its reference to `x` forces `x` to be166# represented as a closure cell rather than a local.167def force_x_to_be_a_cell(): # pragma: no cover168return x169170try:171# Extract the code object and make sure our assumptions about172# the closure behavior are correct.173if PY2:174co = set_first_cellvar_to.func_code175else:176co = set_first_cellvar_to.__code__177if co.co_cellvars != ("x",) or co.co_freevars != ():178raise AssertionError # pragma: no cover179180# Convert this code object to a code object that sets the181# function's first _freevar_ (not cellvar) to the argument.182if sys.version_info >= (3, 8):183# CPython 3.8+ has an incompatible CodeType signature184# (added a posonlyargcount argument) but also added185# CodeType.replace() to do this without counting parameters.186set_first_freevar_code = co.replace(187co_cellvars=co.co_freevars, co_freevars=co.co_cellvars188)189else:190args = [co.co_argcount]191if not PY2:192args.append(co.co_kwonlyargcount)193args.extend(194[195co.co_nlocals,196co.co_stacksize,197co.co_flags,198co.co_code,199co.co_consts,200co.co_names,201co.co_varnames,202co.co_filename,203co.co_name,204co.co_firstlineno,205co.co_lnotab,206# These two arguments are reversed:207co.co_cellvars,208co.co_freevars,209]210)211set_first_freevar_code = types.CodeType(*args)212213def set_closure_cell(cell, value):214# Create a function using the set_first_freevar_code,215# whose first closure cell is `cell`. Calling it will216# change the value of that cell.217setter = types.FunctionType(218set_first_freevar_code, {}, "setter", (), (cell,)219)220# And call it to set the cell.221setter(value)222223# Make sure it works on this interpreter:224def make_func_with_cell():225x = None226227def func():228return x # pragma: no cover229230return func231232if PY2:233cell = make_func_with_cell().func_closure[0]234else:235cell = make_func_with_cell().__closure__[0]236set_closure_cell(cell, 100)237if cell.cell_contents != 100:238raise AssertionError # pragma: no cover239240except Exception:241return just_warn242else:243return set_closure_cell244245246set_closure_cell = make_set_closure_cell()247248# Thread-local global to track attrs instances which are already being repr'd.249# This is needed because there is no other (thread-safe) way to pass info250# about the instances that are already being repr'd through the call stack251# in order to ensure we don't perform infinite recursion.252#253# For instance, if an instance contains a dict which contains that instance,254# we need to know that we're already repr'ing the outside instance from within255# the dict's repr() call.256#257# This lives here rather than in _make.py so that the functions in _make.py258# don't have a direct reference to the thread-local in their globals dict.259# If they have such a reference, it breaks cloudpickle.260repr_context = threading.local()261262263