[pypy-commit] pypy more-rposix: Port most remaining functions of ll_os.py

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


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

Log:	Port most remaining functions of ll_os.py

	To do: os.stat...

diff --git a/pypy/module/posix/interp_posix.py b/pypy/module/posix/interp_posix.py
--- a/pypy/module/posix/interp_posix.py
+++ b/pypy/module/posix/interp_posix.py
@@ -665,7 +665,7 @@
 def kill(space, pid, sig):
     "Kill a process with a signal."
     try:
-        rposix.os_kill(pid, sig)
+        rposix.kill(pid, sig)
     except OSError, e:
         raise wrap_oserror(space, e)
 
@@ -681,7 +681,7 @@
     """Abort the interpreter immediately.  This 'dumps core' or otherwise fails
 in the hardest way possible on the hosting operating system."""
     import signal
-    rposix.os_kill(os.getpid(), signal.SIGABRT)
+    rposix.kill(os.getpid(), signal.SIGABRT)
 
 @unwrap_spec(src='str0', dst='str0')
 def link(space, src, dst):
diff --git a/rpython/rlib/rposix.py b/rpython/rlib/rposix.py
--- a/rpython/rlib/rposix.py
+++ b/rpython/rlib/rposix.py
@@ -275,10 +275,6 @@
     return os.statvfs(_as_bytes(path))
 
 @specialize.argtype(0, 1)
-def symlink(src, dest):
-    os.symlink(_as_bytes(src), _as_bytes(dest))
-
- at specialize.argtype(0, 1)
 def putenv(name, value):
     os.environ[_as_bytes(name)] = _as_bytes(value)
 
@@ -286,12 +282,6 @@
 def unsetenv(name):
     del os.environ[_as_bytes(name)]
 
-if os.name == 'nt':
-    from rpython.rlib import rwin32
-    os_kill = rwin32.os_kill
-else:
-    os_kill = os.kill
-
 #___________________________________________________________________
 # Implementation of many posix functions.
 # They usually check the return value and raise an (RPython) OSError
@@ -957,6 +947,23 @@
 def mknod(path, mode, dev):
     handle_posix_error('mknod', c_mknod(_as_bytes0(path), mode, dev))
 
+c_link = external('link', [rffi.CCHARP, rffi.CCHARP], rffi.INT)
+c_symlink = external('symlink', [rffi.CCHARP, rffi.CCHARP], rffi.INT)
+
+ at replace_os_function('link')
+ at specialize.argtype(0, 1)
+def link(oldpath, newpath):
+    oldpath = _as_bytes0(oldpath)
+    newpath = _as_bytes0(newpath)
+    handle_posix_error('link', c_link(oldpath, newpath))
+
+ at replace_os_function('symlink')
+ at specialize.argtype(0, 1)
+def symlink(oldpath, newpath):
+    oldpath = _as_bytes0(oldpath)
+    newpath = _as_bytes0(newpath)
+    handle_posix_error('symlink', c_symlink(oldpath, newpath))
+
 c_umask = external(UNDERSCORE_ON_WIN32 + 'umask', [rffi.MODE_T], rffi.MODE_T)
 
 @replace_os_function('umask')
@@ -1145,6 +1152,64 @@
             lltype.free(pexit,   flavor='raw')
             lltype.free(pcreate, flavor='raw')
 
+c_kill = external('kill', [rffi.PID_T, rffi.INT], rffi.INT)
+c_killpg = external('killpg', [rffi.INT, rffi.INT], rffi.INT)
+c_exit = external('_exit', [rffi.INT], lltype.Void)
+c_nice = external('nice', [rffi.INT], rffi.INT)
+
+ at replace_os_function('kill')
+def kill(pid, sig):
+    if not _WIN32:
+        return handle_posix_error('kill', c_kill(pid, sig))
+    else:
+        if sig == rwin32.CTRL_C_EVENT or sig == rwin32.CTRL_BREAK_EVENT:
+            if rwin32.GenerateConsoleCtrlEvent(sig, pid) == 0:
+                raise rwin32.lastWindowsError('kill() failed generating event')
+            return
+        handle = rwin32.OpenProcess(rwin32.PROCESS_ALL_ACCESS, False, pid)
+        if not handle:
+            raise rwin32.lastWindowsError('kill() failed opening process')
+        try:
+            if rwin32.TerminateProcess(handle, sig) == 0:
+                raise rwin32.lastWindowsError(
+                    'kill() failed to terminate process')
+        finally:
+            rwin32.CloseHandle(handle)
+
+ at replace_os_function('killpg')
+def killpg(pgrp, sig):
+    return handle_posix_error('killpg', c_killpg(pgrp, sig))
+
+ at replace_os_function('_exit')
+def exit(status):
+    debug.debug_flush()
+    c_exit(status)
+
+ at replace_os_function('nice')
+def nice(inc):
+    # Assume that the system provides a standard-compliant version
+    # of nice() that returns the new priority.  Nowadays, FreeBSD
+    # might be the last major non-compliant system (xxx check me).
+    set_errno(0)
+    res = widen(c_nice(inc))
+    if res == -1:
+        err = get_errno()
+        if err != 0:
+            raise OSError(err, "os_nice failed")
+    return res
+
+c_ctermid = external('ctermid', [rffi.CCHARP], rffi.CCHARP)
+
+ at replace_os_function('ctermid')
+def ctermid():
+    return rffi.charp2str(c_ctermid(lltype.nullptr(rffi.CCHARP.TO)))
+
+c_tmpnam = external('tmpnam', [rffi.CCHARP], rffi.CCHARP)
+
+ at replace_os_function('tmpnam')
+def tmpnam():
+    return rffi.charp2str(c_tmpnam(lltype.nullptr(rffi.CCHARP.TO)))
+
 #___________________________________________________________________
 
 c_getpid = external('getpid', [], rffi.PID_T, releasegil=False)
diff --git a/rpython/rlib/rwin32.py b/rpython/rlib/rwin32.py
--- a/rpython/rlib/rwin32.py
+++ b/rpython/rlib/rwin32.py
@@ -389,18 +389,4 @@
     def GetConsoleOutputCP():
         return rffi.cast(lltype.Signed, _GetConsoleOutputCP())
 
-    def os_kill(pid, sig):
-        if sig == CTRL_C_EVENT or sig == CTRL_BREAK_EVENT:
-            if GenerateConsoleCtrlEvent(sig, pid) == 0:
-                raise lastWindowsError('os_kill failed generating event')
-            return
-        handle = OpenProcess(PROCESS_ALL_ACCESS, False, pid)
-        if handle == NULL_HANDLE:
-            raise lastWindowsError('os_kill failed opening process')
-        try:
-            if TerminateProcess(handle, sig) == 0:
-                raise lastWindowsError('os_kill failed to terminate process')
-        finally:
-            CloseHandle(handle)
-
     _wenviron_items, _wgetenv, _wputenv = make_env_impls(win32=True)
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,109 +173,6 @@
                 separate_module_sources = ["\n".join(defs)]
             ))
 
-    @registering_if(os, 'kill', sys.platform != 'win32')
-    def register_os_kill(self):
-        os_kill = self.llexternal('kill', [rffi.PID_T, rffi.INT],
-                                  rffi.INT)
-        def kill_llimpl(pid, sig):
-            res = rffi.cast(lltype.Signed, os_kill(rffi.cast(rffi.PID_T, pid),
-                                                   rffi.cast(rffi.INT, sig)))
-            if res < 0:
-                raise OSError(rposix.get_errno(), "os_kill failed")
-        return extdef([int, int], s_None, llimpl=kill_llimpl,
-                      export_name="ll_os.ll_os_kill")
-
-    @registering_if(os, 'killpg')
-    def register_os_killpg(self):
-        os_killpg = self.llexternal('killpg', [rffi.INT, rffi.INT],
-                                    rffi.INT)
-
-        def killpg_llimpl(pid, sig):
-            res = rffi.cast(lltype.Signed, os_killpg(rffi.cast(rffi.INT, pid),
-                                                     rffi.cast(rffi.INT, sig)))
-            if res < 0:
-                raise OSError(rposix.get_errno(), "os_killpg failed")
-
-        return extdef([int, int], s_None, llimpl=killpg_llimpl,
-                      export_name="ll_os.ll_os_killpg")
-
-    @registering_if(os, 'link')
-    def register_os_link(self):
-        os_link = self.llexternal('link', [rffi.CCHARP, rffi.CCHARP],
-                                  rffi.INT)
-
-        def link_llimpl(oldpath, newpath):
-            res = rffi.cast(lltype.Signed, os_link(oldpath, newpath))
-            if res < 0:
-                raise OSError(rposix.get_errno(), "os_link failed")
-
-        return extdef([str0, str0], s_None, llimpl=link_llimpl,
-                      export_name="ll_os.ll_os_link")
-
-    @registering_if(os, 'symlink')
-    def register_os_symlink(self):
-        os_symlink = self.llexternal('symlink', [rffi.CCHARP, rffi.CCHARP],
-                                     rffi.INT)
-
-        def symlink_llimpl(oldpath, newpath):
-            res = rffi.cast(lltype.Signed, os_symlink(oldpath, newpath))
-            if res < 0:
-                raise OSError(rposix.get_errno(), "os_symlink failed")
-
-        return extdef([str0, str0], s_None, llimpl=symlink_llimpl,
-                      export_name="ll_os.ll_os_symlink")
-
-    @registering(os._exit)
-    def register_os__exit(self):
-        from rpython.rlib import debug
-        os__exit = self.llexternal('_exit', [rffi.INT], lltype.Void)
-
-        def _exit_llimpl(status):
-            debug.debug_flush()
-            os__exit(rffi.cast(rffi.INT, status))
-
-        return extdef([int], s_None, llimpl=_exit_llimpl,
-                      export_name="ll_os.ll_os__exit")
-
-    @registering_if(os, 'nice')
-    def register_os_nice(self):
-        os_nice = self.llexternal('nice', [rffi.INT], rffi.INT)
-
-        def nice_llimpl(inc):
-            # Assume that the system provides a standard-compliant version
-            # of nice() that returns the new priority.  Nowadays, FreeBSD
-            # might be the last major non-compliant system (xxx check me).
-            rposix.set_errno(0)
-            res = rffi.cast(lltype.Signed, os_nice(inc))
-            if res == -1:
-                err = rposix.get_errno()
-                if err != 0:
-                    raise OSError(err, "os_nice failed")
-            return res
-
-        return extdef([int], int, llimpl=nice_llimpl,
-                      export_name="ll_os.ll_os_nice")
-
-    @registering_if(os, 'ctermid')
-    def register_os_ctermid(self):
-        os_ctermid = self.llexternal('ctermid', [rffi.CCHARP], rffi.CCHARP)
-
-        def ctermid_llimpl():
-            return rffi.charp2str(os_ctermid(lltype.nullptr(rffi.CCHARP.TO)))
-
-        return extdef([], str, llimpl=ctermid_llimpl,
-                      export_name="ll_os.ll_os_ctermid")
-
-    @registering_if(os, 'tmpnam')
-    def register_os_tmpnam(self):
-        os_tmpnam = self.llexternal('tmpnam', [rffi.CCHARP], rffi.CCHARP)
-
-        def tmpnam_llimpl():
-            return rffi.charp2str(os_tmpnam(lltype.nullptr(rffi.CCHARP.TO)))
-
-        return extdef([], str, llimpl=tmpnam_llimpl,
-                      export_name="ll_os.ll_os_tmpnam")
-
 # --------------------------- os.stat & variants ---------------------------
 
     @registering(os.fstat)
diff --git a/rpython/rtyper/module/test/test_ll_os.py b/rpython/rtyper/module/test/test_ll_os.py
--- a/rpython/rtyper/module/test/test_ll_os.py
+++ b/rpython/rtyper/module/test/test_ll_os.py
@@ -89,7 +89,7 @@
     posix = __import__(os.name)
     sysdrv = os.getenv('SystemDrive', 'C:')
     stuff = sysdrv + 'stuff'
-    data = getllimpl(posix._getfullpathname)(stuff)
+    data = rposix.getfullpathname(stuff)
     assert data == posix._getfullpathname(stuff)
     # the most intriguing failure of ntpath.py should not repeat, here:
     assert not data.endswith(stuff)
@@ -116,7 +116,7 @@
     pwd = os.getcwd()
     try:
         check_special_envvar()
-        getllimpl(os.chdir)('..')
+        rposix.chdir('..')
         assert os.getcwd() == os.path.dirname(pwd)
         check_special_envvar()
     finally:
@@ -124,20 +124,19 @@
 
 def test_mkdir():
     filename = str(udir.join('test_mkdir.dir'))
-    getllimpl(os.mkdir)(filename, 0)
-    exc = py.test.raises(OSError, getllimpl(os.mkdir), filename, 0)
+    rposix.mkdir(filename, 0)
+    exc = py.test.raises(OSError, rposix.mkdir, filename, 0)
     assert exc.value.errno == errno.EEXIST
     if sys.platform == 'win32':
         assert exc.type is WindowsError
 
 def test_strerror():
-    data = getllimpl(os.strerror)(2)
-    assert data == os.strerror(2)
+    assert rposix.strerror(2) == os.strerror(2)
 
 def test_system():
     filename = str(udir.join('test_system.txt'))
     arg = '%s -c "print 1+1" > %s' % (sys.executable, filename)
-    data = getllimpl(os.system)(arg)
+    data = rposix.system(arg)
     assert data == 0
     assert file(filename).read().strip() == '2'
     os.unlink(filename)
@@ -267,9 +266,6 @@
 
 
 def test_os_kill():
-    if not hasattr(os,'kill') or sys.platform == 'win32':
-        py.test.skip('No kill in os')
-    f = getllimpl(os.kill)
     import subprocess
     import signal
     proc = subprocess.Popen([sys.executable, "-c",
@@ -277,16 +273,12 @@
                          "time.sleep(10)",
                          ],
                         )
-    f(proc.pid, signal.SIGTERM)
+    rposix.kill(proc.pid, signal.SIGTERM)
     expected = -signal.SIGTERM
     assert proc.wait() == expected
 
 def test_isatty():
-    try:
-        f = getllimpl(os.isatty)
-    except:
-        py.test.skip('No isatty in os')
-    assert f(-1)  == False
+    assert rposix.isatty(-1) is False
 
 
 class TestOsExpect(ExpectTest):


More information about the pypy-commit mailing list