Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/django/core/files/temp.py: 21%
33 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1"""
2The temp module provides a NamedTemporaryFile that can be reopened in the same
3process on any platform. Most platforms use the standard Python
4tempfile.NamedTemporaryFile class, but Windows users are given a custom class.
6This is needed because the Python implementation of NamedTemporaryFile uses the
7O_TEMPORARY flag under Windows, which prevents the file from being reopened
8if the same flag is not provided [1][2]. Note that this does not address the
9more general issue of opening a file for writing and reading in multiple
10processes in a manner that works across platforms.
12The custom version of NamedTemporaryFile doesn't support the same keyword
13arguments available in tempfile.NamedTemporaryFile.
151: https://mail.python.org/pipermail/python-list/2005-December/336957.html
162: https://bugs.python.org/issue14243
17"""
19import os
20import tempfile
22from django.core.files.utils import FileProxyMixin
24__all__ = (
25 "NamedTemporaryFile",
26 "gettempdir",
27)
30if os.name == "nt": 30 ↛ 32line 30 didn't jump to line 32, because the condition on line 30 was never true
32 class TemporaryFile(FileProxyMixin):
33 """
34 Temporary file object constructor that supports reopening of the
35 temporary file in Windows.
37 Unlike tempfile.NamedTemporaryFile from the standard library,
38 __init__() doesn't support the 'delete', 'buffering', 'encoding', or
39 'newline' keyword arguments.
40 """
42 def __init__(self, mode="w+b", bufsize=-1, suffix="", prefix="", dir=None):
43 fd, name = tempfile.mkstemp(suffix=suffix, prefix=prefix, dir=dir)
44 self.name = name
45 self.file = os.fdopen(fd, mode, bufsize)
46 self.close_called = False
48 # Because close can be called during shutdown
49 # we need to cache os.unlink and access it
50 # as self.unlink only
51 unlink = os.unlink
53 def close(self):
54 if not self.close_called:
55 self.close_called = True
56 try:
57 self.file.close()
58 except OSError:
59 pass
60 try:
61 self.unlink(self.name)
62 except OSError:
63 pass
65 def __del__(self):
66 self.close()
68 def __enter__(self):
69 self.file.__enter__()
70 return self
72 def __exit__(self, exc, value, tb):
73 self.file.__exit__(exc, value, tb)
75 NamedTemporaryFile = TemporaryFile
76else:
77 NamedTemporaryFile = tempfile.NamedTemporaryFile
79gettempdir = tempfile.gettempdir