Debian Codegraph @

pycparser223/tests/test_util.py

raw symbols SCIP
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 def _bytes2str(b):
37 return b.decode('latin-1')
38
39 def run_exe(exe_path, args=[], echo=False):
40 """ Runs the given executable as a subprocess, given the
41 list of arguments. Captures its return code (rc) and stdout and
42 returns a tuple: rc, stdout, stderr
43 """
44 popen_cmd = [exe_path] + args
45 if os.path.splitext(exe_path)[1] == '.py':
46 popen_cmd.insert(0, sys.executable)
47 if echo:
48 print('[cmd]', ' '.join(popen_cmd))
49 proc = subprocess.Popen(popen_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
50 stdout, stderr = proc.communicate()
51 return proc.returncode, _bytes2str(stdout), _bytes2str(stderr)