Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../proc/self/root/lib64/python3....
File: abc.py
# Copyright 2007 Google, Inc. All Rights Reserved.
[0] Fix | Delete
# Licensed to PSF under a Contributor Agreement.
[1] Fix | Delete
[2] Fix | Delete
"""Abstract Base Classes (ABCs) according to PEP 3119."""
[3] Fix | Delete
[4] Fix | Delete
from _weakrefset import WeakSet
[5] Fix | Delete
[6] Fix | Delete
[7] Fix | Delete
def abstractmethod(funcobj):
[8] Fix | Delete
"""A decorator indicating abstract methods.
[9] Fix | Delete
[10] Fix | Delete
Requires that the metaclass is ABCMeta or derived from it. A
[11] Fix | Delete
class that has a metaclass derived from ABCMeta cannot be
[12] Fix | Delete
instantiated unless all of its abstract methods are overridden.
[13] Fix | Delete
The abstract methods can be called using any of the normal
[14] Fix | Delete
'super' call mechanisms.
[15] Fix | Delete
[16] Fix | Delete
Usage:
[17] Fix | Delete
[18] Fix | Delete
class C(metaclass=ABCMeta):
[19] Fix | Delete
@abstractmethod
[20] Fix | Delete
def my_abstract_method(self, ...):
[21] Fix | Delete
...
[22] Fix | Delete
"""
[23] Fix | Delete
funcobj.__isabstractmethod__ = True
[24] Fix | Delete
return funcobj
[25] Fix | Delete
[26] Fix | Delete
[27] Fix | Delete
class abstractclassmethod(classmethod):
[28] Fix | Delete
"""
[29] Fix | Delete
A decorator indicating abstract classmethods.
[30] Fix | Delete
[31] Fix | Delete
Similar to abstractmethod.
[32] Fix | Delete
[33] Fix | Delete
Usage:
[34] Fix | Delete
[35] Fix | Delete
class C(metaclass=ABCMeta):
[36] Fix | Delete
@abstractclassmethod
[37] Fix | Delete
def my_abstract_classmethod(cls, ...):
[38] Fix | Delete
...
[39] Fix | Delete
[40] Fix | Delete
'abstractclassmethod' is deprecated. Use 'classmethod' with
[41] Fix | Delete
'abstractmethod' instead.
[42] Fix | Delete
"""
[43] Fix | Delete
[44] Fix | Delete
__isabstractmethod__ = True
[45] Fix | Delete
[46] Fix | Delete
def __init__(self, callable):
[47] Fix | Delete
callable.__isabstractmethod__ = True
[48] Fix | Delete
super().__init__(callable)
[49] Fix | Delete
[50] Fix | Delete
[51] Fix | Delete
class abstractstaticmethod(staticmethod):
[52] Fix | Delete
"""
[53] Fix | Delete
A decorator indicating abstract staticmethods.
[54] Fix | Delete
[55] Fix | Delete
Similar to abstractmethod.
[56] Fix | Delete
[57] Fix | Delete
Usage:
[58] Fix | Delete
[59] Fix | Delete
class C(metaclass=ABCMeta):
[60] Fix | Delete
@abstractstaticmethod
[61] Fix | Delete
def my_abstract_staticmethod(...):
[62] Fix | Delete
...
[63] Fix | Delete
[64] Fix | Delete
'abstractstaticmethod' is deprecated. Use 'staticmethod' with
[65] Fix | Delete
'abstractmethod' instead.
[66] Fix | Delete
"""
[67] Fix | Delete
[68] Fix | Delete
__isabstractmethod__ = True
[69] Fix | Delete
[70] Fix | Delete
def __init__(self, callable):
[71] Fix | Delete
callable.__isabstractmethod__ = True
[72] Fix | Delete
super().__init__(callable)
[73] Fix | Delete
[74] Fix | Delete
[75] Fix | Delete
class abstractproperty(property):
[76] Fix | Delete
"""
[77] Fix | Delete
A decorator indicating abstract properties.
[78] Fix | Delete
[79] Fix | Delete
Requires that the metaclass is ABCMeta or derived from it. A
[80] Fix | Delete
class that has a metaclass derived from ABCMeta cannot be
[81] Fix | Delete
instantiated unless all of its abstract properties are overridden.
[82] Fix | Delete
The abstract properties can be called using any of the normal
[83] Fix | Delete
'super' call mechanisms.
[84] Fix | Delete
[85] Fix | Delete
Usage:
[86] Fix | Delete
[87] Fix | Delete
class C(metaclass=ABCMeta):
[88] Fix | Delete
@abstractproperty
[89] Fix | Delete
def my_abstract_property(self):
[90] Fix | Delete
...
[91] Fix | Delete
[92] Fix | Delete
This defines a read-only property; you can also define a read-write
[93] Fix | Delete
abstract property using the 'long' form of property declaration:
[94] Fix | Delete
[95] Fix | Delete
class C(metaclass=ABCMeta):
[96] Fix | Delete
def getx(self): ...
[97] Fix | Delete
def setx(self, value): ...
[98] Fix | Delete
x = abstractproperty(getx, setx)
[99] Fix | Delete
[100] Fix | Delete
'abstractproperty' is deprecated. Use 'property' with 'abstractmethod'
[101] Fix | Delete
instead.
[102] Fix | Delete
"""
[103] Fix | Delete
[104] Fix | Delete
__isabstractmethod__ = True
[105] Fix | Delete
[106] Fix | Delete
[107] Fix | Delete
class ABCMeta(type):
[108] Fix | Delete
[109] Fix | Delete
"""Metaclass for defining Abstract Base Classes (ABCs).
[110] Fix | Delete
[111] Fix | Delete
Use this metaclass to create an ABC. An ABC can be subclassed
[112] Fix | Delete
directly, and then acts as a mix-in class. You can also register
[113] Fix | Delete
unrelated concrete classes (even built-in classes) and unrelated
[114] Fix | Delete
ABCs as 'virtual subclasses' -- these and their descendants will
[115] Fix | Delete
be considered subclasses of the registering ABC by the built-in
[116] Fix | Delete
issubclass() function, but the registering ABC won't show up in
[117] Fix | Delete
their MRO (Method Resolution Order) nor will method
[118] Fix | Delete
implementations defined by the registering ABC be callable (not
[119] Fix | Delete
even via super()).
[120] Fix | Delete
[121] Fix | Delete
"""
[122] Fix | Delete
[123] Fix | Delete
# A global counter that is incremented each time a class is
[124] Fix | Delete
# registered as a virtual subclass of anything. It forces the
[125] Fix | Delete
# negative cache to be cleared before its next use.
[126] Fix | Delete
# Note: this counter is private. Use `abc.get_cache_token()` for
[127] Fix | Delete
# external code.
[128] Fix | Delete
_abc_invalidation_counter = 0
[129] Fix | Delete
[130] Fix | Delete
def __new__(mcls, name, bases, namespace, **kwargs):
[131] Fix | Delete
cls = super().__new__(mcls, name, bases, namespace, **kwargs)
[132] Fix | Delete
# Compute set of abstract method names
[133] Fix | Delete
abstracts = {name
[134] Fix | Delete
for name, value in namespace.items()
[135] Fix | Delete
if getattr(value, "__isabstractmethod__", False)}
[136] Fix | Delete
for base in bases:
[137] Fix | Delete
for name in getattr(base, "__abstractmethods__", set()):
[138] Fix | Delete
value = getattr(cls, name, None)
[139] Fix | Delete
if getattr(value, "__isabstractmethod__", False):
[140] Fix | Delete
abstracts.add(name)
[141] Fix | Delete
cls.__abstractmethods__ = frozenset(abstracts)
[142] Fix | Delete
# Set up inheritance registry
[143] Fix | Delete
cls._abc_registry = WeakSet()
[144] Fix | Delete
cls._abc_cache = WeakSet()
[145] Fix | Delete
cls._abc_negative_cache = WeakSet()
[146] Fix | Delete
cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter
[147] Fix | Delete
return cls
[148] Fix | Delete
[149] Fix | Delete
def register(cls, subclass):
[150] Fix | Delete
"""Register a virtual subclass of an ABC.
[151] Fix | Delete
[152] Fix | Delete
Returns the subclass, to allow usage as a class decorator.
[153] Fix | Delete
"""
[154] Fix | Delete
if not isinstance(subclass, type):
[155] Fix | Delete
raise TypeError("Can only register classes")
[156] Fix | Delete
if issubclass(subclass, cls):
[157] Fix | Delete
return subclass # Already a subclass
[158] Fix | Delete
# Subtle: test for cycles *after* testing for "already a subclass";
[159] Fix | Delete
# this means we allow X.register(X) and interpret it as a no-op.
[160] Fix | Delete
if issubclass(cls, subclass):
[161] Fix | Delete
# This would create a cycle, which is bad for the algorithm below
[162] Fix | Delete
raise RuntimeError("Refusing to create an inheritance cycle")
[163] Fix | Delete
cls._abc_registry.add(subclass)
[164] Fix | Delete
ABCMeta._abc_invalidation_counter += 1 # Invalidate negative cache
[165] Fix | Delete
return subclass
[166] Fix | Delete
[167] Fix | Delete
def _dump_registry(cls, file=None):
[168] Fix | Delete
"""Debug helper to print the ABC registry."""
[169] Fix | Delete
print("Class: %s.%s" % (cls.__module__, cls.__qualname__), file=file)
[170] Fix | Delete
print("Inv.counter: %s" % ABCMeta._abc_invalidation_counter, file=file)
[171] Fix | Delete
for name in sorted(cls.__dict__):
[172] Fix | Delete
if name.startswith("_abc_"):
[173] Fix | Delete
value = getattr(cls, name)
[174] Fix | Delete
if isinstance(value, WeakSet):
[175] Fix | Delete
value = set(value)
[176] Fix | Delete
print("%s: %r" % (name, value), file=file)
[177] Fix | Delete
[178] Fix | Delete
def __instancecheck__(cls, instance):
[179] Fix | Delete
"""Override for isinstance(instance, cls)."""
[180] Fix | Delete
# Inline the cache checking
[181] Fix | Delete
subclass = instance.__class__
[182] Fix | Delete
if subclass in cls._abc_cache:
[183] Fix | Delete
return True
[184] Fix | Delete
subtype = type(instance)
[185] Fix | Delete
if subtype is subclass:
[186] Fix | Delete
if (cls._abc_negative_cache_version ==
[187] Fix | Delete
ABCMeta._abc_invalidation_counter and
[188] Fix | Delete
subclass in cls._abc_negative_cache):
[189] Fix | Delete
return False
[190] Fix | Delete
# Fall back to the subclass check.
[191] Fix | Delete
return cls.__subclasscheck__(subclass)
[192] Fix | Delete
return any(cls.__subclasscheck__(c) for c in {subclass, subtype})
[193] Fix | Delete
[194] Fix | Delete
def __subclasscheck__(cls, subclass):
[195] Fix | Delete
"""Override for issubclass(subclass, cls)."""
[196] Fix | Delete
# Check cache
[197] Fix | Delete
if subclass in cls._abc_cache:
[198] Fix | Delete
return True
[199] Fix | Delete
# Check negative cache; may have to invalidate
[200] Fix | Delete
if cls._abc_negative_cache_version < ABCMeta._abc_invalidation_counter:
[201] Fix | Delete
# Invalidate the negative cache
[202] Fix | Delete
cls._abc_negative_cache = WeakSet()
[203] Fix | Delete
cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter
[204] Fix | Delete
elif subclass in cls._abc_negative_cache:
[205] Fix | Delete
return False
[206] Fix | Delete
# Check the subclass hook
[207] Fix | Delete
ok = cls.__subclasshook__(subclass)
[208] Fix | Delete
if ok is not NotImplemented:
[209] Fix | Delete
assert isinstance(ok, bool)
[210] Fix | Delete
if ok:
[211] Fix | Delete
cls._abc_cache.add(subclass)
[212] Fix | Delete
else:
[213] Fix | Delete
cls._abc_negative_cache.add(subclass)
[214] Fix | Delete
return ok
[215] Fix | Delete
# Check if it's a direct subclass
[216] Fix | Delete
if cls in getattr(subclass, '__mro__', ()):
[217] Fix | Delete
cls._abc_cache.add(subclass)
[218] Fix | Delete
return True
[219] Fix | Delete
# Check if it's a subclass of a registered class (recursive)
[220] Fix | Delete
for rcls in cls._abc_registry:
[221] Fix | Delete
if issubclass(subclass, rcls):
[222] Fix | Delete
cls._abc_cache.add(subclass)
[223] Fix | Delete
return True
[224] Fix | Delete
# Check if it's a subclass of a subclass (recursive)
[225] Fix | Delete
for scls in cls.__subclasses__():
[226] Fix | Delete
if issubclass(subclass, scls):
[227] Fix | Delete
cls._abc_cache.add(subclass)
[228] Fix | Delete
return True
[229] Fix | Delete
# No dice; update negative cache
[230] Fix | Delete
cls._abc_negative_cache.add(subclass)
[231] Fix | Delete
return False
[232] Fix | Delete
[233] Fix | Delete
[234] Fix | Delete
class ABC(metaclass=ABCMeta):
[235] Fix | Delete
"""Helper class that provides a standard way to create an ABC using
[236] Fix | Delete
inheritance.
[237] Fix | Delete
"""
[238] Fix | Delete
pass
[239] Fix | Delete
[240] Fix | Delete
[241] Fix | Delete
def get_cache_token():
[242] Fix | Delete
"""Returns the current ABC cache token.
[243] Fix | Delete
[244] Fix | Delete
The token is an opaque object (supporting equality testing) identifying the
[245] Fix | Delete
current version of the ABC cache for virtual subclasses. The token changes
[246] Fix | Delete
with every call to ``register()`` on any ABC.
[247] Fix | Delete
"""
[248] Fix | Delete
return ABCMeta._abc_invalidation_counter
[249] Fix | Delete
[250] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function