diff options
author | Georg Brandl <georg@python.org> | 2019-05-28 06:33:24 +0200 |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2019-05-28 06:33:24 +0200 |
commit | c6735d522a71c410b21f07a210f2dfd1808c4327 (patch) | |
tree | b80c08685f961c2f995524a8870270c8f09bce56 /pygments/lexers | |
parent | 59b770d2cb06b62c5398235d588f12a73fab4c75 (diff) | |
download | pygments-c6735d522a71c410b21f07a210f2dfd1808c4327.tar.gz |
Properly decode text in guess_lexer if necessary.
fixes #1438
Diffstat (limited to 'pygments/lexers')
-rw-r--r-- | pygments/lexers/__init__.py | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/pygments/lexers/__init__.py b/pygments/lexers/__init__.py index 50f39d4e..a1cf3767 100644 --- a/pygments/lexers/__init__.py +++ b/pygments/lexers/__init__.py @@ -18,7 +18,7 @@ from os.path import basename from pygments.lexers._mapping import LEXERS from pygments.modeline import get_filetype_from_buffer from pygments.plugin import find_plugin_lexers -from pygments.util import ClassNotFound, itervalues, guess_decode +from pygments.util import ClassNotFound, itervalues, guess_decode, text_type __all__ = ['get_lexer_by_name', 'get_lexer_for_filename', 'find_lexer_class', @@ -289,6 +289,13 @@ def guess_lexer_for_filename(_fn, _text, **options): def guess_lexer(_text, **options): """Guess a lexer by strong distinctions in the text (eg, shebang).""" + if not isinstance(_text, text_type): + inencoding = options.get('inencoding', options.get('encoding')) + if inencoding: + _text = _text.decode(inencoding or 'utf8') + else: + _text, _ = guess_decode(_text) + # try to get a vim modeline first ft = get_filetype_from_buffer(_text) |