1. Georg Brandl
  2. pygments-main
  3. Issues
Issue #761 duplicate

Different lexers retruned for the same file depending on the Python version

pombredanne NA
created an issue

Depending on the Python version a different lexer is returned in this test: 2.5 detects a GasLexer and 2.6 or 2.7 an SLexer:

{{{ $ export PYTHONPATH=~/pyg/Pygments-1.5-py2.5.egg $ python2.5 -c 'from pygments.lexers import get_lexer_for_filename; print get_lexer_for_filename("/tmp/a.S")' <pygments.lexers.GasLexer>

$ export PYTHONPATH=~/pyg/Pygments-1.5-py2.6.egg $ python2.6 -c 'from pygments.lexers import get_lexer_for_filename; print get_lexer_for_filename("/tmp/a.S")' <pygments.lexers.SLexer>

$ export PYTHONPATH=~/pyg/Pygments-1.5-py2.7.egg $ python2.7 -c 'from pygments.lexers import get_lexer_for_filename; print get_lexer_for_filename("/tmp/a.S")' <pygments.lexers.SLexer>

}}} Weirdly enough this is the opposite behavior of the 1.0 version (2.5 detects an Slexer and 2.6 a GasLexer)

{{{ $ export PYTHONPATH=~/pyg/Pygments-1.0-py2.5.egg $ python2.5 -c 'from pygments.lexers import get_lexer_for_filename; print get_lexer_for_filename("/tmp/a.S")' <pygments.lexers.SLexer> $ export PYTHONPATH=~/pyg/Pygments-1.0-py2.6.egg $ python2.6 -c 'from pygments.lexers import get_lexer_for_filename; print get_lexer_for_filename("/tmp/a.S")' <pygments.lexers.GasLexer> }}}

Comments (3)

  1. Tim Hatch

    Likely due to hash ordering -- it needs to pick arbitrarily for cases where we have more than one lexer matching the same filename. The fix is typically adjust your usage to call guess_lexer instead which takes the contents of the file so it can do something smarter.

  2. Tim Hatch

    Which is to say, the API is confusing and it's on my list to improve... but I'm not sure what we can do better with multiple lexers for the same filename. I guess we could break the tie arbitrarily so that the alphabetically-first lexer is always returned or something.

  3. Log in to comment