Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1)

Side by Side Diff: Modules/timemodule.c

Issue 10639: reindent.py converts newlines to platform default
Patch Set: Created 8 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Modules/_ssl.c ('k') | Objects/bytes_methods.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* Time module */ 1 /* Time module */
2 2
3 #include "Python.h" 3 #include "Python.h"
4 #include "_time.h" 4 #include "_time.h"
5
6 #define TZNAME_ENCODING "utf-8"
5 7
6 #include <ctype.h> 8 #include <ctype.h>
7 9
8 #ifdef HAVE_SYS_TYPES_H 10 #ifdef HAVE_SYS_TYPES_H
9 #include <sys/types.h> 11 #include <sys/types.h>
10 #endif /* HAVE_SYS_TYPES_H */ 12 #endif /* HAVE_SYS_TYPES_H */
11 13
12 #ifdef QUICKWIN 14 #ifdef QUICKWIN
13 #include <io.h> 15 #include <io.h>
14 #endif 16 #endif
(...skipping 21 matching lines...) Expand all
36 38
37 #if defined(__BORLANDC__) 39 #if defined(__BORLANDC__)
38 /* These overrides not needed for Win32 */ 40 /* These overrides not needed for Win32 */
39 #define timezone _timezone 41 #define timezone _timezone
40 #define tzname _tzname 42 #define tzname _tzname
41 #define daylight _daylight 43 #define daylight _daylight
42 #endif /* __BORLANDC__ */ 44 #endif /* __BORLANDC__ */
43 #endif /* MS_WINDOWS */ 45 #endif /* MS_WINDOWS */
44 #endif /* !__WATCOMC__ || __QNX__ */ 46 #endif /* !__WATCOMC__ || __QNX__ */
45 47
46 #if defined(HAVE_MBCS) 48 #if defined(MS_WINDOWS) && !defined(__BORLANDC__)
47 # define TZNAME_ENCODING "mbcs" 49 /* Win32 has better clock replacement; we have our own version below. */
48 #else 50 #undef HAVE_CLOCK
49 # define TZNAME_ENCODING "utf-8" 51 #undef TZNAME_ENCODING
50 #endif 52 #define TZNAME_ENCODING "mbcs"
53 #endif /* MS_WINDOWS && !defined(__BORLANDC__) */
51 54
52 #if defined(PYOS_OS2) 55 #if defined(PYOS_OS2)
53 #define INCL_DOS 56 #define INCL_DOS
54 #define INCL_ERRORS 57 #define INCL_ERRORS
55 #include <os2.h> 58 #include <os2.h>
56 #endif 59 #endif
57 60
58 #if defined(PYCC_VACPP) 61 #if defined(PYCC_VACPP)
59 #include <sys/time.h> 62 #include <sys/time.h>
60 #endif 63 #endif
(...skipping 13 matching lines...) Expand all
74 } 77 }
75 return PyFloat_FromDouble(secs); 78 return PyFloat_FromDouble(secs);
76 } 79 }
77 80
78 PyDoc_STRVAR(time_doc, 81 PyDoc_STRVAR(time_doc,
79 "time() -> floating point number\n\ 82 "time() -> floating point number\n\
80 \n\ 83 \n\
81 Return the current time in seconds since the Epoch.\n\ 84 Return the current time in seconds since the Epoch.\n\
82 Fractions of a second may be present if the system clock provides them."); 85 Fractions of a second may be present if the system clock provides them.");
83 86
84 #if defined(MS_WINDOWS) && !defined(__BORLANDC__) 87 #ifdef HAVE_CLOCK
85 /* Win32 has better clock replacement; we have our own version, due to Mark 88
86 Hammond and Tim Peters */ 89 #ifndef CLOCKS_PER_SEC
90 #ifdef CLK_TCK
91 #define CLOCKS_PER_SEC CLK_TCK
92 #else
93 #define CLOCKS_PER_SEC 1000000
94 #endif
95 #endif
96
87 static PyObject * 97 static PyObject *
88 time_clock(PyObject *self, PyObject *unused) 98 time_clock(PyObject *self, PyObject *unused)
89 { 99 {
100 return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC);
101 }
102 #endif /* HAVE_CLOCK */
103
104 #if defined(MS_WINDOWS) && !defined(__BORLANDC__)
105 /* Due to Mark Hammond and Tim Peters */
106 static PyObject *
107 time_clock(PyObject *self, PyObject *unused)
108 {
90 static LARGE_INTEGER ctrStart; 109 static LARGE_INTEGER ctrStart;
91 static double divisor = 0.0; 110 static double divisor = 0.0;
92 LARGE_INTEGER now; 111 LARGE_INTEGER now;
93 double diff; 112 double diff;
94 113
95 if (divisor == 0.0) { 114 if (divisor == 0.0) {
96 LARGE_INTEGER freq; 115 LARGE_INTEGER freq;
97 QueryPerformanceCounter(&ctrStart); 116 QueryPerformanceCounter(&ctrStart);
98 if (!QueryPerformanceFrequency(&freq) || freq.QuadPart == 0) { 117 if (!QueryPerformanceFrequency(&freq) || freq.QuadPart == 0) {
99 /* Unlikely to happen - this works on all intel 118 /* Unlikely to happen - this works on all intel
100 machines at least! Revert to clock() */ 119 machines at least! Revert to clock() */
101 return PyFloat_FromDouble(((double)clock()) / 120 return PyFloat_FromDouble(((double)clock()) /
102 CLOCKS_PER_SEC); 121 CLOCKS_PER_SEC);
103 } 122 }
104 divisor = (double)freq.QuadPart; 123 divisor = (double)freq.QuadPart;
105 } 124 }
106 QueryPerformanceCounter(&now); 125 QueryPerformanceCounter(&now);
107 diff = (double)(now.QuadPart - ctrStart.QuadPart); 126 diff = (double)(now.QuadPart - ctrStart.QuadPart);
108 return PyFloat_FromDouble(diff / divisor); 127 return PyFloat_FromDouble(diff / divisor);
109 } 128 }
110 129
111 #elif defined(HAVE_CLOCK) 130 #define HAVE_CLOCK /* So it gets included in the methods */
112 131 #endif /* MS_WINDOWS && !defined(__BORLANDC__) */
113 #ifndef CLOCKS_PER_SEC
114 #ifdef CLK_TCK
115 #define CLOCKS_PER_SEC CLK_TCK
116 #else
117 #define CLOCKS_PER_SEC 1000000
118 #endif
119 #endif
120
121 static PyObject *
122 time_clock(PyObject *self, PyObject *unused)
123 {
124 return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC);
125 }
126 #endif /* HAVE_CLOCK */
127
128 132
129 #ifdef HAVE_CLOCK 133 #ifdef HAVE_CLOCK
130 PyDoc_STRVAR(clock_doc, 134 PyDoc_STRVAR(clock_doc,
131 "clock() -> floating point number\n\ 135 "clock() -> floating point number\n\
132 \n\ 136 \n\
133 Return the CPU time or real time since the start of the process or since\n\ 137 Return the CPU time or real time since the start of the process or since\n\
134 the first call to clock(). This has as much precision as the system\n\ 138 the first call to clock(). This has as much precision as the system\n\
135 records."); 139 records.");
136 #endif 140 #endif
137 141
138 static PyObject * 142 static PyObject *
139 time_sleep(PyObject *self, PyObject *args) 143 time_sleep(PyObject *self, PyObject *args)
140 { 144 {
141 double secs; 145 double secs;
142 if (!PyArg_ParseTuple(args, "d:sleep", &secs)) 146 if (!PyArg_ParseTuple(args, "d:sleep", &secs))
143 return NULL; 147 return NULL;
144 if (secs < 0) {
145 PyErr_SetString(PyExc_ValueError,
146 "sleep length must be non-negative");
147 return NULL;
148 }
149 if (floatsleep(secs) != 0) 148 if (floatsleep(secs) != 0)
150 return NULL; 149 return NULL;
151 Py_INCREF(Py_None); 150 Py_INCREF(Py_None);
152 return Py_None; 151 return Py_None;
153 } 152 }
154 153
155 PyDoc_STRVAR(sleep_doc, 154 PyDoc_STRVAR(sleep_doc,
156 "sleep(seconds)\n\ 155 "sleep(seconds)\n\
157 \n\ 156 \n\
158 Delay execution for a given number of seconds. The argument may be\n\ 157 Delay execution for a given number of seconds. The argument may be\n\
(...skipping 619 matching lines...) Expand 10 before | Expand all | Expand 10 after
778 PyModule_AddIntConstant(m, "daylight", _daylight); 777 PyModule_AddIntConstant(m, "daylight", _daylight);
779 PyModule_AddObject(m, "tzname", 778 PyModule_AddObject(m, "tzname",
780 Py_BuildValue("(zz)", _tzname[0], _tzname[1])); 779 Py_BuildValue("(zz)", _tzname[0], _tzname[1]));
781 #endif /* __CYGWIN__ */ 780 #endif /* __CYGWIN__ */
782 #endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/ 781 #endif /* !HAVE_TZNAME || __GLIBC__ || __CYGWIN__*/
783 } 782 }
784 783
785 784
786 static PyMethodDef time_methods[] = { 785 static PyMethodDef time_methods[] = {
787 {"time", time_time, METH_NOARGS, time_doc}, 786 {"time", time_time, METH_NOARGS, time_doc},
788 #if (defined(MS_WINDOWS) && !defined(__BORLANDC__)) || defined(HAVE_CLOCK) 787 #ifdef HAVE_CLOCK
789 {"clock", time_clock, METH_NOARGS, clock_doc}, 788 {"clock", time_clock, METH_NOARGS, clock_doc},
790 #endif 789 #endif
791 {"sleep", time_sleep, METH_VARARGS, sleep_doc}, 790 {"sleep", time_sleep, METH_VARARGS, sleep_doc},
792 {"gmtime", time_gmtime, METH_VARARGS, gmtime_doc}, 791 {"gmtime", time_gmtime, METH_VARARGS, gmtime_doc},
793 {"localtime", time_localtime, METH_VARARGS, localtime_doc}, 792 {"localtime", time_localtime, METH_VARARGS, localtime_doc},
794 {"asctime", time_asctime, METH_VARARGS, asctime_doc}, 793 {"asctime", time_asctime, METH_VARARGS, asctime_doc},
795 {"ctime", time_ctime, METH_VARARGS, ctime_doc}, 794 {"ctime", time_ctime, METH_VARARGS, ctime_doc},
796 #ifdef HAVE_MKTIME 795 #ifdef HAVE_MKTIME
797 {"mktime", time_mktime, METH_O, mktime_doc}, 796 {"mktime", time_mktime, METH_O, mktime_doc},
798 #endif 797 #endif
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
909 When interrupted (or when another error occurs), return -1 and 908 When interrupted (or when another error occurs), return -1 and
910 set an exception; else return 0. */ 909 set an exception; else return 0. */
911 910
912 static int 911 static int
913 floatsleep(double secs) 912 floatsleep(double secs)
914 { 913 {
915 /* XXX Should test for MS_WINDOWS first! */ 914 /* XXX Should test for MS_WINDOWS first! */
916 #if defined(HAVE_SELECT) && !defined(__EMX__) 915 #if defined(HAVE_SELECT) && !defined(__EMX__)
917 struct timeval t; 916 struct timeval t;
918 double frac; 917 double frac;
919 int err;
920
921 frac = fmod(secs, 1.0); 918 frac = fmod(secs, 1.0);
922 secs = floor(secs); 919 secs = floor(secs);
923 t.tv_sec = (long)secs; 920 t.tv_sec = (long)secs;
924 t.tv_usec = (long)(frac*1000000.0); 921 t.tv_usec = (long)(frac*1000000.0);
925 Py_BEGIN_ALLOW_THREADS 922 Py_BEGIN_ALLOW_THREADS
926 err = select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t); 923 if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
927 Py_END_ALLOW_THREADS
928 if (err != 0) {
929 #ifdef EINTR 924 #ifdef EINTR
930 if (errno == EINTR) { 925 if (errno != EINTR) {
931 if (PyErr_CheckSignals()) 926 #else
932 return -1; 927 if (1) {
933 }
934 else
935 #endif 928 #endif
936 { 929 Py_BLOCK_THREADS
937 PyErr_SetFromErrno(PyExc_IOError); 930 PyErr_SetFromErrno(PyExc_IOError);
938 return -1; 931 return -1;
939 } 932 }
940 } 933 }
934 Py_END_ALLOW_THREADS
941 #elif defined(__WATCOMC__) && !defined(__QNX__) 935 #elif defined(__WATCOMC__) && !defined(__QNX__)
942 /* XXX Can't interrupt this sleep */ 936 /* XXX Can't interrupt this sleep */
943 Py_BEGIN_ALLOW_THREADS 937 Py_BEGIN_ALLOW_THREADS
944 delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */ 938 delay((int)(secs * 1000 + 0.5)); /* delay() uses milliseconds */
945 Py_END_ALLOW_THREADS 939 Py_END_ALLOW_THREADS
946 #elif defined(MS_WINDOWS) 940 #elif defined(MS_WINDOWS)
947 { 941 {
948 double millisecs = secs * 1000.0; 942 double millisecs = secs * 1000.0;
949 unsigned long ul_millis; 943 unsigned long ul_millis;
950 944
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
989 Py_END_ALLOW_THREADS 983 Py_END_ALLOW_THREADS
990 #else 984 #else
991 /* XXX Can't interrupt this sleep */ 985 /* XXX Can't interrupt this sleep */
992 Py_BEGIN_ALLOW_THREADS 986 Py_BEGIN_ALLOW_THREADS
993 sleep((int)secs); 987 sleep((int)secs);
994 Py_END_ALLOW_THREADS 988 Py_END_ALLOW_THREADS
995 #endif 989 #endif
996 990
997 return 0; 991 return 0;
998 } 992 }
OLDNEW
« no previous file with comments | « Modules/_ssl.c ('k') | Objects/bytes_methods.c » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+