summaryrefslogtreecommitdiff
path: root/pygments/lexers/__init__.py
diff options
context:
space:
mode:
Diffstat (limited to 'pygments/lexers/__init__.py')
-rw-r--r--pygments/lexers/__init__.py14
1 files changed, 11 insertions, 3 deletions
diff --git a/pygments/lexers/__init__.py b/pygments/lexers/__init__.py
index 328e072c..1baf93b2 100644
--- a/pygments/lexers/__init__.py
+++ b/pygments/lexers/__init__.py
@@ -5,7 +5,7 @@
Pygments lexers.
- :copyright: Copyright 2006-2017 by the Pygments team, see AUTHORS.
+ :copyright: Copyright 2006-2019 by the Pygments team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
@@ -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',
@@ -133,7 +133,8 @@ def load_lexer_from_file(filename, lexername="CustomLexer", **options):
try:
# This empty dict will contain the namespace for the exec'd file
custom_namespace = {}
- exec(open(filename, 'rb').read(), custom_namespace)
+ with open(filename, 'rb') as f:
+ exec(f.read(), custom_namespace)
# Retrieve the class `lexername` from that namespace
if lexername not in custom_namespace:
raise ClassNotFound('no valid %s class found in %s' %
@@ -288,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)