Fix #13327. utimensat now has the atime and mtime arguments set as optional,

defaulting to None like the other utimes family members. It now accepts
keyword arguments because, unlike other other functions in the family,
it has a `flags` value at the end of the argument list (which
retains its 0 default).
This commit is contained in:
Brian Curtin 2011-11-07 16:09:20 -06:00
parent 7ef53ef916
commit 569b494320
3 changed files with 21 additions and 11 deletions

View File

@ -1285,17 +1285,17 @@ as internal buffering of data.
.. versionadded:: 3.3 .. versionadded:: 3.3
.. function:: utimensat(dirfd, path, (atime_sec, atime_nsec), (mtime_sec, mtime_nsec), flags) .. function:: utimensat(dirfd, path[, atime=(atime_sec, atime_nsec), mtime=(mtime_sec, mtime_nsec), flags=0])
utimensat(dirfd, path, None, None, flags)
Updates the timestamps of a file with nanosecond precision. Updates the timestamps of a file with nanosecond precision.
The second form sets *atime* and *mtime* to the current time. The *atime* and *mtime* tuples default to ``None``, which sets those
values to the current time.
If *atime_nsec* or *mtime_nsec* is specified as :data:`UTIME_NOW`, the corresponding If *atime_nsec* or *mtime_nsec* is specified as :data:`UTIME_NOW`, the corresponding
timestamp is updated to the current time. timestamp is updated to the current time.
If *atime_nsec* or *mtime_nsec* is specified as :data:`UTIME_OMIT`, the corresponding If *atime_nsec* or *mtime_nsec* is specified as :data:`UTIME_OMIT`, the corresponding
timestamp is not updated. timestamp is not updated.
If *path* is relative, it is taken as relative to *dirfd*. If *path* is relative, it is taken as relative to *dirfd*.
*flags* is optional and may be 0 or :data:`AT_SYMLINK_NOFOLLOW`. *flags* is optional and may be 0 (the default) or :data:`AT_SYMLINK_NOFOLLOW`.
If *path* is relative and *dirfd* is the special value :data:`AT_FDCWD`, then *path* If *path* is relative and *dirfd* is the special value :data:`AT_FDCWD`, then *path*
is interpreted relative to the current working directory. is interpreted relative to the current working directory.

View File

@ -815,11 +815,16 @@ class PosixTester(unittest.TestCase):
try: try:
now = time.time() now = time.time()
posix.utimensat(f, support.TESTFN, None, None) posix.utimensat(f, support.TESTFN, None, None)
posix.utimensat(f, support.TESTFN)
posix.utimensat(f, support.TESTFN, flags=os.AT_SYMLINK_NOFOLLOW)
self.assertRaises(TypeError, posix.utimensat, f, support.TESTFN, (None, None), (None, None)) self.assertRaises(TypeError, posix.utimensat, f, support.TESTFN, (None, None), (None, None))
self.assertRaises(TypeError, posix.utimensat, f, support.TESTFN, (now, 0), None) self.assertRaises(TypeError, posix.utimensat, f, support.TESTFN, (now, 0), None)
self.assertRaises(TypeError, posix.utimensat, f, support.TESTFN, None, (now, 0)) self.assertRaises(TypeError, posix.utimensat, f, support.TESTFN, None, (now, 0))
posix.utimensat(f, support.TESTFN, (int(now), int((now - int(now)) * 1e9)), posix.utimensat(f, support.TESTFN, (int(now), int((now - int(now)) * 1e9)),
(int(now), int((now - int(now)) * 1e9))) (int(now), int((now - int(now)) * 1e9)))
posix.utimensat(dirfd=f, path=support.TESTFN,
atime=(int(now), int((now - int(now)) * 1e9)),
mtime=(int(now), int((now - int(now)) * 1e9)))
finally: finally:
posix.close(f) posix.close(f)

View File

@ -10019,12 +10019,13 @@ posix_unlinkat(PyObject *self, PyObject *args)
#ifdef HAVE_UTIMENSAT #ifdef HAVE_UTIMENSAT
PyDoc_STRVAR(posix_utimensat__doc__, PyDoc_STRVAR(posix_utimensat__doc__,
"utimensat(dirfd, path, (atime_sec, atime_nsec),\n\ "utimensat(dirfd, path[, atime=(atime_sec, atime_nsec),\n\
(mtime_sec, mtime_nsec), flags)\n\ mtime=(mtime_sec, mtime_nsec), flags=0])\n\
utimensat(dirfd, path, None, None, flags)\n\n\ utimensat(dirfd, path, None, None, flags)\n\n\
Updates the timestamps of a file with nanosecond precision. If path is\n\ Updates the timestamps of a file with nanosecond precision. If path is\n\
relative, it is taken as relative to dirfd.\n\ relative, it is taken as relative to dirfd.\n\
The second form sets atime and mtime to the current time.\n\ If atime and mtime are both None, which is the default, set atime and\n\
mtime to the current time.\n\
flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\ flags is optional and may be 0 or AT_SYMLINK_NOFOLLOW.\n\
If path is relative and dirfd is the special value AT_FDCWD, then path\n\ If path is relative and dirfd is the special value AT_FDCWD, then path\n\
is interpreted relative to the current working directory.\n\ is interpreted relative to the current working directory.\n\
@ -10033,16 +10034,19 @@ current time.\n\
If *_nsec is specified as UTIME_OMIT, the timestamp is not updated."); If *_nsec is specified as UTIME_OMIT, the timestamp is not updated.");
static PyObject * static PyObject *
posix_utimensat(PyObject *self, PyObject *args) posix_utimensat(PyObject *self, PyObject *args, PyObject *kwargs)
{ {
PyObject *opath; PyObject *opath;
char *path; char *path;
int res, dirfd, flags = 0; int res, dirfd, flags = 0;
PyObject *atime, *mtime; PyObject *atime = Py_None;
PyObject *mtime = Py_None;
static char *kwlist[] = {"dirfd", "path", "atime", "mtime", "flags", NULL};
struct timespec buf[2]; struct timespec buf[2];
if (!PyArg_ParseTuple(args, "iO&OO|i:utimensat", if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iO&|OOi:utimensat", kwlist,
&dirfd, PyUnicode_FSConverter, &opath, &atime, &mtime, &flags)) &dirfd, PyUnicode_FSConverter, &opath, &atime, &mtime, &flags))
return NULL; return NULL;
path = PyBytes_AsString(opath); path = PyBytes_AsString(opath);
@ -10939,7 +10943,8 @@ static PyMethodDef posix_methods[] = {
{"unlinkat", posix_unlinkat, METH_VARARGS, posix_unlinkat__doc__}, {"unlinkat", posix_unlinkat, METH_VARARGS, posix_unlinkat__doc__},
#endif #endif
#ifdef HAVE_UTIMENSAT #ifdef HAVE_UTIMENSAT
{"utimensat", posix_utimensat, METH_VARARGS, posix_utimensat__doc__}, {"utimensat", posix_utimensat, METH_VARARGS | METH_KEYWORDS,
posix_utimensat__doc__},
#endif #endif
#ifdef HAVE_MKFIFOAT #ifdef HAVE_MKFIFOAT
{"mkfifoat", posix_mkfifoat, METH_VARARGS, posix_mkfifoat__doc__}, {"mkfifoat", posix_mkfifoat, METH_VARARGS, posix_mkfifoat__doc__},