Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d662548

Browse files
committed
Patch #810914: Return absolute path for mkstemp. Fixes #810408.
This should not be backported to 2.3, as it might break backwards compatibility.
1 parent 411c602 commit d662548

3 files changed

Lines changed: 10 additions & 2 deletions

File tree

Lib/tempfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ def _mkstemp_inner(dir, pre, suf, flags):
214214
try:
215215
fd = _os.open(file, flags, 0600)
216216
_set_cloexec(fd)
217-
return (fd, file)
217+
return (fd, _os.path.abspath(file))
218218
except OSError, e:
219219
if e.errno == _errno.EEXIST:
220220
continue # try again

Lib/test/test_tempfile.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ def nameCheck(self, name, dir, pre, suf):
4949
npre = nbase[:len(pre)]
5050
nsuf = nbase[len(nbase)-len(suf):]
5151

52-
self.assertEqual(ndir, dir,
52+
# check for equality of the absolute paths!
53+
self.assertEqual(os.path.abspath(ndir), os.path.abspath(dir),
5354
"file '%s' not in directory '%s'" % (name, dir))
5455
self.assertEqual(npre, pre,
5556
"file '%s' does not begin with '%s'" % (nbase, pre))
@@ -384,6 +385,10 @@ def do_create(self, dir=None, pre="", suf="", ):
384385
dir = tempfile.gettempdir()
385386
try:
386387
(fd, name) = tempfile.mkstemp(dir=dir, prefix=pre, suffix=suf)
388+
(ndir, nbase) = os.path.split(name)
389+
adir = os.path.abspath(dir)
390+
self.assertEqual(adir, ndir,
391+
"Directory '%s' incorrectly returned as '%s'" % (adir, ndir))
387392
except:
388393
self.failOnException("mkstemp")
389394

@@ -400,6 +405,7 @@ def test_basic(self):
400405
self.do_create(suf="b")
401406
self.do_create(pre="a", suf="b")
402407
self.do_create(pre="aa", suf=".txt")
408+
self.do_create(dir=".")
403409

404410
def test_choose_directory(self):
405411
# mkstemp can create directories in a user-selected directory

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ Extension modules
5555
Library
5656
-------
5757

58+
- tmpfile.mkstemp now returns an absolute path even if dir is relative.
59+
5860
- urlparse is RFC 2396 compliant.
5961

6062
- The fieldnames argument to the csv module's DictReader constructor is now

0 commit comments

Comments
 (0)