Ticket #815: process_815.py

File process_815.py, 2.2 KB (added by therve, 8 years ago)
Line 
1Index: twisted/internet/process.py
2===================================================================
3--- twisted/internet/process.py (revision 18372)
4+++ twisted/internet/process.py (working copy)
5@@ -266,6 +266,18 @@
6         nuances of setXXuid on UNIX: it will assume that either your effective
7         or real UID is 0.)
8         """
9+        if not isinstance(args, (tuple, list)):
10+            raise TypeError("Arguments must be a tuple or list")
11+        for arg in args:
12+            if not isinstance(arg, (str, unicode)):
13+               raise TypeError("Arguments contain a non-string value")
14+        if environment is not None:
15+            for key, val in environment.items():
16+                if not isinstance(key, (str, unicode)):
17+                    raise TypeError("Environment contains a non-string key")
18+                if not isinstance(val, (str, unicode)):
19+                    raise TypeError("Environment contains a non-string value")
20+
21         if not proto:
22             assert 'r' not in childFDs.values()
23             assert 'w' not in childFDs.values()
24Index: twisted/test/test_process.py
25===================================================================
26--- twisted/test/test_process.py        (revision 18372)
27+++ twisted/test/test_process.py        (working copy)
28@@ -288,6 +288,22 @@
29             self.assertEquals(recvdArgs, args)
30         return d.addCallback(processEnded)
31 
32+    def testWrongArguments(self):
33+        """
34+        Test invalid arguments to spawnProcess: arguments and environment must
35+        only contains string or unicode.
36+        """
37+        exe = sys.executable
38+        scriptPath = util.sibpath(__file__, "process_tester.py")
39+        d = defer.Deferred()
40+        p = TestProcessProtocol()
41+        p.deferred = d
42+        self.assertRaises(TypeError, reactor.spawnProcess, p, exe,
43+                          [exe, "-u", scriptPath], env={"foo": 2})
44+        self.assertRaises(TypeError, reactor.spawnProcess, p, exe,
45+                          [exe, "-u", scriptPath], env={3: "bar"})
46+        self.assertRaises(TypeError, reactor.spawnProcess, p, exe,
47+                          [exe, 2], env=None)
48 
49 class TwoProcessProtocol(protocol.ProcessProtocol):
50     num = -1