[pypy-commit] pypy more-rposix: os.waitpid(). Not tested on Windows

amauryfa noreply at buildbot.pypy.org
Fri Nov 7 20:09:27 CET 2014


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: more-rposix
Changeset: r74384:fe529528d3fa
Date: 2014-11-07 19:52 +0100
http://bitbucket.org/pypy/pypy/changeset/fe529528d3fa/

Log:	os.waitpid(). Not tested on Windows

diff --git a/rpython/rlib/rposix.py b/rpython/rlib/rposix.py
--- a/rpython/rlib/rposix.py
+++ b/rpython/rlib/rposix.py
@@ -13,6 +13,7 @@
 from rpython.rlib import debug, rthread
 
 _WIN32 = sys.platform.startswith('win')
+_CYGWIN = sys.platform == 'cygwin'
 UNDERSCORE_ON_WIN32 = '_' if _WIN32 else ''
 
 class CConstantErrno(CConstant):
@@ -178,7 +179,6 @@
 c_dup2 = external(UNDERSCORE_ON_WIN32 + 'dup2', [rffi.INT, rffi.INT], rffi.INT)
 c_open = external(UNDERSCORE_ON_WIN32 + 'open',
                   [rffi.CCHARP, rffi.INT, rffi.MODE_T], rffi.INT)
-c_getlogin = external('getlogin', [], rffi.CCHARP, releasegil=False)
 
 # Win32 Unicode functions
 c_wopen = external(UNDERSCORE_ON_WIN32 + 'wopen',
@@ -460,6 +460,39 @@
     finally:
         lltype.free(master_p, flavor='raw')
 
+if _WIN32:
+    # emulate waitpid() with the _cwait() of Microsoft's compiler
+    c__cwait = external('_cwait',
+                        [rffi.INTP, rffi.PID_T, rffi.INT], rffi.PID_T)
+    def c_waitpid(pid, status_p, options):
+        result = c__cwait(status_p, pid, options)
+        # shift the status left a byte so this is more
+        # like the POSIX waitpid
+        status_p[0] = rffi.cast(rffi.INT, intmask(status_p[0]) << 8)
+        return result
+elif _CYGWIN:
+    c_waitpid = external('cygwin_waitpid',
+                         [rffi.PID_T, rffi.INTP, rffi.INT], rffi.PID_T)
+else:
+    c_waitpid = external('waitpid',
+                         [rffi.PID_T, rffi.INTP, rffi.INT], rffi.PID_T)
+
+ at replace_os_function('waitpid')
+def waitpid(pid, options):
+    status_p = lltype.malloc(rffi.INTP.TO, 1, flavor='raw')
+    status_p[0] = rffi.cast(rffi.INT, 0)
+    try:
+        result = handle_posix_error('waitpid',
+                                    c_waitpid(pid, status_p, options))
+        status = intmask(status_p[0])
+        return (result, status)
+    finally:
+        lltype.free(status_p, flavor='raw')
+
+c_getlogin = external('getlogin', [], rffi.CCHARP, releasegil=False)
+c_getloadavg = external('getloadavg', 
+                        [rffi.CArrayPtr(lltype.Float), rffi.INT], rffi.INT)
+
 @replace_os_function('getlogin')
 def getlogin():
     result = c_getlogin()
@@ -467,6 +500,16 @@
         raise OSError(get_errno(), "getlogin failed")
     return rffi.charp2str(result)
 
+ at replace_os_function('getloadavg')
+def getloadavg():
+    load = lltype.malloc(rffi.CArrayPtr(lltype.Float).TO, 3, flavor='raw')
+    try:
+        r = c_getloadavg(load, 3)
+        if r != 3:
+            raise OSError
+        return (load[0], load[1], load[2])
+    finally:
+        lltype.free(load, flavor='raw')
 
 #___________________________________________________________________
 
diff --git a/rpython/rtyper/module/ll_os.py b/rpython/rtyper/module/ll_os.py
--- a/rpython/rtyper/module/ll_os.py
+++ b/rpython/rtyper/module/ll_os.py
@@ -235,22 +235,6 @@
         return extdef([int], int, llimpl=c_func_llimpl,
                       export_name='ll_os.ll_os_' + name)
 
-    @registering_if(os, 'getloadavg')
-    def register_os_getloadavg(self):
-        AP = rffi.CArrayPtr(lltype.Float)
-        c_getloadavg = self.llexternal('getloadavg', [AP, rffi.INT], rffi.INT)
-
-        def getloadavg_llimpl():
-            load = lltype.malloc(AP.TO, 3, flavor='raw')
-            r = c_getloadavg(load, 3)
-            result_tuple = load[0], load[1], load[2]
-            lltype.free(load, flavor='raw')
-            if r != 3:
-                raise OSError
-            return result_tuple
-        return extdef([], (float, float, float),
-                      "ll_os.ll_getloadavg", llimpl=getloadavg_llimpl)
-
 # ------------------------------- os.read -------------------------------
 
     @registering(os.read)
@@ -678,50 +662,6 @@
                       "ll_os.ll_os_readlink",
                       llimpl=os_readlink_llimpl)
 
-    @registering(os.waitpid)
-    def register_os_waitpid(self):
-        if sys.platform.startswith('win'):
-            # emulate waitpid() with the _cwait() of Microsoft's compiler
-            os__cwait = self.llexternal('_cwait',
-                                        [rffi.INTP, rffi.PID_T, rffi.INT],
-                                        rffi.PID_T)
-            def os_waitpid(pid, status_p, options):
-                result = os__cwait(status_p, pid, options)
-                # shift the status left a byte so this is more
-                # like the POSIX waitpid
-                tmp = rffi.cast(rffi.SIGNED, status_p[0])
-                tmp <<= 8
-                status_p[0] = rffi.cast(rffi.INT, tmp)
-                return result
-        else:
-            # Posix
-            if _CYGWIN:
-                os_waitpid = self.llexternal('cygwin_waitpid',
-                                             [rffi.PID_T, rffi.INTP, rffi.INT],
-                                             rffi.PID_T)
-            else:
-                os_waitpid = self.llexternal('waitpid',
-                                             [rffi.PID_T, rffi.INTP, rffi.INT],
-                                             rffi.PID_T)
-
-        def os_waitpid_llimpl(pid, options):
-            status_p = lltype.malloc(rffi.INTP.TO, 1, flavor='raw')
-            status_p[0] = rffi.cast(rffi.INT, 0)
-            result = os_waitpid(rffi.cast(rffi.PID_T, pid),
-                                status_p,
-                                rffi.cast(rffi.INT, options))
-            result = rffi.cast(lltype.Signed, result)
-            status = status_p[0]
-            lltype.free(status_p, flavor='raw')
-            if result == -1:
-                raise OSError(rposix.get_errno(), "os_waitpid failed")
-            return (rffi.cast(lltype.Signed, result),
-                    rffi.cast(lltype.Signed, status))
-
-        return extdef([int, int], (int, int),
-                      "ll_os.ll_os_waitpid",
-                      llimpl=os_waitpid_llimpl)
-
     @registering(os.isatty)
     def register_os_isatty(self):
         os_isatty = self.llexternal(UNDERSCORE_ON_WIN32 + 'isatty',


More information about the pypy-commit mailing list