[pypy-commit] pypy follow_symlinks: Separate implementations of execv() and execve() since their signature and validation logic differ

rlamy pypy.commits at gmail.com
Fri Apr 1 15:36:46 EDT 2016


Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: follow_symlinks
Changeset: r83505:9dc07d8a2654
Date: 2016-04-01 20:35 +0100
http://bitbucket.org/pypy/pypy/changeset/9dc07d8a2654/

Log:	Separate implementations of execv() and execve() since their
	signature and validation logic differ

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
@@ -1151,7 +1151,23 @@
         path: path of executable file
         args: iterable of strings
     """
-    execve(space, w_path, w_args, None)
+    command = space.fsencode_w(w_path)
+    try:
+        args_w = space.unpackiterable(w_args)
+        if len(args_w) < 1:
+            raise oefmt(space.w_ValueError,
+                "execv() arg 2 must not be empty")
+        args = [space.fsencode_w(w_arg) for w_arg in args_w]
+    except OperationError, e:
+        if not e.match(space, space.w_TypeError):
+            raise
+        raise oefmt(space.w_TypeError,
+            "execv() arg 2 must be an iterable of strings")
+    try:
+        os.execv(command, args)
+    except OSError as e:
+        raise wrap_oserror(space, e)
+
 
 def _env2interp(space, w_env):
     env = {}
@@ -1161,7 +1177,8 @@
         env[space.fsencode_w(w_key)] = space.fsencode_w(w_value)
     return env
 
-def execve(space, w_path, w_args, w_env):
+
+def execve(space, w_path, w_argv, w_environment):
     """execve(path, args, env)
 
 Execute a path with arguments and environment, replacing current process.
@@ -1175,29 +1192,16 @@
   If this functionality is unavailable, using it raises NotImplementedError.
     """
     command = space.fsencode_w(w_path)
+    if not (space.isinstance_w(w_argv, space.w_list)
+            or space.isinstance_w(w_argv, space.w_tuple)):
+        raise oefmt(space.w_TypeError,
+            "execve: argv must be a tuple or a list")
+    args = [space.fsencode_w(w_arg) for w_arg in space.unpackiterable(w_argv)]
+    env = _env2interp(space, w_environment)
     try:
-        args_w = space.unpackiterable(w_args)
-        if len(args_w) < 1:
-            w_msg = space.wrap("execv() must have at least one argument")
-            raise OperationError(space.w_ValueError, w_msg)
-        args = [space.fsencode_w(w_arg) for w_arg in args_w]
-    except OperationError, e:
-        if not e.match(space, space.w_TypeError):
-            raise
-        msg = "execv() arg 2 must be an iterable of strings"
-        raise OperationError(space.w_TypeError, space.wrap(str(msg)))
-    #
-    if w_env is None:    # when called via execv() above
-        try:
-            os.execv(command, args)
-        except OSError, e:
-            raise wrap_oserror(space, e)
-    else:
-        env = _env2interp(space, w_env)
-        try:
-            os.execve(command, args, env)
-        except OSError, e:
-            raise wrap_oserror(space, e)
+        os.execve(command, args, env)
+    except OSError, e:
+        raise wrap_oserror(space, e)
 
 @unwrap_spec(mode=int, path='fsencode')
 def spawnv(space, mode, path, w_args):
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
@@ -415,7 +415,6 @@
         def test_execv_no_args(self):
             os = self.posix
             raises(ValueError, os.execv, "notepad", [])
-            raises(ValueError, os.execve, "notepad", [], {})
 
         def test_execv_raising2(self):
             os = self.posix


More information about the pypy-commit mailing list