[Patches] utime(path, NULL)

Barry A. Warsaw bwarsaw@python.org
Fri, 28 Apr 2000 20:20:20 -0400 (EDT)


On some OSes (Solaris and Linux at least), the second argument to
utime() can be NULL, which sets the atime and mtime of the file to the
current time.  Here is a patch that gives this functionality if second
argument to os.utime() is omitted.  E.g.

import os
os.utime(path)

==

#include <utime.h>
utime(path, NULL)

One minor bogosity: because of the way default arguments work for
longs, os.utime(path) is equivalent to os.utime(path, (-1, -1)).  Is
this a big deal?  Should other magic be used instead?

-Barry

Index: libos.tex
===================================================================
RCS file: /projects/cvsroot/python/dist/src/Doc/lib/libos.tex,v
retrieving revision 1.38
diff -c -r1.38 libos.tex
*** libos.tex	2000/04/03 20:13:53	1.38
--- libos.tex	2000/04/29 00:15:42
***************
*** 704,710 ****
  
  \begin{funcdesc}{utime}{path, (atime, mtime)}
  Set the access and modified time of the file to the given values.
! (The second argument is a tuple of two items.)
  Availability: Macintosh, \UNIX{}, Windows.
  \end{funcdesc}
  
--- 704,711 ----
  
  \begin{funcdesc}{utime}{path, (atime, mtime)}
  Set the access and modified time of the file to the given values.
! The second argument is a tuple of two items; if it is omitted, then
! these times are set to the current time.
  Availability: Macintosh, \UNIX{}, Windows.
  \end{funcdesc}
  
Index: posixmodule.c
===================================================================
RCS file: /projects/cvsroot/python/dist/src/Modules/posixmodule.c,v
retrieving revision 2.132
diff -c -r2.132 posixmodule.c
*** posixmodule.c	2000/04/26 20:34:28	2.132
--- posixmodule.c	2000/04/29 00:10:52
***************
*** 1258,1264 ****
  	PyObject *args;
  {
  	char *path;
! 	long atime, mtime;
  	int res;
  
  /* XXX should define struct utimbuf instead, above */
--- 1258,1264 ----
  	PyObject *args;
  {
  	char *path;
! 	long atime=-1, mtime=-1;
  	int res;
  
  /* XXX should define struct utimbuf instead, above */
***************
*** 1274,1286 ****
  #define UTIME_ARG buf
  #endif /* HAVE_UTIME_H */
  
! 	if (!PyArg_ParseTuple(args, "s(ll):utime", &path, &atime, &mtime))
  		return NULL;
! 	ATIME = atime;
! 	MTIME = mtime;
! 	Py_BEGIN_ALLOW_THREADS
! 	res = utime(path, UTIME_ARG);
! 	Py_END_ALLOW_THREADS
  	if (res < 0)
  		return posix_error_with_filename(path);
  	Py_INCREF(Py_None);
--- 1274,1294 ----
  #define UTIME_ARG buf
  #endif /* HAVE_UTIME_H */
  
! 	if (!PyArg_ParseTuple(args, "s|(ll):utime", &path, &atime, &mtime))
  		return NULL;
! 	if (atime == -1 && mtime == -1) {
! 		/* optional time values not given */
! 		Py_BEGIN_ALLOW_THREADS
! 		res = utime(path, NULL);
! 		Py_END_ALLOW_THREADS
! 	}
! 	else {
! 		ATIME = atime;
! 		MTIME = mtime;
! 		Py_BEGIN_ALLOW_THREADS
! 		res = utime(path, UTIME_ARG);
! 		Py_END_ALLOW_THREADS
! 	}
  	if (res < 0)
  		return posix_error_with_filename(path);
  	Py_INCREF(Py_None);