[Python-checkins] cpython: Issue #22117: Cleanup pytime.c/.h

victor.stinner python-checkins at python.org
Mon Mar 30 00:32:00 CEST 2015


https://hg.python.org/cpython/rev/b16fc95b66e4
changeset:   95257:b16fc95b66e4
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Mon Mar 30 00:25:38 2015 +0200
summary:
  Issue #22117: Cleanup pytime.c/.h

files:
  Include/pytime.h |  76 ++++++++++++++++++------------------
  Python/pytime.c  |  12 ++---
  2 files changed, 43 insertions(+), 45 deletions(-)


diff --git a/Include/pytime.h b/Include/pytime.h
--- a/Include/pytime.h
+++ b/Include/pytime.h
@@ -13,13 +13,16 @@
 extern "C" {
 #endif
 
-/* Structure used by time.get_clock_info() */
-typedef struct {
-    const char *implementation;
-    int monotonic;
-    int adjustable;
-    double resolution;
-} _Py_clock_info_t;
+#ifdef PY_INT64_T
+/* _PyTime_t: Python timestamp with subsecond precision. It can be used to
+   store a duration, and so indirectly a date (related to another date, like
+   UNIX epoch). */
+typedef PY_INT64_T _PyTime_t;
+#define _PyTime_MIN PY_LLONG_MIN
+#define _PyTime_MAX PY_LLONG_MAX
+#else
+#  error "_PyTime_t need signed 64-bit integer type"
+#endif
 
 typedef enum {
     /* Round towards zero. */
@@ -32,12 +35,6 @@
     _PyTime_ROUND_FLOOR
 } _PyTime_round_t;
 
-/* Convert a number of seconds, int or float, to time_t. */
-PyAPI_FUNC(int) _PyTime_ObjectToTime_t(
-    PyObject *obj,
-    time_t *sec,
-    _PyTime_round_t);
-
 /* Convert a time_t to a PyLong. */
 PyAPI_FUNC(PyObject *) _PyLong_FromTime_t(
     time_t sec);
@@ -46,6 +43,12 @@
 PyAPI_FUNC(time_t) _PyLong_AsTime_t(
     PyObject *obj);
 
+/* Convert a number of seconds, int or float, to time_t. */
+PyAPI_FUNC(int) _PyTime_ObjectToTime_t(
+    PyObject *obj,
+    time_t *sec,
+    _PyTime_round_t);
+
 /* Convert a number of seconds, int or float, to a timeval structure.
    usec is in the range [0; 999999] and rounded towards zero.
    For example, -1.2 is converted to (-2, 800000). */
@@ -64,22 +67,6 @@
     long *nsec,
     _PyTime_round_t);
 
-/* Initialize time.
-   Return 0 on success, raise an exception and return -1 on error. */
-PyAPI_FUNC(int) _PyTime_Init(void);
-
-/****************** NEW _PyTime_t API **********************/
-
-#ifdef PY_INT64_T
-/* _PyTime_t: Python timestamp with subsecond precision. It can be used to
-   store a duration, and so indirectly a date (related to another date, like
-   UNIX epoch). */
-typedef PY_INT64_T _PyTime_t;
-#define _PyTime_MIN PY_LLONG_MIN
-#define _PyTime_MAX PY_LLONG_MAX
-#else
-#  error "_PyTime_t need signed 64-bit integer type"
-#endif
 
 /* Create a timestamp from a number of nanoseconds (C long). */
 PyAPI_FUNC(_PyTime_t) _PyTime_FromNanoseconds(PY_LONG_LONG ns);
@@ -125,6 +112,24 @@
    works. */
 PyAPI_FUNC(_PyTime_t) _PyTime_GetSystemClock(void);
 
+/* Get the time of a monotonic clock, i.e. a clock that cannot go backwards.
+   The clock is not affected by system clock updates. The reference point of
+   the returned value is undefined, so that only the difference between the
+   results of consecutive calls is valid.
+
+   The function cannot fail. _PyTime_Init() ensures that a monotonic clock
+   is available and works. */
+PyAPI_FUNC(_PyTime_t) _PyTime_GetMonotonicClock(void);
+
+
+/* Structure used by time.get_clock_info() */
+typedef struct {
+    const char *implementation;
+    int monotonic;
+    int adjustable;
+    double resolution;
+} _Py_clock_info_t;
+
 /* Get the current time from the system clock.
  * Fill clock information if info is not NULL.
  * Raise an exception and return -1 on error, return 0 on success.
@@ -138,15 +143,6 @@
    the returned value is undefined, so that only the difference between the
    results of consecutive calls is valid.
 
-   The function cannot fail. _PyTime_Init() ensures that a monotonic clock
-   is available and works. */
-PyAPI_FUNC(_PyTime_t) _PyTime_GetMonotonicClock(void);
-
-/* Get the time of a monotonic clock, i.e. a clock that cannot go backwards.
-   The clock is not affected by system clock updates. The reference point of
-   the returned value is undefined, so that only the difference between the
-   results of consecutive calls is valid.
-
    Fill info (if set) with information of the function used to get the time.
 
    Return 0 on success, raise an exception and return -1 on error. */
@@ -155,6 +151,10 @@
     _Py_clock_info_t *info);
 
 
+/* Initialize time.
+   Return 0 on success, raise an exception and return -1 on error. */
+PyAPI_FUNC(int) _PyTime_Init(void);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/Python/pytime.c b/Python/pytime.c
--- a/Python/pytime.c
+++ b/Python/pytime.c
@@ -151,8 +151,6 @@
     return _PyTime_ObjectToDenominator(obj, sec, usec, 1e6, round);
 }
 
-/****************** NEW _PyTime_t API **********************/
-
 static void
 _PyTime_overflow(void)
 {
@@ -161,7 +159,7 @@
 }
 
 int
-_PyTime_RoundTowardsInfinity(int is_neg, _PyTime_round_t round)
+_PyTime_RoundTowardsPosInf(int is_neg, _PyTime_round_t round)
 {
     if (round == _PyTime_ROUND_FLOOR)
         return 0;
@@ -196,7 +194,7 @@
     *tp = t;
     return res;
 }
-#else
+#elif !defined(MS_WINDOWS)
 static int
 _PyTime_FromTimeval(_PyTime_t *tp, struct timeval *tv, int raise)
 {
@@ -227,7 +225,7 @@
         d = PyFloat_AsDouble(obj);
         d *= 1e9;
 
-        if (_PyTime_RoundTowardsInfinity(d < 0, round))
+        if (_PyTime_RoundTowardsPosInf(d < 0, round))
             d = ceil(d);
         else
             d = floor(d);
@@ -293,7 +291,7 @@
     _PyTime_t k;
     if (multiply < SEC_TO_NS) {
         k = SEC_TO_NS / multiply;
-        if (_PyTime_RoundTowardsInfinity(t < 0, round))
+        if (_PyTime_RoundTowardsPosInf(t < 0, round))
             return (t + k - 1) / k;
         else
             return t / k;
@@ -353,7 +351,7 @@
         res = -1;
 #endif
 
-    if (_PyTime_RoundTowardsInfinity(tv->tv_sec < 0, round))
+    if (_PyTime_RoundTowardsPosInf(tv->tv_sec < 0, round))
         tv->tv_usec = (int)((ns + US_TO_NS - 1) / US_TO_NS);
     else
         tv->tv_usec = (int)(ns / US_TO_NS);

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list