forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkeyword.rs
More file actions
29 lines (26 loc) · 724 Bytes
/
Copy pathkeyword.rs
File metadata and controls
29 lines (26 loc) · 724 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/// Testing if a string is a keyword.
pub(crate) use decl::make_module;
#[pymodule(name = "keyword")]
mod decl {
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) -> PyObjectRef {
vm.ctx.new_list(
lexer::KEYWORDS
.keys()
.sorted()
.map(|k| vm.ctx.new_utf8_str(k))
.collect(),
)
}
}