[pypy-svn] r45986 - in pypy/branch/pypy-more-rtti-inprogress: module/posix module/posix/test rpython/module rpython/module/test

exarkun at codespeak.net exarkun at codespeak.net
Sat Aug 25 17:05:20 CEST 2007


Author: exarkun
Date: Sat Aug 25 17:05:20 2007
New Revision: 45986

Modified:
   pypy/branch/pypy-more-rtti-inprogress/module/posix/__init__.py
   pypy/branch/pypy-more-rtti-inprogress/module/posix/interp_posix.py
   pypy/branch/pypy-more-rtti-inprogress/module/posix/test/test_posix2.py
   pypy/branch/pypy-more-rtti-inprogress/rpython/module/ll_os.py
   pypy/branch/pypy-more-rtti-inprogress/rpython/module/test/test_ll_os.py
   pypy/branch/pypy-more-rtti-inprogress/rpython/module/test/test_posix.py
Log:
implement os.times for posix

Modified: pypy/branch/pypy-more-rtti-inprogress/module/posix/__init__.py
==============================================================================
--- pypy/branch/pypy-more-rtti-inprogress/module/posix/__init__.py	(original)
+++ pypy/branch/pypy-more-rtti-inprogress/module/posix/__init__.py	Sat Aug 25 17:05:20 2007
@@ -32,6 +32,7 @@
     'dup'       : 'interp_posix.dup',
     'dup2'      : 'interp_posix.dup2',
     'access'    : 'interp_posix.access',
+    'times'     : 'interp_posix.times',
     'system'    : 'interp_posix.system',
     'unlink'    : 'interp_posix.unlink',
     'remove'    : 'interp_posix.remove',

Modified: pypy/branch/pypy-more-rtti-inprogress/module/posix/interp_posix.py
==============================================================================
--- pypy/branch/pypy-more-rtti-inprogress/module/posix/interp_posix.py	(original)
+++ pypy/branch/pypy-more-rtti-inprogress/module/posix/interp_posix.py	Sat Aug 25 17:05:20 2007
@@ -187,6 +187,21 @@
         return space.wrap(ok)
 access.unwrap_spec = [ObjSpace, str, int]
 
+
+def times(space):
+    """
+    times() -> (utime, stime, cutime, cstime, elapsed_time)
+
+    Return a tuple of floating point numbers indicating process times.
+    """
+    try:
+        times = os.times()
+    except OSError, e:
+        raise wrap_oserror(space, e)
+    else:
+        return space.wrap(times)
+times.unwrap_spec = [ObjSpace]
+
 def system(space, cmd):
     """Execute the command (a string) in a subshell."""
     try:

Modified: pypy/branch/pypy-more-rtti-inprogress/module/posix/test/test_posix2.py
==============================================================================
--- pypy/branch/pypy-more-rtti-inprogress/module/posix/test/test_posix2.py	(original)
+++ pypy/branch/pypy-more-rtti-inprogress/module/posix/test/test_posix2.py	Sat Aug 25 17:05:20 2007
@@ -146,6 +146,19 @@
             assert not posix.access(pdir, posix.X_OK)
 
 
+    def test_times(self):
+        """
+        posix.times() should return a five-tuple giving float-representations
+        (seconds, effectively) of the four fields from the underlying struct
+        tms and the return value.
+        """
+        result = self.posix.times()
+        assert isinstance(result, tuple)
+        assert len(result) == 5
+        for value in result:
+            assert isinstance(value, float)
+
+
     def test_strerror(self):
         assert isinstance(self.posix.strerror(0), str)
         assert isinstance(self.posix.strerror(1), str)

Modified: pypy/branch/pypy-more-rtti-inprogress/rpython/module/ll_os.py
==============================================================================
--- pypy/branch/pypy-more-rtti-inprogress/rpython/module/ll_os.py	(original)
+++ pypy/branch/pypy-more-rtti-inprogress/rpython/module/ll_os.py	Sat Aug 25 17:05:20 2007
@@ -17,14 +17,47 @@
 from pypy.rpython.lltypesystem import rffi
 from pypy.rpython.lltypesystem.rffi import platform
 from pypy.rpython.lltypesystem import lltype
+from pypy.rpython.tool import rffi_platform
 posix = __import__(os.name)
 
+class CConfig:
+    """
+    Definitions for platform integration.
+
+    Note: this must be processed through rffi_platform.configure() to provide
+    usable objects.  For example::
+
+        CLOCK_T = rffi_platform.configure(CConfig)['CLOCK_T']
+        register(function, [CLOCK_T], ...)
+
+    """
+    includes = ['sys/times.h']
+    # XXX argh, argh, argh, should be automatic
+    _header_ = "\n".join(["#include <%s>" % name for name in includes])
+
+    CLOCK_T = rffi_platform.SimpleType('clock_t', rffi.INT)
+
+    TMS = rffi_platform.Struct(
+        'struct tms', [('tms_utime', rffi.INT),
+                       ('tms_stime', rffi.INT),
+                       ('tms_cutime', rffi.INT),
+                       ('tms_cstime', rffi.INT)])
+
+
 class RegisterOs(BaseLazyRegistering):
     UNISTD_INCL = ['unistd.h', 'sys/types.h']
 
     def __init__(self):
-        pass   # XXX <arigo> fijal: why do I need this?
-    
+        # Grab all of the platform type definitions and stash them as instance
+        # attributes (this is quite a hack, what a lazy programmer I must be).
+        self.__dict__.update(rffi_platform.configure(CConfig))
+        # Make some pointer types too
+        self.TMSP = lltype.Ptr(self.TMS)
+        # Here is a random extra platform parameter which is important.
+        # Strictly speaking, this should probably be retrieved at runtime, not
+        # at translation time.
+        self.CLOCK_TICKS_PER_SECOND = float(os.sysconf('SC_CLK_TCK'))
+
     # a simple, yet usefull factory
     def extdef_for_os_function_returning_int(self, name, **kwds):
         c_func = rffi.llexternal(name, [], rffi.INT, **kwds)
@@ -159,6 +192,31 @@
                       "ll_os.ll_os_utime",
                       llimpl=os_utime_llimpl)
 
+
+    @registering(os.times)
+    def register_os_times(self):
+        # XXX sys/times.h isn't so portable, maybe.
+        os_times = rffi.llexternal('times', [self.TMSP], self.CLOCK_T,
+                                   includes=['sys/times.h'])
+
+        def times_lltypeimpl():
+            l_tmsbuf = lltype.malloc(self.TMSP.TO, flavor='raw')
+            try:
+                result = os_times(l_tmsbuf)
+                if result == -1:
+                    raise OSError(rffi.get_errno(), "times failed")
+                return (
+                    l_tmsbuf.c_tms_utime / self.CLOCK_TICKS_PER_SECOND,
+                    l_tmsbuf.c_tms_stime / self.CLOCK_TICKS_PER_SECOND,
+                    l_tmsbuf.c_tms_cutime / self.CLOCK_TICKS_PER_SECOND,
+                    l_tmsbuf.c_tms_cstime / self.CLOCK_TICKS_PER_SECOND,
+                    result / self.CLOCK_TICKS_PER_SECOND)
+            finally:
+                lltype.free(l_tmsbuf, flavor='raw')
+        self.register(os.times, [], (float, float, float, float, float),
+                      "ll_os.ll_times", llimpl=times_lltypeimpl)
+
+
     @registering_if(os, 'setsid')
     def register_os_setsid(self):
         os_setsid = rffi.llexternal('setsid', [], rffi.PID_T,

Modified: pypy/branch/pypy-more-rtti-inprogress/rpython/module/test/test_ll_os.py
==============================================================================
--- pypy/branch/pypy-more-rtti-inprogress/rpython/module/test/test_ll_os.py	(original)
+++ pypy/branch/pypy-more-rtti-inprogress/rpython/module/test/test_ll_os.py	Sat Aug 25 17:05:20 2007
@@ -19,6 +19,19 @@
         assert result == os.access(filename, mode)
 
 
+def test_times():
+    """
+    posix.times should compile as an RPython function and should return a
+    five-tuple giving float-representations (seconds, effectively) of the four
+    fields from the underlying struct tms and the return value.
+    """
+    times = compile(lambda: os.times(), ())()
+    assert isinstance(times, tuple)
+    assert len(times) == 5
+    for value in times:
+        assert isinstance(value, float)
+
+
 def test_getcwd():
     data = getllimpl(os.getcwd)()
     assert data == os.getcwd()

Modified: pypy/branch/pypy-more-rtti-inprogress/rpython/module/test/test_posix.py
==============================================================================
--- pypy/branch/pypy-more-rtti-inprogress/rpython/module/test/test_posix.py	(original)
+++ pypy/branch/pypy-more-rtti-inprogress/rpython/module/test/test_posix.py	Sat Aug 25 17:05:20 2007
@@ -35,6 +35,17 @@
             stat0 = getattr(func, 'item%d' % i)
             assert stat0 == stat[i]
 
+
+    def test_times(self):
+        import py; py.test.skip("llinterp does not like tuple returns")
+        from pypy.rpython.test.test_llinterp import interpret
+        times = interpret(lambda: posix.times(), ())
+        assert isinstance(times, tuple)
+        assert len(times) == 5
+        for value in times:
+            assert isinstance(value, int)
+
+
     def test_lseek(self):
         def f(fi,pos):
             posix.lseek(fi,pos,0)



More information about the Pypy-commit mailing list