1. Georg Brandl
  2. pygments-main
  3. Issues
Issue #707 resolved

Python 3 shebang guessed for SourcesListLexer

Chris Mayo
created an issue

Running the following with Pygments 1.4 and Python 2.7.2: {{{

!python

from pygments.lexers import guess_lexer

lines = ("#!/usr/bin/python", "#!/usr/bin/python2", "#!/usr/bin/python2.7", "#!/usr/bin/python3", "#!/usr/bin/python3.2" )

for line in lines: print(line) print(guess_lexer(line)) }}} outputs: {{{

!/usr/bin/python

<pygments.lexers.PythonLexer>

!/usr/bin/python2

<pygments.lexers.SourcesListLexer>

!/usr/bin/python2.7

<pygments.lexers.PythonLexer>

!/usr/bin/python3

<pygments.lexers.SourcesListLexer>

!/usr/bin/python3.2

<pygments.lexers.SourcesListLexer> }}} With the attached patch plus the fix for Python 2 shebang <<issue 650>>: {{{

!/usr/bin/python

<pygments.lexers.NumPyLexer>

!/usr/bin/python2

<pygments.lexers.NumPyLexer>

!/usr/bin/python2.7

<pygments.lexers.NumPyLexer>

!/usr/bin/python3

<pygments.lexers.Python3Lexer>

!/usr/bin/python3.2

<pygments.lexers.Python3Lexer> }}}

_iter_lexerclasses() does not return items in the order in LEXERS in _mapping.py.

CPython implementation detail: Keys and values are listed in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary’s history of insertions and deletions.

http://docs.python.org/library/stdtypes.html#dict.items

grepping for Python in the output of this: {{{

!python

from pygments.lexers import _iter_lexerclasses

i = 0 for a in _iter_lexerclasses(): print(i, a) i += 1 }}} gives: {{{ (23, <class 'pygments.lexers.agile.PythonLexer'>) (44, <class 'pygments.lexers.agile.PythonTracebackLexer'>) (63, <class 'pygments.lexers.agile.PythonConsoleLexer'>) (143, <class 'pygments.lexers.agile.Python3TracebackLexer'>) (160, <class 'pygments.lexers.parsers.AntlrPythonLexer'>) (163, <class 'pygments.lexers.agile.Python3Lexer'>) }}}

and with the attached patch: {{{ (11, <class 'pygments.lexers.parsers.AntlrPythonLexer'>) (143, <class 'pygments.lexers.agile.Python3Lexer'>) (144, <class 'pygments.lexers.agile.Python3TracebackLexer'>) (145, <class 'pygments.lexers.agile.PythonConsoleLexer'>) (146, <class 'pygments.lexers.agile.PythonLexer'>) (147, <class 'pygments.lexers.agile.PythonTracebackLexer'>) }}}

Comments (1)

  1. Log in to comment