From 1aa750b1e465ce438bd5b0496dbdc775b7445700 Mon Sep 17 00:00:00 2001 From: snowapril Date: Mon, 29 Nov 2021 23:40:24 +0900 Subject: [PATCH 1/2] Remove keyword module from rust-side Signed-off-by: snowapril --- stdlib/src/keyword.rs | 27 --------------------------- stdlib/src/lib.rs | 6 ------ 2 files changed, 33 deletions(-) delete mode 100644 stdlib/src/keyword.rs diff --git a/stdlib/src/keyword.rs b/stdlib/src/keyword.rs deleted file mode 100644 index 0cfaeafdbc..0000000000 --- a/stdlib/src/keyword.rs +++ /dev/null @@ -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::() { - lexer::KEYWORDS.contains_key(s.as_str()) - } else { - false - } - } - - #[pyattr] - fn kwlist(vm: &VirtualMachine) -> Vec { - lexer::KEYWORDS - .keys() - .sorted() - .map(|&k| vm.ctx.new_str(k).into()) - .collect() - } -} diff --git a/stdlib/src/lib.rs b/stdlib/src/lib.rs index 7b5e01bf16..0be798fe9b 100644 --- a/stdlib/src/lib.rs +++ b/stdlib/src/lib.rs @@ -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, StdlibInit { "_ast" => ast::make_module, } - #[cfg(feature = "rustpython-parser")] - { - "keyword" => keyword::make_module, - } #[cfg(any(unix, target_os = "wasi"))] { "fcntl" => fcntl::make_module, From 9a2b67e3a7c1bbbf0859a22d90fdfb5ec018bb54 Mon Sep 17 00:00:00 2001 From: snowapril Date: Mon, 29 Nov 2021 23:40:42 +0900 Subject: [PATCH 2/2] Add keyword module from cpython 3.10 Signed-off-by: snowapril --- Lib/keyword.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 Lib/keyword.py diff --git a/Lib/keyword.py b/Lib/keyword.py new file mode 100644 index 0000000000..eab8204c91 --- /dev/null +++ b/Lib/keyword.py @@ -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__