[Python-checkins] cpython (merge default -> default): Merge

brett.cannon python-checkins at python.org
Fri Jan 27 01:09:52 CET 2012


http://hg.python.org/cpython/rev/082eea3fc9bc
changeset:   74644:082eea3fc9bc
parent:      74643:ecf4a7bb8807
parent:      74642:856f0864437a
user:        Brett Cannon <brett at python.org>
date:        Thu Jan 26 19:09:44 2012 -0500
summary:
  Merge

files:
  Lib/test/test_time.py |  27 +++++++++++++--------
  Misc/NEWS             |   3 +-
  Modules/timemodule.c  |  38 ++++++++++++++++++++++--------
  3 files changed, 45 insertions(+), 23 deletions(-)


diff --git a/Lib/test/test_time.py b/Lib/test/test_time.py
--- a/Lib/test/test_time.py
+++ b/Lib/test/test_time.py
@@ -309,7 +309,7 @@
         for t in (-2, -1, 0, 1):
             try:
                 tt = time.localtime(t)
-            except (OverflowError, ValueError):
+            except (OverflowError, OSError):
                 pass
             else:
                 self.assertEqual(time.mktime(tt), t)
@@ -345,16 +345,21 @@
 
     def test_localtime_failure(self):
         # Issue #13847: check for localtime() failure
-        invalid_time_t = 2**60
-        try:
-            time.localtime(invalid_time_t)
-        except ValueError as err:
-            if str(err) == "timestamp out of range for platform time_t":
-                self.skipTest("need 64-bit time_t")
-            else:
-                raise
-        except OSError:
-            pass
+        invalid_time_t = None
+        for time_t in (-1, 2**30, 2**33, 2**60):
+            try:
+                time.localtime(time_t)
+            except ValueError as err:
+                if str(err) == "timestamp out of range for platform time_t":
+                    self.skipTest("need 64-bit time_t")
+                else:
+                    raise
+            except OSError:
+                invalid_time_t = time_t
+                break
+        if invalid_time_t is None:
+            self.skipTest("unable to find an invalid time_t value")
+
         self.assertRaises(OSError, time.localtime, invalid_time_t)
         self.assertRaises(OSError, time.gmtime, invalid_time_t)
         self.assertRaises(OSError, time.ctime, invalid_time_t)
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -463,7 +463,8 @@
 
 - Issue #13847: time.localtime() and time.gmtime() now raise an OSError instead
   of ValueError on failure. time.ctime() and time.asctime() now raises an
-  OSError if localtime() failed.
+  OSError if localtime() failed. time.clock() now raises a RuntimeError if the
+  processor time used is not available or its value cannot be represented
 
 - Issue #13862: Fix spurious failure in test_zlib due to runtime/compile time
   minor versions not matching.
diff --git a/Modules/timemodule.c b/Modules/timemodule.c
--- a/Modules/timemodule.c
+++ b/Modules/timemodule.c
@@ -62,6 +62,31 @@
 Return the current time in seconds since the Epoch.\n\
 Fractions of a second may be present if the system clock provides them.");
 
+#if defined(HAVE_CLOCK)
+
+#ifndef CLOCKS_PER_SEC
+#ifdef CLK_TCK
+#define CLOCKS_PER_SEC CLK_TCK
+#else
+#define CLOCKS_PER_SEC 1000000
+#endif
+#endif
+
+static PyObject *
+pyclock(void)
+{
+    clock_t value;
+    value = clock();
+    if (value == (clock_t)-1) {
+        PyErr_SetString(PyExc_RuntimeError,
+                "the processor time used is not available "
+                "or its value cannot be represented");
+        return NULL;
+    }
+    return PyFloat_FromDouble((double)value / CLOCKS_PER_SEC);
+}
+#endif /* HAVE_CLOCK */
+
 #if defined(MS_WINDOWS) && !defined(__BORLANDC__)
 /* Win32 has better clock replacement; we have our own version, due to Mark
    Hammond and Tim Peters */
@@ -79,8 +104,7 @@
         if (!QueryPerformanceFrequency(&freq) || freq.QuadPart == 0) {
             /* Unlikely to happen - this works on all intel
                machines at least!  Revert to clock() */
-            return PyFloat_FromDouble(((double)clock()) /
-                                      CLOCKS_PER_SEC);
+            return pyclock();
         }
         divisor = (double)freq.QuadPart;
     }
@@ -91,18 +115,10 @@
 
 #elif defined(HAVE_CLOCK)
 
-#ifndef CLOCKS_PER_SEC
-#ifdef CLK_TCK
-#define CLOCKS_PER_SEC CLK_TCK
-#else
-#define CLOCKS_PER_SEC 1000000
-#endif
-#endif
-
 static PyObject *
 time_clock(PyObject *self, PyObject *unused)
 {
-    return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC);
+    return pyclock();
 }
 #endif /* HAVE_CLOCK */
 

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


More information about the Python-checkins mailing list