diff -r 3c34ab550358 Lib/threading.py --- a/Lib/threading.py Wed Dec 04 01:47:46 2013 +0100 +++ b/Lib/threading.py Wed Dec 04 02:02:18 2013 +0100 @@ -16,6 +16,8 @@ try: except ImportError: from collections import deque as _deque +import tracemalloc + # Note regarding PEP 8 compliant names # This threading model was originally inspired by Java, and inherited # the convention of camelCase function and method names from that @@ -848,6 +850,7 @@ class Thread: with _active_limbo_lock: _limbo[self] = self try: + tracemalloc.stop() _start_new_thread(self._bootstrap, ()) except Exception: with _active_limbo_lock: @@ -885,6 +888,7 @@ class Thread: # _bootstrap_inner() during normal business hours are properly # reported. Also, we only suppress them for daemonic threads; # if a non-daemonic encounters this, something else is wrong. + tracemalloc.stop() try: self._bootstrap_inner() except: diff -r 3c34ab550358 Lib/unittest/case.py --- a/Lib/unittest/case.py Wed Dec 04 01:47:46 2013 +0100 +++ b/Lib/unittest/case.py Wed Dec 04 02:02:18 2013 +0100 @@ -9,6 +9,8 @@ import re import warnings import collections import contextlib +import tracemalloc +import random from . import result from .util import (strclass, safe_repr, _count_diff_all_purpose, @@ -567,8 +569,13 @@ class TestCase(object): self.setUp() if outcome.success: outcome.expecting_failure = expecting_failure + limit = random.randint(100, 1024*50) with outcome.testPartExecutor(self, isTest=True): - testMethod() + tracemalloc.start(memory_limit=limit) + try: + testMethod() + finally: + tracemalloc.stop() outcome.expecting_failure = False with outcome.testPartExecutor(self): self.tearDown()