diff -r c3cec0f77eff Lib/unittest/loader.py --- a/Lib/unittest/loader.py Tue Oct 20 18:22:36 2015 +0300 +++ b/Lib/unittest/loader.py Tue Oct 20 21:52:28 2015 +0200 @@ -479,6 +479,8 @@ return tests, True finally: self._loading_packages.discard(name) + else: + return None, False defaultTestLoader = TestLoader() diff -r c3cec0f77eff Lib/unittest/test/test_discovery.py --- a/Lib/unittest/test/test_discovery.py Tue Oct 20 18:22:36 2015 +0300 +++ b/Lib/unittest/test/test_discovery.py Tue Oct 20 21:52:28 2015 +0200 @@ -53,7 +53,8 @@ os.path.isdir = original_isdir path_lists = [['test2.py', 'test1.py', 'not_a_test.py', 'test_dir', - 'test.foo', 'test-not-a-module.py', 'another_dir'], + 'test.foo', 'test-not-a-module.py', 'another_dir', + 'socket'], ['test4.py', 'test3.py', ]] os.listdir = lambda path: path_lists.pop(0) self.addCleanup(restore_listdir) @@ -64,8 +65,15 @@ self.addCleanup(restore_isdir) def isfile(path): + if isdir(path): + return False # another_dir is not a package and so shouldn't be recursed into - return not path.endswith('dir') and not 'another_dir' in path + if 'another_dir' in path: + return False + # a socket is neither a directory nor a regular file (issue 22457) + if path.endswith('socket'): + return False + return True os.path.isfile = isfile self.addCleanup(restore_isfile)