MCPcopy Index your code
hub / github.com/python/cpython / remove_testfn

Function remove_testfn

Lib/test/libregrtest/utils.py:493–527  ·  view source on GitHub ↗
(test_name: TestName, verbose: int)

Source from the content-addressed store, hash-verified

491
492
493def remove_testfn(test_name: TestName, verbose: int) -> None:
494 # Try to clean up os_helper.TESTFN if left behind.
495 #
496 # While tests shouldn't leave any files or directories behind, when a test
497 # fails that can be tedious for it to arrange. The consequences can be
498 # especially nasty on Windows, since if a test leaves a file open, it
499 # cannot be deleted by name (while there's nothing we can do about that
500 # here either, we can display the name of the offending test, which is a
501 # real help).
502 name = os_helper.TESTFN
503 if not os.path.exists(name):
504 return
505
506 nuker: Callable[[str], None]
507 if os.path.isdir(name):
508 import shutil
509 kind, nuker = "directory", shutil.rmtree
510 elif os.path.isfile(name):
511 kind, nuker = "file", os.unlink
512 else:
513 raise RuntimeError(f"os.path says {name!r} exists but is neither "
514 f"directory nor file")
515
516 if verbose:
517 print_warning(f"{test_name} left behind {kind} {name!r}")
518 support.environment_altered = True
519
520 try:
521 import stat
522 # fix possible permissions problems that might prevent cleanup
523 os.chmod(name, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
524 nuker(name)
525 except Exception as exc:
526 print_warning(f"{test_name} left behind {kind} {name!r} "
527 f"and it couldn't be removed: {exc}")
528
529
530def abs_module_name(test_name: TestName, test_dir: StrPath | None) -> TestName:

Callers 1

_load_run_testFunction · 0.85

Calls 5

print_warningFunction · 0.70
existsMethod · 0.45
isdirMethod · 0.45
isfileMethod · 0.45
chmodMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…