forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sys_executable.py
More file actions
62 lines (53 loc) · 2.09 KB
/
test_sys_executable.py
File metadata and controls
62 lines (53 loc) · 2.09 KB
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
import unittest
import subprocess
import sys
import os
# These should go into a test_sys.py file, but given these are the only
# ones being tested currently that seems unnecessary.
class SysExecutableTest(unittest.TestCase):
# This is a copy of test.test_sys.SysModuleTest.test_executable from cpython.
def cpython_tests(self):
# sys.executable should be absolute
self.assertEqual(os.path.abspath(sys.executable), sys.executable)
# Issue #7774: Ensure that sys.executable is an empty string if argv[0]
# has been set to a non existent program name and Python is unable to
# retrieve the real program name
# For a normal installation, it should work without 'cwd'
# argument. For test runs in the build directory, see #7774.
python_dir = os.path.dirname(os.path.realpath(sys.executable))
p = subprocess.Popen(
[
"nonexistent",
"-c",
'import sys; print(sys.executable.encode("ascii", "backslashreplace"))',
],
executable=sys.executable,
stdout=subprocess.PIPE,
cwd=python_dir,
)
stdout = p.communicate()[0]
executable = stdout.strip().decode("ASCII")
p.wait()
self.assertIn(
executable,
["b''", repr(sys.executable.encode("ascii", "backslashreplace"))],
)
def test_no_follow_symlink(self):
paths = [os.path.abspath("test_symlink"), "./test_symlink"]
for path in paths:
with self.subTest(path=path):
os.symlink(sys.executable, path)
command = [
path,
"-c",
"import sys; print(sys.executable, end='')",
]
try:
process = subprocess.run(command, capture_output=True)
finally:
os.remove(path)
self.assertEqual(
os.path.abspath(path), process.stdout.decode("utf-8")
)
if __name__ == "__main__":
unittest.main()