Merge pull request #3482 from Snowapril/keyword_module

Replace keyword module with cpython code
This commit is contained in:
Noa
2021-11-29 18:23:04 -06:00
committed by GitHub
3 changed files with 63 additions and 33 deletions

63
Lib/keyword.py vendored Normal file
View File

@@ -0,0 +1,63 @@
"""Keywords (from "Grammar/python.gram")
This file is automatically generated; please don't muck it up!
To update the symbols in this file, 'cd' to the top directory of
the python source tree and run:
PYTHONPATH=Tools/peg_generator python3 -m pegen.keywordgen \
Grammar/python.gram \
Grammar/Tokens \
Lib/keyword.py
Alternatively, you can run 'make regen-keyword'.
"""
__all__ = ["iskeyword", "issoftkeyword", "kwlist", "softkwlist"]
kwlist = [
'False',
'None',
'True',
'and',
'as',
'assert',
'async',
'await',
'break',
'class',
'continue',
'def',
'del',
'elif',
'else',
'except',
'finally',
'for',
'from',
'global',
'if',
'import',
'in',
'is',
'lambda',
'nonlocal',
'not',
'or',
'pass',
'raise',
'return',
'try',
'while',
'with',
'yield'
]
softkwlist = [
'_',
'case',
'match'
]
iskeyword = frozenset(kwlist).__contains__
issoftkeyword = frozenset(softkwlist).__contains__

View File

@@ -1,27 +0,0 @@
/// Testing if a string is a keyword.
pub(crate) use keyword::make_module;
#[pymodule]
mod keyword {
use crate::vm::{builtins::PyStr, PyObjectRef, VirtualMachine};
use itertools::Itertools;
use rustpython_parser::lexer;
#[pyfunction]
fn iskeyword(s: PyObjectRef) -> bool {
if let Some(s) = s.payload::<PyStr>() {
lexer::KEYWORDS.contains_key(s.as_str())
} else {
false
}
}
#[pyattr]
fn kwlist(vm: &VirtualMachine) -> Vec<PyObjectRef> {
lexer::KEYWORDS
.keys()
.sorted()
.map(|&k| vm.ctx.new_str(k).into())
.collect()
}
}

View File

@@ -14,8 +14,6 @@ mod dis;
mod gc;
mod hashlib;
mod json;
#[cfg(feature = "rustpython-parser")]
mod keyword;
mod math;
mod platform;
mod pyexpat;
@@ -96,10 +94,6 @@ pub fn get_module_inits() -> impl Iterator<Item = (Cow<'static, str>, StdlibInit
{
"_ast" => ast::make_module,
}
#[cfg(feature = "rustpython-parser")]
{
"keyword" => keyword::make_module,
}
#[cfg(any(unix, target_os = "wasi"))]
{
"fcntl" => fcntl::make_module,