This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Current os.tmpfile() implementation requires admin privs on Vista/2k8.
Type: behavior Stage:
Components: Windows Versions: Python 2.6
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: Nosy List: JosephArmbruster, benjamin.peterson, christian.heimes, loewis, trent
Priority: normal Keywords: patch

Created on 2008-03-04 17:01 by trent, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
test_os.py.patch trent, 2008-03-05 22:45 Patch to trunk/Lib/test/test_os.py
test_os.py.2.patch trent, 2008-03-05 22:53 Updated patch to trunk/Lib/test/test_os.py
Messages (9)
msg63254 - (view) Author: Trent Nelson (trent) * (Python committer) Date: 2008-03-04 17:01
posix_tmpfile() needs to be reworked such that tmpfile() isn't used if 
MS_WINDOWS is defined, as it requires administrative privileges to run 
(it creates the file in the root directory) on Vista/2k8 (although 
there are reports of this not working on XP w/ 2.5: 
http://www.thescripts.com/forum/thread600423.html).

The recommended approach in MSDN is to use, quote, "tmpname or tempnam 
in conjunction with fopen": http://msdn2.microsoft.com/en-
us/library/x8x7sakw(VS.80).aspx

Assuming no one beats me to it, I'll look at creating a patch in the 
next day or two when I get some spare time.
msg63260 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2008-03-04 21:14
Side note:
I've removed the methods from Python 3.0 about half a year ago. Code
should use the tempfile module anyway. Does any of the Python 2.6 stdlib
code use an os.tmp* method?
msg63262 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2008-03-04 21:52
According to grep, the only place where os.tmp* is referenced is in test_os.
msg63279 - (view) Author: Trent Nelson (trent) * (Python committer) Date: 2008-03-05 14:49
With Chris and Ben's comments taken into account, what's the best way to
handle this?

1.  Change the test: if the user is not admin, assert os.tmpfile()
returns  a permission denied OSError, otherwise, assert return value is
a current file.
2.  Alter posix_tmpfile() to warn the user of the situation if the call
fails and we're on Windows:
Index: posixmodule.c
===================================================================
--- posixmodule.c       (revision 61233)
+++ posixmodule.c       (working copy)
@@ -7029,8 +7029,15 @@
     FILE *fp;

     fp = tmpfile();
-    if (fp == NULL)
+    if (fp == NULL) {
+#ifdef MS_WINDOWS
+        PyErr_Warn(PyExc_RuntimeWarning,
+                   "tmpfile creates a file in the root directory and "   \
+                   "requires administrative privileges, consider using " \
+                   "tempfile.mkstemp() instead");
+#endif
         return posix_error();
+    }
     return PyFile_FromFile(fp, "<tmpfile>", "w+b", fclose);
 }
 #endif

3. Alter posix_tmpfile() to use _tempnam() on Windows instead, such
admin privileges are no longer required.  (Possibly adding a similar
warning that tempfile.mkstemp should be used instead though, as above?)
msg63294 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2008-03-05 21:35
> With Chris and Ben's comments taken into account, what's the best way to
> handle this?
I think, given that is being removed, we can safely go with option 1.
msg63299 - (view) Author: Trent Nelson (trent) * (Python committer) Date: 2008-03-05 22:45
I agree.  Following patch fixes the issue for me:

Index: test_os.py
===================================================================
--- test_os.py  (revision 61260)
+++ test_os.py  (working copy)
@@ -65,6 +65,44 @@
     def test_tmpfile(self):
         if not hasattr(os, "tmpfile"):
             return
+        # As with test_tmpnam() below, the Windows implementation of 
tmpfile()
+        # attempts to create a file in the root directory of the 
current drive.
+        # On Vista and Server 2008, this test will always fail for 
normal users
+        # as writing to the root directory requires elevated 
privileges.  With
+        # XP and below, the semantics of tmpfile() are the same, but 
the user
+        # running the test is more likely to have administrative 
privileges on
+        # their account already.  If that's the case, then os.tmpfile
() should
+        # work.  In order to make this test as useful as possible, 
rather than
+        # trying to detect Windows versions or whether or not the user 
has the
+        # right permissions, just try and create a file in the root 
directory
+        # and see if it raises a 'Permission denied' OSError.  If it 
does, then
+        # test that a subsequent call to os.tmpfile() raises the same 
error. If
+        # it doesn't, assume we're on XP or below and the user running 
the test
+        # has administrative privileges, and proceed with the test as 
normal.
+        if sys.platform == 'win32':
+            name = '\\python_test_os_test_tmpfile.txt'
+            if os.path.exists(name):
+                os.remove(name)
+            try:
+                fp = open(name, 'w')
+            except IOError, first:
+                # open() failed, assert tmpfile() fails in the same 
way.
+                # Although open() raises an IOError and os.tmpfile() 
raises an
+                # OSError(), 'args' will be (12, 'Permission denied') 
in both
+                # cases.
+                try:
+                    fp = os.tmpfile()
+                except OSError, second:
+                    self.assertEqual(first.args, second.args)
+                else:
+                    self.fail("expected os.tmpfile() to raise OSError")
+                return
+            else:
+                # open() worked, therefore, tmpfile() should work.  
Close our
+                # dummy file and proceed with the test as normal.
+                fp.close()
+                os.remove(name)
+
         fp = os.tmpfile()
         fp.write("foobar")
         fp.seek(0,0)
msg63300 - (view) Author: Trent Nelson (trent) * (Python committer) Date: 2008-03-05 22:53
Er, errno being referred to in a comment in that last patch should be
13, not 12.
msg63301 - (view) Author: Joseph Armbruster (JosephArmbruster) Date: 2008-03-05 23:47
Tested patch against: http://svn.python.org/projects/python/trunk @ 61260

OS Name:                   Microsoft Windows XP Professional
OS Version:                5.1.2600 Service Pack 2 Build 260

rt test_os
Deleting .pyc/.pyo files ...
(57, '.pyc deleted,', 0, '.pyo deleted')

python  -E -tt ../lib/test/regrtest.py test_os
test_os
1 test OK.
msg63305 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2008-03-06 06:57
Thanks for the patch. Committed as r61264 and r61266.
History
Date User Action Args
2022-04-11 14:56:31adminsetgithub: 46485
2008-03-06 06:57:46loewissetstatus: open -> closed
resolution: accepted
messages: + msg63305
nosy: + loewis
2008-03-05 23:47:36JosephArmbrustersetnosy: + JosephArmbruster
messages: + msg63301
2008-03-05 22:53:14trentsetfiles: + test_os.py.2.patch
messages: + msg63300
2008-03-05 22:45:31trentsetfiles: + test_os.py.patch
keywords: + patch
messages: + msg63299
2008-03-05 21:35:36benjamin.petersonsetmessages: + msg63294
2008-03-05 14:49:01trentsetmessages: + msg63279
2008-03-04 21:52:09benjamin.petersonsetnosy: + benjamin.peterson
messages: + msg63262
2008-03-04 21:14:59christian.heimessetnosy: + christian.heimes
messages: + msg63260
2008-03-04 17:01:17trentcreate