Author: Yannick Jadoul <[email protected]>
Branch: py3.7-pep564
Changeset: r97980:b2be19ccfa88
Date: 2019-11-07 17:44 +0100
http://bitbucket.org/pypy/pypy/changeset/b2be19ccfa88/

Log:    Using r_int64 to handle nanoseconds in time module without
        overflowing

diff --git a/pypy/module/time/interp_time.py b/pypy/module/time/interp_time.py
--- a/pypy/module/time/interp_time.py
+++ b/pypy/module/time/interp_time.py
@@ -10,7 +10,7 @@
     str_decode_locale_surrogateescape, unicode_encode_locale_surrogateescape)
 from rpython.rtyper.lltypesystem import lltype
 from rpython.rlib.rarithmetic import (
-    intmask, r_ulonglong, r_longfloat, widen, ovfcheck, ovfcheck_float_to_int)
+    intmask, r_ulonglong, r_longfloat, r_int64, widen, ovfcheck, 
ovfcheck_float_to_int)
 from rpython.rlib.rtime import (GETTIMEOFDAY_NO_TZ, TIMEVAL,
                                 HAVE_GETTIMEOFDAY, HAVE_FTIME)
 from rpython.rlib import rposix, rtime
@@ -277,7 +277,7 @@
                     _setinfo(space, w_info, "GetSystemTimeAsFileTime()",
                              time_increment[0] * 1e-7, False, True)
             if return_ns:
-                return space.newint(microseconds * 10**3)
+                return space.newint(r_int64(microseconds) * 10**3)
             else:
                 tv_sec = microseconds / 10**6
                 tv_usec = microseconds % 10**6
@@ -303,8 +303,8 @@
                         _setinfo(space, w_info, "gettimeofday()", 1e-6, False, 
True)
                     if return_ns:
                         return space.newint(
-                            widen(timeval.c_tv_sec) * 10**9 +
-                            widen(timeval.c_tv_usec) * 10**3)
+                            r_int64(timeval.c_tv_sec) * 10**9 +
+                            r_int64(timeval.c_tv_usec) * 10**3)
                     else:
                         return space.newfloat(
                             widen(timeval.c_tv_sec) +
@@ -316,8 +316,8 @@
                     _setinfo(space, w_info, "ftime()", 1e-3, False, True)
                 if return_ns:
                     return space.newint(
-                        widen(t.c_time) * 10**9 +
-                        widen(t.c_millitm) * 10**6)
+                        r_int64(t.c_time) * 10**9 +
+                        r_int64(t.c_millitm) * 10**6)
                 else:
                     return space.newfloat(
                         widen(t.c_time) +
@@ -327,7 +327,7 @@
                 _setinfo(space, w_info, "time()", 1.0, False, True)
             result = c_time(lltype.nullptr(rffi.TIME_TP.TO))
             if return_ns:
-                return space.newint(widen(result) * 10**9)
+                return space.newint(r_int64(result) * 10**9)
             else:
                 return space.newfloat(float(result))
     
@@ -821,7 +821,7 @@
         return widen(timespec.c_tv_sec) + widen(timespec.c_tv_nsec) * 1e-9
 
     def _timespec_to_nanoseconds(timespec):
-        return widen(timespec.c_tv_sec) * 10**9 + widen(timespec.c_tv_nsec)
+        return r_int64(timespec.c_tv_sec) * 10**9 + r_int64(timespec.c_tv_nsec)
     
     def _clock_gettime_impl(space, clk_id, return_ns):
         with lltype.scoped_alloc(TIMESPEC) as timespec:
@@ -861,7 +861,7 @@
             if ret != 0:
                 raise exception_from_saved_errno(space, space.w_OSError)
 
-    @unwrap_spec(clk_id='c_int', ns=int)
+    @unwrap_spec(clk_id='c_int', ns=r_int64)
     def clock_settime_ns(space, clk_id, ns):
         """clock_settime_ns(clk_id, time)
     
@@ -993,7 +993,7 @@
                 _setinfo(space, w_info, implementation, resolution, True, 
False)
 
             if return_ns:
-                return space.newint(widen(tick_count) * 10**6)
+                return space.newint(r_int64(tick_count) * 10**6)
             else:
                 return space.newfloat(tick_count * 1e-3)
 
@@ -1012,7 +1012,7 @@
             time = rffi.cast(lltype.Signed, c_mach_absolute_time())
             numer = rffi.getintfield(timebase_info, 'c_numer')
             denom = rffi.getintfield(timebase_info, 'c_denom')
-            nanosecs = time * numer / denom
+            nanosecs = r_int64(time) * numer / denom
             if w_info is not None:
                 res = (numer / denom) * 1e-9
                 _setinfo(space, w_info, "mach_absolute_time()", res, True, 
False)
@@ -1084,7 +1084,7 @@
                 _setinfo(space, w_info, "QueryPerformanceCounter()", 
resolution,
                          True, False)
             if return_ns:
-                return space.newint(widen(diff) * 10**9 // time_state.divisor)
+                return space.newint(r_int64(diff) * 10**9 // 
time_state.divisor)
             else:
                 return space.newfloat(float(diff) / float(time_state.divisor))
 
@@ -1139,7 +1139,7 @@
         if w_info is not None:
             _setinfo(space, w_info, "GetProcessTimes()", 1e-7, True, False)
         if return_ns:
-            return space.newint((widen(kernel_time2) + widen(user_time2)) * 
10**2)
+            return space.newint((r_int64(kernel_time2) + r_int64(user_time2)) 
* 10**2)
         else:
             return space.newfloat((float(kernel_time2) + float(user_time2)) * 
1e-7)
 else:
@@ -1198,20 +1198,20 @@
                                  1.0 / rposix.CLOCK_TICKS_PER_SECOND,
                                  True, False)
                     if return_ns:
-                        return space.newint(widen(cpu_time) * 10**9 // 
int(rposix.CLOCK_TICKS_PER_SECOND))
+                        return space.newint(r_int64(cpu_time) * 10**9 // 
int(rposix.CLOCK_TICKS_PER_SECOND))
                     else:
                         return space.newfloat(float(cpu_time) / 
rposix.CLOCK_TICKS_PER_SECOND)
         return _clock_impl(space, w_info, return_ns)
 
 def process_time(space, w_info=None):
     """process_time() -> float
-    
+
     Process time for profiling: sum of the kernel and user-space CPU time."""
     return _process_time_impl(space, w_info, False)
 
 def process_time_ns(space, w_info=None):
     """process_time() -> int
-    
+
     Process time for profiling as nanoseconds:
     sum of the kernel and user-space CPU time"""
     return _process_time_impl(space, w_info, True)
@@ -1232,7 +1232,7 @@
         _setinfo(space, w_info,
                  "clock()", 1.0 / CLOCKS_PER_SEC, True, False)
     if return_ns:
-        return space.newint(value * 10**9 // CLOCKS_PER_SEC)
+        return space.newint(r_int64(value) * 10**9 // CLOCKS_PER_SEC)
     else:
         return space.newfloat(float(value) / CLOCKS_PER_SEC)
 
diff --git a/rpython/rlib/rtime.py b/rpython/rlib/rtime.py
--- a/rpython/rlib/rtime.py
+++ b/rpython/rlib/rtime.py
@@ -9,7 +9,7 @@
 from rpython.rtyper.tool import rffi_platform
 from rpython.rtyper.lltypesystem import rffi, lltype
 from rpython.rlib.objectmodel import register_replacement_for
-from rpython.rlib.rarithmetic import intmask, UINT_MAX, widen
+from rpython.rlib.rarithmetic import intmask, r_int64, UINT_MAX
 from rpython.rlib import rposix
 
 _WIN32 = sys.platform.startswith('win')
@@ -95,8 +95,8 @@
             float(rffi.getintfield(t, 'c_tv_usec')) * 0.000001)
 
 def decode_timeval_ns(t):
-    return (widen(rffi.getintfield(t, 'c_tv_sec')) * 10**9 +
-            widen(rffi.getintfield(t, 'c_tv_usec')) * 10**3)
+    return (r_int64(rffi.getintfield(t, 'c_tv_sec')) * 10**9 +
+            r_int64(rffi.getintfield(t, 'c_tv_usec')) * 10**3)
 
 
 def external(name, args, result, compilation_info=eci, **kwds):
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to