Author: Amaury Forgeot d'Arc <[email protected]>
Branch: kill_ll_time
Changeset: r75285:06a7386c87a3
Date: 2015-01-10 16:03 +0100
http://bitbucket.org/pypy/pypy/changeset/06a7386c87a3/
Log: Create rtime.py, move implmentation of time.time()
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
@@ -4,7 +4,7 @@
from pypy.interpreter.gateway import unwrap_spec
from rpython.rtyper.lltypesystem import lltype
from rpython.rlib.rarithmetic import intmask
-from rpython.rlib import rposix
+from rpython.rlib import rposix, rtime
from rpython.translator.tool.cbuild import ExternalCompilationInfo
import os
import sys
@@ -359,7 +359,7 @@
# w_seconds can be a wrapped None (it will be automatically wrapped
# in the callers, so we never get a real None here).
if space.is_none(w_seconds):
- seconds = pytime.time()
+ seconds = rtime.floattime()
else:
seconds = space.float_w(w_seconds)
#
@@ -396,7 +396,7 @@
raise OperationError(space.w_TypeError,
space.wrap("tuple expected"))
# default to the current local time
- tt = rffi.r_time_t(int(pytime.time()))
+ tt = rffi.r_time_t(int(rtime.floattime()))
t_ref = lltype.malloc(rffi.TIME_TP.TO, 1, flavor='raw')
t_ref[0] = tt
pbuf = c_localtime(t_ref)
@@ -476,8 +476,7 @@
Return the current time in seconds since the Epoch.
Fractions of a second may be present if the system clock provides them."""
- secs = pytime.time()
- return space.wrap(secs)
+ return space.wrap(rtime.floattime())
if _WIN:
class PCCache:
diff --git a/rpython/rlib/rtermios.py b/rpython/rlib/rtermios.py
--- a/rpython/rlib/rtermios.py
+++ b/rpython/rlib/rtermios.py
@@ -1,8 +1,3 @@
-# This are here only because it's always better safe than sorry.
-# The issue is that from-time-to-time CPython's termios.tcgetattr
-# returns list of mostly-strings of length one, but with few ints
-# inside, so we make sure it works
-
from rpython.rtyper.lltypesystem import rffi, lltype
from rpython.rtyper.tool import rffi_platform
from rpython.translator.tool.cbuild import ExternalCompilationInfo
diff --git a/rpython/rlib/rtime.py b/rpython/rlib/rtime.py
new file mode 100644
--- /dev/null
+++ b/rpython/rlib/rtime.py
@@ -0,0 +1,118 @@
+import sys, time
+from rpython.rtyper.lltypesystem import rffi, lltype
+from rpython.rtyper.tool import rffi_platform
+from rpython.translator.tool.cbuild import ExternalCompilationInfo
+from rpython.rtyper.extregistry import replacement_for
+
+from rpython.rlib.rarithmetic import intmask
+
+if sys.platform == 'win32':
+ TIME_H = 'time.h'
+ FTIME = '_ftime64'
+ STRUCT_TIMEB = 'struct __timeb64'
+ includes = ['winsock2.h', 'windows.h',
+ TIME_H, 'sys/types.h', 'sys/timeb.h']
+ need_rusage = False
+else:
+ TIME_H = 'sys/time.h'
+ FTIME = 'ftime'
+ STRUCT_TIMEB = 'struct timeb'
+ includes = [TIME_H, 'time.h', 'errno.h', 'sys/select.h',
+ 'sys/types.h', 'unistd.h',
+ 'sys/time.h', 'sys/resource.h']
+
+ if not sys.platform.startswith("openbsd"):
+ includes.append('sys/timeb.h')
+
+ need_rusage = True
+
+eci = ExternalCompilationInfo(
+ includes=includes
+)
+
+class CConfig:
+ _compilation_info_ = eci
+ TIMEVAL = rffi_platform.Struct('struct timeval', [('tv_sec', rffi.INT),
+ ('tv_usec', rffi.INT)])
+ HAVE_GETTIMEOFDAY = rffi_platform.Has('gettimeofday')
+ HAVE_FTIME = rffi_platform.Has(FTIME)
+ if need_rusage:
+ RUSAGE = rffi_platform.Struct('struct rusage', [('ru_utime', TIMEVAL),
+ ('ru_stime', TIMEVAL)])
+
+ TIMEB = rffi_platform.Struct(STRUCT_TIMEB, [('time', rffi.INT),
+ ('millitm', rffi.INT)])
+
+constant_names = ['RUSAGE_SELF', 'EINTR', 'CLOCK_PROCESS_CPUTIME_ID']
+for const in constant_names:
+ setattr(CConfig, const, rffi_platform.DefinedConstantInteger(const))
+defs_names = ['GETTIMEOFDAY_NO_TZ']
+for const in defs_names:
+ setattr(CConfig, const, rffi_platform.Defined(const))
+
+globals().update(rffi_platform.configure(CConfig))
+TIMEVALP = lltype.Ptr(TIMEVAL)
+
+def external(name, args, result, **kwargs):
+ return rffi.llexternal(name, args, result, compilation_info=eci, **kwargs)
+
+if HAVE_GETTIMEOFDAY:
+ if GETTIMEOFDAY_NO_TZ:
+ c_gettimeofday = external('gettimeofday',
+ [TIMEVALP], rffi.INT,
+ _nowrapper=True, releasegil=False)
+ else:
+ c_gettimeofday = external('gettimeofday',
+ [TIMEVALP, rffi.VOIDP], rffi.INT,
+ _nowrapper=True, releasegil=False)
+
+# On some systems (e.g. SCO ODT 3.0) gettimeofday() may
+# fail, so we fall back on ftime() or time().
+if HAVE_FTIME:
+ c_ftime = external(FTIME, [lltype.Ptr(TIMEB)],
+ lltype.Void,
+ _nowrapper=True, releasegil=False)
+
+c_time = external('time', [rffi.VOIDP], rffi.TIME_T,
+ _nowrapper=True, releasegil=False)
+
+
+def decode_timeval(t):
+ return (float(rffi.getintfield(t, 'c_tv_sec')) +
+ float(rffi.getintfield(t, 'c_tv_usec')) * 0.000001)
+
+
+@replacement_for(time.time, sandboxed_name='ll_time.ll_time_time')
+def floattime():
+ # There are three ways to get the time:
+ # (1) gettimeofday() -- resolution in microseconds
+ # (2) ftime() -- resolution in milliseconds
+ # (3) time() -- resolution in seconds
+ # In all cases the return value is a float in seconds.
+ # Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
+ # fail, so we fall back on ftime() or time().
+ # Note: clock resolution does not imply clock accuracy!
+
+ void = lltype.nullptr(rffi.VOIDP.TO)
+ result = -1.0
+ if HAVE_GETTIMEOFDAY:
+ with lltype.scoped_alloc(TIMEVAL) as t:
+ errcode = -1
+ if GETTIMEOFDAY_NO_TZ:
+ errcode = c_gettimeofday(t)
+ else:
+ errcode = c_gettimeofday(t, void)
+
+ if intmask(errcode) == 0:
+ result = decode_timeval(t)
+ if result != -1:
+ return result
+ if HAVE_FTIME:
+ with lltype.scoped_alloc(TIMEB) as t:
+ c_ftime(t)
+ result = (float(intmask(t.c_time)) +
+ float(intmask(t.c_millitm)) * 0.001)
+ return result
+ else:
+ return float(c_time(void))
+
diff --git a/rpython/rtyper/extregistry.py b/rpython/rtyper/extregistry.py
--- a/rpython/rtyper/extregistry.py
+++ b/rpython/rtyper/extregistry.py
@@ -144,3 +144,21 @@
except KeyError:
return False
return True
+
+def replacement_for(replaced_function, sandboxed_name=None):
+ # The annotated function replaces calls to the given non-RPython
+ # function.
+ def wrap(func):
+ from rpython.rtyper.extregistry import ExtRegistryEntry
+ class ExtRegistry(ExtRegistryEntry):
+ _about_ = replaced_function
+ def compute_annotation(self):
+ if sandboxed_name:
+ config = self.bookkeeper.annotator.translator.config
+ if config.translation.sandbox:
+ func._sandbox_external_name = sandboxed_name
+ func._dont_inline_ = True
+ return self.bookkeeper.immutablevalue(func)
+ return func
+ return wrap
+
diff --git a/rpython/rtyper/module/ll_time.py b/rpython/rtyper/module/ll_time.py
--- a/rpython/rtyper/module/ll_time.py
+++ b/rpython/rtyper/module/ll_time.py
@@ -84,67 +84,6 @@
self.configure(CConfig)
self.TIMEVALP = lltype.Ptr(self.TIMEVAL)
- @registering(time.time)
- def register_time_time(self):
- # Note: time.time() is used by the framework GC during collect(),
- # which means that we have to be very careful about not allocating
- # GC memory here. This is the reason for the _nowrapper=True.
-
- # AWFUL
- if self.HAVE_GETTIMEOFDAY:
- if self.GETTIMEOFDAY_NO_TZ:
- c_gettimeofday = self.llexternal('gettimeofday',
- [self.TIMEVALP], rffi.INT,
- _nowrapper=True, releasegil=False)
- else:
- c_gettimeofday = self.llexternal('gettimeofday',
- [self.TIMEVALP, rffi.VOIDP], rffi.INT,
- _nowrapper=True, releasegil=False)
- c_ftime = None # We have gettimeofday(2), so force ftime(3) OFF.
- else:
- c_gettimeofday = None
-
- # Only look for ftime(3) if gettimeofday(2) was not found.
- if self.HAVE_FTIME:
- self.configure(CConfigForFTime)
- c_ftime = self.llexternal(FTIME, [lltype.Ptr(self.TIMEB)],
- lltype.Void,
- _nowrapper=True, releasegil=False)
- else:
- c_ftime = None # to not confuse the flow space
-
- c_time = self.llexternal('time', [rffi.VOIDP], rffi.TIME_T,
- _nowrapper=True, releasegil=False)
-
- def time_time_llimpl():
- void = lltype.nullptr(rffi.VOIDP.TO)
- result = -1.0
- if self.HAVE_GETTIMEOFDAY:
- t = lltype.malloc(self.TIMEVAL, flavor='raw')
-
- errcode = -1
- if self.GETTIMEOFDAY_NO_TZ:
- errcode = c_gettimeofday(t)
- else:
- errcode = c_gettimeofday(t, void)
-
- if rffi.cast(rffi.LONG, errcode) == 0:
- result = decode_timeval(t)
- lltype.free(t, flavor='raw')
- if result != -1:
- return result
- else: # assume using ftime(3)
- t = lltype.malloc(self.TIMEB, flavor='raw')
- c_ftime(t)
- result = (float(intmask(t.c_time)) +
- float(intmask(t.c_millitm)) * 0.001)
- lltype.free(t, flavor='raw')
- return result
- return float(c_time(void))
-
- return extdef([], float, llimpl=time_time_llimpl,
- export_name='ll_time.ll_time_time')
-
@registering(time.clock)
def register_time_clock(self):
if sys.platform == 'win32':
diff --git a/rpython/rtyper/module/test/test_ll_time.py
b/rpython/rtyper/module/test/test_ll_time.py
--- a/rpython/rtyper/module/test/test_ll_time.py
+++ b/rpython/rtyper/module/test/test_ll_time.py
@@ -2,6 +2,8 @@
from rpython.rtyper.test.tool import BaseRtypingTest
#from rpython.translator.c.test.test_genc import compile
+from rpython.rlib import rtime # Register functions as side-effect
+
import time, sys
class TestTime(BaseRtypingTest):
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit