Author: Amaury Forgeot d'Arc <[email protected]>
Branch: missing-os-functions
Changeset: r62195:f0b5c549aea8
Date: 2013-03-08 00:11 +0100
http://bitbucket.org/pypy/pypy/changeset/f0b5c549aea8/

Log:    Move WEXITSTATUS &co out of ll_os.py. Looks much simpler like this.

diff --git a/pypy/module/posix/__init__.py b/pypy/module/posix/__init__.py
--- a/pypy/module/posix/__init__.py
+++ b/pypy/module/posix/__init__.py
@@ -1,10 +1,8 @@
 # Package initialisation
 from pypy.interpreter.mixedmodule import MixedModule
-from rpython.rtyper.module.ll_os import RegisterOs
 from rpython.rlib import rposix
 
 import os, sys
-exec 'import %s as posix' % os.name
 
 # this is the list of function which is *not* present in the posix module of
 # IronPython 2.6, and that we want to ignore for now
@@ -144,9 +142,8 @@
         interpleveldefs['pathconf_names'] = 'space.wrap(os.pathconf_names)'
 
     # Macros for process exit statuses: WIFEXITED &co
-    # XXX HAVE_SYS_WAIT_H
-    for name in RegisterOs.w_star:
-        if hasattr(posix, name):
+    if rposix.HAVE_WAIT:
+        for name in rposix.wait_macros:
             interpleveldefs[name] = 'interp_posix.' + name
 
     def __init__(self, space, w_name):
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
@@ -3,9 +3,9 @@
 from rpython.rlib.objectmodel import specialize
 from rpython.rlib.rarithmetic import r_longlong
 from rpython.rlib.unroll import unrolling_iterable
+from rpython.tool.sourcetools import func_renamer
 from pypy.interpreter.error import OperationError, wrap_oserror, wrap_oserror2
 from pypy.interpreter.error import operationerrfmt
-from rpython.rtyper.module.ll_os import RegisterOs
 from rpython.rtyper.module import ll_os_stat
 from rpython.rtyper.lltypesystem import rffi, lltype
 from rpython.rtyper.tool import rffi_platform
@@ -1079,23 +1079,21 @@
         raise wrap_oserror(space, e)
     return space.w_None
 
-def declare_new_w_star(name):
-    if name in RegisterOs.w_star_returning_int:
-        @unwrap_spec(status=c_int)
-        def WSTAR(space, status):
-            return space.wrap(getattr(os, name)(status))
-    else:
-        @unwrap_spec(status=c_int)
-        def WSTAR(space, status):
-            return space.newbool(getattr(os, name)(status))
-    WSTAR.__doc__ = getattr(os, name).__doc__
-    WSTAR.func_name = name
-    return WSTAR
+def declare_wait_macro(name, return_bool=False):
+    @unwrap_spec(status=c_int)
+    @func_renamer(name)
+    def wait_macro(space, status):
+        result = getattr(rposix, name)(status)
+        if return_bool:
+            return space.newbool(result)
+        else:
+            return space.wrap(result)
+    return wait_macro
 
-for name in RegisterOs.w_star:
-    if hasattr(os, name):
-        func = declare_new_w_star(name)
-        globals()[name] = func
+for name in rposix.wait_macros_returning_int:
+    globals()[name] = declare_wait_macro(name)
+for name in rposix.wait_macros_returning_bool:
+    globals()[name] = declare_wait_macro(name, return_bool=True)
 
 @unwrap_spec(fd=c_int)
 def ttyname(space, fd):
diff --git a/pypy/module/posix/test/test_posix2.py 
b/pypy/module/posix/test/test_posix2.py
--- a/pypy/module/posix/test/test_posix2.py
+++ b/pypy/module/posix/test/test_posix2.py
@@ -7,7 +7,6 @@
 from pypy.tool.pytest.objspace import gettestobjspace
 from pypy.conftest import pypydir
 from rpython.rlib import rposix
-from rpython.rtyper.module.ll_os import RegisterOs
 from rpython.translator.c.test.test_extfunc import need_sparse_files
 import os
 import py
@@ -566,7 +565,7 @@
         raises(TypeError, "os.utime('xxx', 3)")
         raises(OSError, "os.utime('somefilewhichihopewouldneverappearhere', 
None)")
 
-    for name in RegisterOs.w_star:
+    for name in rposix.wait_macros:
         if hasattr(os, name):
             values = [0, 1, 127, 128, 255]
             code = py.code.Source("""
diff --git a/rpython/rlib/rposix.py b/rpython/rlib/rposix.py
--- a/rpython/rlib/rposix.py
+++ b/rpython/rlib/rposix.py
@@ -1,6 +1,6 @@
 import os
 from rpython.rtyper.lltypesystem.rffi import CConstant, CExternVariable, INT
-from rpython.rtyper.lltypesystem import ll2ctypes, rffi
+from rpython.rtyper.lltypesystem import ll2ctypes, lltype, rffi
 from rpython.rtyper.tool import rffi_platform
 from rpython.translator.tool.cbuild import ExternalCompilationInfo
 from rpython.rlib.rarithmetic import intmask
@@ -166,13 +166,23 @@
             pass
 
 # Expose posix functions
-def external(name, args, result):
-    return rffi.llexternal(name, args, result,
-                           compilation_info=CConfig._compilation_info_)
+def external(name, args, result, **kwargs):
+    return rffi.llexternal(
+        name, args, result,
+        compilation_info=CConfig._compilation_info_, **kwargs)
 
 c_fchmod = external('fchmod', [rffi.INT, rffi.MODE_T], rffi.INT)
 c_fchown = external('fchown', [rffi.INT, rffi.INT, rffi.INT], rffi.INT)
 
+if HAVE_WAIT:
+    wait_macros_returning_int = ['WEXITSTATUS', 'WSTOPSIG', 'WTERMSIG']
+    wait_macros_returning_bool = ['WCOREDUMP', 'WIFCONTINUED', 'WIFSTOPPED',
+                                 'WIFSIGNALED', 'WIFEXITED']
+    wait_macros = wait_macros_returning_int + wait_macros_returning_bool
+    for name in wait_macros:
+        globals()[name] = external(name, [lltype.Signed], lltype.Signed,
+                                   macro=True)
+
 
 #___________________________________________________________________
 # Wrappers around posix functions, that accept either strings, or
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
@@ -211,29 +211,6 @@
                 '#include <unistd.h>',
                 [])
 
-        # we need an indirection via c functions to get macro calls working on 
llvm XXX still?
-        if hasattr(os, 'WCOREDUMP'):
-            decl_snippet = """
-            %(ret_type)s pypy_macro_wrapper_%(name)s (int status);
-            """
-            def_snippet = """
-            %(ret_type)s pypy_macro_wrapper_%(name)s (int status) {
-            return %(name)s(status);
-            }
-            """
-            decls = []
-            defs = []
-            for name in self.w_star:
-                data = {'ret_type': 'int', 'name': name}
-                decls.append((decl_snippet % data).strip())
-                defs.append((def_snippet % data).strip())
-
-            self.compilation_info = self.compilation_info.merge(
-                ExternalCompilationInfo(
-                post_include_bits = decls,
-                separate_module_sources = ["\n".join(defs)]
-            ))
-
     # a simple, yet useful factory
     def extdef_for_os_function_returning_int(self, name, **kwds):
         c_func = self.llexternal(name, [], rffi.INT, **kwds)
@@ -1742,45 +1719,6 @@
         from rpython.rtyper.module import ll_os_stat
         return ll_os_stat.register_stat_variant('lstat', traits)
 
-    # ------------------------------- os.W* ---------------------------------
-
-    w_star = ['WCOREDUMP', 'WIFCONTINUED', 'WIFSTOPPED',
-              'WIFSIGNALED', 'WIFEXITED', 'WEXITSTATUS',
-              'WSTOPSIG', 'WTERMSIG']
-    # last 3 are returning int
-    w_star_returning_int = dict.fromkeys(w_star[-3:])
-
-
-
-    def declare_new_w_star(self, name):
-        """ stupid workaround for the python late-binding
-        'feature'
-        """
-
-        def fake(status):
-            return int(getattr(os, name)(status))
-        fake.func_name = 'fake_' + name
-
-        os_c_func = self.llexternal("pypy_macro_wrapper_" + name,
-                                    [lltype.Signed], lltype.Signed,
-                                    _callable=fake)
-    
-        if name in self.w_star_returning_int:
-            def llimpl(status):
-                return os_c_func(status)
-            resulttype = int
-        else:
-            def llimpl(status):
-                return bool(os_c_func(status))
-            resulttype = bool
-        llimpl.func_name = name + '_llimpl'
-        return extdef([int], resulttype, "ll_os." + name,
-                      llimpl=llimpl)
-
-    for name in w_star:
-        locals()['register_w_' + name] = registering_if(os, name)(
-            lambda self, xname=name : self.declare_new_w_star(xname))
-
     @registering_if(os, 'ttyname')
     def register_os_ttyname(self):
         os_ttyname = self.llexternal('ttyname', [lltype.Signed], rffi.CCHARP)
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to