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

Function change_cwd

Lib/test/support/os_helper.py:549–579  ·  view source on GitHub ↗

Return a context manager that changes the current working directory. Arguments: path: the directory to use as the temporary current working directory. quiet: if False (the default), the context manager raises an exception on error. Otherwise, it issues only a warning and

(path, quiet=False)

Source from the content-addressed store, hash-verified

547
548@contextlib.contextmanager
549def change_cwd(path, quiet=False):
550 """Return a context manager that changes the current working directory.
551
552 Arguments:
553
554 path: the directory to use as the temporary current working directory.
555
556 quiet: if False (the default), the context manager raises an exception
557 on error. Otherwise, it issues only a warning and keeps the current
558 working directory the same.
559
560 """
561 saved_dir = os.getcwd()
562 try:
563 os.chdir(os.path.realpath(path))
564 except OSError as exc:
565 if not quiet:
566 raise
567 logging.getLogger(__name__).warning(
568 'tests may fail, unable to change the current working directory '
569 'to %r: %s',
570 path,
571 exc,
572 exc_info=exc,
573 stack_info=True,
574 stacklevel=3,
575 )
576 try:
577 yield os.getcwd()
578 finally:
579 os.chdir(saved_dir)
580
581
582@contextlib.contextmanager

Callers 6

globMethod · 0.90
test_recursive_globMethod · 0.90
_do_directoryMethod · 0.90
temp_cwdFunction · 0.85

Calls 3

getLoggerMethod · 0.80
realpathMethod · 0.45
warningMethod · 0.45

Tested by 5

globMethod · 0.72
test_recursive_globMethod · 0.72
_do_directoryMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…