mirror of
https://github.com/python/cpython.git
synced 2025-10-09 16:34:44 +00:00
Rename __whatever variables defined by ABCMeta to _abc_whatever, so as
to simplify legitimate use of these.
This commit is contained in:
parent
d06489945f
commit
c1e315d2b0
2 changed files with 25 additions and 25 deletions
42
Lib/abc.py
42
Lib/abc.py
|
@ -116,7 +116,7 @@ class ABCMeta(type):
|
||||||
# A global counter that is incremented each time a class is
|
# A global counter that is incremented each time a class is
|
||||||
# registered as a virtual subclass of anything. It forces the
|
# registered as a virtual subclass of anything. It forces the
|
||||||
# negative cache to be cleared before its next use.
|
# negative cache to be cleared before its next use.
|
||||||
__invalidation_counter = 0
|
_abc_invalidation_counter = 0
|
||||||
|
|
||||||
def __new__(mcls, name, bases, namespace):
|
def __new__(mcls, name, bases, namespace):
|
||||||
bases = _fix_bases(bases)
|
bases = _fix_bases(bases)
|
||||||
|
@ -132,10 +132,10 @@ class ABCMeta(type):
|
||||||
abstracts.add(name)
|
abstracts.add(name)
|
||||||
cls.__abstractmethods__ = abstracts
|
cls.__abstractmethods__ = abstracts
|
||||||
# Set up inheritance registry
|
# Set up inheritance registry
|
||||||
cls.__registry = set()
|
cls._abc_registry = set()
|
||||||
cls.__cache = set()
|
cls._abc_cache = set()
|
||||||
cls.__negative_cache = set()
|
cls._abc_negative_cache = set()
|
||||||
cls.__negative_cache_version = ABCMeta.__invalidation_counter
|
cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter
|
||||||
return cls
|
return cls
|
||||||
|
|
||||||
def register(cls, subclass):
|
def register(cls, subclass):
|
||||||
|
@ -149,15 +149,15 @@ class ABCMeta(type):
|
||||||
if issubclass(cls, subclass):
|
if issubclass(cls, subclass):
|
||||||
# This would create a cycle, which is bad for the algorithm below
|
# This would create a cycle, which is bad for the algorithm below
|
||||||
raise RuntimeError("Refusing to create an inheritance cycle")
|
raise RuntimeError("Refusing to create an inheritance cycle")
|
||||||
cls.__registry.add(subclass)
|
cls._abc_registry.add(subclass)
|
||||||
ABCMeta.__invalidation_counter += 1 # Invalidate negative cache
|
ABCMeta._abc_invalidation_counter += 1 # Invalidate negative cache
|
||||||
|
|
||||||
def _dump_registry(cls, file=None):
|
def _dump_registry(cls, file=None):
|
||||||
"""Debug helper to print the ABC registry."""
|
"""Debug helper to print the ABC registry."""
|
||||||
print("Class: %s.%s" % (cls.__module__, cls.__name__), file=file)
|
print("Class: %s.%s" % (cls.__module__, cls.__name__), file=file)
|
||||||
print("Inv.counter: %s" % ABCMeta.__invalidation_counter, file=file)
|
print("Inv.counter: %s" % ABCMeta._abc_invalidation_counter, file=file)
|
||||||
for name in sorted(cls.__dict__.keys()):
|
for name in sorted(cls.__dict__.keys()):
|
||||||
if name.startswith("_ABCMeta__"):
|
if name.startswith("_abc_"):
|
||||||
value = getattr(cls, name)
|
value = getattr(cls, name)
|
||||||
print("%s: %r" % (name, value), file=file)
|
print("%s: %r" % (name, value), file=file)
|
||||||
|
|
||||||
|
@ -169,38 +169,38 @@ class ABCMeta(type):
|
||||||
def __subclasscheck__(cls, subclass):
|
def __subclasscheck__(cls, subclass):
|
||||||
"""Override for issubclass(subclass, cls)."""
|
"""Override for issubclass(subclass, cls)."""
|
||||||
# Check cache
|
# Check cache
|
||||||
if subclass in cls.__cache:
|
if subclass in cls._abc_cache:
|
||||||
return True
|
return True
|
||||||
# Check negative cache; may have to invalidate
|
# Check negative cache; may have to invalidate
|
||||||
if cls.__negative_cache_version < ABCMeta.__invalidation_counter:
|
if cls._abc_negative_cache_version < ABCMeta._abc_invalidation_counter:
|
||||||
# Invalidate the negative cache
|
# Invalidate the negative cache
|
||||||
cls.__negative_cache = set()
|
cls._abc_negative_cache = set()
|
||||||
cls.__negative_cache_version = ABCMeta.__invalidation_counter
|
cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter
|
||||||
elif subclass in cls.__negative_cache:
|
elif subclass in cls._abc_negative_cache:
|
||||||
return False
|
return False
|
||||||
# Check the subclass hook
|
# Check the subclass hook
|
||||||
ok = cls.__subclasshook__(subclass)
|
ok = cls.__subclasshook__(subclass)
|
||||||
if ok is not NotImplemented:
|
if ok is not NotImplemented:
|
||||||
assert isinstance(ok, bool)
|
assert isinstance(ok, bool)
|
||||||
if ok:
|
if ok:
|
||||||
cls.__cache.add(subclass)
|
cls._abc_cache.add(subclass)
|
||||||
else:
|
else:
|
||||||
cls.__negative_cache.add(subclass)
|
cls._abc_negative_cache.add(subclass)
|
||||||
return ok
|
return ok
|
||||||
# Check if it's a direct subclass
|
# Check if it's a direct subclass
|
||||||
if cls in subclass.__mro__:
|
if cls in subclass.__mro__:
|
||||||
cls.__cache.add(subclass)
|
cls._abc_cache.add(subclass)
|
||||||
return True
|
return True
|
||||||
# Check if it's a subclass of a registered class (recursive)
|
# Check if it's a subclass of a registered class (recursive)
|
||||||
for rcls in cls.__registry:
|
for rcls in cls._abc_registry:
|
||||||
if issubclass(subclass, rcls):
|
if issubclass(subclass, rcls):
|
||||||
cls.__registry.add(subclass)
|
cls._abc_registry.add(subclass)
|
||||||
return True
|
return True
|
||||||
# Check if it's a subclass of a subclass (recursive)
|
# Check if it's a subclass of a subclass (recursive)
|
||||||
for scls in cls.__subclasses__():
|
for scls in cls.__subclasses__():
|
||||||
if issubclass(subclass, scls):
|
if issubclass(subclass, scls):
|
||||||
cls.__registry.add(subclass)
|
cls._abc_registry.add(subclass)
|
||||||
return True
|
return True
|
||||||
# No dice; update negative cache
|
# No dice; update negative cache
|
||||||
cls.__negative_cache.add(subclass)
|
cls._abc_negative_cache.add(subclass)
|
||||||
return False
|
return False
|
||||||
|
|
|
@ -680,7 +680,7 @@ def dash_R(the_module, test, indirect_test, huntrleaks):
|
||||||
fs = warnings.filters[:]
|
fs = warnings.filters[:]
|
||||||
ps = copy_reg.dispatch_table.copy()
|
ps = copy_reg.dispatch_table.copy()
|
||||||
pic = sys.path_importer_cache.copy()
|
pic = sys.path_importer_cache.copy()
|
||||||
abcs = {obj: obj._ABCMeta__registry.copy()
|
abcs = {obj: obj._abc_registry.copy()
|
||||||
for abc in [getattr(_abcoll, a) for a in _abcoll.__all__]
|
for abc in [getattr(_abcoll, a) for a in _abcoll.__all__]
|
||||||
for obj in abc.__subclasses__() + [abc]}
|
for obj in abc.__subclasses__() + [abc]}
|
||||||
|
|
||||||
|
@ -731,9 +731,9 @@ def dash_R_cleanup(fs, ps, pic, abcs):
|
||||||
# Clear ABC registries, restoring previously saved ABC registries.
|
# Clear ABC registries, restoring previously saved ABC registries.
|
||||||
for abc in [getattr(_abcoll, a) for a in _abcoll.__all__]:
|
for abc in [getattr(_abcoll, a) for a in _abcoll.__all__]:
|
||||||
for obj in abc.__subclasses__() + [abc]:
|
for obj in abc.__subclasses__() + [abc]:
|
||||||
obj._ABCMeta__registry = abcs.get(obj, {}).copy()
|
obj._abc_registry = abcs.get(obj, {}).copy()
|
||||||
obj._ABCMeta__cache.clear()
|
obj._abc_cache.clear()
|
||||||
obj._ABCMeta__negative_cache.clear()
|
obj._abc_negative_cache.clear()
|
||||||
|
|
||||||
# Clear assorted module caches.
|
# Clear assorted module caches.
|
||||||
_path_created.clear()
|
_path_created.clear()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue