提交 543fca03 authored 作者: Olivier Delalleau's avatar Olivier Delalleau

Better error message when compilation fails

1. The g++ error message is now displayed in the exception text rather than hidden before the file content is printed (which made it very hard to notice). 2. If the call to Popen fails for some reason (e.g. because g++ is not found), the command line is printed to stderr so that the user has a better idea of what the problem may be.
上级 a35991d3
...@@ -1384,8 +1384,21 @@ def gcc_module_compile_str(module_name, src_code, location=None, include_dirs=[] ...@@ -1384,8 +1384,21 @@ def gcc_module_compile_str(module_name, src_code, location=None, include_dirs=[]
#print >> sys.stderr, 'COMPILING W CMD', cmd #print >> sys.stderr, 'COMPILING W CMD', cmd
_logger.debug('Running cmd: %s', ' '.join(cmd)) _logger.debug('Running cmd: %s', ' '.join(cmd))
p = subprocess.Popen(cmd) def print_command_line_error():
status = p.wait() # Print command line when a problem occurred.
print >> sys.stderr, ("Problem occurred during compilation with the "
"command line below:")
print >> sys.stderr, ' '.join(cmd)
try:
p = subprocess.Popen(cmd, stderr=subprocess.PIPE)
except Exception:
# An exception can occur here e.g. if `g++` is not found.
print_command_line_error()
raise
compile_stderr = p.communicate()[1]
status = p.returncode
if status: if status:
print '===============================' print '==============================='
...@@ -1393,8 +1406,9 @@ def gcc_module_compile_str(module_name, src_code, location=None, include_dirs=[] ...@@ -1393,8 +1406,9 @@ def gcc_module_compile_str(module_name, src_code, location=None, include_dirs=[]
#gcc put its messages to stderr, so we add ours now #gcc put its messages to stderr, so we add ours now
print >> sys.stderr, '%05i\t%s'%(i+1, l) print >> sys.stderr, '%05i\t%s'%(i+1, l)
print '===============================' print '==============================='
print >> sys.stderr, "command line:",' '.join(cmd) print_command_line_error()
raise Exception('g++ return status', status) raise Exception('Compilation failed (return status=%s):\n%s' %
(status, compile_stderr))
#touch the __init__ file #touch the __init__ file
file(os.path.join(location, "__init__.py"),'w').close() file(os.path.join(location, "__init__.py"),'w').close()
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论