New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Current os.tmpfile() implementation requires admin privs on Vista/2k8. #46485
Comments
posix_tmpfile() needs to be reworked such that tmpfile() isn't used if The recommended approach in MSDN is to use, quote, "tmpname or tempnam Assuming no one beats me to it, I'll look at creating a patch in the |
Side note: |
According to grep, the only place where os.tmp* is referenced is in test_os. |
With Chris and Ben's comments taken into account, what's the best way to
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
|
|
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) |
Er, errno being referred to in a comment in that last patch should be |
Tested patch against: http://svn.python.org/projects/python/trunk @ 61260 OS Name: Microsoft Windows XP Professional rt test_os python -E -tt ../lib/test/regrtest.py test_os |
Thanks for the patch. Committed as r61264 and r61266. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: