Add a new script to generate builtin module names (#12696)

This commit is contained in:
Alex Waygood 2024-08-05 21:33:36 +01:00 committed by GitHub
parent 2393d19f91
commit 7ee7c68f36
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 198 additions and 50 deletions

View file

@ -0,0 +1,55 @@
//! This file is generated by `scripts/generate_builtin_modules.py`
/// Return `true` if `module` is a [builtin module] on the given
/// Python 3 version.
///
/// "Builtin modules" are modules that are compiled directly into the
/// Python interpreter. These can never be shadowed by first-party
/// modules; the normal rules of module resolution do not apply to these
/// modules.
///
/// [builtin module]: https://docs.python.org/3/library/sys.html#sys.builtin_module_names
#[allow(clippy::unnested_or_patterns)]
pub fn is_builtin_module(minor_version: u8, module: &str) -> bool {
matches!(
(minor_version, module),
(
_,
"_abc"
| "_ast"
| "_codecs"
| "_collections"
| "_functools"
| "_imp"
| "_io"
| "_locale"
| "_operator"
| "_signal"
| "_sre"
| "_stat"
| "_string"
| "_symtable"
| "_thread"
| "_tracemalloc"
| "_warnings"
| "_weakref"
| "atexit"
| "builtins"
| "errno"
| "faulthandler"
| "gc"
| "itertools"
| "marshal"
| "posix"
| "pwd"
| "sys"
| "time"
) | (7, "xxsubtype" | "zipimport")
| (8, "xxsubtype")
| (9, "_peg_parser" | "xxsubtype")
| (10, "xxsubtype")
| (11, "_tokenize" | "xxsubtype")
| (12, "_tokenize" | "_typing")
| (13, "_suggestions" | "_sysconfig" | "_tokenize" | "_typing")
)
}

View file

@ -0,0 +1,5 @@
mod builtin_modules;
mod known_stdlib;
pub use builtin_modules::is_builtin_module;
pub use known_stdlib::is_known_standard_library;