Mercurial > cpython-lru_cache-weakref
annotate Python/pytime.c @ 76333:4dce3afc392c
Issue #13959: Simplify imp.reload() by relying on a module's
__loader__.
Since import now sets __loader__ on all modules it creates and
imp.reload() already relied on the attribute for modules that import
didn't create, the only potential compatibility issue is if people
were deleting the attribute on modules and expecting imp.reload() to
continue to work.
| author | Brett Cannon <brett@python.org> |
|---|---|
| date | Sun, 15 Apr 2012 17:56:09 -0400 |
| parents | 9d69a2418d80 |
| children | f554043badec |
| rev | line source |
|---|---|
|
63757
c16803bbd790
Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
1 #include "Python.h" |
|
74821
bee7943d38c6
Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63757
diff
changeset
|
2 #ifdef MS_WINDOWS |
|
bee7943d38c6
Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63757
diff
changeset
|
3 #include <windows.h> |
|
bee7943d38c6
Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63757
diff
changeset
|
4 #endif |
|
63757
c16803bbd790
Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
5 |
|
74821
bee7943d38c6
Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63757
diff
changeset
|
6 #if defined(__APPLE__) && defined(HAVE_GETTIMEOFDAY) && defined(HAVE_FTIME) |
|
63757
c16803bbd790
Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
7 /* |
|
c16803bbd790
Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
8 * _PyTime_gettimeofday falls back to ftime when getttimeofday fails because the latter |
|
c16803bbd790
Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
9 * might fail on some platforms. This fallback is unwanted on MacOSX because |
|
c16803bbd790
Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
10 * that makes it impossible to use a binary build on OSX 10.4 on earlier |
|
c16803bbd790
Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
11 * releases of the OS. Therefore claim we don't support ftime. |
|
c16803bbd790
Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
12 */ |
|
c16803bbd790
Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
13 # undef HAVE_FTIME |
|
c16803bbd790
Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
14 #endif |
|
c16803bbd790
Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
15 |
|
74821
bee7943d38c6
Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63757
diff
changeset
|
16 #if defined(HAVE_FTIME) && !defined(MS_WINDOWS) |
|
63757
c16803bbd790
Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
17 #include <sys/timeb.h> |
|
c16803bbd790
Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
18 extern int ftime(struct timeb *); |
|
74821
bee7943d38c6
Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63757
diff
changeset
|
19 #endif |
|
63757
c16803bbd790
Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
20 |
|
c16803bbd790
Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
21 void |
|
74834
90eda29a8e03
Backout f8409b3d6449: the PEP 410 is not accepted yet
Victor Stinner <victor.stinner@haypocalc.com>
parents:
74832
diff
changeset
|
22 _PyTime_gettimeofday(_PyTime_timeval *tp) |
|
63757
c16803bbd790
Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
23 { |
|
74821
bee7943d38c6
Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63757
diff
changeset
|
24 #ifdef MS_WINDOWS |
|
bee7943d38c6
Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63757
diff
changeset
|
25 FILETIME system_time; |
|
bee7943d38c6
Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63757
diff
changeset
|
26 ULARGE_INTEGER large; |
|
74834
90eda29a8e03
Backout f8409b3d6449: the PEP 410 is not accepted yet
Victor Stinner <victor.stinner@haypocalc.com>
parents:
74832
diff
changeset
|
27 ULONGLONG microseconds; |
|
74821
bee7943d38c6
Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63757
diff
changeset
|
28 |
|
bee7943d38c6
Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63757
diff
changeset
|
29 GetSystemTimeAsFileTime(&system_time); |
|
bee7943d38c6
Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63757
diff
changeset
|
30 large.u.LowPart = system_time.dwLowDateTime; |
|
bee7943d38c6
Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63757
diff
changeset
|
31 large.u.HighPart = system_time.dwHighDateTime; |
|
74834
90eda29a8e03
Backout f8409b3d6449: the PEP 410 is not accepted yet
Victor Stinner <victor.stinner@haypocalc.com>
parents:
74832
diff
changeset
|
32 /* 11,644,473,600,000,000: number of microseconds between |
|
74821
bee7943d38c6
Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63757
diff
changeset
|
33 the 1st january 1601 and the 1st january 1970 (369 years + 89 leap |
|
bee7943d38c6
Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63757
diff
changeset
|
34 days). */ |
|
74834
90eda29a8e03
Backout f8409b3d6449: the PEP 410 is not accepted yet
Victor Stinner <victor.stinner@haypocalc.com>
parents:
74832
diff
changeset
|
35 microseconds = large.QuadPart / 10 - 11644473600000000; |
|
90eda29a8e03
Backout f8409b3d6449: the PEP 410 is not accepted yet
Victor Stinner <victor.stinner@haypocalc.com>
parents:
74832
diff
changeset
|
36 tp->tv_sec = microseconds / 1000000; |
|
90eda29a8e03
Backout f8409b3d6449: the PEP 410 is not accepted yet
Victor Stinner <victor.stinner@haypocalc.com>
parents:
74832
diff
changeset
|
37 tp->tv_usec = microseconds % 1000000; |
|
74821
bee7943d38c6
Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63757
diff
changeset
|
38 #else |
|
63757
c16803bbd790
Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
39 /* There are three ways to get the time: |
|
c16803bbd790
Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
40 (1) gettimeofday() -- resolution in microseconds |
|
c16803bbd790
Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
41 (2) ftime() -- resolution in milliseconds |
|
c16803bbd790
Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
42 (3) time() -- resolution in seconds |
|
c16803bbd790
Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
43 In all cases the return value in a timeval struct. |
|
c16803bbd790
Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
44 Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may |
|
c16803bbd790
Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
45 fail, so we fall back on ftime() or time(). |
|
c16803bbd790
Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
46 Note: clock resolution does not imply clock accuracy! */ |
|
74821
bee7943d38c6
Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63757
diff
changeset
|
47 |
|
63757
c16803bbd790
Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
48 #ifdef HAVE_GETTIMEOFDAY |
|
c16803bbd790
Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
49 #ifdef GETTIMEOFDAY_NO_TZ |
|
74834
90eda29a8e03
Backout f8409b3d6449: the PEP 410 is not accepted yet
Victor Stinner <victor.stinner@haypocalc.com>
parents:
74832
diff
changeset
|
50 if (gettimeofday(tp) == 0) |
|
90eda29a8e03
Backout f8409b3d6449: the PEP 410 is not accepted yet
Victor Stinner <victor.stinner@haypocalc.com>
parents:
74832
diff
changeset
|
51 return; |
|
63757
c16803bbd790
Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
52 #else /* !GETTIMEOFDAY_NO_TZ */ |
|
74834
90eda29a8e03
Backout f8409b3d6449: the PEP 410 is not accepted yet
Victor Stinner <victor.stinner@haypocalc.com>
parents:
74832
diff
changeset
|
53 if (gettimeofday(tp, (struct timezone *)NULL) == 0) |
|
90eda29a8e03
Backout f8409b3d6449: the PEP 410 is not accepted yet
Victor Stinner <victor.stinner@haypocalc.com>
parents:
74832
diff
changeset
|
54 return; |
| 74832 | 55 #endif /* !GETTIMEOFDAY_NO_TZ */ |
|
63757
c16803bbd790
Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
56 #endif /* !HAVE_GETTIMEOFDAY */ |
|
74821
bee7943d38c6
Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63757
diff
changeset
|
57 |
|
63757
c16803bbd790
Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
58 #if defined(HAVE_FTIME) |
|
74834
90eda29a8e03
Backout f8409b3d6449: the PEP 410 is not accepted yet
Victor Stinner <victor.stinner@haypocalc.com>
parents:
74832
diff
changeset
|
59 { |
|
90eda29a8e03
Backout f8409b3d6449: the PEP 410 is not accepted yet
Victor Stinner <victor.stinner@haypocalc.com>
parents:
74832
diff
changeset
|
60 struct timeb t; |
|
90eda29a8e03
Backout f8409b3d6449: the PEP 410 is not accepted yet
Victor Stinner <victor.stinner@haypocalc.com>
parents:
74832
diff
changeset
|
61 ftime(&t); |
|
90eda29a8e03
Backout f8409b3d6449: the PEP 410 is not accepted yet
Victor Stinner <victor.stinner@haypocalc.com>
parents:
74832
diff
changeset
|
62 tp->tv_sec = t.time; |
|
90eda29a8e03
Backout f8409b3d6449: the PEP 410 is not accepted yet
Victor Stinner <victor.stinner@haypocalc.com>
parents:
74832
diff
changeset
|
63 tp->tv_usec = t.millitm * 1000; |
|
90eda29a8e03
Backout f8409b3d6449: the PEP 410 is not accepted yet
Victor Stinner <victor.stinner@haypocalc.com>
parents:
74832
diff
changeset
|
64 } |
|
63757
c16803bbd790
Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
65 #else /* !HAVE_FTIME */ |
|
74834
90eda29a8e03
Backout f8409b3d6449: the PEP 410 is not accepted yet
Victor Stinner <victor.stinner@haypocalc.com>
parents:
74832
diff
changeset
|
66 tp->tv_sec = time(NULL); |
|
90eda29a8e03
Backout f8409b3d6449: the PEP 410 is not accepted yet
Victor Stinner <victor.stinner@haypocalc.com>
parents:
74832
diff
changeset
|
67 tp->tv_usec = 0; |
|
63757
c16803bbd790
Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
68 #endif /* !HAVE_FTIME */ |
|
74821
bee7943d38c6
Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63757
diff
changeset
|
69 |
|
bee7943d38c6
Issue #13845: time.time() now uses GetSystemTimeAsFileTime() instead of ftime()
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63757
diff
changeset
|
70 #endif /* MS_WINDOWS */ |
|
63757
c16803bbd790
Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
71 } |
|
c16803bbd790
Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
72 |
|
75590
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
73 static void |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
74 error_time_t_overflow(void) |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
75 { |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
76 PyErr_SetString(PyExc_OverflowError, |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
77 "timestamp out of range for platform time_t"); |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
78 } |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
79 |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
80 static time_t |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
81 _PyLong_AsTime_t(PyObject *obj) |
|
75372
67d9595a833c
Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents:
74834
diff
changeset
|
82 { |
|
75590
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
83 #if defined(HAVE_LONG_LONG) && SIZEOF_TIME_T == SIZEOF_LONG_LONG |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
84 PY_LONG_LONG val; |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
85 val = PyLong_AsLongLong(obj); |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
86 #else |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
87 long val; |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
88 assert(sizeof(time_t) <= sizeof(long)); |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
89 val = PyLong_AsLong(obj); |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
90 #endif |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
91 if (val == -1 && PyErr_Occurred()) { |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
92 if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
93 error_time_t_overflow(); |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
94 return -1; |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
95 } |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
96 return (time_t)val; |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
97 } |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
98 |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
99 static int |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
100 _PyTime_ObjectToDenominator(PyObject *obj, time_t *sec, long *numerator, |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
101 double denominator) |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
102 { |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
103 assert(denominator <= LONG_MAX); |
|
75372
67d9595a833c
Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents:
74834
diff
changeset
|
104 if (PyFloat_Check(obj)) { |
|
75595
9d69a2418d80
Issue #14180: Fix an invalid rounding when compiler optimization are enabled
Victor Stinner <vstinner@wyplay.com>
parents:
75591
diff
changeset
|
105 double d, intpart, err; |
|
9d69a2418d80
Issue #14180: Fix an invalid rounding when compiler optimization are enabled
Victor Stinner <vstinner@wyplay.com>
parents:
75591
diff
changeset
|
106 /* volatile avoids unsafe optimization on float enabled by gcc -O3 */ |
|
9d69a2418d80
Issue #14180: Fix an invalid rounding when compiler optimization are enabled
Victor Stinner <vstinner@wyplay.com>
parents:
75591
diff
changeset
|
107 volatile double floatpart; |
|
75372
67d9595a833c
Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents:
74834
diff
changeset
|
108 |
|
67d9595a833c
Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents:
74834
diff
changeset
|
109 d = PyFloat_AsDouble(obj); |
|
67d9595a833c
Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents:
74834
diff
changeset
|
110 floatpart = modf(d, &intpart); |
|
67d9595a833c
Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents:
74834
diff
changeset
|
111 if (floatpart < 0) { |
|
67d9595a833c
Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents:
74834
diff
changeset
|
112 floatpart = 1.0 + floatpart; |
|
67d9595a833c
Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents:
74834
diff
changeset
|
113 intpart -= 1.0; |
|
67d9595a833c
Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents:
74834
diff
changeset
|
114 } |
|
67d9595a833c
Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents:
74834
diff
changeset
|
115 |
|
67d9595a833c
Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents:
74834
diff
changeset
|
116 *sec = (time_t)intpart; |
|
67d9595a833c
Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents:
74834
diff
changeset
|
117 err = intpart - (double)*sec; |
|
75590
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
118 if (err <= -1.0 || err >= 1.0) { |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
119 error_time_t_overflow(); |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
120 return -1; |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
121 } |
|
75372
67d9595a833c
Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents:
74834
diff
changeset
|
122 |
|
75590
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
123 floatpart *= denominator; |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
124 *numerator = (long)floatpart; |
|
75372
67d9595a833c
Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents:
74834
diff
changeset
|
125 return 0; |
|
67d9595a833c
Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents:
74834
diff
changeset
|
126 } |
|
67d9595a833c
Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents:
74834
diff
changeset
|
127 else { |
|
75590
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
128 *sec = _PyLong_AsTime_t(obj); |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
129 if (*sec == (time_t)-1 && PyErr_Occurred()) |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
130 return -1; |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
131 *numerator = 0; |
|
75372
67d9595a833c
Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents:
74834
diff
changeset
|
132 return 0; |
|
67d9595a833c
Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents:
74834
diff
changeset
|
133 } |
|
75590
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
134 } |
|
75372
67d9595a833c
Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents:
74834
diff
changeset
|
135 |
|
75590
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
136 int |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
137 _PyTime_ObjectToTime_t(PyObject *obj, time_t *sec) |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
138 { |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
139 if (PyFloat_Check(obj)) { |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
140 double d, intpart, err; |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
141 |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
142 d = PyFloat_AsDouble(obj); |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
143 (void)modf(d, &intpart); |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
144 |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
145 *sec = (time_t)intpart; |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
146 err = intpart - (double)*sec; |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
147 if (err <= -1.0 || err >= 1.0) { |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
148 error_time_t_overflow(); |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
149 return -1; |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
150 } |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
151 return 0; |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
152 } |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
153 else { |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
154 *sec = _PyLong_AsTime_t(obj); |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
155 if (*sec == (time_t)-1 && PyErr_Occurred()) |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
156 return -1; |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
157 return 0; |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
158 } |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
159 } |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
160 |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
161 int |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
162 _PyTime_ObjectToTimespec(PyObject *obj, time_t *sec, long *nsec) |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
163 { |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
164 return _PyTime_ObjectToDenominator(obj, sec, nsec, 1e9); |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
165 } |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
166 |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
167 int |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
168 _PyTime_ObjectToTimeval(PyObject *obj, time_t *sec, long *usec) |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
169 { |
|
1e9cc1a03365
Close #14180: Factorize code to convert a number of seconds to time_t, timeval or timespec
Victor Stinner <vstinner@wyplay.com>
parents:
75372
diff
changeset
|
170 return _PyTime_ObjectToDenominator(obj, sec, usec, 1e6); |
|
75372
67d9595a833c
Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents:
74834
diff
changeset
|
171 } |
|
67d9595a833c
Issue #13964: signal.sigtimedwait() timeout is now a float instead of a tuple
Victor Stinner <victor.stinner@gmail.com>
parents:
74834
diff
changeset
|
172 |
|
63757
c16803bbd790
Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
173 void |
|
c16803bbd790
Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
174 _PyTime_Init() |
|
c16803bbd790
Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
175 { |
|
c16803bbd790
Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
176 /* Do nothing. Needed to force linking. */ |
|
c16803bbd790
Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff
changeset
|
177 } |
