]> granicus.if.org Git - python/commitdiff
Fix #13327. Remove the need for an explicit None as the second argument to
authorBrian Curtin <brian@python.org>
Sun, 6 Nov 2011 19:41:17 +0000 (13:41 -0600)
committerBrian Curtin <brian@python.org>
Sun, 6 Nov 2011 19:41:17 +0000 (13:41 -0600)
os.utime in order to update to the current time. The second argument is now
optional.

Doc/library/os.rst
Lib/test/test_os.py
Misc/NEWS
Modules/posixmodule.c

index fb794d33b7116b5f389fc1cdeffec1e68e35961e..76ad7e0a9a77a6dd4da3e92c9d8824d1121add10 100644 (file)
@@ -2134,18 +2134,19 @@ Files and Directories
    Availability: Unix, Windows.
 
 
-.. function:: utime(path, times)
+.. function:: utime(path[, times])
 
    Set the access and modified times of the file specified by *path*. If *times*
-   is ``None``, then the file's access and modified times are set to the current
-   time. (The effect is similar to running the Unix program :program:`touch` on
-   the path.)  Otherwise, *times* must be a 2-tuple of numbers, of the form
-   ``(atime, mtime)`` which is used to set the access and modified times,
-   respectively. Whether a directory can be given for *path* depends on whether
-   the operating system implements directories as files (for example, Windows
-   does not).  Note that the exact times you set here may not be returned by a
-   subsequent :func:`~os.stat` call, depending on the resolution with which your
-   operating system records access and modification times; see :func:`~os.stat`.
+   is ``None`` or not specified, then the file's access and modified times are
+   set to the current time. (The effect is similar to running the Unix program
+   :program:`touch` on the path.)  Otherwise, *times* must be a 2-tuple of
+   numbers, of the form ``(atime, mtime)`` which is used to set the access and
+   modified times, respectively. Whether a directory can be given for *path*
+   depends on whether the operating system implements directories as files
+   (for example, Windows does not).  Note that the exact times you set here may
+   not be returned by a subsequent :func:`~os.stat` call, depending on the
+   resolution with which your operating system records access and modification
+   times; see :func:`~os.stat`.
 
    Availability: Unix, Windows.
 
index 878162f8adb4f375f3d7810bb5948a990deb9a50..b74d051a6b3dccd0177a8e8ddbc3a09ee7e5ff23 100644 (file)
@@ -270,6 +270,21 @@ class StatAttributeTests(unittest.TestCase):
         st2 = os.stat(support.TESTFN)
         self.assertEqual(st2.st_mtime, int(st.st_mtime-delta))
 
+    def test_utime_noargs(self):
+        # (insert issue#) removed the requirement to pass None as the
+        # second argument. Check that the previous methods of passing
+        # a time tuple or None work in addition to no argument.
+        st = os.stat(support.TESTFN)
+        # Doesn't set anything new, but sets the time tuple way
+        os.utime(support.TESTFN, (st.st_atime, st.st_mtime))
+        # Set to the current time in the old explicit way.
+        os.utime(support.TESTFN, None)
+        st1 = os.stat(support.TESTFN)
+        # Set to the current time in the new way
+        os.utime(support.TESTFN)
+        st2 = os.stat(support.TESTFN)
+        self.assertAlmostEqual(st1.st_mtime, st2.st_mtime, delta=10)
+
     # Restrict test to Win32, since there is no guarantee other
     # systems support centiseconds
     if sys.platform == 'win32':
index 7346348867920fc02a4f2795537f6a9c604c0ede..51f68f4034fced5dacb39ea37d6fd0033b033253 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.3 Alpha 1?
 Core and Builtins
 -----------------
 
+- Issue #13327: Remove the need for an explicit None as the second argument
+  to os.utime in order to update to the current time.
+
 - Issue #13350: Simplify some C code by replacing most usages of
   PyUnicode_Format by PyUnicode_FromFormat.
 
index f7baad4aef30eb736977d002412cb8e8aa1238db..2e33022af8e46f19bcd3125dbe67dfcdade6db00 100644 (file)
@@ -3543,7 +3543,7 @@ static PyObject *
 posix_utime(PyObject *self, PyObject *args)
 {
 #ifdef MS_WINDOWS
-    PyObject *arg;
+    PyObject *arg = NULL;
     PyObject *obwpath;
     wchar_t *wpath = NULL;
     PyObject *oapath;
@@ -3554,7 +3554,7 @@ posix_utime(PyObject *self, PyObject *args)
     FILETIME atime, mtime;
     PyObject *result = NULL;
 
-    if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
+    if (PyArg_ParseTuple(args, "U|O:utime", &obwpath, &arg)) {
         wpath = PyUnicode_AsUnicode(obwpath);
         if (wpath == NULL)
             return NULL;
@@ -3571,7 +3571,7 @@ posix_utime(PyObject *self, PyObject *args)
            are also valid. */
         PyErr_Clear();
 
-        if (!PyArg_ParseTuple(args, "O&O:utime",
+        if (!PyArg_ParseTuple(args, "O&|O:utime",
                         PyUnicode_FSConverter, &oapath, &arg))
             return NULL;
 
@@ -3589,7 +3589,7 @@ posix_utime(PyObject *self, PyObject *args)
         Py_DECREF(oapath);
     }
 
-    if (arg == Py_None) {
+    if (!arg || (arg == Py_None)) {
         SYSTEMTIME now;
         GetSystemTime(&now);
         if (!SystemTimeToFileTime(&now, &mtime) ||
@@ -3633,13 +3633,13 @@ done:
     time_t atime, mtime;
     long ausec, musec;
     int res;
-    PyObject* arg;
+    PyObject* arg = NULL;
 
-    if (!PyArg_ParseTuple(args, "O&O:utime",
+    if (!PyArg_ParseTuple(args, "O&|O:utime",
                           PyUnicode_FSConverter, &opath, &arg))
         return NULL;
     path = PyBytes_AsString(opath);
-    if (arg == Py_None) {
+    if (!arg || (arg == Py_None)) {
         /* optional time values not given */
         Py_BEGIN_ALLOW_THREADS
         res = utime(path, NULL);