Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2....
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
import types
[5] Fix | Delete
[6] Fix | Delete
from _weakrefset import WeakSet
[7] Fix | Delete
[8] Fix | Delete
# Instance of old-style class
[9] Fix | Delete
class _C: pass
[10] Fix | Delete
_InstanceType = type(_C())
[11] Fix | Delete
[12] Fix | Delete
[13] Fix | Delete
def abstractmethod(funcobj):
[14] Fix | Delete
"""A decorator indicating abstract methods.
[15] Fix | Delete
[16] Fix | Delete
Requires that the metaclass is ABCMeta or derived from it. A
[17] Fix | Delete
class that has a metaclass derived from ABCMeta cannot be
[18] Fix | Delete
instantiated unless all of its abstract methods are overridden.
[19] Fix | Delete
The abstract methods can be called using any of the normal
[20] Fix | Delete
'super' call mechanisms.
[21] Fix | Delete
[22] Fix | Delete
Usage:
[23] Fix | Delete
[24] Fix | Delete
class C:
[25] Fix | Delete
__metaclass__ = ABCMeta
[26] Fix | Delete
@abstractmethod
[27] Fix | Delete
def my_abstract_method(self, ...):
[28] Fix | Delete
...
[29] Fix | Delete
"""
[30] Fix | Delete
funcobj.__isabstractmethod__ = True
[31] Fix | Delete
return funcobj
[32] Fix | Delete
[33] Fix | Delete
[34] Fix | Delete
class abstractproperty(property):
[35] Fix | Delete
"""A decorator indicating abstract properties.
[36] Fix | Delete
[37] Fix | Delete
Requires that the metaclass is ABCMeta or derived from it. A
[38] Fix | Delete
class that has a metaclass derived from ABCMeta cannot be
[39] Fix | Delete
instantiated unless all of its abstract properties are overridden.
[40] Fix | Delete
The abstract properties can be called using any of the normal
[41] Fix | Delete
'super' call mechanisms.
[42] Fix | Delete
[43] Fix | Delete
Usage:
[44] Fix | Delete
[45] Fix | Delete
class C:
[46] Fix | Delete
__metaclass__ = ABCMeta
[47] Fix | Delete
@abstractproperty
[48] Fix | Delete
def my_abstract_property(self):
[49] Fix | Delete
...
[50] Fix | Delete
[51] Fix | Delete
This defines a read-only property; you can also define a read-write
[52] Fix | Delete
abstract property using the 'long' form of property declaration:
[53] Fix | Delete
[54] Fix | Delete
class C:
[55] Fix | Delete
__metaclass__ = ABCMeta
[56] Fix | Delete
def getx(self): ...
[57] Fix | Delete
def setx(self, value): ...
[58] Fix | Delete
x = abstractproperty(getx, setx)
[59] Fix | Delete
"""
[60] Fix | Delete
__isabstractmethod__ = True
[61] Fix | Delete
[62] Fix | Delete
[63] Fix | Delete
class ABCMeta(type):
[64] Fix | Delete
[65] Fix | Delete
"""Metaclass for defining Abstract Base Classes (ABCs).
[66] Fix | Delete
[67] Fix | Delete
Use this metaclass to create an ABC. An ABC can be subclassed
[68] Fix | Delete
directly, and then acts as a mix-in class. You can also register
[69] Fix | Delete
unrelated concrete classes (even built-in classes) and unrelated
[70] Fix | Delete
ABCs as 'virtual subclasses' -- these and their descendants will
[71] Fix | Delete
be considered subclasses of the registering ABC by the built-in
[72] Fix | Delete
issubclass() function, but the registering ABC won't show up in
[73] Fix | Delete
their MRO (Method Resolution Order) nor will method
[74] Fix | Delete
implementations defined by the registering ABC be callable (not
[75] Fix | Delete
even via super()).
[76] Fix | Delete
[77] Fix | Delete
"""
[78] Fix | Delete
[79] Fix | Delete
# A global counter that is incremented each time a class is
[80] Fix | Delete
# registered as a virtual subclass of anything. It forces the
[81] Fix | Delete
# negative cache to be cleared before its next use.
[82] Fix | Delete
_abc_invalidation_counter = 0
[83] Fix | Delete
[84] Fix | Delete
def __new__(mcls, name, bases, namespace):
[85] Fix | Delete
cls = super(ABCMeta, mcls).__new__(mcls, name, bases, namespace)
[86] Fix | Delete
# Compute set of abstract method names
[87] Fix | Delete
abstracts = set(name
[88] Fix | Delete
for name, value in namespace.items()
[89] Fix | Delete
if getattr(value, "__isabstractmethod__", False))
[90] Fix | Delete
for base in bases:
[91] Fix | Delete
for name in getattr(base, "__abstractmethods__", set()):
[92] Fix | Delete
value = getattr(cls, name, None)
[93] Fix | Delete
if getattr(value, "__isabstractmethod__", False):
[94] Fix | Delete
abstracts.add(name)
[95] Fix | Delete
cls.__abstractmethods__ = frozenset(abstracts)
[96] Fix | Delete
# Set up inheritance registry
[97] Fix | Delete
cls._abc_registry = WeakSet()
[98] Fix | Delete
cls._abc_cache = WeakSet()
[99] Fix | Delete
cls._abc_negative_cache = WeakSet()
[100] Fix | Delete
cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter
[101] Fix | Delete
return cls
[102] Fix | Delete
[103] Fix | Delete
def register(cls, subclass):
[104] Fix | Delete
"""Register a virtual subclass of an ABC."""
[105] Fix | Delete
if not isinstance(subclass, (type, types.ClassType)):
[106] Fix | Delete
raise TypeError("Can only register classes")
[107] Fix | Delete
if issubclass(subclass, cls):
[108] Fix | Delete
return # Already a subclass
[109] Fix | Delete
# Subtle: test for cycles *after* testing for "already a subclass";
[110] Fix | Delete
# this means we allow X.register(X) and interpret it as a no-op.
[111] Fix | Delete
if issubclass(cls, subclass):
[112] Fix | Delete
# This would create a cycle, which is bad for the algorithm below
[113] Fix | Delete
raise RuntimeError("Refusing to create an inheritance cycle")
[114] Fix | Delete
cls._abc_registry.add(subclass)
[115] Fix | Delete
ABCMeta._abc_invalidation_counter += 1 # Invalidate negative cache
[116] Fix | Delete
[117] Fix | Delete
def _dump_registry(cls, file=None):
[118] Fix | Delete
"""Debug helper to print the ABC registry."""
[119] Fix | Delete
print >> file, "Class: %s.%s" % (cls.__module__, cls.__name__)
[120] Fix | Delete
print >> file, "Inv.counter: %s" % ABCMeta._abc_invalidation_counter
[121] Fix | Delete
for name in sorted(cls.__dict__.keys()):
[122] Fix | Delete
if name.startswith("_abc_"):
[123] Fix | Delete
value = getattr(cls, name)
[124] Fix | Delete
print >> file, "%s: %r" % (name, value)
[125] Fix | Delete
[126] Fix | Delete
def __instancecheck__(cls, instance):
[127] Fix | Delete
"""Override for isinstance(instance, cls)."""
[128] Fix | Delete
# Inline the cache checking when it's simple.
[129] Fix | Delete
subclass = getattr(instance, '__class__', None)
[130] Fix | Delete
if subclass is not None and subclass in cls._abc_cache:
[131] Fix | Delete
return True
[132] Fix | Delete
subtype = type(instance)
[133] Fix | Delete
# Old-style instances
[134] Fix | Delete
if subtype is _InstanceType:
[135] Fix | Delete
subtype = subclass
[136] Fix | Delete
if subtype is subclass or subclass is None:
[137] Fix | Delete
if (cls._abc_negative_cache_version ==
[138] Fix | Delete
ABCMeta._abc_invalidation_counter and
[139] Fix | Delete
subtype in cls._abc_negative_cache):
[140] Fix | Delete
return False
[141] Fix | Delete
# Fall back to the subclass check.
[142] Fix | Delete
return cls.__subclasscheck__(subtype)
[143] Fix | Delete
return (cls.__subclasscheck__(subclass) or
[144] Fix | Delete
cls.__subclasscheck__(subtype))
[145] Fix | Delete
[146] Fix | Delete
def __subclasscheck__(cls, subclass):
[147] Fix | Delete
"""Override for issubclass(subclass, cls)."""
[148] Fix | Delete
# Check cache
[149] Fix | Delete
if subclass in cls._abc_cache:
[150] Fix | Delete
return True
[151] Fix | Delete
# Check negative cache; may have to invalidate
[152] Fix | Delete
if cls._abc_negative_cache_version < ABCMeta._abc_invalidation_counter:
[153] Fix | Delete
# Invalidate the negative cache
[154] Fix | Delete
cls._abc_negative_cache = WeakSet()
[155] Fix | Delete
cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter
[156] Fix | Delete
elif subclass in cls._abc_negative_cache:
[157] Fix | Delete
return False
[158] Fix | Delete
# Check the subclass hook
[159] Fix | Delete
ok = cls.__subclasshook__(subclass)
[160] Fix | Delete
if ok is not NotImplemented:
[161] Fix | Delete
assert isinstance(ok, bool)
[162] Fix | Delete
if ok:
[163] Fix | Delete
cls._abc_cache.add(subclass)
[164] Fix | Delete
else:
[165] Fix | Delete
cls._abc_negative_cache.add(subclass)
[166] Fix | Delete
return ok
[167] Fix | Delete
# Check if it's a direct subclass
[168] Fix | Delete
if cls in getattr(subclass, '__mro__', ()):
[169] Fix | Delete
cls._abc_cache.add(subclass)
[170] Fix | Delete
return True
[171] Fix | Delete
# Check if it's a subclass of a registered class (recursive)
[172] Fix | Delete
for rcls in cls._abc_registry:
[173] Fix | Delete
if issubclass(subclass, rcls):
[174] Fix | Delete
cls._abc_cache.add(subclass)
[175] Fix | Delete
return True
[176] Fix | Delete
# Check if it's a subclass of a subclass (recursive)
[177] Fix | Delete
for scls in cls.__subclasses__():
[178] Fix | Delete
if issubclass(subclass, scls):
[179] Fix | Delete
cls._abc_cache.add(subclass)
[180] Fix | Delete
return True
[181] Fix | Delete
# No dice; update negative cache
[182] Fix | Delete
cls._abc_negative_cache.add(subclass)
[183] Fix | Delete
return False
[184] Fix | Delete
[185] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function