mirror of
https://github.com/python/cpython.git
synced 2025-07-24 03:35:53 +00:00
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.
This commit is contained in:
parent
9589ab1745
commit
52fbea1d87
4 changed files with 36 additions and 17 deletions
|
@ -2134,18 +2134,19 @@ Files and Directories
|
||||||
Availability: Unix, Windows.
|
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*
|
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
|
is ``None`` or not specified, then the file's access and modified times are
|
||||||
time. (The effect is similar to running the Unix program :program:`touch` on
|
set to the current time. (The effect is similar to running the Unix program
|
||||||
the path.) Otherwise, *times* must be a 2-tuple of numbers, of the form
|
:program:`touch` on the path.) Otherwise, *times* must be a 2-tuple of
|
||||||
``(atime, mtime)`` which is used to set the access and modified times,
|
numbers, of the form ``(atime, mtime)`` which is used to set the access and
|
||||||
respectively. Whether a directory can be given for *path* depends on whether
|
modified times, respectively. Whether a directory can be given for *path*
|
||||||
the operating system implements directories as files (for example, Windows
|
depends on whether the operating system implements directories as files
|
||||||
does not). Note that the exact times you set here may not be returned by a
|
(for example, Windows does not). Note that the exact times you set here may
|
||||||
subsequent :func:`~os.stat` call, depending on the resolution with which your
|
not be returned by a subsequent :func:`~os.stat` call, depending on the
|
||||||
operating system records access and modification times; see :func:`~os.stat`.
|
resolution with which your operating system records access and modification
|
||||||
|
times; see :func:`~os.stat`.
|
||||||
|
|
||||||
Availability: Unix, Windows.
|
Availability: Unix, Windows.
|
||||||
|
|
||||||
|
|
|
@ -270,6 +270,21 @@ class StatAttributeTests(unittest.TestCase):
|
||||||
st2 = os.stat(support.TESTFN)
|
st2 = os.stat(support.TESTFN)
|
||||||
self.assertEqual(st2.st_mtime, int(st.st_mtime-delta))
|
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
|
# Restrict test to Win32, since there is no guarantee other
|
||||||
# systems support centiseconds
|
# systems support centiseconds
|
||||||
if sys.platform == 'win32':
|
if sys.platform == 'win32':
|
||||||
|
|
|
@ -10,6 +10,9 @@ What's New in Python 3.3 Alpha 1?
|
||||||
Core and Builtins
|
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
|
- Issue #13350: Simplify some C code by replacing most usages of
|
||||||
PyUnicode_Format by PyUnicode_FromFormat.
|
PyUnicode_Format by PyUnicode_FromFormat.
|
||||||
|
|
||||||
|
|
|
@ -3543,7 +3543,7 @@ static PyObject *
|
||||||
posix_utime(PyObject *self, PyObject *args)
|
posix_utime(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
#ifdef MS_WINDOWS
|
#ifdef MS_WINDOWS
|
||||||
PyObject *arg;
|
PyObject *arg = NULL;
|
||||||
PyObject *obwpath;
|
PyObject *obwpath;
|
||||||
wchar_t *wpath = NULL;
|
wchar_t *wpath = NULL;
|
||||||
PyObject *oapath;
|
PyObject *oapath;
|
||||||
|
@ -3554,7 +3554,7 @@ posix_utime(PyObject *self, PyObject *args)
|
||||||
FILETIME atime, mtime;
|
FILETIME atime, mtime;
|
||||||
PyObject *result = NULL;
|
PyObject *result = NULL;
|
||||||
|
|
||||||
if (PyArg_ParseTuple(args, "UO|:utime", &obwpath, &arg)) {
|
if (PyArg_ParseTuple(args, "U|O:utime", &obwpath, &arg)) {
|
||||||
wpath = PyUnicode_AsUnicode(obwpath);
|
wpath = PyUnicode_AsUnicode(obwpath);
|
||||||
if (wpath == NULL)
|
if (wpath == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -3571,7 +3571,7 @@ posix_utime(PyObject *self, PyObject *args)
|
||||||
are also valid. */
|
are also valid. */
|
||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "O&O:utime",
|
if (!PyArg_ParseTuple(args, "O&|O:utime",
|
||||||
PyUnicode_FSConverter, &oapath, &arg))
|
PyUnicode_FSConverter, &oapath, &arg))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
@ -3589,7 +3589,7 @@ posix_utime(PyObject *self, PyObject *args)
|
||||||
Py_DECREF(oapath);
|
Py_DECREF(oapath);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (arg == Py_None) {
|
if (!arg || (arg == Py_None)) {
|
||||||
SYSTEMTIME now;
|
SYSTEMTIME now;
|
||||||
GetSystemTime(&now);
|
GetSystemTime(&now);
|
||||||
if (!SystemTimeToFileTime(&now, &mtime) ||
|
if (!SystemTimeToFileTime(&now, &mtime) ||
|
||||||
|
@ -3633,13 +3633,13 @@ done:
|
||||||
time_t atime, mtime;
|
time_t atime, mtime;
|
||||||
long ausec, musec;
|
long ausec, musec;
|
||||||
int res;
|
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))
|
PyUnicode_FSConverter, &opath, &arg))
|
||||||
return NULL;
|
return NULL;
|
||||||
path = PyBytes_AsString(opath);
|
path = PyBytes_AsString(opath);
|
||||||
if (arg == Py_None) {
|
if (!arg || (arg == Py_None)) {
|
||||||
/* optional time values not given */
|
/* optional time values not given */
|
||||||
Py_BEGIN_ALLOW_THREADS
|
Py_BEGIN_ALLOW_THREADS
|
||||||
res = utime(path, NULL);
|
res = utime(path, NULL);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue