diff -r 63bbe9f80909 Lib/ctypes/test/test_refcounts.py --- a/Lib/ctypes/test/test_refcounts.py Sat Sep 12 17:20:47 2015 -0700 +++ b/Lib/ctypes/test/test_refcounts.py Tue Sep 15 21:14:10 2015 +0300 @@ -90,6 +90,7 @@ class AnotherLeak(unittest.TestCase): return a * b * 2 f = proto(func) + gc.collect() a = sys.getrefcount(ctypes.c_int) f(1, 2) self.assertEqual(sys.getrefcount(ctypes.c_int), a) diff -r 63bbe9f80909 Lib/lib-tk/test/test_tkinter/test_images.py --- a/Lib/lib-tk/test/test_tkinter/test_images.py Sat Sep 12 17:20:47 2015 -0700 +++ b/Lib/lib-tk/test/test_tkinter/test_images.py Tue Sep 15 21:14:10 2015 +0300 @@ -37,6 +37,7 @@ class BitmapImageTest(AbstractTkTest, un self.assertEqual(image.height(), 16) self.assertIn('::img::test', self.root.image_names()) del image + support.gc_collect() self.assertNotIn('::img::test', self.root.image_names()) def test_create_from_data(self): @@ -51,6 +52,7 @@ class BitmapImageTest(AbstractTkTest, un self.assertEqual(image.height(), 16) self.assertIn('::img::test', self.root.image_names()) del image + support.gc_collect() self.assertNotIn('::img::test', self.root.image_names()) def assertEqualStrList(self, actual, expected): @@ -131,6 +133,7 @@ class PhotoImageTest(AbstractTkTest, uni self.assertEqual(image['file'], testfile) self.assertIn('::img::test', self.root.image_names()) del image + support.gc_collect() self.assertNotIn('::img::test', self.root.image_names()) def check_create_from_data(self, ext): @@ -148,6 +151,7 @@ class PhotoImageTest(AbstractTkTest, uni self.assertEqual(image['file'], '') self.assertIn('::img::test', self.root.image_names()) del image + support.gc_collect() self.assertNotIn('::img::test', self.root.image_names()) def test_create_from_ppm_file(self): diff -r 63bbe9f80909 Lib/lib-tk/test/test_tkinter/test_variables.py --- a/Lib/lib-tk/test/test_tkinter/test_variables.py Sat Sep 12 17:20:47 2015 -0700 +++ b/Lib/lib-tk/test/test_tkinter/test_variables.py Tue Sep 15 21:14:10 2015 +0300 @@ -1,4 +1,5 @@ import unittest +from test.test_support import gc_collect from Tkinter import (Variable, StringVar, IntVar, DoubleVar, BooleanVar, Tcl, TclError) @@ -33,6 +34,7 @@ class TestVariable(TestBase): v = Variable(self.root, "sample string", "varname") self.assertTrue(self.info_exists("varname")) del v + gc_collect() self.assertFalse(self.info_exists("varname")) def test_dont_unset_not_existing(self): @@ -40,9 +42,11 @@ class TestVariable(TestBase): v1 = Variable(self.root, name="name") v2 = Variable(self.root, name="name") del v1 + gc_collect() self.assertFalse(self.info_exists("name")) # shouldn't raise exception del v2 + gc_collect() self.assertFalse(self.info_exists("name")) def test___eq__(self): diff -r 63bbe9f80909 Lib/lib-tk/test/test_ttk/test_extensions.py --- a/Lib/lib-tk/test/test_ttk/test_extensions.py Sat Sep 12 17:20:47 2015 -0700 +++ b/Lib/lib-tk/test/test_ttk/test_extensions.py Tue Sep 15 21:14:10 2015 +0300 @@ -2,7 +2,7 @@ import sys import unittest import Tkinter as tkinter import ttk -from test.test_support import requires, run_unittest, swap_attr +from test.test_support import requires, run_unittest, swap_attr, gc_collect from test_ttk.support import AbstractTkTest, destroy_default_root requires('gui') @@ -18,6 +18,7 @@ class LabeledScaleTest(AbstractTkTest, u x = ttk.LabeledScale(self.root) var = x._variable._name x.destroy() + gc_collect() self.assertRaises(tkinter.TclError, x.tk.globalgetvar, var) # manually created variable @@ -25,11 +26,13 @@ class LabeledScaleTest(AbstractTkTest, u name = myvar._name x = ttk.LabeledScale(self.root, variable=myvar) x.destroy() + gc_collect() if self.wantobjects: self.assertEqual(x.tk.globalgetvar(name), myvar.get()) else: self.assertEqual(float(x.tk.globalgetvar(name)), myvar.get()) del myvar + gc_collect() self.assertRaises(tkinter.TclError, x.tk.globalgetvar, name) # checking that the tracing callback is properly removed @@ -37,6 +40,7 @@ class LabeledScaleTest(AbstractTkTest, u # LabeledScale will start tracing myvar x = ttk.LabeledScale(self.root, variable=myvar) x.destroy() + gc_collect() # Unless the tracing callback was removed, creating a new # LabeledScale with the same var will cause an error now. This # happens because the variable will be set to (possibly) a new @@ -216,6 +220,7 @@ class OptionMenuTest(AbstractTkTest, uni optmenu.destroy() self.assertEqual(optmenu.tk.globalgetvar(name), var.get()) del var + gc_collect() self.assertRaises(tkinter.TclError, optmenu.tk.globalgetvar, name) diff -r 63bbe9f80909 Lib/lib-tk/test/test_ttk/test_widgets.py --- a/Lib/lib-tk/test/test_ttk/test_widgets.py Sat Sep 12 17:20:47 2015 -0700 +++ b/Lib/lib-tk/test/test_ttk/test_widgets.py Tue Sep 15 21:14:10 2015 +0300 @@ -2,7 +2,7 @@ import unittest import Tkinter as tkinter from Tkinter import TclError import ttk -from test.test_support import requires, run_unittest +from test.test_support import requires, run_unittest, gc_collect import sys from test_functions import MockTclObj @@ -838,6 +838,7 @@ class ScaleTest(AbstractWidgetTest, unit self.assertEqual(conv(self.scale.get()), var.get()) self.assertEqual(conv(self.scale.get()), max + 5) del var + gc_collect() # the same happens with the value option self.scale['value'] = max + 10 diff -r 63bbe9f80909 Lib/test/test_argparse.py --- a/Lib/test/test_argparse.py Sat Sep 12 17:20:47 2015 -0700 +++ b/Lib/test/test_argparse.py Tue Sep 15 21:14:10 2015 +0300 @@ -10,6 +10,7 @@ import textwrap import tempfile import unittest import argparse +import gc from StringIO import StringIO @@ -47,6 +48,8 @@ class TempDirMixin(object): def tearDown(self): os.chdir(self.old_dir) + # Force a collection which should close FileType() options + gc.collect() for root, dirs, files in os.walk(self.temp_dir, topdown=False): for name in files: os.chmod(os.path.join(self.temp_dir, name), stat.S_IWRITE) diff -r 63bbe9f80909 Lib/test/test_array.py --- a/Lib/test/test_array.py Sat Sep 12 17:20:47 2015 -0700 +++ b/Lib/test/test_array.py Tue Sep 15 21:14:10 2015 +0300 @@ -783,6 +783,7 @@ class BaseTest(unittest.TestCase): p = proxy(s) self.assertEqual(p.tostring(), s.tostring()) s = None + test_support.gc_collect() self.assertRaises(ReferenceError, len, p) @unittest.skipUnless(hasattr(sys, 'getrefcount'), diff -r 63bbe9f80909 Lib/test/test_bz2.py --- a/Lib/test/test_bz2.py Sat Sep 12 17:20:47 2015 -0700 +++ b/Lib/test/test_bz2.py Tue Sep 15 21:14:10 2015 +0300 @@ -51,6 +51,7 @@ class BZ2FileTest(BaseTest): self.filename = TESTFN def tearDown(self): + support.gc_collect() if os.path.isfile(self.filename): os.unlink(self.filename) @@ -247,6 +248,8 @@ class BZ2FileTest(BaseTest): for i in xrange(10000): o = BZ2File(self.filename) del o + if i % 100 == 0: + support.gc_collect() def testOpenNonexistent(self): # "Test opening a nonexistent file" diff -r 63bbe9f80909 Lib/test/test_code.py --- a/Lib/test/test_code.py Sat Sep 12 17:20:47 2015 -0700 +++ b/Lib/test/test_code.py Tue Sep 15 21:14:10 2015 +0300 @@ -83,6 +83,7 @@ consts: ("'doc string'", 'None') import unittest import weakref from test.test_support import run_doctest, run_unittest, cpython_only +from test.test_support import gc_collect def consts(t): @@ -134,6 +135,7 @@ class CodeWeakRefTest(unittest.TestCase) coderef = weakref.ref(f.__code__, callback) self.assertTrue(bool(coderef())) del f + gc_collect() self.assertFalse(bool(coderef())) self.assertTrue(self.called) diff -r 63bbe9f80909 Lib/test/test_copy.py --- a/Lib/test/test_copy.py Sat Sep 12 17:20:47 2015 -0700 +++ b/Lib/test/test_copy.py Tue Sep 15 21:14:10 2015 +0300 @@ -637,6 +637,7 @@ class TestCopy(unittest.TestCase): self.assertEqual(v[c], d) self.assertEqual(len(v), 2) del c, d + test_support.gc_collect() self.assertEqual(len(v), 1) x, y = C(), C() # The underlying containers are decoupled @@ -666,6 +667,7 @@ class TestCopy(unittest.TestCase): self.assertEqual(v[a].i, b.i) self.assertEqual(v[c].i, d.i) del c + test_support.gc_collect() self.assertEqual(len(v), 1) def test_deepcopy_weakvaluedict(self): @@ -689,6 +691,7 @@ class TestCopy(unittest.TestCase): self.assertTrue(t is d) del x, y, z, t del d + test_support.gc_collect() self.assertEqual(len(v), 1) def test_deepcopy_bound_method(self): diff -r 63bbe9f80909 Lib/test/test_deque.py --- a/Lib/test/test_deque.py Sat Sep 12 17:20:47 2015 -0700 +++ b/Lib/test/test_deque.py Tue Sep 15 21:14:10 2015 +0300 @@ -515,7 +515,7 @@ class TestBasic(unittest.TestCase): container = reversed(deque([obj, 1])) obj.x = iter(container) del obj, container - gc.collect() + test_support.gc_collect() self.assertTrue(ref() is None, "Cycle was not collected") check_sizeof = test_support.check_sizeof @@ -648,6 +648,7 @@ class TestSubclass(unittest.TestCase): p = weakref.proxy(d) self.assertEqual(str(p), str(d)) d = None + test_support.gc_collect() self.assertRaises(ReferenceError, str, p) def test_strange_subclass(self): diff -r 63bbe9f80909 Lib/test/test_file.py --- a/Lib/test/test_file.py Sat Sep 12 17:20:47 2015 -0700 +++ b/Lib/test/test_file.py Tue Sep 15 21:14:10 2015 +0300 @@ -12,7 +12,7 @@ from weakref import proxy import io import _pyio as pyio -from test.test_support import TESTFN, run_unittest +from test.test_support import TESTFN, run_unittest, gc_collect from UserList import UserList class AutoFileTests(unittest.TestCase): @@ -33,6 +33,7 @@ class AutoFileTests(unittest.TestCase): self.assertEqual(self.f.tell(), p.tell()) self.f.close() self.f = None + gc_collect() self.assertRaises(ReferenceError, getattr, p, 'tell') def testAttributes(self): diff -r 63bbe9f80909 Lib/test/test_file2k.py --- a/Lib/test/test_file2k.py Sat Sep 12 17:20:47 2015 -0700 +++ b/Lib/test/test_file2k.py Tue Sep 15 21:14:10 2015 +0300 @@ -15,7 +15,7 @@ except ImportError: threading = None from test import test_support -from test.test_support import TESTFN, run_unittest, requires +from test.test_support import TESTFN, run_unittest, gc_collect, requires from UserList import UserList class AutoFileTests(unittest.TestCase): @@ -36,6 +36,7 @@ class AutoFileTests(unittest.TestCase): self.assertEqual(self.f.tell(), p.tell()) self.f.close() self.f = None + gc_collect() self.assertRaises(ReferenceError, getattr, p, 'tell') def testAttributes(self): diff -r 63bbe9f80909 Lib/test/test_fileio.py --- a/Lib/test/test_fileio.py Sat Sep 12 17:20:47 2015 -0700 +++ b/Lib/test/test_fileio.py Tue Sep 15 21:14:10 2015 +0300 @@ -13,6 +13,7 @@ from UserList import UserList from test.test_support import TESTFN, check_warnings, run_unittest, make_bad_fd from test.test_support import py3k_bytes as bytes, cpython_only +from test.test_support import gc_collect from test.script_helper import run_python from _io import FileIO as _FileIO @@ -35,6 +36,7 @@ class AutoFileTests(unittest.TestCase): self.assertEqual(self.f.tell(), p.tell()) self.f.close() self.f = None + gc_collect() self.assertRaises(ReferenceError, getattr, p, 'tell') def testSeekTell(self): diff -r 63bbe9f80909 Lib/test/test_functools.py --- a/Lib/test/test_functools.py Sat Sep 12 17:20:47 2015 -0700 +++ b/Lib/test/test_functools.py Tue Sep 15 21:14:10 2015 +0300 @@ -136,6 +136,7 @@ class TestPartial(unittest.TestCase): p = proxy(f) self.assertEqual(f.func, p.func) f = None + test_support.gc_collect() self.assertRaises(ReferenceError, getattr, p, 'func') def test_with_bound_and_unbound_methods(self): diff -r 63bbe9f80909 Lib/test/test_generators.py --- a/Lib/test/test_generators.py Sat Sep 12 17:20:47 2015 -0700 +++ b/Lib/test/test_generators.py Tue Sep 15 21:14:10 2015 +0300 @@ -1497,6 +1497,8 @@ True """ coroutine_tests = """\ +>>> from test.test_support import gc_collect + Sending a value into a started generator: >>> def f(): @@ -1571,7 +1573,7 @@ SyntaxError: 'yield' outside function >>> def f(): return lambda x=(yield): 1 Traceback (most recent call last): ... -SyntaxError: 'return' with argument inside generator (, line 1) +SyntaxError: 'return' with argument inside generator (, line 1) >>> def f(): x = yield = y Traceback (most recent call last): @@ -1698,7 +1700,7 @@ And finalization: >>> g = f() >>> g.next() ->>> del g +>>> del g; gc_collect() exiting >>> class context(object): @@ -1709,7 +1711,7 @@ exiting ... yield >>> g = f() >>> g.next() ->>> del g +>>> del g; gc_collect() exiting @@ -1722,7 +1724,7 @@ GeneratorExit is not caught by except Ex >>> g = f() >>> g.next() ->>> del g +>>> del g; gc_collect() finally @@ -1748,6 +1750,7 @@ Our ill-behaved code should be invoked d >>> g = f() >>> g.next() >>> del g +>>> gc_collect() >>> sys.stderr.getvalue().startswith( ... "Exception RuntimeError: 'generator ignored GeneratorExit' in " ... ) @@ -1813,6 +1816,8 @@ Prior to adding cycle-GC support to iter references. We add it to the standard suite so the routine refleak-tests would trigger if it starts being uncleanable again. +>>> from test.test_support import gc_collect + >>> import itertools >>> def leak(): ... class gen: @@ -1864,6 +1869,7 @@ to test. ... ... l = Leaker() ... del l +... gc_collect() ... err = sys.stderr.getvalue().strip() ... err.startswith( ... "Exception RuntimeError: RuntimeError() in <" diff -r 63bbe9f80909 Lib/test/test_io.py --- a/Lib/test/test_io.py Sat Sep 12 17:20:47 2015 -0700 +++ b/Lib/test/test_io.py Tue Sep 15 21:14:10 2015 +0300 @@ -3057,6 +3057,31 @@ class SignalsTest(unittest.TestCase): """Check that a partial write, when it gets interrupted, properly invokes the signal handler, and bubbles up the exception raised in the latter.""" + + # XXX This test has three flaws that appear when objects are + # XXX not reference counted. + + # - if wio.write() happens to trigger a garbage collection, + # the signal exception may be raised when some __del__ + # method is running; it will not reach the assertRaises() + # call. + + # - more subtle, if the wio object is not destroyed at once + # and survives this function, the next opened file is likely + # to have the same fileno (since the file descriptor was + # actively closed). When wio.__del__ is finally called, it + # will close the other's test file... To trigger this with + # CPython, try adding "global wio" in this function. + + # - This happens only for streams created by the _pyio module, + # because a wio.close() that fails still consider that the + # file needs to be closed again. You can try adding an + # "assert wio.closed" at the end of the function. + + # Fortunately, a little gc.gollect() seems to be enough to + # work around all these issues. + support.gc_collect() + read_results = [] def _read(): s = os.read(r, 1) diff -r 63bbe9f80909 Lib/test/test_itertools.py --- a/Lib/test/test_itertools.py Sat Sep 12 17:20:47 2015 -0700 +++ b/Lib/test/test_itertools.py Tue Sep 15 21:14:10 2015 +0300 @@ -944,6 +944,7 @@ class TestBasicOps(unittest.TestCase): p = weakref.proxy(a) self.assertEqual(getattr(p, '__class__'), type(b)) del a + test_support.gc_collect() self.assertRaises(ReferenceError, getattr, p, '__class__') # Issue 13454: Crash when deleting backward iterator from tee() diff -r 63bbe9f80909 Lib/test/test_multiprocessing.py --- a/Lib/test/test_multiprocessing.py Sat Sep 12 17:20:47 2015 -0700 +++ b/Lib/test/test_multiprocessing.py Tue Sep 15 21:14:10 2015 +0300 @@ -1520,6 +1520,7 @@ class _TestManagerRestart(BaseTestCase): queue = manager.get_queue() self.assertEqual(queue.get(), 'hello world') del queue + test_support.gc_collect() manager.shutdown() manager = QueueManager( address=addr, authkey=authkey, serializer=SERIALIZER) @@ -1922,6 +1923,10 @@ class _TestHeap(BaseTestCase): if len(blocks) > maxblocks: i = random.randrange(maxblocks) del blocks[i] + # XXX There should be a better way to release resources for a + # single block + if i % maxblocks == 0: + import gc; gc.collect() # get the heap object heap = multiprocessing.heap.BufferWrapper._heap @@ -2046,6 +2051,7 @@ class _TestFinalize(BaseTestCase): a = Foo() util.Finalize(a, conn.send, args=('a',)) del a # triggers callback for a + test_support.gc_collect() b = Foo() close_b = util.Finalize(b, conn.send, args=('b',)) diff -r 63bbe9f80909 Lib/test/test_scope.py --- a/Lib/test/test_scope.py Sat Sep 12 17:20:47 2015 -0700 +++ b/Lib/test/test_scope.py Tue Sep 15 21:14:10 2015 +0300 @@ -1,6 +1,6 @@ import unittest from test.test_support import check_syntax_error, check_py3k_warnings, \ - check_warnings, run_unittest + check_warnings, run_unittest, gc_collect class ScopeTests(unittest.TestCase): @@ -432,6 +432,7 @@ self.assertEqual(g.get(), 13) for i in range(100): f1() + gc_collect() self.assertEqual(Foo.count, 0) diff -r 63bbe9f80909 Lib/test/test_set.py --- a/Lib/test/test_set.py Sat Sep 12 17:20:47 2015 -0700 +++ b/Lib/test/test_set.py Tue Sep 15 21:14:10 2015 +0300 @@ -559,6 +559,7 @@ class TestSet(TestJointOps): p = weakref.proxy(s) self.assertEqual(str(p), str(s)) s = None + test_support.gc_collect() self.assertRaises(ReferenceError, str, p) @unittest.skipUnless(hasattr(set, "test_c_api"), diff -r 63bbe9f80909 Lib/test/test_socket.py --- a/Lib/test/test_socket.py Sat Sep 12 17:20:47 2015 -0700 +++ b/Lib/test/test_socket.py Tue Sep 15 21:14:10 2015 +0300 @@ -268,6 +268,7 @@ class GeneralModuleTests(unittest.TestCa self.assertEqual(p.fileno(), s.fileno()) s.close() s = None + test_support.gc_collect() try: p.fileno() except ReferenceError: diff -r 63bbe9f80909 Lib/test/test_subprocess.py --- a/Lib/test/test_subprocess.py Sat Sep 12 17:20:47 2015 -0700 +++ b/Lib/test/test_subprocess.py Tue Sep 15 21:14:10 2015 +0300 @@ -1139,6 +1139,7 @@ class POSIXProcessTestCase(BaseTestCase) ident = id(p) pid = p.pid del p + test_support.gc_collect() # check that p is in the active processes list self.assertIn(ident, [id(o) for o in subprocess._active]) @@ -1158,6 +1159,7 @@ class POSIXProcessTestCase(BaseTestCase) ident = id(p) pid = p.pid del p + test_support.gc_collect() os.kill(pid, signal.SIGKILL) # check that p is in the active processes list self.assertIn(ident, [id(o) for o in subprocess._active]) diff -r 63bbe9f80909 Lib/test/test_sys_settrace.py --- a/Lib/test/test_sys_settrace.py Sat Sep 12 17:20:47 2015 -0700 +++ b/Lib/test/test_sys_settrace.py Tue Sep 15 21:14:10 2015 +0300 @@ -213,12 +213,16 @@ def generator_function(): "finally" def generator_example(): # any() will leave the generator before its end - x = any(generator_function()) + x = any(generator_function()); gc.collect() # the following lines were not traced for x in range(10): y = x +# On CPython, when the generator is decref'ed to zero, we see the trace +# for the "finally:" portion. On PyPy, we don't see it before the next +# garbage collection. That's why we put gc.collect() on the same line above. + generator_example.events = ([(0, 'call'), (2, 'line'), (-6, 'call'), diff -r 63bbe9f80909 Lib/test/test_tempfile.py --- a/Lib/test/test_tempfile.py Sat Sep 12 17:20:47 2015 -0700 +++ b/Lib/test/test_tempfile.py Tue Sep 15 21:14:10 2015 +0300 @@ -374,6 +374,7 @@ class test__mkstemp_inner(TestBadTempdir dir = tempfile.mkdtemp() try: self.do_create(dir=dir).write("blat") + support.gc_collect() finally: os.rmdir(dir) @@ -709,12 +710,15 @@ class test_mktemp(TC): self.do_create(suf="b") self.do_create(pre="a", suf="b") self.do_create(pre="aa", suf=".txt") + support.gc_collect() def test_many(self): # mktemp can choose many usable file names (stochastic) extant = range(TEST_FILES) for i in extant: extant[i] = self.do_create(pre="aa") + del extant + support.gc_collect() ## def test_warning(self): ## # mktemp issues a warning when used diff -r 63bbe9f80909 Lib/test/test_thread.py --- a/Lib/test/test_thread.py Sat Sep 12 17:20:47 2015 -0700 +++ b/Lib/test/test_thread.py Tue Sep 15 21:14:10 2015 +0300 @@ -124,6 +124,7 @@ class ThreadRunningTests(BasicThreadTest del task while not done: time.sleep(0.01) + test_support.gc_collect() self.assertEqual(thread._count(), orig) def test_save_exception_state_on_error(self): diff -r 63bbe9f80909 Lib/test/test_weakref.py --- a/Lib/test/test_weakref.py Sat Sep 12 17:20:47 2015 -0700 +++ b/Lib/test/test_weakref.py Tue Sep 15 21:14:10 2015 +0300 @@ -8,6 +8,7 @@ import contextlib import copy from test import test_support +from test.test_support import gc_collect # Used in ReferencesTestCase.test_ref_created_during_del() . ref_from_del = None @@ -93,6 +94,7 @@ class ReferencesTestCase(TestBase): ref1 = weakref.ref(o, self.callback) ref2 = weakref.ref(o, self.callback) del o + gc_collect() self.assertIsNone(ref1(), "expected reference to be invalidated") self.assertIsNone(ref2(), "expected reference to be invalidated") self.assertEqual(self.cbcalled, 2, @@ -122,13 +124,16 @@ class ReferencesTestCase(TestBase): ref1 = weakref.proxy(o, self.callback) ref2 = weakref.proxy(o, self.callback) del o + gc_collect() def check(proxy): proxy.bar self.assertRaises(weakref.ReferenceError, check, ref1) self.assertRaises(weakref.ReferenceError, check, ref2) - self.assertRaises(weakref.ReferenceError, bool, weakref.proxy(C())) + ref3 = weakref.proxy(C()) + gc_collect() + self.assertRaises(weakref.ReferenceError, bool, ref3) self.assertEqual(self.cbcalled, 2) def check_basic_ref(self, factory): @@ -145,6 +150,7 @@ class ReferencesTestCase(TestBase): o = factory() ref = weakref.ref(o, self.callback) del o + gc_collect() self.assertEqual(self.cbcalled, 1, "callback did not properly set 'cbcalled'") self.assertIsNone(ref(), @@ -169,6 +175,7 @@ class ReferencesTestCase(TestBase): self.assertEqual(weakref.getweakrefcount(o), 2, "wrong weak ref count for object") del proxy + gc_collect() self.assertEqual(weakref.getweakrefcount(o), 1, "wrong weak ref count for object after deleting proxy") @@ -345,6 +352,7 @@ class ReferencesTestCase(TestBase): "got wrong number of weak reference objects") del ref1, ref2, proxy1, proxy2 + gc_collect() self.assertEqual(weakref.getweakrefcount(o), 0, "weak reference objects not unlinked from" " referent when discarded.") @@ -358,6 +366,7 @@ class ReferencesTestCase(TestBase): ref1 = weakref.ref(o, self.callback) ref2 = weakref.ref(o, self.callback) del ref1 + gc_collect() self.assertEqual(weakref.getweakrefs(o), [ref2], "list of refs does not match") @@ -365,10 +374,12 @@ class ReferencesTestCase(TestBase): ref1 = weakref.ref(o, self.callback) ref2 = weakref.ref(o, self.callback) del ref2 + gc_collect() self.assertEqual(weakref.getweakrefs(o), [ref1], "list of refs does not match") del ref1 + gc_collect() self.assertEqual(weakref.getweakrefs(o), [], "list of refs not cleared") @@ -420,13 +431,11 @@ class ReferencesTestCase(TestBase): # when the second attempt to remove the instance from the "list # of all objects" occurs. - import gc - class C(object): pass c = C() - wr = weakref.ref(c, lambda ignore: gc.collect()) + wr = weakref.ref(c, lambda ignore: gc_collect()) del c # There endeth the first part. It gets worse. @@ -434,7 +443,7 @@ class ReferencesTestCase(TestBase): c1 = C() c1.i = C() - wr = weakref.ref(c1.i, lambda ignore: gc.collect()) + wr = weakref.ref(c1.i, lambda ignore: gc_collect()) c2 = C() c2.c1 = c1 @@ -450,8 +459,6 @@ class ReferencesTestCase(TestBase): del c2 def test_callback_in_cycle_1(self): - import gc - class J(object): pass @@ -487,11 +494,9 @@ class ReferencesTestCase(TestBase): # search II.__mro__, but that's NULL. The result was a segfault in # a release build, and an assert failure in a debug build. del I, J, II - gc.collect() + gc_collect() def test_callback_in_cycle_2(self): - import gc - # This is just like test_callback_in_cycle_1, except that II is an # old-style class. The symptom is different then: an instance of an # old-style class looks in its own __dict__ first. 'J' happens to @@ -516,11 +521,9 @@ class ReferencesTestCase(TestBase): I.wr = weakref.ref(J, I.acallback) del I, J, II - gc.collect() + gc_collect() def test_callback_in_cycle_3(self): - import gc - # This one broke the first patch that fixed the last two. In this # case, the objects reachable from the callback aren't also reachable # from the object (c1) *triggering* the callback: you can get to @@ -540,11 +543,9 @@ class ReferencesTestCase(TestBase): c2.wr = weakref.ref(c1, c2.cb) del c1, c2 - gc.collect() + gc_collect() def test_callback_in_cycle_4(self): - import gc - # Like test_callback_in_cycle_3, except c2 and c1 have different # classes. c2's class (C) isn't reachable from c1 then, so protecting # objects reachable from the dying object (c1) isn't enough to stop @@ -568,11 +569,9 @@ class ReferencesTestCase(TestBase): c2.wr = weakref.ref(c1, c2.cb) del c1, c2, C, D - gc.collect() + gc_collect() def test_callback_in_cycle_resurrection(self): - import gc - # Do something nasty in a weakref callback: resurrect objects # from dead cycles. For this to be attempted, the weakref and # its callback must also be part of the cyclic trash (else the @@ -603,7 +602,7 @@ class ReferencesTestCase(TestBase): del c1, c2, C # make them all trash self.assertEqual(alist, []) # del isn't enough to reclaim anything - gc.collect() + gc_collect() # c1.wr and c2.wr were part of the cyclic trash, so should have # been cleared without their callbacks executing. OTOH, the weakref # to C is bound to a function local (wr), and wasn't trash, so that @@ -613,12 +612,10 @@ class ReferencesTestCase(TestBase): self.assertEqual(wr(), None) del alist[:] - gc.collect() + gc_collect() self.assertEqual(alist, []) def test_callbacks_on_callback(self): - import gc - # Set up weakref callbacks *on* weakref callbacks. alist = [] def safe_callback(ignore): @@ -646,12 +643,12 @@ class ReferencesTestCase(TestBase): del callback, c, d, C self.assertEqual(alist, []) # del isn't enough to clean up cycles - gc.collect() + gc_collect() self.assertEqual(alist, ["safe_callback called"]) self.assertEqual(external_wr(), None) del alist[:] - gc.collect() + gc_collect() self.assertEqual(alist, []) def test_gc_during_ref_creation(self): @@ -703,7 +700,7 @@ class ReferencesTestCase(TestBase): r = weakref.ref(Exception) self.assertRaises(TypeError, r.__init__, 0, 0, 0, 0, 0) # No exception should be raised here - gc.collect() + gc_collect() def test_classes(self): # Check that both old-style classes and new-style classes @@ -716,12 +713,12 @@ class ReferencesTestCase(TestBase): weakref.ref(int) a = weakref.ref(A, l.append) A = None - gc.collect() + gc_collect() self.assertEqual(a(), None) self.assertEqual(l, [a]) b = weakref.ref(B, l.append) B = None - gc.collect() + gc_collect() self.assertEqual(b(), None) self.assertEqual(l, [a, b]) @@ -811,6 +808,7 @@ class SubclassableWeakrefTestCase(TestBa self.assertTrue(mr.called) self.assertEqual(mr.value, 24) del o + gc_collect() self.assertIsNone(mr()) self.assertTrue(mr.called) @@ -917,7 +915,7 @@ class MappingTestCase(TestBase): n1 = len(dct) list(it) del it - gc.collect() + test_support.gc_collect() n2 = len(dct) # iteration should prevent garbage collection here # Note that this is a test on an implementation detail. The requirement @@ -982,15 +980,18 @@ class MappingTestCase(TestBase): del items1, items2 self.assertEqual(len(dict), self.COUNT) del objects[0] + gc_collect() self.assertEqual(len(dict), (self.COUNT - 1), "deleting object did not cause dictionary update") del objects, o + gc_collect() self.assertEqual(len(dict), 0, "deleting the values did not clear the dictionary") # regression on SF bug #447152: dict = weakref.WeakValueDictionary() self.assertRaises(KeyError, dict.__getitem__, 1) dict[2] = C() + gc_collect() self.assertRaises(KeyError, dict.__getitem__, 2) def test_weak_keys(self): @@ -1011,9 +1012,11 @@ class MappingTestCase(TestBase): del items1, items2 self.assertEqual(len(dict), self.COUNT) del objects[0] + gc_collect() self.assertEqual(len(dict), (self.COUNT - 1), "deleting object did not cause dictionary update") del objects, o + gc_collect() self.assertEqual(len(dict), 0, "deleting the keys did not clear the dictionary") o = Object(42) @@ -1362,6 +1365,7 @@ class MappingTestCase(TestBase): for o in objs: count += 1 del d[o] + gc_collect() self.assertEqual(len(d), 0) self.assertEqual(count, 2) @@ -1402,6 +1406,7 @@ True >>> o is o2 True >>> del o, o2 +>>> gc_collect() >>> print r() None @@ -1454,6 +1459,7 @@ True >>> id2obj(a_id) is a True >>> del a +>>> gc_collect() >>> try: ... id2obj(a_id) ... except KeyError: diff -r 63bbe9f80909 Lib/test/test_weakset.py --- a/Lib/test/test_weakset.py Sat Sep 12 17:20:47 2015 -0700 +++ b/Lib/test/test_weakset.py Tue Sep 15 21:14:10 2015 +0300 @@ -67,6 +67,7 @@ class TestWeakSet(unittest.TestCase): self.assertEqual(len(self.s), len(self.d)) self.assertEqual(len(self.fs), 1) del self.obj + test_support.gc_collect() self.assertEqual(len(self.fs), 0) def test_contains(self): @@ -76,6 +77,7 @@ class TestWeakSet(unittest.TestCase): self.assertNotIn(1, self.s) self.assertIn(self.obj, self.fs) del self.obj + test_support.gc_collect() self.assertNotIn(SomeClass('F'), self.fs) def test_union(self): @@ -234,6 +236,7 @@ class TestWeakSet(unittest.TestCase): self.assertEqual(self.s, dup) self.assertRaises(TypeError, self.s.add, []) self.fs.add(Foo()) + test_support.gc_collect() self.assertTrue(len(self.fs) == 1) self.fs.add(self.obj) self.assertTrue(len(self.fs) == 1) @@ -366,10 +369,11 @@ class TestWeakSet(unittest.TestCase): next(it) # Trigger internal iteration # Destroy an item del items[-1] - gc.collect() # just in case + test_support.gc_collect() # We have removed either the first consumed items, or another one self.assertIn(len(list(it)), [len(items), len(items) - 1]) del it + test_support.gc_collect() # The removal has been committed self.assertEqual(len(s), len(items)) @@ -418,7 +422,7 @@ class TestWeakSet(unittest.TestCase): gc.collect() n1 = len(s) del it - gc.collect() + test_support.gc_collect() n2 = len(s) # one item may be kept alive inside the iterator self.assertIn(n1, (0, 1)) diff -r 63bbe9f80909 Lib/test/test_xmlrpc.py --- a/Lib/test/test_xmlrpc.py Sat Sep 12 17:20:47 2015 -0700 +++ b/Lib/test/test_xmlrpc.py Tue Sep 15 21:14:10 2015 +0300 @@ -440,6 +440,7 @@ class BaseServerTestCase(unittest.TestCa def tearDown(self): # wait on the server thread to terminate + test_support.gc_collect() # to close the active connections self.evt.wait(10) # disable traceback reporting