[pypy-svn] r36565 - in pypy/dist/pypy: rpython rpython/module rpython/module/test translator/c translator/c/src

fijal at codespeak.net fijal at codespeak.net
Fri Jan 12 13:45:18 CET 2007


Author: fijal
Date: Fri Jan 12 13:45:16 2007
New Revision: 36565

Modified:
   pypy/dist/pypy/rpython/extfunc.py
   pypy/dist/pypy/rpython/extfunctable.py
   pypy/dist/pypy/rpython/module/ll_os.py
   pypy/dist/pypy/rpython/module/support.py
   pypy/dist/pypy/rpython/module/test/test_ll_os.py
   pypy/dist/pypy/rpython/rtyper.py
   pypy/dist/pypy/translator/c/extfunc.py
   pypy/dist/pypy/translator/c/src/ll_os.h
Log:
Add attempt to implement os.execv to a branch. Looks like it should be working, but no proper support from rctypes.


Modified: pypy/dist/pypy/rpython/extfunc.py
==============================================================================
--- pypy/dist/pypy/rpython/extfunc.py	(original)
+++ pypy/dist/pypy/rpython/extfunc.py	Fri Jan 12 13:45:16 2007
@@ -2,12 +2,14 @@
 from pypy.rpython.extregistry import ExtRegistryEntry
 from pypy.rpython.lltypesystem.lltype import typeOf
 from pypy.objspace.flow.model import Constant
+from pypy.annotation.model import unionof
 
 class ExtFuncEntry(ExtRegistryEntry):
     def compute_result_annotation(self, *args_s):
         assert len(args_s) == len(self.signature_args),\
                "Argument number mismatch"
         for arg, expected in zip(args_s, self.signature_args):
+            arg = unionof(arg, expected)
             assert expected.contains(arg)
 
         for type_system in ['lltype', 'ootype']:
@@ -17,6 +19,7 @@
                 pbc = self.bookkeeper.immutablevalue(impl.im_func)
                 s_result = self.bookkeeper.emulate_pbc_call(key, pbc,
                        self.signature_args)
+                s_result = unionof(s_result, self.signature_result)
                 assert self.signature_result.contains(s_result)
         
         return self.signature_result

Modified: pypy/dist/pypy/rpython/extfunctable.py
==============================================================================
--- pypy/dist/pypy/rpython/extfunctable.py	(original)
+++ pypy/dist/pypy/rpython/extfunctable.py	Fri Jan 12 13:45:16 2007
@@ -235,9 +235,9 @@
     declare(os.spawnv,    int,            'll_os/spawnv')
 if hasattr(os, 'waitpid'):
     declare(os.waitpid ,  waitpidannotation, 'll_os/waitpid')
-if hasattr(os, 'execv'):
-    declare(os.execv, noneannotation, 'll_os/execv')
-    declare(os.execve, noneannotation, 'll_os/execve')
+#if hasattr(os, 'execv'):
+#    declare(os.execv, noneannotation, 'll_os/execv')
+#    declare(os.execve, noneannotation, 'll_os/execve')
 
 declare(os.path.exists, bool        , 'll_os_path/exists')
 declare(os.path.isdir, bool         , 'll_os_path/isdir')

Modified: pypy/dist/pypy/rpython/module/ll_os.py
==============================================================================
--- pypy/dist/pypy/rpython/module/ll_os.py	(original)
+++ pypy/dist/pypy/rpython/module/ll_os.py	Fri Jan 12 13:45:16 2007
@@ -14,13 +14,39 @@
 # and buffer preparation stuff is not useful.
 
 import os, errno
-from pypy.rpython.module.support import ll_strcpy, _ll_strfill, ll_execve, \
-    from_rdict
+from pypy.rpython.module.support import ll_strcpy, _ll_strfill
 from pypy.rpython.module.support import to_opaque_object, from_opaque_object
 from pypy.rlib import ros
 from pypy.rlib.rarithmetic import r_longlong
 from pypy.tool.staticmethods import ClassMethods
 import stat
+from pypy.rpython.extfunc import ExtFuncEntry
+from pypy.annotation.model import SomeString, s_ImpossibleValue
+from pypy.annotation.listdef import s_list_of_strings
+import ctypes
+import pypy.rpython.rctypes.implementation
+from pypy.rpython.rctypes.tool.libc import libc
+from pypy.rpython.rctypes.aerrno import geterrno
+
+if hasattr(os, 'execv'):
+
+    os_execv = libc.execv
+    os_execv.argtypes = [ctypes.c_char_p, ctypes.POINTER(ctypes.c_char_p)]
+    os_execv.restype = ctypes.c_int
+
+    class ExecvFuncEntry(ExtFuncEntry):
+        _about_ = os.execv
+        name = "ll_os_execv"
+        signature_args = [SomeString(), s_list_of_strings]
+        signature_result = s_ImpossibleValue
+
+        def lltypeimpl(path, args):
+            typ = ctypes.c_char_p * (len(args) + 1)
+            array = typ()
+            for num in range(len(args)):
+                array[num] = args[num]
+            os_execv(path, array)
+            raise OSError(geterrno())
 
 class BaseOS:
     __metaclass__ = ClassMethods
@@ -101,13 +127,13 @@
         return os.system(cls.from_rstr(cmd))
     ll_os_system.suggested_primitive = True
 
-    def ll_os_execv(cls, cmd, args):
-        os.execv(cmd, args)
-    ll_os_execv.suggested_primitive = True
-
-    def ll_os_execve(cls, cmd, args, env):
-        env_list = from_rdict(env)
-        ll_execve(cmd, args, env_list)
+    #def ll_os_execv(cls, cmd, args):
+    #    os.execv(cmd, args)
+    #ll_os_execv.suggested_primitive = True
+
+    #def ll_os_execve(cls, cmd, args, env):
+    #    env_list = from_rdict(env)
+    #    ll_execve(cmd, args, env_list)
 
     def ll_os_unlink(cls, path):
         os.unlink(cls.from_rstr(path))

Modified: pypy/dist/pypy/rpython/module/support.py
==============================================================================
--- pypy/dist/pypy/rpython/module/support.py	(original)
+++ pypy/dist/pypy/rpython/module/support.py	Fri Jan 12 13:45:16 2007
@@ -15,16 +15,6 @@
 from pypy.annotation.model import SomeString
 import os
 
-# This whole mess is just to make annotator happy...
-list_repr = ListRepr(None, string_repr)
-list_repr.setup()
-LIST = list_repr.lowleveltype.TO
-tuple_repr = TupleRepr(None, [string_repr, string_repr])
-tuple_repr.setup()
-tuple_list_repr = ListRepr(None, tuple_repr)
-tuple_list_repr.setup()
-LIST_TUPLE = tuple_list_repr.lowleveltype.TO
-
 # utility conversion functions
 class LLSupport:
     _mixin_ = True
@@ -46,32 +36,6 @@
             return ''.join([rs.chars[i] for i in range(len(rs.chars))])
     from_rstr = staticmethod(from_rstr)
 
-def from_rdict(rs):
-    ritems = ll_kvi(rs, LIST_TUPLE, dum_items)
-    res = ll_newlist(LIST, 0)
-    index = 0
-    while index < ritems.ll_length():
-        ritem = ll_getitem_fast(ritems, index)
-        ll_append(res, LLSupport.to_rstr("%s=%s" % (LLSupport.from_rstr(ritem.item0),
-            LLSupport.from_rstr(ritem.item1))))
-        index += 1
-    return res
-    
-def to_rdict(rs):
-    d = {}
-    index = 0
-    while index < rs.ll_length():
-        item = LLSupport.from_rstr(ll_getitem_fast(rs, index))
-        key, value = item.split("=")
-        d[key] = value
-        index += 1
-    return d
-
-def ll_execve(cmd, args, env_list):
-    env = to_rdict(env_list)
-    os.execve(cmd, args, env)
-ll_execve.suggested_primitive = True
-
 class OOSupport:
     _mixin_ = True
 

Modified: pypy/dist/pypy/rpython/module/test/test_ll_os.py
==============================================================================
--- pypy/dist/pypy/rpython/module/test/test_ll_os.py	(original)
+++ pypy/dist/pypy/rpython/module/test/test_ll_os.py	Fri Jan 12 13:45:16 2007
@@ -1,7 +1,7 @@
 import os
 from pypy.tool.udir import udir
 from pypy.rpython.lltypesystem.module.ll_os import Implementation as impl
-
+import sys
 
 def test_access():
     filename = str(udir.join('test_access.txt'))
@@ -112,3 +112,22 @@
     compared_with = os.listdir(dirname)
     compared_with.sort()
     assert result == compared_with
+
+if hasattr(os, 'execv'):
+    from pypy.rpython.extregistry import lookup
+    os_execv = lookup(os.execv).lltypeimpl.im_func
+    
+    def test_execv():
+        filename = str(udir.join('test_execv_ctypes.txt'))
+
+        progname = str(sys.executable)
+        l = ['', '']
+        l[0] = progname
+        l[1] = "-c"
+        l.append('open("%s","w").write("1")' % filename)
+        pid = os.fork()
+        if pid == 0:
+            os_execv(progname, l)
+        else:
+            os.waitpid(pid, 0)
+        assert open(filename).read() == "1"

Modified: pypy/dist/pypy/rpython/rtyper.py
==============================================================================
--- pypy/dist/pypy/rpython/rtyper.py	(original)
+++ pypy/dist/pypy/rpython/rtyper.py	Fri Jan 12 13:45:16 2007
@@ -919,6 +919,7 @@
 from pypy.rpython import rstr, rdict, rlist
 from pypy.rpython import rclass, rbuiltin, rpbc, rspecialcase
 from pypy.rpython import rexternalobj
+from pypy.rpython import rgeneric
 from pypy.rpython import rptr
 from pypy.rpython import raddress # memory addresses
 from pypy.rpython.ootypesystem import rootype

Modified: pypy/dist/pypy/translator/c/extfunc.py
==============================================================================
--- pypy/dist/pypy/translator/c/extfunc.py	(original)
+++ pypy/dist/pypy/translator/c/extfunc.py	Fri Jan 12 13:45:16 2007
@@ -5,9 +5,8 @@
 from pypy.rpython.lltypesystem.rstr import STR, mallocstr
 from pypy.rpython.lltypesystem import rstr
 from pypy.rpython.lltypesystem import rlist
-from pypy.rpython.module import ll_time, ll_math
+from pypy.rpython.module import ll_time, ll_math, ll_os
 from pypy.rpython.module import ll_stackless, ll_stack
-from pypy.rpython.module.support import ll_execve
 from pypy.rpython.lltypesystem.module.ll_os import STAT_RESULT, PIPE_RESULT
 from pypy.rpython.lltypesystem.module.ll_os import WAITPID_RESULT
 from pypy.rpython.lltypesystem.module.ll_os import Implementation as impl
@@ -64,8 +63,6 @@
     impl.ll_os_spawnv.im_func:  'LL_os_spawnv',
     impl.ll_os_waitpid.im_func: 'LL_os_waitpid',
     impl.ll_os__exit.im_func:   'LL_os__exit',
-    impl.ll_os_execv.im_func:   'LL_os_execv',
-    ll_execve:                  'LL_os_execve',
     ll_time.ll_time_clock: 'LL_time_clock',
     ll_time.ll_time_sleep: 'LL_time_sleep',
     ll_time.ll_time_time:  'LL_time_time',

Modified: pypy/dist/pypy/translator/c/src/ll_os.h
==============================================================================
--- pypy/dist/pypy/translator/c/src/ll_os.h	(original)
+++ pypy/dist/pypy/translator/c/src/ll_os.h	Fri Jan 12 13:45:16 2007
@@ -81,11 +81,7 @@
 void LL_os_symlink(RPyString * path1, RPyString * path2);
 long LL_readlink_into(RPyString *path, RPyString *buffer);
 long LL_os_fork(void);
-#ifdef HAVE_RPY_LIST_OF_STRING     /* argh */
-#ifdef HAVE_EXECV
-void LL_os_execv(RPyString *cmd, RPyListOfString *args);
-void LL_os_execve(RPyString *cmd, RPyListOfString *args, RPyListOfString *env);
-#endif
+#if defined(HAVE_SPAWNV) && defined(HAVE_RPY_LIST_OF_STRING) /* argh */
 long LL_os_spawnv(int mode, RPyString *path, RPyListOfString *args);
 #endif
 RPyWAITPID_RESULT* LL_os_waitpid(long pid, long options);
@@ -404,39 +400,6 @@
 }
 #endif
 
-#if defined(HAVE_EXECV) && defined(HAVE_RPY_LIST_OF_STRING)
-char** get_slist(RPyListOfString *args)
-{
-	int i, nargs = _RPyListOfString_Length(args);
-  char **slist = malloc((nargs+1) * sizeof(char*));
-	if (slist) {
-		for (i=0; i<nargs; i++)
-			slist[i] = RPyString_AsString(_RPyListOfString_GetItem(args, i));
-		slist[nargs] = NULL;
-    return slist;
-  } else {
-    RPYTHON_RAISE_OSERROR(errno);
-    return NULL;
-  }
-}
-
-void LL_os_execv(RPyString *cmd, RPyListOfString *args) {
-  char **slist = get_slist(args);
-  execv(RPyString_AsString(cmd), slist);
-  /* should never return */
-  RPYTHON_RAISE_OSERROR(errno);
-}
-
-void LL_os_execve(RPyString *cmd, RPyListOfString *args, RPyListOfString *env)
-{
-  char **arglist = get_slist(args);
-  char **envlist = get_slist(env);
-  execve(RPyString_AsString(cmd), arglist, envlist);
-  /* should never return */
-  RPYTHON_RAISE_OSERROR(errno);
-}
-#endif
-
 /*
   The following code is only generated if spawnv exists and
   if RPyListOfString exists. The latter is a bit tricky:



More information about the Pypy-commit mailing list