[pypy-commit] pypy more-rposix: os.pipe(), os.chown()

amauryfa noreply at buildbot.pypy.org
Sat Nov 8 23:44:52 CET 2014


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: more-rposix
Changeset: r74405:70d839558bf3
Date: 2014-11-08 18:23 +0100
http://bitbucket.org/pypy/pypy/changeset/70d839558bf3/

Log:	os.pipe(), os.chown()

diff --git a/rpython/rlib/rposix.py b/rpython/rlib/rposix.py
--- a/rpython/rlib/rposix.py
+++ b/rpython/rlib/rposix.py
@@ -736,6 +736,46 @@
     finally:
         lltype.free(status_p, flavor='raw')
 
+if _WIN32:
+    CreatePipe = external('CreatePipe', [rwin32.LPHANDLE,
+                                         rwin32.LPHANDLE,
+                                         rffi.VOIDP,
+                                         rwin32.DWORD],
+                          rwin32.BOOL)
+    c_open_osfhandle = external('_open_osfhandle', [rffi.INTPTR_T,
+                                                    rffi.INT],
+                                rffi.INT)
+else:
+    INT_ARRAY_P = rffi.CArrayPtr(rffi.INT)
+    c_pipe = external('pipe', [INT_ARRAY_P], rffi.INT)
+
+ at replace_os_function('pipe')
+def pipe():
+    if _WIN32:
+        pread  = lltype.malloc(rwin32.LPHANDLE.TO, 1, flavor='raw')
+        pwrite = lltype.malloc(rwin32.LPHANDLE.TO, 1, flavor='raw')
+        try:
+            if not CreatePipe(
+                    pread, pwrite, lltype.nullptr(rffi.VOIDP.TO), 0):
+                raise WindowsError(rwin32.GetLastError(), "CreatePipe failed")
+            hread = rffi.cast(rffi.INTPTR_T, pread[0])
+            hwrite = rffi.cast(rffi.INTPTR_T, pwrite[0])
+        finally:
+            lltype.free(pwrite, flavor='raw')
+            lltype.free(pread, flavor='raw')
+        fdread = _open_osfhandle(hread, 0)
+        fdwrite = _open_osfhandle(hwrite, 1)
+        return (fdread, fdwrite)
+    else:
+        filedes = lltype.malloc(INT_ARRAY_P.TO, 2, flavor='raw')
+        try:
+            handle_posix_error('pipe', c_pipe(filedes))
+            return (widen(filedes[0]), widen(filedes[1]))
+        finally:
+            lltype.free(filedes, flavor='raw')
+
+#___________________________________________________________________
+
 c_getlogin = external('getlogin', [], rffi.CCHARP, releasegil=False)
 c_getloadavg = external('getloadavg', 
                         [rffi.CArrayPtr(lltype.Float), rffi.INT], rffi.INT)
@@ -760,6 +800,24 @@
 
 #___________________________________________________________________
 
+c_chown = external('chown', [rffi.CCHARP, rffi.INT, rffi.INT], rffi.INT)
+c_lchown = external('lchown', [rffi.CCHARP, rffi.INT, rffi.INT], rffi.INT)
+c_fchown = external('fchown', [rffi.INT, rffi.INT, rffi.INT], rffi.INT)
+
+ at replace_os_function('chown')
+def chown(path, uid, gid):
+    handle_posix_error('chown', c_chown(path, uid, gid))
+
+ at replace_os_function('lchown')
+def lchown(path, uid, gid):
+    handle_posix_error('lchown', c_lchown(path, uid, gid))
+
+ at replace_os_function('fchown')
+def fchown(fd, uid, gid):
+    handle_posix_error('fchown', c_fchown(fd, uid, gid))
+
+#___________________________________________________________________
+
 UTIMBUFP = lltype.Ptr(UTIMBUF)
 c_utime = external('utime', [rffi.CCHARP, UTIMBUFP], rffi.INT)
 if HAVE_UTIMES:
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
@@ -173,97 +173,6 @@
                 separate_module_sources = ["\n".join(defs)]
             ))
 
-    @registering(os.pipe)
-    def register_os_pipe(self):
-        # we need a different approach on Windows and on Posix
-        if sys.platform.startswith('win'):
-            from rpython.rlib import rwin32
-            CreatePipe = self.llexternal('CreatePipe', [rwin32.LPHANDLE,
-                                                        rwin32.LPHANDLE,
-                                                        rffi.VOIDP,
-                                                        rwin32.DWORD],
-                                         rwin32.BOOL)
-            _open_osfhandle = self.llexternal('_open_osfhandle', [rffi.INTPTR_T,
-                                                                  rffi.INT],
-                                              rffi.INT)
-            null = lltype.nullptr(rffi.VOIDP.TO)
-
-            def os_pipe_llimpl():
-                pread  = lltype.malloc(rwin32.LPHANDLE.TO, 1, flavor='raw')
-                pwrite = lltype.malloc(rwin32.LPHANDLE.TO, 1, flavor='raw')
-                ok = CreatePipe(pread, pwrite, null, 0)
-                if ok:
-                    error = 0
-                else:
-                    error = rwin32.GetLastError()
-                hread = rffi.cast(rffi.INTPTR_T, pread[0])
-                hwrite = rffi.cast(rffi.INTPTR_T, pwrite[0])
-                lltype.free(pwrite, flavor='raw')
-                lltype.free(pread, flavor='raw')
-                if error:
-                    raise WindowsError(error, "os_pipe failed")
-                fdread = _open_osfhandle(hread, 0)
-                fdwrite = _open_osfhandle(hwrite, 1)
-                return (fdread, fdwrite)
-
-        else:
-            INT_ARRAY_P = rffi.CArrayPtr(rffi.INT)
-            os_pipe = self.llexternal('pipe', [INT_ARRAY_P], rffi.INT)
-
-            def os_pipe_llimpl():
-                filedes = lltype.malloc(INT_ARRAY_P.TO, 2, flavor='raw')
-                error = rffi.cast(lltype.Signed, os_pipe(filedes))
-                read_fd = filedes[0]
-                write_fd = filedes[1]
-                lltype.free(filedes, flavor='raw')
-                if error != 0:
-                    raise OSError(rposix.get_errno(), "os_pipe failed")
-                return (rffi.cast(lltype.Signed, read_fd),
-                        rffi.cast(lltype.Signed, write_fd))
-
-        return extdef([], (int, int),
-                      "ll_os.ll_os_pipe",
-                      llimpl=os_pipe_llimpl)
-
-    @registering_if(os, 'chown')
-    def register_os_chown(self):
-        os_chown = self.llexternal('chown', [rffi.CCHARP, rffi.INT, rffi.INT],
-                                   rffi.INT)
-
-        def os_chown_llimpl(path, uid, gid):
-            res = os_chown(path, uid, gid)
-            if res == -1:
-                raise OSError(rposix.get_errno(), "os_chown failed")
-
-        return extdef([str0, int, int], None, "ll_os.ll_os_chown",
-                      llimpl=os_chown_llimpl)
-
-    @registering_if(os, 'lchown')
-    def register_os_lchown(self):
-        os_lchown = self.llexternal('lchown',[rffi.CCHARP, rffi.INT, rffi.INT],
-                                    rffi.INT)
-
-        def os_lchown_llimpl(path, uid, gid):
-            res = os_lchown(path, uid, gid)
-            if res == -1:
-                raise OSError(rposix.get_errno(), "os_lchown failed")
-
-        return extdef([str0, int, int], None, "ll_os.ll_os_lchown",
-                      llimpl=os_lchown_llimpl)
-
-    @registering_if(os, 'fchown')
-    def register_os_fchown(self):
-        os_fchown = self.llexternal('fchown',[rffi.INT, rffi.INT, rffi.INT],
-                                    rffi.INT)
-
-        def os_fchown_llimpl(fd, uid, gid):
-            res = os_fchown(fd, uid, gid)
-            if res == -1:
-                raise OSError(rposix.get_errno(), "os_fchown failed")
-
-        return extdef([int, int, int], None, "ll_os.ll_os_fchown",
-                      llimpl=os_fchown_llimpl)
-
     @registering_if(os, 'readlink')
     def register_os_readlink(self):
         os_readlink = self.llexternal('readlink',


More information about the pypy-commit mailing list