Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -1064,9 +1064,14 @@ def test_large_time(self):
if self.get_file_system(self.dirname) != "NTFS":
self.skipTest("requires NTFS")

large = 5000000000 # some day in 2128
os.utime(self.fname, (large, large))
self.assertEqual(os.stat(self.fname).st_mtime, large)
times = (
5000000000, # some day in 2128
# boundaries of the fast path cutoff in posixmodule.c:fill_time
-9223372037, -9223372036, 9223372035, 9223372036,
)
for large in times:
os.utime(self.fname, (large, large))
self.assertEqual(os.stat(self.fname).st_mtime, large)

def test_utime_invalid_arguments(self):
# seconds and nanoseconds parameters are mutually exclusive
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Speed up :func:`os.stat` for files with reasonable timestamps. Contributed
by Jeffrey Bosboom.
89 changes: 51 additions & 38 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2588,55 +2588,68 @@ static int
fill_time(PyObject *module, PyObject *v, int s_index, int f_index, int ns_index, time_t sec, unsigned long nsec)
{
assert(!PyErr_Occurred());

int res = -1;
PyObject *s_in_ns = NULL;
PyObject *ns_total = NULL;
PyObject *float_s = NULL;

PyObject *s = _PyLong_FromTime_t(sec);
PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec);
if (!(s && ns_fractional)) {
goto exit;
}

s_in_ns = PyNumber_Multiply(s, get_posix_state(module)->billion);
if (!s_in_ns) {
goto exit;
}

ns_total = PyNumber_Add(s_in_ns, ns_fractional);
if (!ns_total)
goto exit;

float_s = PyFloat_FromDouble(sec + 1e-9*nsec);
if (!float_s) {
goto exit;
}
#define S_TO_NS (1000000000LL)
assert(nsec < S_TO_NS);

if (s_index >= 0) {
PyObject *s = _PyLong_FromTime_t(sec);
if (!s) {
return -1;
}
PyStructSequence_SET_ITEM(v, s_index, s);
s = NULL;
}

if (f_index >= 0) {
PyObject *float_s = PyFloat_FromDouble(sec + 1e-9 * nsec);
if (!float_s) {
return -1;
}
PyStructSequence_SET_ITEM(v, f_index, float_s);
float_s = NULL;
}

int res = -1;
if (ns_index >= 0) {
PyStructSequence_SET_ITEM(v, ns_index, ns_total);
ns_total = NULL;
}
/* 1677-09-21 00:12:44 to 2262-04-11 23:47:15 UTC inclusive */
if ((LLONG_MIN / S_TO_NS) <= sec && sec <= (LLONG_MAX / S_TO_NS - 1)) {
PyObject *ns_total = PyLong_FromLongLong(sec * S_TO_NS + nsec);
if (!ns_total) {
return -1;
}
PyStructSequence_SET_ITEM(v, ns_index, ns_total);
assert(!PyErr_Occurred());
res = 0;
}
else {
PyObject *s_in_ns = NULL;
PyObject *ns_total = NULL;
PyObject *s = _PyLong_FromTime_t(sec);
PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec);
if (!(s && ns_fractional)) {
goto exit;
}

assert(!PyErr_Occurred());
res = 0;
s_in_ns = PyNumber_Multiply(s, get_posix_state(module)->billion);
if (!s_in_ns) {
goto exit;
}

ns_total = PyNumber_Add(s_in_ns, ns_fractional);
if (!ns_total) {
goto exit;
}
PyStructSequence_SET_ITEM(v, ns_index, ns_total);
assert(!PyErr_Occurred());
res = 0;

exit:
Py_XDECREF(s);
Py_XDECREF(ns_fractional);
Py_XDECREF(s_in_ns);
}
}

exit:
Py_XDECREF(s);
Py_XDECREF(ns_fractional);
Py_XDECREF(s_in_ns);
Py_XDECREF(ns_total);
Py_XDECREF(float_s);
return res;
#undef S_TO_NS
}

#ifdef MS_WINDOWS
Expand Down
Loading