Python 3 shebang guessed for SourcesListLexer

Chris Mayo avatarChris Mayo created an issue

Running the following with Pygments 1.4 and Python 2.7.2:

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:

<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 #650:

<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:

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
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.