tags:

views:

102

answers:

2

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.

A: 

works fine for me. Could it be that you're not setting doraise to True somehow?

SilentGhost
A: 

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.

Andrew Dalke
Thanks. I think I'll just remove my question, as that is trying to go about it the wrong way around. I'm quite happy with my little for loop.And GothPy sounds nice. Where do you guys meet?
Marcus Lindblom