Tuesday, September 14, 2010

Emacs and PyFlakes

If you happen to be running Ubuntu v8.04, doing an apt-get install Pyflakes will install version 0.20. The Ubuntu packages confirmed this finding for me:

https://launchpad.net/ubuntu/+source/pyflakes/0.2.1+svn14502-1ubuntu1/+build/545566

Doing a Google search, you will see reports that Emacs hangs when using triple quotes ('''). You end up seeing really high CPU load and the only way to solve the issue is by killing the process. In fact, there was a $500 bounty to solve this issue 2 years ago:

http://www.plope.com/bounty_solved
I have solved our problem with triple-quotes.

 The problem is that Emacs expects PyFlakes to only output error
 messages, but on a syntax error, PyFlakes prints out an error message,
 then the *entire* contents of the module that it cannot import, and
 *finally* a line that contains a number of spaces equal to the offset
 into the file of the syntax error (in the case of my real-world file,
 the triple-quote was 3,896 characters into the file, so PyFlake's line 
 of spaces was that long as well).  The offending code is in the
 "pyflakes" command-line program and looks like this:

    print >> sys.stderr, 'could not compile %r:%d:' % (filename, lineno)
    print >> sys.stderr, line
    print >> sys.stderr, " " * (offset-2), "^"

 By removing or commenting out those last two lines, so that PyFlakes 
 only outputs its error message, you will stop flooding the Emacs
 regular-expression engine with data.  It's actually the long line of
 spaces that causes the problem, and it's one regular expression that's
 really sensitive to it (this is from the Emacs 22 flymake.el):

     ;; ant/javac
     (" *\\(\\[javac\\] *\\)?\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\)\:\\([0-9]+\\)\:[ \t\n]*\\(.+\\)"
      2 4 nil 5))

 For some reason (I'm not an RE engine guru), something about the way
 it's matching spaces takes exponential time when given several thousand
 spaces.  Go figure.  Anyway, I see no point in throwing anything but 
 error messages at Flymake, so I comment out both the printing of the
 module and the spaces from PyFlakes.


The problem was corrected in PyFlakes v0.3.0 according to the release notes:
http://divmod.org/trac/browser/tags/releases/Pyflakes-0.4.0/NEWS.txt
0.3.0 (2009-01-30):
20   - Display more informative SyntaxError messages.
21   - Don't hang flymake with unmatched triple quotes (only report a single
22     line of source for a multiline syntax error).
23   - Recognize __builtins__ as a defined name.
24   - Improve pyflakes support for python versions 2.3-2.5
25   - Support for if-else expressions and with statements.
26   - Warn instead of error on non-existant file paths.
27   - Check for __future__ imports after other statements.
28   - Add reporting for some types of import shadowing.
29   - Improve reporting of unbound locals
The trick to fixing this issue on Ubuntu v8.04? Use pip install pyflakes, which will download the 0.4.0 version.

sudo apt-get remove pyflakes
sudo pip install pyflakes

No comments:

Post a Comment