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

Function TemporaryFile

Lib/tempfile.py:631–704  ·  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

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

Source from the content-addressed store, hash-verified

629 _O_TMPFILE_WORKS = hasattr(_os, 'O_TMPFILE')
630
631 def TemporaryFile(mode='w+b', buffering=-1, encoding=None,
632 newline=None, suffix=None, prefix=None,
633 dir=None, *, errors=None):
634 """Create and return a temporary file.
635 Arguments:
636 'prefix', 'suffix', 'dir' -- as for mkstemp.
637 'mode' -- the mode argument to io.open (default "w+b").
638 'buffering' -- the buffer size argument to io.open (default -1).
639 'encoding' -- the encoding argument to io.open (default None)
640 'newline' -- the newline argument to io.open (default None)
641 'errors' -- the errors argument to io.open (default None)
642 The file is created as mkstemp() would do it.
643
644 Returns an object with a file-like interface. The file has no
645 name, and will cease to exist when it is closed.
646 """
647 global _O_TMPFILE_WORKS
648
649 if "b" not in mode:
650 encoding = _io.text_encoding(encoding)
651
652 prefix, suffix, dir, output_type = _sanitize_params(prefix, suffix, dir)
653
654 flags = _bin_openflags
655 if _O_TMPFILE_WORKS:
656 fd = None
657 def opener(*args):
658 nonlocal fd
659 flags2 = (flags | _os.O_TMPFILE) & ~_os.O_CREAT & ~_os.O_EXCL
660 fd = _os.open(dir, flags2, 0o600)
661 return fd
662 try:
663 file = _io.open(dir, mode, buffering=buffering,
664 newline=newline, encoding=encoding,
665 errors=errors, opener=opener)
666 raw = getattr(file, 'buffer', file)
667 raw = getattr(raw, 'raw', raw)
668 raw.name = fd
669 return file
670 except IsADirectoryError:
671 # Linux kernel older than 3.11 ignores the O_TMPFILE flag:
672 # O_TMPFILE is read as O_DIRECTORY. Trying to open a directory
673 # with O_RDWR|O_DIRECTORY fails with IsADirectoryError, a
674 # directory cannot be open to write. Set flag to False to not
675 # try again.
676 _O_TMPFILE_WORKS = False
677 except OSError:
678 # The filesystem of the directory does not support O_TMPFILE.
679 # For example, OSError(95, 'Operation not supported').
680 #
681 # On Linux kernel older than 3.11, trying to open a regular
682 # file (or a symbolic link to a regular file) with O_TMPFILE
683 # fails with NotADirectoryError, because O_TMPFILE is read as
684 # O_DIRECTORY.
685 pass
686 # Fallback to _mkstemp_inner().
687
688 fd = None

Callers 15

_write_testMethod · 0.90
_write_error_testMethod · 0.90
test_writerowsMethod · 0.90
test_writerows_errorsMethod · 0.90
test_space_dialectMethod · 0.90
compare_dialect_123Method · 0.90
readerAssertEqualMethod · 0.90
writerAssertEqualMethod · 0.90

Calls 2

_sanitize_paramsFunction · 0.85
openMethod · 0.45

Tested by 15

_write_testMethod · 0.72
_write_error_testMethod · 0.72
test_writerowsMethod · 0.72
test_writerows_errorsMethod · 0.72
test_space_dialectMethod · 0.72
compare_dialect_123Method · 0.72
readerAssertEqualMethod · 0.72
writerAssertEqualMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…