Title: _top_level_dir state leaks on defaultTestLoader
Type: Stage:
Components: Library (Lib) Versions: Python 3.5
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: ZackerySpytz, rbcollins
Priority: normal Keywords:

Created on 2014-11-07 06:05 by rbcollins, last changed 2019-08-13 01:13 by ZackerySpytz.

Messages (2)
msg230770 - (view) Author: Robert Collins (rbcollins) * (Python committer) Date: 2014-11-07 06:05
TestProgram uses defaultTestLoader to load tests. Calling discover() on a TestLoader sets _top_level_dir. Calling discover with no top_level_dir implictly sets _top_level_dir but only the first time: the second time it is called with a different start_dir the prior one is used.

This leads to errors like:
ERROR: test_run_list_failed_import (testtools.tests.test_run.TestRun)
_StringException: Traceback (most recent call last):
  File "/home/robertc/personal/testtools/testtools/tests/", line 175, in test_run_list_failed_import
    run.main, ['prog', 'discover', '-l', broken.package.base, '*.py'], out)
  File "/home/robertc/personal/testtools/testtools/", line 420, in assertRaises
    self.assertThat(our_callable, matcher)
  File "/home/robertc/personal/testtools/testtools/", line 431, in assertThat
    mismatch_error = self._matchHelper(matchee, matcher, message, verbose)
  File "/home/robertc/personal/testtools/testtools/", line 481, in _matchHelper
    mismatch = matcher.match(matchee)
  File "/home/robertc/personal/testtools/testtools/matchers/", line 108, in match
    mismatch = self.exception_matcher.match(exc_info)
  File "/home/robertc/personal/testtools/testtools/matchers/", line 62, in match
    mismatch = matcher.match(matchee)
  File "/home/robertc/personal/testtools/testtools/", line 412, in match
  File "/home/robertc/personal/testtools/testtools/matchers/", line 101, in match
    result = matchee()
  File "/home/robertc/personal/testtools/testtools/", line 965, in __call__
    return self._callable_object(*self._args, **self._kwargs)
  File "/home/robertc/personal/testtools/testtools/", line 413, in main
  File "/home/robertc/personal/testtools/testtools/", line 208, in __init__
  File "/home/robertc/personal/testtools/testtools/", line 252, in parseArgs
  File "/home/robertc/personal/testtools/testtools/", line 368, in _do_discovery
    loaded =, self.pattern,
  File "/home/robertc/.virtualenvs/testtools-2.7/local/lib/python2.7/site-packages/unittest2/", line 353, in discover
    raise ImportError('Start directory is not importable: %r' % start_dir)
ImportError: Start directory is not importable: '/tmp/tmpac4uwI'

When a test tries to test the behaviour of TestProgram (or a subclass thereof).

The fix is straight forward: at the end of discover restore the value of _top_level_dir that it had at entry.
msg349512 - (view) Author: Zackery Spytz (ZackerySpytz) * (Python triager) Date: 2019-08-13 01:13
It seems that this issue is a duplicate of bpo-15010.
Date User Action Args
2019-08-13 01:13:05ZackerySpytzsetnosy: + ZackerySpytz
messages: + msg349512
2014-11-07 06:05:42rbcollinssetcomponents: + Library (Lib)
versions: + Python 3.5
2014-11-07 06:05:12rbcollinscreate