summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2021-01-17 11:00:46 +0100
committerGeorg Brandl <georg@python.org>2021-01-17 11:00:46 +0100
commitff1e7567b3c67eb07b0c188f87c062bb8df39def (patch)
tree43fecce6e5fdce592c71cf2efe8cacb53c4bcc66
parent6c7205fee13ade278c2c802c5faa1e6c9cbd2a26 (diff)
downloadpygments-git-ff1e7567b3c67eb07b0c188f87c062bb8df39def.tar.gz
Make guessing prefer Python 3 lexer
-rw-r--r--CHANGES1
-rw-r--r--pygments/lexers/python.py11
2 files changed, 7 insertions, 5 deletions
diff --git a/CHANGES b/CHANGES
index dde8772c..2b43239d 100644
--- a/CHANGES
+++ b/CHANGES
@@ -12,6 +12,7 @@ Version 2.8.0
(not released yet)
- Fix escapes in JavaScript backtick strings (#1679)
+- Make guessing prefer Python 3 lexer
Version 2.7.4
diff --git a/pygments/lexers/python.py b/pygments/lexers/python.py
index 2abca04b..f995d36f 100644
--- a/pygments/lexers/python.py
+++ b/pygments/lexers/python.py
@@ -372,7 +372,8 @@ class PythonLexer(RegexLexer):
}
def analyse_text(text):
- return shebang_matches(text, r'pythonw?(3(\.\d)?)?')
+ return shebang_matches(text, r'pythonw?(3(\.\d)?)?') or \
+ 'import ' in text[:1000]
Python3Lexer = PythonLexer
@@ -596,8 +597,7 @@ class Python2Lexer(RegexLexer):
}
def analyse_text(text):
- return shebang_matches(text, r'pythonw?2(\.\d)?') or \
- 'import ' in text[:1000]
+ return shebang_matches(text, r'pythonw?2(\.\d)?')
class PythonConsoleLexer(Lexer):
@@ -1146,6 +1146,7 @@ class NumPyLexer(PythonLexer):
yield index, token, value
def analyse_text(text):
+ ltext = text[:1000]
return (shebang_matches(text, r'pythonw?(3(\.\d)?)?') or
- 'import ' in text[:1000]) \
- and ('import numpy' in text or 'from numpy import' in text)
+ 'import ' in ltext) \
+ and ('import numpy' in ltext or 'from numpy import' in ltext)