Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove filename pattern caches #2153

Merged
merged 1 commit into from Jun 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 3 additions & 13 deletions pygments/formatters/__init__.py
Expand Up @@ -11,7 +11,7 @@
import re
import sys
import types
import fnmatch
from fnmatch import fnmatch
from os.path import basename

from pygments.formatters._mapping import FORMATTERS
Expand All @@ -22,16 +22,6 @@
'get_all_formatters', 'load_formatter_from_file'] + list(FORMATTERS)

_formatter_cache = {} # classes by name
_pattern_cache = {}


def _fn_matches(fn, glob):
"""Return whether the supplied file name fn matches pattern filename."""
if glob not in _pattern_cache:
pattern = _pattern_cache[glob] = re.compile(fnmatch.translate(glob))
return pattern.match(fn)
return _pattern_cache[glob].match(fn)


def _load_formatters(module_name):
"""Load a formatter (and all others in the module too)."""
Expand Down Expand Up @@ -122,13 +112,13 @@ def get_formatter_for_filename(fn, **options):
fn = basename(fn)
for modname, name, _, filenames, _ in FORMATTERS.values():
for filename in filenames:
if _fn_matches(fn, filename):
if fnmatch(fn, filename):
if name not in _formatter_cache:
_load_formatters(modname)
return _formatter_cache[name](**options)
for cls in find_plugin_formatters():
for filename in cls.filenames:
if _fn_matches(fn, filename):
if fnmatch(fn, filename):
return cls(**options)
raise ClassNotFound("no formatter found for file name %r" % fn)

Expand Down
20 changes: 5 additions & 15 deletions pygments/lexers/__init__.py
Expand Up @@ -11,7 +11,7 @@
import re
import sys
import types
import fnmatch
from fnmatch import fnmatch
from os.path import basename

from pygments.lexers._mapping import LEXERS
Expand All @@ -28,16 +28,6 @@
'guess_lexer', 'load_lexer_from_file'] + list(LEXERS) + list(COMPAT)

_lexer_cache = {}
_pattern_cache = {}


def _fn_matches(fn, glob):
"""Return whether the supplied file name fn matches pattern filename."""
if glob not in _pattern_cache:
pattern = _pattern_cache[glob] = re.compile(fnmatch.translate(glob))
return pattern.match(fn)
return _pattern_cache[glob].match(fn)


def _load_lexers(module_name):
"""Load a lexer (and all others in the module too)."""
Expand Down Expand Up @@ -169,13 +159,13 @@ def find_lexer_class_for_filename(_fn, code=None):
fn = basename(_fn)
for modname, name, _, filenames, _ in LEXERS.values():
for filename in filenames:
if _fn_matches(fn, filename):
if fnmatch(fn, filename):
if name not in _lexer_cache:
_load_lexers(modname)
matches.append((_lexer_cache[name], filename))
for cls in find_plugin_lexers():
for filename in cls.filenames:
if _fn_matches(fn, filename):
if fnmatch(fn, filename):
matches.append((cls, filename))

if isinstance(code, bytes):
Expand Down Expand Up @@ -262,11 +252,11 @@ def guess_lexer_for_filename(_fn, _text, **options):
matching_lexers = set()
for lexer in _iter_lexerclasses():
for filename in lexer.filenames:
if _fn_matches(fn, filename):
if fnmatch(fn, filename):
matching_lexers.add(lexer)
primary[lexer] = True
for filename in lexer.alias_filenames:
if _fn_matches(fn, filename):
if fnmatch(fn, filename):
matching_lexers.add(lexer)
primary[lexer] = False
if not matching_lexers:
Expand Down