def test_gcc_exec(): f = localudir.join("x.c") f.write(""" #include <stdio.h> #include <test_gcc_exec.h> int main() { printf("%d\\n", ANSWER); return 0; } """) dir1 = localudir.join('test_gcc_exec_dir1').ensure(dir=1) dir2 = localudir.join('test_gcc_exec_dir2').ensure(dir=1) dir1.join('test_gcc_exec.h').write('#define ANSWER 3\n') dir2.join('test_gcc_exec.h').write('#define ANSWER 42\n') eci = ExternalCompilationInfo(include_dirs=[str(dir1)]) # remove cache path = cache_file_path([f], eci, 'build_executable_cache') if path.check(): path.remove() res = build_executable_cache([f], eci) assert res == "3\n" assert build_executable_cache([f], eci) == "3\n" eci2 = ExternalCompilationInfo(include_dirs=[str(dir2)]) assert build_executable_cache([f], eci2) == "42\n" f.write("#error BOOM\n") err = py.test.raises(CompilationError, build_executable_cache, [f], eci2) print '<<<' print err print '>>>'
def test_gcc_ask(): f = localudir.join("y.c") f.write(""" #include <stdio.h> #include <test_gcc_ask.h> int main() { printf("hello\\n"); return 0; } """) dir1 = localudir.join('test_gcc_ask_dir1').ensure(dir=1) dir2 = localudir.join('test_gcc_ask_dir2').ensure(dir=1) dir1.join('test_gcc_ask.h').write('/* hello world */\n') dir2.join('test_gcc_ask.h').write('#error boom\n') eci = ExternalCompilationInfo(include_dirs=[str(dir1)]) # remove cache path = cache_file_path([f], eci, 'try_compile_cache') if path.check(): path.remove() assert try_compile_cache([f], eci) assert try_compile_cache([f], eci) assert build_executable_cache([f], eci) == "hello\n" eci2 = ExternalCompilationInfo(include_dirs=[str(dir2)]) err = py.test.raises(CompilationError, try_compile_cache, [f], eci2) print '<<<' print err print '>>>'
def run_example_code(filepath, eci, ignore_errors=False): eci = eci.convert_sources_to_files() files = [filepath] output = build_executable_cache(files, eci, ignore_errors=ignore_errors) if not output.startswith('-+- '): raise Exception("run_example_code failed!\nlocals = %r" % (locals(), )) section = None for line in output.splitlines(): line = line.strip() if line.startswith('-+- '): # start of a new section section = {} elif line == '---': # section end assert section is not None yield section section = None elif line: assert section is not None key, value = line.split(': ') section[key] = int(value)
def run_example_code(filepath, eci, ignore_errors=False): eci = eci.convert_sources_to_files() files = [filepath] output = build_executable_cache(files, eci, ignore_errors=ignore_errors) if not output.startswith('-+- '): raise Exception("run_example_code failed!\nlocals = %r" % (locals(),)) section = None for line in output.splitlines(): line = line.strip() if line.startswith('-+- '): # start of a new section section = {} elif line == '---': # section end assert section is not None yield section section = None elif line: assert section is not None key, value = line.split(': ') section[key] = int(value)
def test_execute_code_show_runtime_error(): f = localudir.join('z.c') f.write(""" #include <stdio.h> int main() { fprintf(stderr, "hello\\n"); return 0; } """) for i in range(2): eci = ExternalCompilationInfo() oldstderr = sys.stderr try: sys.stderr = capture = cStringIO.StringIO() output = build_executable_cache([f], eci, True) finally: sys.stderr = oldstderr assert 'hello' in capture.getvalue() assert output == ''
def ask_gcc(question, add_source="", ignore_errors=False): from rpython.translator.platform import platform includes = ['stdlib.h', 'stdio.h', 'sys/types.h'] if platform.name != 'msvc': includes += ['inttypes.h'] include_string = "\n".join(["#include <%s>" % i for i in includes]) c_source = py.code.Source(''' // includes %s %s // checking code int main(void) { %s return (0); } ''' % (include_string, add_source, str(question))) c_file = udir.join("gcctest.c") c_file.write(str(c_source) + '\n') eci = ExternalCompilationInfo() return build_executable_cache([c_file], eci, ignore_errors=ignore_errors)
def ask_gcc(question, add_source="", ignore_errors=False): from rpython.translator.platform import platform includes = ['stdlib.h', 'stdio.h', 'sys/types.h'] if platform.name != 'msvc': includes += ['inttypes.h', 'stddef.h'] include_string = "\n".join(["#include <%s>" % i for i in includes]) c_source = py.code.Source(''' // includes %s %s // checking code int main(void) { %s return (0); } ''' % (include_string, add_source, str(question))) c_file = udir.join("gcctest.c") c_file.write(str(c_source) + '\n') eci = ExternalCompilationInfo() return build_executable_cache([c_file], eci, ignore_errors=ignore_errors)