MCPcopy Index your code
hub / github.com/RustPython/RustPython / change_cwd

Function change_cwd

Lib/test/support/os_helper.py:548–578  ·  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

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

Callers 5

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

Calls 3

realpathMethod · 0.80
getLoggerMethod · 0.80
warningMethod · 0.45

Tested by 4

globMethod · 0.72
test_recursive_globMethod · 0.72
_do_directoryMethod · 0.72