annotate Python/pytime.c @ 75177:fcd0a67e708e

Issue #14084: Fix a file descriptor leak when importing a module with a bad encoding.
author Antoine Pitrou <solipsis@pitrou.net>
date Wed, 22 Feb 2012 18:08:30 +0100
parents 90eda29a8e03
children 67d9595a833c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
f8409b3d6449 PEP 410
Victor Stinner <victor.stinner@haypocalc.com>
parents: 74821
diff changeset
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
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
73 void
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
74 _PyTime_Init()
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
75 {
c16803bbd790 Issue #9079: Added _PyTime_gettimeofday(_PyTime_timeval *tp) to C API
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
diff changeset
76 /* 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
77 }