Skip to content

Commit 9122fdd

Browse files
author
Victor Stinner
committed
Issue python#9642: Fix the definition of time.clock() on Windows
Don't unset and set againt the HAVE_CLOCK define, reorder the #if tests instead. Fix also the definition of the timezone encoding.
1 parent d64e8a7 commit 9122fdd

1 file changed

Lines changed: 25 additions & 29 deletions

File tree

Modules/timemodule.c

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
#include "Python.h"
44
#include "_time.h"
55

6-
#define TZNAME_ENCODING "utf-8"
7-
86
#include <ctype.h>
97

108
#ifdef HAVE_SYS_TYPES_H
@@ -45,12 +43,11 @@ static long main_thread;
4543
#endif /* MS_WINDOWS */
4644
#endif /* !__WATCOMC__ || __QNX__ */
4745

48-
#if defined(MS_WINDOWS) && !defined(__BORLANDC__)
49-
/* Win32 has better clock replacement; we have our own version below. */
50-
#undef HAVE_CLOCK
51-
#undef TZNAME_ENCODING
52-
#define TZNAME_ENCODING "mbcs"
53-
#endif /* MS_WINDOWS && !defined(__BORLANDC__) */
46+
#if defined(MS_WINDOWS)
47+
# define TZNAME_ENCODING "mbcs"
48+
#else
49+
# define TZNAME_ENCODING "utf-8"
50+
#endif
5451

5552
#if defined(PYOS_OS2)
5653
#define INCL_DOS
@@ -84,25 +81,9 @@ PyDoc_STRVAR(time_doc,
8481
Return the current time in seconds since the Epoch.\n\
8582
Fractions of a second may be present if the system clock provides them.");
8683

87-
#ifdef HAVE_CLOCK
88-
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-
97-
static PyObject *
98-
time_clock(PyObject *self, PyObject *unused)
99-
{
100-
return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC);
101-
}
102-
#endif /* HAVE_CLOCK */
103-
10484
#if defined(MS_WINDOWS) && !defined(__BORLANDC__)
105-
/* Due to Mark Hammond and Tim Peters */
85+
/* Win32 has better clock replacement; we have our own version, due to Mark
86+
Hammond and Tim Peters */
10687
static PyObject *
10788
time_clock(PyObject *self, PyObject *unused)
10889
{
@@ -127,8 +108,23 @@ time_clock(PyObject *self, PyObject *unused)
127108
return PyFloat_FromDouble(diff / divisor);
128109
}
129110

130-
#define HAVE_CLOCK /* So it gets included in the methods */
131-
#endif /* MS_WINDOWS && !defined(__BORLANDC__) */
111+
#elif defined(HAVE_CLOCK)
112+
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+
132128

133129
#ifdef HAVE_CLOCK
134130
PyDoc_STRVAR(clock_doc,
@@ -784,7 +780,7 @@ PyInit_timezone(PyObject *m) {
784780

785781
static PyMethodDef time_methods[] = {
786782
{"time", time_time, METH_NOARGS, time_doc},
787-
#ifdef HAVE_CLOCK
783+
#if (defined(MS_WINDOWS) && !defined(__BORLANDC__)) || defined(HAVE_CLOCK)
788784
{"clock", time_clock, METH_NOARGS, clock_doc},
789785
#endif
790786
{"sleep", time_sleep, METH_VARARGS, sleep_doc},

0 commit comments

Comments
 (0)