Kaydet (Commit) b0614615 authored tarafından Antoine Pitrou's avatar Antoine Pitrou

Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k

DeprecationWarning.
üst f11d1832
...@@ -80,16 +80,18 @@ class TemporaryFileTests(unittest.TestCase): ...@@ -80,16 +80,18 @@ class TemporaryFileTests(unittest.TestCase):
def test_tempnam(self): def test_tempnam(self):
if not hasattr(os, "tempnam"): if not hasattr(os, "tempnam"):
return return
warnings.filterwarnings("ignore", "tempnam", RuntimeWarning, with warnings.catch_warnings():
r"test_os$") warnings.filterwarnings("ignore", "tempnam", RuntimeWarning,
self.check_tempfile(os.tempnam()) r"test_os$")
warnings.filterwarnings("ignore", "tempnam", DeprecationWarning)
self.check_tempfile(os.tempnam())
name = os.tempnam(test_support.TESTFN) name = os.tempnam(test_support.TESTFN)
self.check_tempfile(name) self.check_tempfile(name)
name = os.tempnam(test_support.TESTFN, "pfx") name = os.tempnam(test_support.TESTFN, "pfx")
self.assertTrue(os.path.basename(name)[:3] == "pfx") self.assertTrue(os.path.basename(name)[:3] == "pfx")
self.check_tempfile(name) self.check_tempfile(name)
def test_tmpfile(self): def test_tmpfile(self):
if not hasattr(os, "tmpfile"): if not hasattr(os, "tmpfile"):
...@@ -108,63 +110,69 @@ class TemporaryFileTests(unittest.TestCase): ...@@ -108,63 +110,69 @@ class TemporaryFileTests(unittest.TestCase):
# test that a subsequent call to os.tmpfile() raises the same error. If # 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 # 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. # has administrative privileges, and proceed with the test as normal.
if sys.platform == 'win32': with warnings.catch_warnings():
name = '\\python_test_os_test_tmpfile.txt' warnings.filterwarnings("ignore", "tmpfile", DeprecationWarning)
if os.path.exists(name):
os.remove(name) if sys.platform == 'win32':
try: name = '\\python_test_os_test_tmpfile.txt'
fp = open(name, 'w') if os.path.exists(name):
except IOError, first: os.remove(name)
# open() failed, assert tmpfile() fails in the same way.
# Although open() raises an IOError and os.tmpfile() raises an
# OSError(), 'args' will be (13, 'Permission denied') in both
# cases.
try: try:
fp = os.tmpfile() fp = open(name, 'w')
except OSError, second: except IOError, first:
self.assertEqual(first.args, second.args) # open() failed, assert tmpfile() fails in the same way.
# Although open() raises an IOError and os.tmpfile() raises an
# OSError(), 'args' will be (13, '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: else:
self.fail("expected os.tmpfile() to raise OSError") # open() worked, therefore, tmpfile() should work. Close our
return # dummy file and proceed with the test as normal.
else: fp.close()
# open() worked, therefore, tmpfile() should work. Close our os.remove(name)
# dummy file and proceed with the test as normal.
fp.close() fp = os.tmpfile()
os.remove(name) fp.write("foobar")
fp.seek(0,0)
fp = os.tmpfile() s = fp.read()
fp.write("foobar") fp.close()
fp.seek(0,0) self.assertTrue(s == "foobar")
s = fp.read()
fp.close()
self.assertTrue(s == "foobar")
def test_tmpnam(self): def test_tmpnam(self):
if not hasattr(os, "tmpnam"): if not hasattr(os, "tmpnam"):
return return
warnings.filterwarnings("ignore", "tmpnam", RuntimeWarning, with warnings.catch_warnings():
r"test_os$") warnings.filterwarnings("ignore", "tmpnam", RuntimeWarning,
name = os.tmpnam() r"test_os$")
if sys.platform in ("win32",): warnings.filterwarnings("ignore", "tmpnam", DeprecationWarning)
# The Windows tmpnam() seems useless. From the MS docs:
# name = os.tmpnam()
# The character string that tmpnam creates consists of if sys.platform in ("win32",):
# the path prefix, defined by the entry P_tmpdir in the # The Windows tmpnam() seems useless. From the MS docs:
# file STDIO.H, followed by a sequence consisting of the #
# digit characters '0' through '9'; the numerical value # The character string that tmpnam creates consists of
# of this string is in the range 1 - 65,535. Changing the # the path prefix, defined by the entry P_tmpdir in the
# definitions of L_tmpnam or P_tmpdir in STDIO.H does not # file STDIO.H, followed by a sequence consisting of the
# change the operation of tmpnam. # digit characters '0' through '9'; the numerical value
# # of this string is in the range 1 - 65,535. Changing the
# The really bizarre part is that, at least under MSVC6, # definitions of L_tmpnam or P_tmpdir in STDIO.H does not
# P_tmpdir is "\\". That is, the path returned refers to # change the operation of tmpnam.
# the root of the current drive. That's a terrible place to #
# put temp files, and, depending on privileges, the user # The really bizarre part is that, at least under MSVC6,
# may not even be able to open a file in the root directory. # P_tmpdir is "\\". That is, the path returned refers to
self.assertFalse(os.path.exists(name), # the root of the current drive. That's a terrible place to
"file already exists for temporary file") # put temp files, and, depending on privileges, the user
else: # may not even be able to open a file in the root directory.
self.check_tempfile(name) self.assertFalse(os.path.exists(name),
"file already exists for temporary file")
else:
self.check_tempfile(name)
# Test attributes on return values from os.*stat* family. # Test attributes on return values from os.*stat* family.
class StatAttributeTests(unittest.TestCase): class StatAttributeTests(unittest.TestCase):
......
...@@ -38,11 +38,13 @@ class PosixTester(unittest.TestCase): ...@@ -38,11 +38,13 @@ class PosixTester(unittest.TestCase):
"getpid", "getpgrp", "getppid", "getuid", "getpid", "getpgrp", "getppid", "getuid",
] ]
for name in NO_ARG_FUNCTIONS: with warnings.catch_warnings():
posix_func = getattr(posix, name, None) warnings.filterwarnings("ignore", "", DeprecationWarning)
if posix_func is not None: for name in NO_ARG_FUNCTIONS:
posix_func() posix_func = getattr(posix, name, None)
self.assertRaises(TypeError, posix_func, 1) if posix_func is not None:
posix_func()
self.assertRaises(TypeError, posix_func, 1)
if hasattr(posix, 'getresuid'): if hasattr(posix, 'getresuid'):
def test_getresuid(self): def test_getresuid(self):
...@@ -290,14 +292,18 @@ class PosixTester(unittest.TestCase): ...@@ -290,14 +292,18 @@ class PosixTester(unittest.TestCase):
def test_tempnam(self): def test_tempnam(self):
if hasattr(posix, 'tempnam'): if hasattr(posix, 'tempnam'):
self.assertTrue(posix.tempnam()) with warnings.catch_warnings():
self.assertTrue(posix.tempnam(os.curdir)) warnings.filterwarnings("ignore", "tempnam", DeprecationWarning)
self.assertTrue(posix.tempnam(os.curdir, 'blah')) self.assertTrue(posix.tempnam())
self.assertTrue(posix.tempnam(os.curdir))
self.assertTrue(posix.tempnam(os.curdir, 'blah'))
def test_tmpfile(self): def test_tmpfile(self):
if hasattr(posix, 'tmpfile'): if hasattr(posix, 'tmpfile'):
fp = posix.tmpfile() with warnings.catch_warnings():
fp.close() warnings.filterwarnings("ignore", "tmpfile", DeprecationWarning)
fp = posix.tmpfile()
fp.close()
def test_utime(self): def test_utime(self):
if hasattr(posix, 'utime'): if hasattr(posix, 'utime'):
......
...@@ -22,6 +22,9 @@ Core and Builtins ...@@ -22,6 +22,9 @@ Core and Builtins
Library Library
------- -------
- Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
DeprecationWarning.
- Subclasses of collections.OrderedDict now work correctly with __missing__. - Subclasses of collections.OrderedDict now work correctly with __missing__.
- Issue 10753 - Characters ';','=' and ',' in the PATH_INFO environment - Issue 10753 - Characters ';','=' and ',' in the PATH_INFO environment
......
...@@ -7295,6 +7295,10 @@ posix_tempnam(PyObject *self, PyObject *args) ...@@ -7295,6 +7295,10 @@ posix_tempnam(PyObject *self, PyObject *args)
"tempnam is a potential security risk to your program") < 0) "tempnam is a potential security risk to your program") < 0)
return NULL; return NULL;
if (PyErr_WarnPy3k("tempnam has been removed in 3.x; "
"use the tempfile module", 1) < 0)
return NULL;
#ifdef MS_WINDOWS #ifdef MS_WINDOWS
name = _tempnam(dir, pfx); name = _tempnam(dir, pfx);
#else #else
...@@ -7319,6 +7323,10 @@ posix_tmpfile(PyObject *self, PyObject *noargs) ...@@ -7319,6 +7323,10 @@ posix_tmpfile(PyObject *self, PyObject *noargs)
{ {
FILE *fp; FILE *fp;
if (PyErr_WarnPy3k("tmpfile has been removed in 3.x; "
"use the tempfile module", 1) < 0)
return NULL;
fp = tmpfile(); fp = tmpfile();
if (fp == NULL) if (fp == NULL)
return posix_error(); return posix_error();
...@@ -7342,6 +7350,10 @@ posix_tmpnam(PyObject *self, PyObject *noargs) ...@@ -7342,6 +7350,10 @@ posix_tmpnam(PyObject *self, PyObject *noargs)
"tmpnam is a potential security risk to your program") < 0) "tmpnam is a potential security risk to your program") < 0)
return NULL; return NULL;
if (PyErr_WarnPy3k("tmpnam has been removed in 3.x; "
"use the tempfile module", 1) < 0)
return NULL;
#ifdef USE_TMPNAM_R #ifdef USE_TMPNAM_R
name = tmpnam_r(buffer); name = tmpnam_r(buffer);
#else #else
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment