aboutsummaryrefslogtreecommitdiff
blob: df692355dee1e76825860f7e94d130522e6ad4ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import os
import sys
from pytest import raises, skip

python = sys.executable

if hasattr(os, "execv"):
    def test_execv():
        if not hasattr(os, "fork"):
            skip("Need fork() to test execv()")
        pid = os.fork()
        if pid == 0:
            os.execv("/usr/bin/env", ["env", python, "-c",
                     ("fid = open('onefile', 'w'); "
                      "fid.write('1'); "
                      "fid.close()")])
        os.waitpid(pid, 0)
        assert open("onefile").read() == "1"
        os.unlink("onefile")

    def test_execv_raising():
        with raises(OSError):
            os.execv("saddsadsadsadsa", ["saddsadsasaddsa"])

    def test_execv_no_args():
        with raises(ValueError):
            os.execv("notepad", [])
        # PyPy needs at least one arg, CPython 2.7 is fine without
        with raises(ValueError):
            os.execve("notepad", [], {})

    def test_execv_raising2():
        for n in 3, [3, "a"]:
            with raises(TypeError):
                os.execv("xxx", n)

    def test_execv_unicode():
        if not hasattr(os, "fork"):
            skip("Need fork() to test execv()")
        try:
            output = u"caf\xe9 \u1234\n".encode(sys.getfilesystemencoding())
        except UnicodeEncodeError:
            skip("encoding not good enough")
        pid = os.fork()
        if pid == 0:
            os.execv(u"/bin/sh", ["sh", "-c",
                                  u"echo caf\xe9 \u1234 > onefile"])
        os.waitpid(pid, 0)
        with open("onefile") as fid:
            assert fid.read() == output
        os.unlink("onefile")

    def test_execve():
        if not hasattr(os, "fork"):
            skip("Need fork() to test execve()")
        pid = os.fork()
        if pid == 0:
            os.execve("/usr/bin/env", ["env", python, "-c",
                      ("import os; fid = open('onefile', 'w'); "
                       "fid.write(os.environ['ddd']); "
                       "fid.close()")],
                      {'ddd': 'xxx'})
        os.waitpid(pid, 0)
        assert open("onefile").read() == "xxx"
        os.unlink("onefile")

    def test_execve_unicode():
        if not hasattr(os, "fork"):
            skip("Need fork() to test execve()")
        try:
            output = u"caf\xe9 \u1234\n".encode(sys.getfilesystemencoding())
        except UnicodeEncodeError:
            skip("encoding not good enough")
        pid = os.fork()
        if pid == 0:
            os.execve(u"/bin/sh", ["sh", "-c",
                                   u"echo caf\xe9 \u1234 > onefile"],
                      {'ddd': 'xxx'})
        os.waitpid(pid, 0)
        with open("onefile") as fid:
            assert fid.read() == output
        os.unlink("onefile")
    pass  # <- please, inspect.getsource(), don't crash

if hasattr(os, "spawnv"):
    def test_spawnv():
        ret = os.spawnv(os.P_WAIT, python,
                        [python, '-c', 'raise(SystemExit(42))'])
        assert ret == 42

if hasattr(os, "spawnve"):
    def test_spawnve():
        env = {'PATH': os.environ['PATH'], 'FOOBAR': '42'}
        cmd = "raise(SystemExit(int(__import__('os').environ['FOOBAR'])))"
        ret = os.spawnve(os.P_WAIT, python, [python, '-c', cmd], env)
        assert ret == 42