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

Function temp_dir

Lib/test/support/os_helper.py:502–544  ·  view source on GitHub ↗

Return a context manager that creates a temporary directory. Arguments: path: the directory to create temporarily. If omitted or None, defaults to creating a temporary directory using tempfile.mkdtemp. quiet: if False (the default), the context manager raises an exception

(path=None, quiet=False)

Source from the content-addressed store, hash-verified

500
501@contextlib.contextmanager
502def temp_dir(path=None, quiet=False):
503 """Return a context manager that creates a temporary directory.
504
505 Arguments:
506
507 path: the directory to create temporarily. If omitted or None,
508 defaults to creating a temporary directory using tempfile.mkdtemp.
509
510 quiet: if False (the default), the context manager raises an exception
511 on error. Otherwise, if the path is specified and cannot be
512 created, only a warning is issued.
513
514 """
515 import tempfile
516 dir_created = False
517 if path is None:
518 path = tempfile.mkdtemp()
519 dir_created = True
520 path = os.path.realpath(path)
521 else:
522 try:
523 os.mkdir(path)
524 dir_created = True
525 except OSError as exc:
526 if not quiet:
527 raise
528 logging.getLogger(__name__).warning(
529 "tests may fail, unable to create temporary directory %r: %s",
530 path,
531 exc,
532 exc_info=exc,
533 stack_info=True,
534 stacklevel=3,
535 )
536 if dir_created:
537 pid = os.getpid()
538 try:
539 yield path
540 finally:
541 # In case the process forks, let only the parent remove the
542 # directory. The child has a different process id. (bpo-30028)
543 if dir_created and pid == os.getpid():
544 rmtree(path)
545
546
547@contextlib.contextmanager

Calls 6

mkdtempMethod · 0.80
realpathMethod · 0.80
getLoggerMethod · 0.80
rmtreeFunction · 0.70
mkdirMethod · 0.45
warningMethod · 0.45

Tested by 15

test_basic_scriptMethod · 0.72
test_script_compiledMethod · 0.72
test_directoryMethod · 0.72
test_directory_errorMethod · 0.72
test_zipfileMethod · 0.72
test_zipfile_compiledMethod · 0.72
test_zipfile_errorMethod · 0.72