[Python-checkins] r87643 - in python/branches/release27-maint: Lib/test/test_os.py Lib/test/test_posix.py Misc/NEWS Modules/posixmodule.c

antoine.pitrou python-checkins at python.org
Sun Jan 2 21:04:52 CET 2011


Author: antoine.pitrou
Date: Sun Jan  2 21:04:52 2011
New Revision: 87643

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



Modified:
   python/branches/release27-maint/Lib/test/test_os.py
   python/branches/release27-maint/Lib/test/test_posix.py
   python/branches/release27-maint/Misc/NEWS
   python/branches/release27-maint/Modules/posixmodule.c

Modified: python/branches/release27-maint/Lib/test/test_os.py
==============================================================================
--- python/branches/release27-maint/Lib/test/test_os.py	(original)
+++ python/branches/release27-maint/Lib/test/test_os.py	Sun Jan  2 21:04:52 2011
@@ -80,16 +80,18 @@
     def test_tempnam(self):
         if not hasattr(os, "tempnam"):
             return
-        warnings.filterwarnings("ignore", "tempnam", RuntimeWarning,
-                                r"test_os$")
-        self.check_tempfile(os.tempnam())
-
-        name = os.tempnam(test_support.TESTFN)
-        self.check_tempfile(name)
-
-        name = os.tempnam(test_support.TESTFN, "pfx")
-        self.assertTrue(os.path.basename(name)[:3] == "pfx")
-        self.check_tempfile(name)
+        with warnings.catch_warnings():
+            warnings.filterwarnings("ignore", "tempnam", RuntimeWarning,
+                                    r"test_os$")
+            warnings.filterwarnings("ignore", "tempnam", DeprecationWarning)
+            self.check_tempfile(os.tempnam())
+
+            name = os.tempnam(test_support.TESTFN)
+            self.check_tempfile(name)
+
+            name = os.tempnam(test_support.TESTFN, "pfx")
+            self.assertTrue(os.path.basename(name)[:3] == "pfx")
+            self.check_tempfile(name)
 
     def test_tmpfile(self):
         if not hasattr(os, "tmpfile"):
@@ -108,63 +110,69 @@
         # 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 (13, 'Permission denied') in both
-                # cases.
+        with warnings.catch_warnings():
+            warnings.filterwarnings("ignore", "tmpfile", DeprecationWarning)
+
+            if sys.platform == 'win32':
+                name = '\\python_test_os_test_tmpfile.txt'
+                if os.path.exists(name):
+                    os.remove(name)
                 try:
-                    fp = os.tmpfile()
-                except OSError, second:
-                    self.assertEqual(first.args, second.args)
+                    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 (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:
-                    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)
-        s = fp.read()
-        fp.close()
-        self.assertTrue(s == "foobar")
+                    # 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)
+            s = fp.read()
+            fp.close()
+            self.assertTrue(s == "foobar")
 
     def test_tmpnam(self):
         if not hasattr(os, "tmpnam"):
             return
-        warnings.filterwarnings("ignore", "tmpnam", RuntimeWarning,
-                                r"test_os$")
-        name = os.tmpnam()
-        if sys.platform in ("win32",):
-            # The Windows tmpnam() seems useless.  From the MS docs:
-            #
-            #     The character string that tmpnam creates consists of
-            #     the path prefix, defined by the entry P_tmpdir in the
-            #     file STDIO.H, followed by a sequence consisting of the
-            #     digit characters '0' through '9'; the numerical value
-            #     of this string is in the range 1 - 65,535.  Changing the
-            #     definitions of L_tmpnam or P_tmpdir in STDIO.H does not
-            #     change the operation of tmpnam.
-            #
-            # The really bizarre part is that, at least under MSVC6,
-            # P_tmpdir is "\\".  That is, the path returned refers to
-            # the root of the current drive.  That's a terrible place to
-            # put temp files, and, depending on privileges, the user
-            # may not even be able to open a file in the root directory.
-            self.assertFalse(os.path.exists(name),
-                        "file already exists for temporary file")
-        else:
-            self.check_tempfile(name)
+        with warnings.catch_warnings():
+            warnings.filterwarnings("ignore", "tmpnam", RuntimeWarning,
+                                    r"test_os$")
+            warnings.filterwarnings("ignore", "tmpnam", DeprecationWarning)
+
+            name = os.tmpnam()
+            if sys.platform in ("win32",):
+                # The Windows tmpnam() seems useless.  From the MS docs:
+                #
+                #     The character string that tmpnam creates consists of
+                #     the path prefix, defined by the entry P_tmpdir in the
+                #     file STDIO.H, followed by a sequence consisting of the
+                #     digit characters '0' through '9'; the numerical value
+                #     of this string is in the range 1 - 65,535.  Changing the
+                #     definitions of L_tmpnam or P_tmpdir in STDIO.H does not
+                #     change the operation of tmpnam.
+                #
+                # The really bizarre part is that, at least under MSVC6,
+                # P_tmpdir is "\\".  That is, the path returned refers to
+                # the root of the current drive.  That's a terrible place to
+                # put temp files, and, depending on privileges, the user
+                # may not even be able to open a file in the root directory.
+                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.
 class StatAttributeTests(unittest.TestCase):

Modified: python/branches/release27-maint/Lib/test/test_posix.py
==============================================================================
--- python/branches/release27-maint/Lib/test/test_posix.py	(original)
+++ python/branches/release27-maint/Lib/test/test_posix.py	Sun Jan  2 21:04:52 2011
@@ -38,11 +38,13 @@
                              "getpid", "getpgrp", "getppid", "getuid",
                            ]
 
-        for name in NO_ARG_FUNCTIONS:
-            posix_func = getattr(posix, name, None)
-            if posix_func is not None:
-                posix_func()
-                self.assertRaises(TypeError, posix_func, 1)
+        with warnings.catch_warnings():
+            warnings.filterwarnings("ignore", "", DeprecationWarning)
+            for name in NO_ARG_FUNCTIONS:
+                posix_func = getattr(posix, name, None)
+                if posix_func is not None:
+                    posix_func()
+                    self.assertRaises(TypeError, posix_func, 1)
 
     if hasattr(posix, 'getresuid'):
         def test_getresuid(self):
@@ -290,14 +292,18 @@
 
     def test_tempnam(self):
         if hasattr(posix, 'tempnam'):
-            self.assertTrue(posix.tempnam())
-            self.assertTrue(posix.tempnam(os.curdir))
-            self.assertTrue(posix.tempnam(os.curdir, 'blah'))
+            with warnings.catch_warnings():
+                warnings.filterwarnings("ignore", "tempnam", DeprecationWarning)
+                self.assertTrue(posix.tempnam())
+                self.assertTrue(posix.tempnam(os.curdir))
+                self.assertTrue(posix.tempnam(os.curdir, 'blah'))
 
     def test_tmpfile(self):
         if hasattr(posix, 'tmpfile'):
-            fp = posix.tmpfile()
-            fp.close()
+            with warnings.catch_warnings():
+                warnings.filterwarnings("ignore", "tmpfile", DeprecationWarning)
+                fp = posix.tmpfile()
+                fp.close()
 
     def test_utime(self):
         if hasattr(posix, 'utime'):

Modified: python/branches/release27-maint/Misc/NEWS
==============================================================================
--- python/branches/release27-maint/Misc/NEWS	(original)
+++ python/branches/release27-maint/Misc/NEWS	Sun Jan  2 21:04:52 2011
@@ -22,6 +22,9 @@
 Library
 -------
 
+- Issue #4662: os.tempnam(), os.tmpfile() and os.tmpnam() now raise a py3k
+  DeprecationWarning.
+
 - Subclasses of collections.OrderedDict now work correctly with __missing__.
 
 - Issue 10753 - Characters ';','=' and ',' in the PATH_INFO environment

Modified: python/branches/release27-maint/Modules/posixmodule.c
==============================================================================
--- python/branches/release27-maint/Modules/posixmodule.c	(original)
+++ python/branches/release27-maint/Modules/posixmodule.c	Sun Jan  2 21:04:52 2011
@@ -7295,6 +7295,10 @@
                    "tempnam is a potential security risk to your program") < 0)
         return NULL;
 
+    if (PyErr_WarnPy3k("tempnam has been removed in 3.x; "
+                       "use the tempfile module", 1) < 0)
+        return NULL;
+
 #ifdef MS_WINDOWS
     name = _tempnam(dir, pfx);
 #else
@@ -7319,6 +7323,10 @@
 {
     FILE *fp;
 
+    if (PyErr_WarnPy3k("tmpfile has been removed in 3.x; "
+                       "use the tempfile module", 1) < 0)
+        return NULL;
+
     fp = tmpfile();
     if (fp == NULL)
     return posix_error();
@@ -7342,6 +7350,10 @@
                    "tmpnam is a potential security risk to your program") < 0)
         return NULL;
 
+    if (PyErr_WarnPy3k("tmpnam has been removed in 3.x; "
+                       "use the tempfile module", 1) < 0)
+        return NULL;
+
 #ifdef USE_TMPNAM_R
     name = tmpnam_r(buffer);
 #else


More information about the Python-checkins mailing list