bpo-23882: unittest: Drop PEP 420 support from discovery. (GH-29745) · python/cpython@0b2b9d2 (original) (raw)
`@@ -264,8 +264,6 @@ def discover(self, start_dir, pattern='test*.py', top_level_dir=None):
`
264
264
`self._top_level_dir = top_level_dir
`
265
265
``
266
266
`is_not_importable = False
`
267
``
`-
is_namespace = False
`
268
``
`-
tests = []
`
269
267
`if os.path.isdir(os.path.abspath(start_dir)):
`
270
268
`start_dir = os.path.abspath(start_dir)
`
271
269
`if start_dir != top_level_dir:
`
`@@ -281,50 +279,25 @@ def discover(self, start_dir, pattern='test*.py', top_level_dir=None):
`
281
279
`top_part = start_dir.split('.')[0]
`
282
280
`try:
`
283
281
`start_dir = os.path.abspath(
`
284
``
`-
os.path.dirname((the_module.file)))
`
``
282
`+
os.path.dirname((the_module.file)))
`
285
283
`except AttributeError:
`
286
``
`-
look for namespace packages
`
287
``
`-
try:
`
288
``
`-
spec = the_module.spec
`
289
``
`-
except AttributeError:
`
290
``
`-
spec = None
`
291
``
-
292
``
`-
if spec and spec.loader is None:
`
293
``
`-
if spec.submodule_search_locations is not None:
`
294
``
`-
is_namespace = True
`
295
``
-
296
``
`-
for path in the_module.path:
`
297
``
`-
if (not set_implicit_top and
`
298
``
`-
not path.startswith(top_level_dir)):
`
299
``
`-
continue
`
300
``
`-
self._top_level_dir = \
`
301
``
`-
(path.split(the_module.name
`
302
``
`-
.replace(".", os.path.sep))[0])
`
303
``
`-
tests.extend(self._find_tests(path,
`
304
``
`-
pattern,
`
305
``
`-
namespace=True))
`
306
``
`-
elif the_module.name in sys.builtin_module_names:
`
``
284
`+
if the_module.name in sys.builtin_module_names:
`
307
285
`# builtin module
`
308
286
`raise TypeError('Can not use builtin modules '
`
309
287
`'as dotted module names') from None
`
310
288
`else:
`
311
289
`raise TypeError(
`
312
``
`-
'don't know how to discover from {!r}'
`
313
``
`-
.format(the_module)) from None
`
``
290
`+
f"don't know how to discover from {the_module!r}"
`
``
291
`+
) from None
`
314
292
``
315
293
`if set_implicit_top:
`
316
``
`-
if not is_namespace:
`
317
``
`-
self._top_level_dir = \
`
318
``
`-
self._get_directory_containing_module(top_part)
`
319
``
`-
sys.path.remove(top_level_dir)
`
320
``
`-
else:
`
321
``
`-
sys.path.remove(top_level_dir)
`
``
294
`+
self._top_level_dir = self._get_directory_containing_module(top_part)
`
``
295
`+
sys.path.remove(top_level_dir)
`
322
296
``
323
297
`if is_not_importable:
`
324
298
`raise ImportError('Start directory is not importable: %r' % start_dir)
`
325
299
``
326
``
`-
if not is_namespace:
`
327
``
`-
tests = list(self._find_tests(start_dir, pattern))
`
``
300
`+
tests = list(self._find_tests(start_dir, pattern))
`
328
301
`return self.suiteClass(tests)
`
329
302
``
330
303
`def _get_directory_containing_module(self, module_name):
`
`@@ -359,7 +332,7 @@ def _match_path(self, path, full_path, pattern):
`
359
332
`# override this method to use alternative matching strategy
`
360
333
`return fnmatch(path, pattern)
`
361
334
``
362
``
`-
def _find_tests(self, start_dir, pattern, namespace=False):
`
``
335
`+
def _find_tests(self, start_dir, pattern):
`
363
336
`"""Used by discovery. Yields test suites it loads."""
`
364
337
`# Handle the init in this package
`
365
338
`name = self._get_name_from_path(start_dir)
`
`@@ -368,8 +341,7 @@ def _find_tests(self, start_dir, pattern, namespace=False):
`
368
341
`if name != '.' and name not in self._loading_packages:
`
369
342
`# name is in self._loading_packages while we have called into
`
370
343
`# loadTestsFromModule with name.
`
371
``
`-
tests, should_recurse = self._find_test_path(
`
372
``
`-
start_dir, pattern, namespace)
`
``
344
`+
tests, should_recurse = self._find_test_path(start_dir, pattern)
`
373
345
`if tests is not None:
`
374
346
`yield tests
`
375
347
`if not should_recurse:
`
`@@ -380,20 +352,19 @@ def _find_tests(self, start_dir, pattern, namespace=False):
`
380
352
`paths = sorted(os.listdir(start_dir))
`
381
353
`for path in paths:
`
382
354
`full_path = os.path.join(start_dir, path)
`
383
``
`-
tests, should_recurse = self._find_test_path(
`
384
``
`-
full_path, pattern, namespace)
`
``
355
`+
tests, should_recurse = self._find_test_path(full_path, pattern)
`
385
356
`if tests is not None:
`
386
357
`yield tests
`
387
358
`if should_recurse:
`
388
359
`# we found a package that didn't use load_tests.
`
389
360
`name = self._get_name_from_path(full_path)
`
390
361
`self._loading_packages.add(name)
`
391
362
`try:
`
392
``
`-
yield from self._find_tests(full_path, pattern, namespace)
`
``
363
`+
yield from self._find_tests(full_path, pattern)
`
393
364
`finally:
`
394
365
`self._loading_packages.discard(name)
`
395
366
``
396
``
`-
def _find_test_path(self, full_path, pattern, namespace=False):
`
``
367
`+
def _find_test_path(self, full_path, pattern):
`
397
368
`"""Used by discovery.
`
398
369
``
399
370
` Loads tests from a single file, or a directories' init.py when
`
`@@ -437,8 +408,7 @@ def _find_test_path(self, full_path, pattern, namespace=False):
`
437
408
`msg % (mod_name, module_dir, expected_dir))
`
438
409
`return self.loadTestsFromModule(module, pattern=pattern), False
`
439
410
`elif os.path.isdir(full_path):
`
440
``
`-
if (not namespace and
`
441
``
`-
not os.path.isfile(os.path.join(full_path, 'init.py'))):
`
``
411
`+
if not os.path.isfile(os.path.join(full_path, 'init.py')):
`
442
412
`return None, False
`
443
413
``
444
414
`load_tests = None
`