|
45 | 45 |
|
46 | 46 | /* Forward declarations */ |
47 | 47 | static int floatsleep(double); |
48 | | -static double floattime(void); |
| 48 | +static PyObject* floattime(void); |
49 | 49 |
|
50 | 50 | static PyObject * |
51 | 51 | time_time(PyObject *self, PyObject *unused) |
52 | 52 | { |
53 | | - double secs; |
54 | | - secs = floattime(); |
55 | | - if (secs == 0.0) { |
56 | | - PyErr_SetFromErrno(PyExc_IOError); |
57 | | - return NULL; |
58 | | - } |
59 | | - return PyFloat_FromDouble(secs); |
| 53 | + return floattime(); |
60 | 54 | } |
61 | 55 |
|
62 | 56 | PyDoc_STRVAR(time_doc, |
@@ -768,11 +762,11 @@ the local timezone used by methods such as localtime, but this behaviour\n\ |
768 | 762 | should not be relied on."); |
769 | 763 | #endif /* HAVE_WORKING_TZSET */ |
770 | 764 |
|
771 | | -static PyObject * |
772 | | -time_steady(PyObject *self, PyObject *unused) |
| 765 | +static PyObject* |
| 766 | +steady_clock(int strict) |
773 | 767 | { |
774 | 768 | #if defined(MS_WINDOWS) && !defined(__BORLANDC__) |
775 | | - return win32_clock(1); |
| 769 | + return win32_clock(!strict); |
776 | 770 | #elif defined(__APPLE__) |
777 | 771 | uint64_t time = mach_absolute_time(); |
778 | 772 | double secs; |
@@ -806,14 +800,37 @@ time_steady(PyObject *self, PyObject *unused) |
806 | 800 | if (Py_ARRAY_LENGTH(clk_ids) <= clk_index) |
807 | 801 | clk_index = -1; |
808 | 802 | } |
809 | | - return time_time(self, NULL); |
| 803 | + if (strict) { |
| 804 | + PyErr_SetFromErrno(PyExc_OSError); |
| 805 | + return NULL; |
| 806 | + } |
| 807 | + return floattime(); |
810 | 808 | #else |
811 | | - return time_time(self, NULL); |
| 809 | + if (strict) { |
| 810 | + PyErr_SetString(PyExc_NotImplementedError, |
| 811 | + "no steady clock available on your platform"); |
| 812 | + return NULL; |
| 813 | + } |
| 814 | + return floattime(); |
812 | 815 | #endif |
813 | 816 | } |
814 | 817 |
|
| 818 | +static PyObject * |
| 819 | +time_steady(PyObject *self, PyObject *args, PyObject *kwargs) |
| 820 | +{ |
| 821 | + static char *kwlist[] = {"strict", NULL}; |
| 822 | + int strict = 0; |
| 823 | + |
| 824 | + if (!PyArg_ParseTupleAndKeywords( |
| 825 | + args, kwargs, "|i:steady", kwlist, |
| 826 | + &strict)) |
| 827 | + return NULL; |
| 828 | + |
| 829 | + return steady_clock(strict); |
| 830 | +} |
| 831 | + |
815 | 832 | PyDoc_STRVAR(steady_doc, |
816 | | -"steady() -> float\n\ |
| 833 | +"steady(strict=False) -> float\n\ |
817 | 834 | \n\ |
818 | 835 | Return the current time as a floating point number expressed in seconds.\n\ |
819 | 836 | This clock advances at a steady rate relative to real time and it may not\n\ |
@@ -949,7 +966,8 @@ static PyMethodDef time_methods[] = { |
949 | 966 | #ifdef HAVE_MKTIME |
950 | 967 | {"mktime", time_mktime, METH_O, mktime_doc}, |
951 | 968 | #endif |
952 | | - {"steady", time_steady, METH_NOARGS, steady_doc}, |
| 969 | + {"steady", (PyCFunction)time_steady, METH_VARARGS|METH_KEYWORDS, |
| 970 | + steady_doc}, |
953 | 971 | #ifdef HAVE_STRFTIME |
954 | 972 | {"strftime", time_strftime, METH_VARARGS, strftime_doc}, |
955 | 973 | #endif |
@@ -1041,12 +1059,18 @@ PyInit_time(void) |
1041 | 1059 | return m; |
1042 | 1060 | } |
1043 | 1061 |
|
1044 | | -static double |
| 1062 | +static PyObject* |
1045 | 1063 | floattime(void) |
1046 | 1064 | { |
1047 | 1065 | _PyTime_timeval t; |
| 1066 | + double secs; |
1048 | 1067 | _PyTime_gettimeofday(&t); |
1049 | | - return (double)t.tv_sec + t.tv_usec*0.000001; |
| 1068 | + secs = (double)t.tv_sec + t.tv_usec*0.000001; |
| 1069 | + if (secs == 0.0) { |
| 1070 | + PyErr_SetFromErrno(PyExc_OSError); |
| 1071 | + return NULL; |
| 1072 | + } |
| 1073 | + return PyFloat_FromDouble(secs); |
1050 | 1074 | } |
1051 | 1075 |
|
1052 | 1076 |
|
|
0 commit comments