Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
ca54982b
Commit
ca54982b
authored
Aug 12, 1997
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added TemporaryFile and wrapper class by Jim Fulton.
Look in more env vars (for NT: TEMP, TMP).
parent
3fb1aea0
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
41 additions
and
2 deletions
+41
-2
Lib/tempfile.py
Lib/tempfile.py
+41
-2
No files found.
Lib/tempfile.py
View file @
ca54982b
...
...
@@ -32,8 +32,9 @@ def gettempdir():
attempdirs
.
insert
(
0
,
dirname
)
except
macfs
.
error
:
pass
if
os
.
environ
.
has_key
(
'TMPDIR'
):
attempdirs
.
insert
(
0
,
os
.
environ
[
'TMPDIR'
])
for
envname
in
'TMPDIR'
,
'TEMP'
,
'TMP'
:
if
os
.
environ
.
has_key
(
envname
):
attempdirs
.
insert
(
0
,
os
.
environ
[
envname
])
testfile
=
gettempprefix
()
+
'test'
for
dir
in
attempdirs
:
try
:
...
...
@@ -82,3 +83,41 @@ def mktemp():
file
=
os
.
path
.
join
(
dir
,
pre
+
`counter`
)
if
not
os
.
path
.
exists
(
file
):
return
file
class
TemporaryFileWrapper
:
"""Temporary file wrapper
This class provides a wrapper around files opened for temporary use.
In particular, it seeks to automatically remove the file when it is
no longer needed.
"""
def
__init__
(
self
,
file
,
path
):
self
.
file
=
file
self
.
path
=
path
def
close
(
self
):
self
.
file
.
close
()
os
.
unlink
(
self
.
path
)
def
__del__
(
self
):
try
:
self
.
close
()
except
:
pass
def
__getattr__
(
self
,
name
):
file
=
self
.
__dict__
[
'file'
]
a
=
getattr
(
file
,
name
)
setattr
(
self
,
name
,
a
)
return
a
def
TemporaryFile
(
mode
=
'w+b'
,
bufsize
=-
1
):
name
=
mktemp
()
file
=
open
(
name
,
mode
,
bufsize
)
try
:
os
.
unlink
(
name
)
except
os
.
error
:
# Non-unix -- can't unlink file that's still open, use wrapper
return
TemporaryFileWrapper
(
file
,
name
)
else
:
return
file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment