How to detect errors from compileall.compile_dir?
Question:
How do I detect an error when compiling a directory of python files using compile_dir
?
Currently I get something on stderr, but no way to detect it in my app.
py_compile.compile()
takes a doraise
argument, but nothing here.
Or is there a better way to do this from a python script?
Edit:
I fixed it with os.walk
and calling py_compile.compile
for each file. But the question remains.
Answers:
works fine for me. Could it be that you’re not setting doraise
to True
somehow?
I don’t see a better way. The code is designed to support the command-line program, and the API doesn’t seem fully meant to be used as a library.
If you really had to use the compileall then you could fake it out with this hack, which notices that “quiet” is tested for boolean-ness while in the caught exception handler. I can override that with nonzero, check the exception state to see if it came from py_compile (quiet is tested in other contexts) and do something with that information:
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 finns GothPy på första måndagen varje månad, om du skulle ta sällskap med andra Python-användare i Gbg.
How do I detect an error when compiling a directory of python files using compile_dir
?
Currently I get something on stderr, but no way to detect it in my app.
py_compile.compile()
takes a doraise
argument, but nothing here.
Or is there a better way to do this from a python script?
Edit:
I fixed it with os.walk
and calling py_compile.compile
for each file. But the question remains.
works fine for me. Could it be that you’re not setting doraise
to True
somehow?
I don’t see a better way. The code is designed to support the command-line program, and the API doesn’t seem fully meant to be used as a library.
If you really had to use the compileall then you could fake it out with this hack, which notices that “quiet” is tested for boolean-ness while in the caught exception handler. I can override that with nonzero, check the exception state to see if it came from py_compile (quiet is tested in other contexts) and do something with that information:
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 finns GothPy på första måndagen varje månad, om du skulle ta sällskap med andra Python-användare i Gbg.