Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 52fbea1

Browse files
committed
Fix #13327. Remove the need for an explicit None as the second argument to
os.utime in order to update to the current time. The second argument is now optional.
1 parent 9589ab1 commit 52fbea1

4 files changed

Lines changed: 36 additions & 17 deletions

File tree

Doc/library/os.rst

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2134,18 +2134,19 @@ Files and Directories
21342134
Availability: Unix, Windows.
21352135

21362136

2137-
.. function:: utime(path, times)
2137+
.. function:: utime(path[, times])
21382138

21392139
Set the access and modified times of the file specified by *path*. If *times*
2140-
is ``None``, then the file's access and modified times are set to the current
2141-
time. (The effect is similar to running the Unix program :program:`touch` on
2142-
the path.) Otherwise, *times* must be a 2-tuple of numbers, of the form
2143-
``(atime, mtime)`` which is used to set the access and modified times,
2144-
respectively. Whether a directory can be given for *path* depends on whether
2145-
the operating system implements directories as files (for example, Windows
2146-
does not). Note that the exact times you set here may not be returned by a
2147-
subsequent :func:`~os.stat` call, depending on the resolution with which your
2148-
operating system records access and modification times; see :func:`~os.stat`.
2140+
is ``None`` or not specified, then the file's access and modified times are
2141+
set to the current time. (The effect is similar to running the Unix program
2142+
:program:`touch` on the path.) Otherwise, *times* must be a 2-tuple of
2143+
numbers, of the form ``(atime, mtime)`` which is used to set the access and
2144+
modified times, respectively. Whether a directory can be given for *path*
2145+
depends on whether the operating system implements directories as files
2146+
(for example, Windows does not). Note that the exact times you set here may
2147+
not be returned by a subsequent :func:`~os.stat` call, depending on the
2148+
resolution with which your operating system records access and modification
2149+
times; see :func:`~os.stat`.
21492150

21502151
Availability: Unix, Windows.
21512152

Lib/test/test_os.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,21 @@ def test_utime_dir(self):
270270
st2 = os.stat(support.TESTFN)
271271
self.assertEqual(st2.st_mtime, int(st.st_mtime-delta))
272272

273+
def test_utime_noargs(self):
274+
# (insert issue#) removed the requirement to pass None as the
275+
# second argument. Check that the previous methods of passing
276+
# a time tuple or None work in addition to no argument.
277+
st = os.stat(support.TESTFN)
278+
# Doesn't set anything new, but sets the time tuple way
279+
os.utime(support.TESTFN, (st.st_atime, st.st_mtime))
280+
# Set to the current time in the old explicit way.
281+
os.utime(support.TESTFN, None)
282+
st1 = os.stat(support.TESTFN)
283+
# Set to the current time in the new way
284+
os.utime(support.TESTFN)
285+
st2 = os.stat(support.TESTFN)
286+
self.assertAlmostEqual(st1.st_mtime, st2.st_mtime, delta=10)
287+
273288
# Restrict test to Win32, since there is no guarantee other
274289
# systems support centiseconds
275290
if sys.platform == 'win32':

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.3 Alpha 1?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #13327: Remove the need for an explicit None as the second argument
14+
to os.utime in order to update to the current time.
15+
1316
- Issue #13350: Simplify some C code by replacing most usages of
1417
PyUnicode_Format by PyUnicode_FromFormat.
1518

Modules/posixmodule.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3543,7 +3543,7 @@ static PyObject *
35433543
posix_utime(PyObject *self, PyObject *args)
35443544
{
35453545
#ifdef MS_WINDOWS
3546-
PyObject *arg;
3546+
PyObject *arg = NULL;
35473547
PyObject *obwpath;
35483548
wchar_t *wpath = NULL;
35493549
PyObject *oapath;
@@ -3554,7 +3554,7 @@ posix_utime(PyObject *self, PyObject *args)
35543554
FILETIME atime, mtime;
35553555
PyObject *result = NULL;
35563556

3557-
if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
3557+
if (PyArg_ParseTuple(args, "U|O:utime", &obwpath, &arg)) {
35583558
wpath = PyUnicode_AsUnicode(obwpath);
35593559
if (wpath == NULL)
35603560
return NULL;
@@ -3571,7 +3571,7 @@ posix_utime(PyObject *self, PyObject *args)
35713571
are also valid. */
35723572
PyErr_Clear();
35733573

3574-
if (!PyArg_ParseTuple(args, "O&O:utime",
3574+
if (!PyArg_ParseTuple(args, "O&|O:utime",
35753575
PyUnicode_FSConverter, &oapath, &arg))
35763576
return NULL;
35773577

@@ -3589,7 +3589,7 @@ posix_utime(PyObject *self, PyObject *args)
35893589
Py_DECREF(oapath);
35903590
}
35913591

3592-
if (arg == Py_None) {
3592+
if (!arg || (arg == Py_None)) {
35933593
SYSTEMTIME now;
35943594
GetSystemTime(&now);
35953595
if (!SystemTimeToFileTime(&now, &mtime) ||
@@ -3633,13 +3633,13 @@ posix_utime(PyObject *self, PyObject *args)
36333633
time_t atime, mtime;
36343634
long ausec, musec;
36353635
int res;
3636-
PyObject* arg;
3636+
PyObject* arg = NULL;
36373637

3638-
if (!PyArg_ParseTuple(args, "O&O:utime",
3638+
if (!PyArg_ParseTuple(args, "O&|O:utime",
36393639
PyUnicode_FSConverter, &opath, &arg))
36403640
return NULL;
36413641
path = PyBytes_AsString(opath);
3642-
if (arg == Py_None) {
3642+
if (!arg || (arg == Py_None)) {
36433643
/* optional time values not given */
36443644
Py_BEGIN_ALLOW_THREADS
36453645
res = utime(path, NULL);

0 commit comments

Comments
 (0)