[pypy-svn] r35226 - in pypy/dist/pypy: rpython rpython/module translator/c translator/c/src translator/c/test
fijal at codespeak.net
fijal at codespeak.net
Sun Dec 3 20:39:43 CET 2006
Author: fijal
Date: Sun Dec 3 20:39:32 2006
New Revision: 35226
Modified:
pypy/dist/pypy/rpython/extfunctable.py
pypy/dist/pypy/rpython/module/ll_os.py
pypy/dist/pypy/rpython/module/support.py
pypy/dist/pypy/translator/c/extfunc.py
pypy/dist/pypy/translator/c/src/ll_os.h
pypy/dist/pypy/translator/c/test/test_extfunc.py
Log:
implemented os.execve and added some mess to rpython/module/support.py. It really need new attempt of doing stuff (to kill all the crap which is not run, but only annotated and still causes problems).
Modified: pypy/dist/pypy/rpython/extfunctable.py
==============================================================================
--- pypy/dist/pypy/rpython/extfunctable.py (original)
+++ pypy/dist/pypy/rpython/extfunctable.py Sun Dec 3 20:39:32 2006
@@ -235,6 +235,7 @@
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')
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 Sun Dec 3 20:39:32 2006
@@ -14,7 +14,8 @@
# and buffer preparation stuff is not useful.
import os, errno
-from pypy.rpython.module.support import ll_strcpy, _ll_strfill
+from pypy.rpython.module.support import ll_strcpy, _ll_strfill, ll_execve, \
+ from_rdict
from pypy.rpython.module.support import to_opaque_object, from_opaque_object
from pypy.rlib import ros
from pypy.rlib.rarithmetic import r_longlong
@@ -100,6 +101,10 @@
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))
ll_os_unlink.suggested_primitive = True
Modified: pypy/dist/pypy/rpython/module/support.py
==============================================================================
--- pypy/dist/pypy/rpython/module/support.py (original)
+++ pypy/dist/pypy/rpython/module/support.py Sun Dec 3 20:39:32 2006
@@ -2,10 +2,30 @@
from pypy.rpython.ootypesystem import ootype
from pypy.rpython import extfunctable
from pypy.rpython.lltypesystem.lltype import \
- GcStruct, Signed, Array, Char, Ptr, malloc
+ GcStruct, Signed, Array, Char, Ptr, malloc, GcArray
+from pypy.rpython.rlist import ll_append
+from pypy.rpython.lltypesystem.rlist import ll_newlist, ListRepr,\
+ ll_getitem_fast
+from pypy.rpython.lltypesystem.rstr import string_repr
+from pypy.rpython.lltypesystem.rdict import ll_newdict, DictRepr, dum_items,\
+ ll_kvi, dum_keys, ll_dict_getitem, ll_dict_setitem
+from pypy.rpython.lltypesystem.rstr import StringRepr
+from pypy.rpython.lltypesystem.rtuple import TupleRepr
+from pypy.annotation.dictdef import DictKey, DictValue
+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
@@ -17,7 +37,7 @@
for i in range(len(s)):
p.chars[i] = s[i]
return p
- to_rstr = staticmethod(to_rstr)
+ to_rstr = staticmethod(to_rstr)
def from_rstr(rs):
if not rs: # null pointer
@@ -26,6 +46,31 @@
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/translator/c/extfunc.py
==============================================================================
--- pypy/dist/pypy/translator/c/extfunc.py (original)
+++ pypy/dist/pypy/translator/c/extfunc.py Sun Dec 3 20:39:32 2006
@@ -7,11 +7,13 @@
from pypy.rpython.lltypesystem import rlist
from pypy.rpython.module import ll_time, ll_math, ll_strtod
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
from pypy.rpython.lltypesystem.module import ll_math as ll_math2
from pypy.rpython.lltypesystem.module import ll_strtod as ll_strtod2
+from pypy.rlib import ros
try:
from pypy.module.thread.rpython import ll_thread
@@ -61,6 +63,7 @@
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 Sun Dec 3 20:39:32 2006
@@ -80,7 +80,10 @@
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
long LL_os_spawnv(int mode, RPyString *path, RPyListOfString *args);
#endif
RPyWAITPID_RESULT* LL_os_waitpid(long pid, long options);
@@ -391,15 +394,34 @@
#endif
#if defined(HAVE_EXECV) && defined(HAVE_RPY_LIST_OF_STRING)
-void LL_os_execv(RPyString *cmd, RPyListOfString *args) {
+char** get_slist(RPyListOfString *args)
+{
int i, nargs = args->l_length;
char **slist = malloc((nargs+1) * sizeof(char*));
if (slist) {
for (i=0; i<nargs; i++)
slist[i] = RPyString_AsString(args->l_items->items[i]);
slist[nargs] = NULL;
- execv(RPyString_AsString(cmd), slist);
- } /* should never return */
+ 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
Modified: pypy/dist/pypy/translator/c/test/test_extfunc.py
==============================================================================
--- pypy/dist/pypy/translator/c/test/test_extfunc.py (original)
+++ pypy/dist/pypy/translator/c/test/test_extfunc.py Sun Dec 3 20:39:32 2006
@@ -5,7 +5,6 @@
from pypy.translator.c.test.test_genc import compile
from pypy.translator.c.extfunc import EXTERNALS
from pypy.rlib import ros
-from pypy.translator.stackless.test.test_transform import one
def test_all_suggested_primitives():
for modulename in ['ll_math', 'll_os', 'll_os_path', 'll_time']:
@@ -703,3 +702,33 @@
func()
assert open(filename).read() == "1"
+ def test_execv_raising():
+ def does_stuff():
+ l = []
+ l.append("asddsadw32eewdfwqdqwdqwd")
+ os.execv(l[0], l)
+
+ func = compile(does_stuff, [])
+ py.test.raises(OSError, "func()")
+
+ def test_execve():
+ filename = str(udir.join('test_execve.txt'))
+ def does_stuff():
+ progname = str(sys.executable)
+ l = []
+ l.append(progname)
+ l.append("-c")
+ l.append('import os; open("%s", "w").write(os.environ["STH"])' % filename)
+ env = {}
+ env["STH"] = "42"
+ env["sthelse"] = "a"
+ pid = os.fork()
+ if pid == 0:
+ os.execve(progname, l, env)
+ else:
+ os.waitpid(pid, 0)
+
+ func = compile(does_stuff, [])
+ func()
+ assert open(filename).read() == "42"
+
More information about the Pypy-commit
mailing list