[pypy-svn] r29721 - in pypy/dist/pypy/module/rctime: . test

rhymes at codespeak.net rhymes at codespeak.net
Fri Jul 7 10:51:31 CEST 2006


Author: rhymes
Date: Fri Jul  7 10:51:29 2006
New Revision: 29721

Modified:
   pypy/dist/pypy/module/rctime/__init__.py
   pypy/dist/pypy/module/rctime/app_time.py
   pypy/dist/pypy/module/rctime/interp_time.py
   pypy/dist/pypy/module/rctime/test/test_rctime.py
Log:
(rhymes, arre, sad): implement time.sleep() in POSIX


Modified: pypy/dist/pypy/module/rctime/__init__.py
==============================================================================
--- pypy/dist/pypy/module/rctime/__init__.py	(original)
+++ pypy/dist/pypy/module/rctime/__init__.py	Fri Jul  7 10:51:29 2006
@@ -18,5 +18,5 @@
     buildloaders = classmethod(buildloaders)
 
     appleveldefs = {
-        # 'floattime'    : 'app_time._floattime'
+        'sleep': 'app_time.sleep'    
     }

Modified: pypy/dist/pypy/module/rctime/app_time.py
==============================================================================
--- pypy/dist/pypy/module/rctime/app_time.py	(original)
+++ pypy/dist/pypy/module/rctime/app_time.py	Fri Jul  7 10:51:29 2006
@@ -0,0 +1,32 @@
+import os
+_POSIX = os.name == "posix"
+
+def _check_float(arg):
+    try:
+        float(arg)
+    except ValueError:
+        raise TypeError, "a float is required"
+
+def _float_sleep(secs):
+    import select
+    
+    if _POSIX:
+        select.select([], [], [], secs)
+        return
+    # elif _MS_WINDOWS:
+    #     msecs = secs * 1000.0
+    #     if msecs > float(sys.maxint * 2 - 1): # ULONG_MAX
+    #         raise OverflowError, "sleep length is too large"
+    #     lock.acquire()
+    #     ul_millis = c_ulong(long(msecs))
+    #     windll.kernel32.Sleep(ul_millis)
+    #     lock.release()
+    # else:
+    #     lock.acquire()
+    #     _libc.sleep.argtypes = [c_int]
+    #     _libc.sleep(int(secs))
+    #     lock.release()
+
+def sleep(secs):
+    _check_float(secs)
+    _float_sleep(secs)

Modified: pypy/dist/pypy/module/rctime/interp_time.py
==============================================================================
--- pypy/dist/pypy/module/rctime/interp_time.py	(original)
+++ pypy/dist/pypy/module/rctime/interp_time.py	Fri Jul  7 10:51:29 2006
@@ -22,20 +22,15 @@
     libc.gettimeofday.argtypes = [c_void_p, c_void_p]
     libc.gettimeofday.restype = c_int
     has_gettimeofday = True
-    
 
-# class _timeval(Structure):
-#     _fields_ = [("tv_sec", c_long),
-#                 ("tv_usec", c_long)]
 
 def _init_accept2dyear():
     return (1, 0)[bool(os.getenv("PYTHONY2K"))]
-    
 
 def _get_error_msg():
     errno = geterrno()
     return libc.strerror(errno)
-
+    
 def _floattime():
     """ _floattime() -> computes time since the Epoch for various platforms.
 
@@ -46,16 +41,16 @@
     ftime() has a resolution in milliseconds and it never fails
     time() has a resolution in seconds
     """
-    
+
     # if _MS_WINDOWS:
     #     return libc.time(None)
-    # 
+    #
     if has_gettimeofday:
         t = cConfig.timeval()
         if libc.gettimeofday(byref(t), c_void_p(None)) == 0:
             return float(t.tv_sec) + t.tv_usec * 0.000001
     return 0.0
-    
+
 
     # elif hasattr(_libc, "ftime"):
     #     t = _timeb()

Modified: pypy/dist/pypy/module/rctime/test/test_rctime.py
==============================================================================
--- pypy/dist/pypy/module/rctime/test/test_rctime.py	(original)
+++ pypy/dist/pypy/module/rctime/test/test_rctime.py	Fri Jul  7 10:51:29 2006
@@ -1,5 +1,4 @@
-import py
-#from pypy.module.rctime import interp_time as rctime
+from py.test import raises
 
 # def setup_module(mod):
 #     mod.t = rctime.time()
@@ -17,9 +16,10 @@
     #     assert isinstance(rctime.timezone, int)
     #     assert isinstance(rctime.tzname, tuple)
     # 
-    # def test_sleep():
-    #     py.test.raises(TypeError, rctime.sleep, "foo")
-    #     rctime.sleep(1.2345)
+    def test_sleep(self):
+        import rctime
+        raises(TypeError, rctime.sleep, "foo")
+        rctime.sleep(1.2345)
     # 
     # def test_clock():
     #     assert rctime.clock() != None



More information about the Pypy-commit mailing list