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

Function NamedTemporaryFile

Lib/tempfile.py:558–618  ·  view source on GitHub ↗

Create and return a temporary file. Arguments: 'prefix', 'suffix', 'dir' -- as for mkstemp. 'mode' -- the mode argument to io.open (default "w+b"). 'buffering' -- the buffer size argument to io.open (default -1). 'encoding' -- the encoding argument to io.open (default None) '

(mode='w+b', buffering=-1, encoding=None,
                       newline=None, suffix=None, prefix=None,
                       dir=None, delete=True, *, errors=None,
                       delete_on_close=True)

Source from the content-addressed store, hash-verified

556 yield line
557
558def NamedTemporaryFile(mode='w+b', buffering=-1, encoding=None,
559 newline=None, suffix=None, prefix=None,
560 dir=None, delete=True, *, errors=None,
561 delete_on_close=True):
562 """Create and return a temporary file.
563 Arguments:
564 'prefix', 'suffix', 'dir' -- as for mkstemp.
565 'mode' -- the mode argument to io.open (default "w+b").
566 'buffering' -- the buffer size argument to io.open (default -1).
567 'encoding' -- the encoding argument to io.open (default None)
568 'newline' -- the newline argument to io.open (default None)
569 'delete' -- whether the file is automatically deleted (default True).
570 'delete_on_close' -- if 'delete', whether the file is deleted on close
571 (default True) or otherwise either on context manager exit
572 (if context manager was used) or on object finalization. .
573 'errors' -- the errors argument to io.open (default None)
574 The file is created as mkstemp() would do it.
575
576 Returns an object with a file-like interface; the name of the file
577 is accessible as its 'name' attribute. The file will be automatically
578 deleted when it is closed unless the 'delete' argument is set to False.
579
580 On POSIX, NamedTemporaryFiles cannot be automatically deleted if
581 the creating process is terminated abruptly with a SIGKILL signal.
582 Windows can delete the file even in this case.
583 """
584
585 prefix, suffix, dir, output_type = _sanitize_params(prefix, suffix, dir)
586
587 flags = _bin_openflags
588
589 # Setting O_TEMPORARY in the flags causes the OS to delete
590 # the file when it is closed. This is only supported by Windows.
591 if _os.name == 'nt' and delete and delete_on_close:
592 flags |= _os.O_TEMPORARY
593
594 if "b" not in mode:
595 encoding = _io.text_encoding(encoding)
596
597 name = None
598 def opener(*args):
599 nonlocal name
600 fd, name = _mkstemp_inner(dir, prefix, suffix, flags, output_type)
601 return fd
602 try:
603 file = _io.open(dir, mode, buffering=buffering,
604 newline=newline, encoding=encoding, errors=errors,
605 opener=opener)
606 try:
607 raw = getattr(file, 'buffer', file)
608 raw = getattr(raw, 'raw', raw)
609 raw.name = name
610 return _TemporaryFileWrapper(file, name, delete, delete_on_close)
611 except:
612 file.close()
613 raise
614 except:
615 if name is not None and not (

Callers

nothing calls this directly

Calls 5

_sanitize_paramsFunction · 0.85
openMethod · 0.45
closeMethod · 0.45
unlinkMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…