如何检测来自compileall.compile_dir的错误?

问题描述:

如何使用compile_dir编译python文件的目录时检测到错误?如何检测来自compileall.compile_dir的错误?

目前我在stderr上找到了一些东西,但没有办法在我的应用程序中检测到它。 py_compile.compile()采用'doraise'参数,但没有任何内容。

或者是否有更好的方法从python脚本执行此操作?

编辑:

我用os.walk,并呼吁py_compile.compile为每个文件固定它。但问题依然存在。

我没有看到更好的方法。该代码旨在支持命令行程序,并且API似乎并不完全用作库。

如果你真的必须使用compileall,那么你可以用这个黑客伪装出来,它注意到“quiet”在被捕获的异常处理程序中测试了布尔值。我可以覆盖与非零,检查异常状态,看它是否从py_compile来到(安静的在其他情况下测试),做一些与信息:

import sys 
import py_compile 
import compileall 

class ReportProblem: 
    def __nonzero__(self): 
     type, value, traceback = sys.exc_info() 
     if type is not None and issubclass(type, py_compile.PyCompileError): 
      print "Problem with", repr(value) 
      raise type, value, traceback 
     return 1 
report_problem = ReportProblem() 

compileall.compile_dir(".", quiet=report_problem) 

Förresten,DET芬兰人GothPyPåförstamåndagen varjemånad,om du skulle tasällskapmed andra Python-användarei Gbg。

+0

谢谢。我想我只是删除我的问题,因为这是试图绕错误的方式。我对我的小循环很满意。 和GothPy听起来不错。你们在哪里见面? – Macke 2009-03-05 21:10:19

适合我。难道你没有设置doraiseTrue莫名其妙吗?