Debian Codegraph @

tests/test_util.py

raw
1 # ------------------------------------------------------------------------------
2 # pycparser: test_util.py
3 #
4 # Utility code for tests.
5 #
6 # Eli Bendersky [https://eli.thegreenplace.net/]
7 # This file contributed by [email protected]
8 # License: BSD
9 # ------------------------------------------------------------------------------
10 import os
11 import platform
12 import subprocess
13 import sys
14
15
16 def cpp_supported():
17 """Is cpp (the C preprocessor) supported as a native command?"""
18 return platform.system() == "Linux"
19
20
21 def cpp_path():
22 """Path to cpp command."""
23 if platform.system() == "Darwin":
24 return "gcc"
25 return "cpp"
26
27
28 def cpp_args(args=[]):
29 """Turn args into a suitable format for passing to cpp."""
30 if isinstance(args, str):
31 args = [args]
32 if platform.system() == "Darwin":
33 return ["-E"] + args
34 return args
35
36
37 def _bytes2str(b):
38 return b.decode("latin-1")
39
40
41 def run_exe(exe_path, args=[], echo=False):
42 """Runs the given executable as a subprocess, given the
43 list of arguments. Captures its return code (rc) and stdout and
44 returns a tuple: rc, stdout, stderr
45 """
46 popen_cmd = [exe_path] + args
47 if os.path.splitext(exe_path)[1] == ".py":
48 popen_cmd.insert(0, sys.executable)
49 if echo:
50 print("[cmd]", " ".join(popen_cmd))
51 proc = subprocess.Popen(popen_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
52 stdout, stderr = proc.communicate()
53 return proc.returncode, _bytes2str(stdout), _bytes2str(stderr)