[Python-checkins] python/dist/src/Modules posixmodule.c,2.255,2.256
loewis@users.sourceforge.net
loewis@users.sourceforge.net
2002年9月10日 02:16:15 -0700
Update of /cvsroot/python/python/dist/src/Modules
In directory usw-pr-cvs1:/tmp/cvs-serv20359/Modules
Modified Files:
posixmodule.c
Log Message:
Use utimes(2) where available to support microsecond timestamps.
Index: posixmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/posixmodule.c,v
retrieving revision 2.255
retrieving revision 2.256
diff -C2 -d -r2.255 -r2.256
*** posixmodule.c 9 Sep 2002 16:17:47 -0000 2.255
--- posixmodule.c 10 Sep 2002 09:16:13 -0000 2.256
***************
*** 1401,1404 ****
--- 1401,1429 ----
#endif /* HAVE_UNAME */
+ static int
+ extract_time(PyObject *t, long* sec, long* usec)
+ {
+ long intval;
+ if (PyFloat_Check(t)) {
+ double tval = PyFloat_AsDouble(t);
+ PyObject *intobj = t->ob_type->tp_as_number->nb_int(t);
+ if (!intobj)
+ return -1;
+ intval = PyInt_AsLong(intobj);
+ Py_DECREF(intobj);
+ *sec = intval;
+ *usec = (tval - intval) * 1e6;
+ if (*usec < 0)
+ /* If rounding gave us a negative number,
+ truncate. */
+ *usec = 0;
+ return 0;
+ }
+ intval = PyInt_AsLong(t);
+ if (intval == -1 && PyErr_Occurred())
+ return -1;
+ *sec = intval;
+ *usec = 0;
+ }
PyDoc_STRVAR(posix_utime__doc__,
***************
*** 1412,1431 ****
{
char *path;
! long atime, mtime;
int res;
PyObject* arg;
/* XXX should define struct utimbuf instead, above */
- #ifdef HAVE_UTIME_H
struct utimbuf buf;
#define ATIME buf.actime
#define MTIME buf.modtime
#define UTIME_ARG &buf
! #else /* HAVE_UTIME_H */
time_t buf[2];
#define ATIME buf[0]
#define MTIME buf[1]
#define UTIME_ARG buf
! #endif /* HAVE_UTIME_H */
if (!PyArg_ParseTuple(args, "sO:utime", &path, &arg))
--- 1437,1460 ----
{
char *path;
! long atime, mtime, ausec, musec;
int res;
PyObject* arg;
+ #if defined(HAVE_UTIMES)
+ struct timeval buf[2];
+ #define ATIME buf[0].tv_sec
+ #define MTIME buf[1].tv_sec
+ #elif defined(HAVE_UTIME_H)
/* XXX should define struct utimbuf instead, above */
struct utimbuf buf;
#define ATIME buf.actime
#define MTIME buf.modtime
#define UTIME_ARG &buf
! #else /* HAVE_UTIMES */
time_t buf[2];
#define ATIME buf[0]
#define MTIME buf[1]
#define UTIME_ARG buf
! #endif /* HAVE_UTIMES */
if (!PyArg_ParseTuple(args, "sO:utime", &path, &arg))
***************
*** 1437,1441 ****
Py_END_ALLOW_THREADS
}
! else if (!PyArg_Parse(arg, "(ll)", &atime, &mtime)) {
PyErr_SetString(PyExc_TypeError,
"utime() arg 2 must be a tuple (atime, mtime)");
--- 1466,1470 ----
Py_END_ALLOW_THREADS
}
! else if (!PyTuple_Check(arg) || PyTuple_Size(arg) != 2) {
PyErr_SetString(PyExc_TypeError,
"utime() arg 2 must be a tuple (atime, mtime)");
***************
*** 1443,1451 ****
--- 1472,1494 ----
}
else {
+ if (extract_time(PyTuple_GET_ITEM(arg, 0),
+ &atime, &ausec) == -1)
+ return NULL;
+ if (extract_time(PyTuple_GET_ITEM(arg, 1),
+ &mtime, &musec) == -1)
+ return NULL;
ATIME = atime;
MTIME = mtime;
+ #ifdef HAVE_UTIMES
+ buf[0].tv_usec = ausec;
+ buf[1].tv_usec = musec;
+ Py_BEGIN_ALLOW_THREADS
+ res = utimes(path, buf);
+ Py_END_ALLOW_THREADS
+ #else
Py_BEGIN_ALLOW_THREADS
res = utime(path, UTIME_ARG);
Py_END_ALLOW_THREADS
+ #endif
}
if (res < 0)