Commit 4d0c1170 authored by Amaury Forgeot d'Arc's avatar Amaury Forgeot d'Arc

Correct the apparent refleak in test_io:

When cls is an ABCMeta, every call to isinstance(x, cls)
records type(x) in the cls._abc_cache of cls_abc_negative_cache.
So we clear these caches at the end of the test.

inspect.isabstract() is not the correct test for all ABCs, because there is no @abstractmethod in io.py (why?)
isinstance(cls, ABCMeta) would be more exact, but it fails with an infinite recursion.
So I used a hack to determine whether a class is an ABCMeta.

The true correction would be to turn cls._abc_cache &co into a WeakSet, as py3k does.
But classic classes are not weak referenceable...

Of course, this change should not be merged into the py3k branch.
parent b1ba7502
...@@ -128,7 +128,6 @@ import sys ...@@ -128,7 +128,6 @@ import sys
import time import time
import traceback import traceback
import warnings import warnings
from inspect import isabstract
# I see no other way to suppress these warnings; # I see no other way to suppress these warnings;
# putting them in test_grammar.py has no effect: # putting them in test_grammar.py has no effect:
...@@ -630,7 +629,7 @@ def cleanup_test_droppings(testname, verbose): ...@@ -630,7 +629,7 @@ def cleanup_test_droppings(testname, verbose):
def dash_R(the_module, test, indirect_test, huntrleaks): def dash_R(the_module, test, indirect_test, huntrleaks):
# This code is hackish and inelegant, but it seems to do the job. # This code is hackish and inelegant, but it seems to do the job.
import copy_reg, _abcoll import copy_reg, _abcoll, io
if not hasattr(sys, 'gettotalrefcount'): if not hasattr(sys, 'gettotalrefcount'):
raise Exception("Tracking reference leaks requires a debug build " raise Exception("Tracking reference leaks requires a debug build "
...@@ -641,8 +640,10 @@ def dash_R(the_module, test, indirect_test, huntrleaks): ...@@ -641,8 +640,10 @@ def dash_R(the_module, test, indirect_test, huntrleaks):
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 = {} abcs = {}
for abc in [getattr(_abcoll, a) for a in _abcoll.__all__]: modules = _abcoll, io
if not isabstract(abc): for abc in [getattr(mod, a) for mod in modules for a in mod.__all__]:
# XXX isinstance(abc, ABCMeta) leads to infinite recursion
if not hasattr(abc, '_abc_registry'):
continue continue
for obj in abc.__subclasses__() + [abc]: for obj in abc.__subclasses__() + [abc]:
abcs[obj] = obj._abc_registry.copy() abcs[obj] = obj._abc_registry.copy()
...@@ -679,7 +680,7 @@ def dash_R_cleanup(fs, ps, pic, abcs): ...@@ -679,7 +680,7 @@ def dash_R_cleanup(fs, ps, pic, abcs):
import gc, copy_reg import gc, copy_reg
import _strptime, linecache, dircache import _strptime, linecache, dircache
import urlparse, urllib, urllib2, mimetypes, doctest import urlparse, urllib, urllib2, mimetypes, doctest
import struct, filecmp, _abcoll import struct, filecmp
from distutils.dir_util import _path_created from distutils.dir_util import _path_created
# Restore some original values. # Restore some original values.
...@@ -693,13 +694,10 @@ def dash_R_cleanup(fs, ps, pic, abcs): ...@@ -693,13 +694,10 @@ def dash_R_cleanup(fs, ps, pic, abcs):
sys._clear_type_cache() sys._clear_type_cache()
# 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, registry in abcs.items():
if not isabstract(abc): abc._abc_registry = registry.copy()
continue abc._abc_cache.clear()
for obj in abc.__subclasses__() + [abc]: abc._abc_negative_cache.clear()
obj._abc_registry = abcs.get(obj, {}).copy()
obj._abc_cache.clear()
obj._abc_negative_cache.clear()
# Clear assorted module caches. # Clear assorted module caches.
_path_created.clear() _path_created.clear()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment