diff options
Diffstat (limited to 'environment.py')
-rw-r--r-- | environment.py | 35 |
1 files changed, 20 insertions, 15 deletions
diff --git a/environment.py b/environment.py index 309a1e3..03fa21b 100644 --- a/environment.py +++ b/environment.py @@ -152,22 +152,13 @@ class CCompiler(): pass return p.returncode == 0 - def sizeof(self, element, prefix): - templ = '''#include<stdio.h> -%s - -int main(int argc, char **argv) { - printf("%%ld\\n", (long)(sizeof(%s))); - return 0; -}; -''' + def run(self, code): (fd, srcname) = tempfile.mkstemp(suffix='.'+self.default_suffix) - exename = srcname + '.exe' # Is guaranteed to be executable on every platform. os.close(fd) ofile = open(srcname, 'w') - code = templ % (prefix, element) ofile.write(code) ofile.close() + exename = srcname + '.exe' # Is guaranteed to be executable on every platform. commands = self.get_exelist() commands.append(srcname) commands += self.get_output_flags(exename) @@ -175,13 +166,27 @@ int main(int argc, char **argv) { p.communicate() os.remove(srcname) if p.returncode != 0: - raise EnvironmentException('Could not compile sizeof test.') + return RunResult(False) pe = subprocess.Popen(exename, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - so = pe.communicate()[0] + (so, se) = pe.communicate() os.remove(exename) - if pe.returncode != 0: + return RunResult(True, pe.returncode, so.decode(), se.decode()) + + def sizeof(self, element, prefix): + templ = '''#include<stdio.h> +%s + +int main(int argc, char **argv) { + printf("%%ld\\n", (long)(sizeof(%s))); + return 0; +}; +''' + res = self.run(templ % (prefix, element)) + if not res.compiled: + raise EnvironmentException('Could not compile sizeof test.') + if res.returncode != 0: raise EnvironmentException('Could not run sizeof test binary.') - return int(so.decode()) + return int(res.stdout) class CPPCompiler(CCompiler): def __init__(self, exelist): |