mirror of
https://github.com/davidhalter/parso.git
synced 2025-12-07 21:34:32 +08:00
Make version a keyword only argument in load_grammar.
This commit is contained in:
@@ -144,35 +144,37 @@ class PythonGrammar(Grammar):
|
|||||||
return tokenize(code, self._version_int)
|
return tokenize(code, self._version_int)
|
||||||
|
|
||||||
|
|
||||||
def load_grammar(version=None):
|
def load_grammar(**kwargs):
|
||||||
"""
|
"""
|
||||||
Loads a Python grammar. The default version is the current Python version.
|
Loads a Python grammar. The default version is the current Python version.
|
||||||
|
|
||||||
If you need support for a specific version, please use e.g.
|
If you need support for a specific version, please use e.g.
|
||||||
`version='3.3'`.
|
`version='3.3'`.
|
||||||
"""
|
"""
|
||||||
version_int = version_string_to_int(version)
|
def load_grammar(version=None):
|
||||||
|
version_int = version_string_to_int(version)
|
||||||
|
|
||||||
# For these versions we use the same grammar files, because nothing
|
# For these versions we use the same grammar files, because nothing
|
||||||
# changed.
|
# changed.
|
||||||
if version_int == 33:
|
if version_int == 33:
|
||||||
version_int = 34
|
version_int = 34
|
||||||
elif version_int == 26:
|
elif version_int == 26:
|
||||||
version_int = 27
|
version_int = 27
|
||||||
|
|
||||||
file = 'python/grammar' + str(version_int) + '.txt'
|
file = 'python/grammar' + str(version_int) + '.txt'
|
||||||
|
|
||||||
global _loaded_grammars
|
global _loaded_grammars
|
||||||
path = os.path.join(os.path.dirname(__file__), file)
|
path = os.path.join(os.path.dirname(__file__), file)
|
||||||
try:
|
|
||||||
return _loaded_grammars[path]
|
|
||||||
except KeyError:
|
|
||||||
try:
|
try:
|
||||||
with open(path) as f:
|
return _loaded_grammars[path]
|
||||||
bnf_text = f.read()
|
except KeyError:
|
||||||
|
try:
|
||||||
|
with open(path) as f:
|
||||||
|
bnf_text = f.read()
|
||||||
|
|
||||||
grammar = PythonGrammar(version_int, bnf_text)
|
grammar = PythonGrammar(version_int, bnf_text)
|
||||||
return _loaded_grammars.setdefault(path, grammar)
|
return _loaded_grammars.setdefault(path, grammar)
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
message = "Python version %s is currently not supported." % version
|
message = "Python version %s is currently not supported." % version
|
||||||
raise NotImplementedError(message)
|
raise NotImplementedError(message)
|
||||||
|
return load_grammar(**kwargs)
|
||||||
|
|||||||
@@ -6,11 +6,11 @@ from parso import utils
|
|||||||
def test_load_inexisting_grammar():
|
def test_load_inexisting_grammar():
|
||||||
# This version shouldn't be out for a while, but if we ever do, wow!
|
# This version shouldn't be out for a while, but if we ever do, wow!
|
||||||
with pytest.raises(NotImplementedError):
|
with pytest.raises(NotImplementedError):
|
||||||
load_grammar('15.8')
|
load_grammar(version='15.8')
|
||||||
# The same is true for very old grammars (even though this is probably not
|
# The same is true for very old grammars (even though this is probably not
|
||||||
# going to be an issue.
|
# going to be an issue.
|
||||||
with pytest.raises(NotImplementedError):
|
with pytest.raises(NotImplementedError):
|
||||||
load_grammar('1.5')
|
load_grammar(version='1.5')
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(('string', 'result'), [
|
@pytest.mark.parametrize(('string', 'result'), [
|
||||||
@@ -23,9 +23,9 @@ def test_parse_version(string, result):
|
|||||||
@pytest.mark.parametrize('string', ['1.', 'a', '#', '1.3.4.5', '1.12'])
|
@pytest.mark.parametrize('string', ['1.', 'a', '#', '1.3.4.5', '1.12'])
|
||||||
def test_invalid_grammar_version(string):
|
def test_invalid_grammar_version(string):
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ValueError):
|
||||||
load_grammar(string)
|
load_grammar(version=string)
|
||||||
|
|
||||||
|
|
||||||
def test_grammar_int_version():
|
def test_grammar_int_version():
|
||||||
with pytest.raises(TypeError):
|
with pytest.raises(TypeError):
|
||||||
load_grammar(3.2)
|
load_grammar(version=3.2)
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import parso
|
|||||||
from parso.python.normalizer import ErrorFinderConfig
|
from parso.python.normalizer import ErrorFinderConfig
|
||||||
|
|
||||||
def _get_error_list(code, version=None):
|
def _get_error_list(code, version=None):
|
||||||
grammar = parso.load_grammar(version)
|
grammar = parso.load_grammar(version=version)
|
||||||
tree = grammar.parse(code)
|
tree = grammar.parse(code)
|
||||||
config = ErrorFinderConfig()
|
config = ErrorFinderConfig()
|
||||||
return list(tree._get_normalizer_issues(config))
|
return list(tree._get_normalizer_issues(config))
|
||||||
|
|||||||
Reference in New Issue
Block a user