Author: Alex Gaynor <alex.gay...@gmail.com> Branch: Changeset: r53294:c8fcc7319050 Date: 2012-03-11 11:56 -0700 http://bitbucket.org/pypy/pypy/changeset/c8fcc7319050/
Log: Backport time.clock_gettime from python 3.3 to __pypy__ diff --git a/pypy/module/__pypy__/__init__.py b/pypy/module/__pypy__/__init__.py --- a/pypy/module/__pypy__/__init__.py +++ b/pypy/module/__pypy__/__init__.py @@ -2,6 +2,7 @@ # Package initialisation from pypy.interpreter.mixedmodule import MixedModule from pypy.module.imp.importing import get_pyc_magic +from pypy.module.__pypy__ import interp_time class BuildersModule(MixedModule): @@ -12,6 +13,19 @@ "UnicodeBuilder": "interp_builders.W_UnicodeBuilder", } +class TimeModule(MixedModule): + appleveldefs = {} + interpleveldefs = {} + if interp_time.HAS_CLOCK_GETTIME: + interpleveldefs["clock_gettime"] = "interp_time.clock_gettime" + interpleveldefs["clock_getres"] = "interp_time.clock_getres" + for name in [ + "CLOCK_REALTIME", "CLOCK_MONOTONIC", "CLOCK_MONOTONIC_RAW", + "CLOCK_PROCESS_CPUTIME_ID", "CLOCK_THREAD_CPUTIME_ID" + ]: + interpleveldefs[name] = "space.wrap(interp_time.%s)" % name + + class Module(MixedModule): appleveldefs = { } @@ -32,6 +46,7 @@ submodules = { "builders": BuildersModule, + "time": TimeModule, } def setup_after_space_initialization(self): diff --git a/pypy/module/__pypy__/interp_time.py b/pypy/module/__pypy__/interp_time.py new file mode 100644 --- /dev/null +++ b/pypy/module/__pypy__/interp_time.py @@ -0,0 +1,63 @@ +from pypy.interpreter.error import exception_from_errno +from pypy.interpreter.gateway import unwrap_spec +from pypy.rpython.lltypesystem import rffi, lltype +from pypy.rpython.tool import rffi_platform +from pypy.translator.tool.cbuild import ExternalCompilationInfo + + + +class CConfig: + _compilation_info_ = ExternalCompilationInfo( + includes=["time.h"], + libraries=["rt"], + ) + + HAS_CLOCK_GETTIME = rffi_platform.Has('clock_gettime') + + CLOCK_REALTIME = rffi_platform.DefinedConstantInteger("CLOCK_REALTIME") + CLOCK_MONOTONIC = rffi_platform.DefinedConstantInteger("CLOCK_MONOTONIC") + CLOCK_MONOTONIC_RAW = rffi_platform.DefinedConstantInteger("CLOCK_MONOTONIC_RAW") + CLOCK_PROCESS_CPUTIME_ID = rffi_platform.DefinedConstantInteger("CLOCK_PROCESS_CPUTIME_ID") + CLOCK_THREAD_CPUTIME_ID = rffi_platform.DefinedConstantInteger("CLOCK_THREAD_CPUTIME_ID") + + TIMESPEC = rffi_platform.Struct("struct timespec", [ + ("tv_sec", rffi.TIME_T), + ("tv_nsec", rffi.LONG), + ]) + +cconfig = rffi_platform.configure(CConfig) + +HAS_CLOCK_GETTIME = cconfig["HAS_CLOCK_GETTIME"] + +CLOCK_REALTIME = cconfig["CLOCK_REALTIME"] +CLOCK_MONOTONIC = cconfig["CLOCK_MONOTONIC"] +CLOCK_MONOTONIC_RAW = cconfig["CLOCK_MONOTONIC_RAW"] +CLOCK_PROCESS_CPUTIME_ID = cconfig["CLOCK_PROCESS_CPUTIME_ID"] +CLOCK_THREAD_CPUTIME_ID = cconfig["CLOCK_THREAD_CPUTIME_ID"] + +TIMESPEC = cconfig["TIMESPEC"] + +c_clock_gettime = rffi.llexternal("clock_gettime", + [lltype.Signed, lltype.Ptr(TIMESPEC)], rffi.INT, + compilation_info=CConfig._compilation_info_, threadsafe=False +) +c_clock_getres = rffi.llexternal("clock_getres", + [lltype.Signed, lltype.Ptr(TIMESPEC)], rffi.INT, + compilation_info=CConfig._compilation_info_, threadsafe=False +) + +@unwrap_spec(clk_id="c_int") +def clock_gettime(space, clk_id): + with lltype.scoped_alloc(TIMESPEC) as tp: + ret = c_clock_gettime(clk_id, tp) + if ret != 0: + raise exception_from_errno(space.w_IOError) + return space.wrap(tp.c_tv_sec + tp.c_tv_nsec * 1e-9) + +@unwrap_spec(clk_id="c_int") +def clock_getres(space, clk_id): + with lltype.scoped_alloc(TIMESPEC) as tp: + ret = c_clock_getres(clk_id, tp) + if ret != 0: + raise exception_from_errno(space.w_IOError) + return space.wrap(tp.c_tv_sec + tp.c_tv_nsec * 1e-9) diff --git a/pypy/module/__pypy__/test/test_time.py b/pypy/module/__pypy__/test/test_time.py new file mode 100644 --- /dev/null +++ b/pypy/module/__pypy__/test/test_time.py @@ -0,0 +1,26 @@ +import py + +from pypy.module.__pypy__.interp_time import HAS_CLOCK_GETTIME + + +class AppTestTime(object): + def setup_class(cls): + if not HAS_CLOCK_GETTIME: + py.test.skip("need time.clock_gettime") + + def test_clock_realtime(self): + from __pypy__ import time + res = time.clock_gettime(time.CLOCK_REALTIME) + assert isinstance(res, float) + + def test_clock_monotonic(self): + from __pypy__ import time + a = time.clock_gettime(time.CLOCK_MONOTONIC) + b = time.clock_gettime(time.CLOCK_MONOTONIC) + assert a <= b + + def test_clock_getres(self): + from __pypy__ import time + res = time.clock_getres(time.CLOCK_REALTIME) + assert res > 0.0 + assert res <= 1.0 _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit