Support builtins (#50)

This commit is contained in:
Charlie Marsh 2022-08-30 14:41:17 -04:00 committed by GitHub
parent e9a3484edf
commit 643797d922
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 188 additions and 0 deletions

View file

@ -12,3 +12,5 @@ def get_name():
def get_name(self):
return self.name
x = list()

163
src/builtins.rs Normal file
View file

@ -0,0 +1,163 @@
pub const BUILTINS: &[&str] = &[
"ArithmeticError",
"AssertionError",
"AttributeError",
"BaseException",
"BlockingIOError",
"BrokenPipeError",
"BufferError",
"BytesWarning",
"ChildProcessError",
"ConnectionAbortedError",
"ConnectionError",
"ConnectionRefusedError",
"ConnectionResetError",
"DeprecationWarning",
"EOFError",
"Ellipsis",
"EnvironmentError",
"Exception",
"False",
"FileExistsError",
"FileNotFoundError",
"FloatingPointError",
"FutureWarning",
"GeneratorExit",
"IOError",
"ImportError",
"ImportWarning",
"IndentationError",
"IndexError",
"InterruptedError",
"IsADirectoryError",
"KeyError",
"KeyboardInterrupt",
"LookupError",
"MemoryError",
"ModuleNotFoundError",
"NameError",
"None",
"NotADirectoryError",
"NotImplemented",
"NotImplementedError",
"OSError",
"OverflowError",
"PendingDeprecationWarning",
"PermissionError",
"ProcessLookupError",
"RecursionError",
"ReferenceError",
"ResourceWarning",
"RuntimeError",
"RuntimeWarning",
"StopAsyncIteration",
"StopIteration",
"SyntaxError",
"SyntaxWarning",
"SystemError",
"SystemExit",
"TabError",
"TimeoutError",
"True",
"TypeError",
"UnboundLocalError",
"UnicodeDecodeError",
"UnicodeEncodeError",
"UnicodeError",
"UnicodeTranslateError",
"UnicodeWarning",
"UserWarning",
"ValueError",
"Warning",
"ZeroDivisionError",
"__build_class__",
"__debug__",
"__doc__",
"__import__",
"__loader__",
"__name__",
"__package__",
"__spec__",
"abs",
"all",
"any",
"ascii",
"bin",
"bool",
"breakpoint",
"bytearray",
"bytes",
"callable",
"chr",
"classmethod",
"compile",
"complex",
"copyright",
"credits",
"delattr",
"dict",
"dir",
"divmod",
"enumerate",
"eval",
"exec",
"exit",
"filter",
"float",
"format",
"frozenset",
"getattr",
"globals",
"hasattr",
"hash",
"help",
"hex",
"id",
"input",
"int",
"isinstance",
"issubclass",
"iter",
"len",
"license",
"list",
"locals",
"map",
"max",
"memoryview",
"min",
"next",
"object",
"oct",
"open",
"ord",
"pow",
"print",
"property",
"quit",
"range",
"repr",
"reversed",
"round",
"set",
"setattr",
"slice",
"sorted",
"staticmethod",
"str",
"sum",
"super",
"tuple",
"type",
"vars",
"zip",
];
// Globally defined names which are not attributes of the builtins module, or are only present on
// some platforms.
pub const MAGIC_GLOBALS: &[&str] = &[
"__file__",
"__builtins__",
"__annotations__",
"WindowsError",
];

View file

@ -1,6 +1,7 @@
use std::collections::{BTreeMap, BTreeSet};
use std::sync::atomic::{AtomicUsize, Ordering};
use crate::builtins::{BUILTINS, MAGIC_GLOBALS};
use rustpython_parser::ast::{
Arg, Arguments, Constant, Expr, ExprContext, ExprKind, Location, Stmt, StmtKind, Suite,
};
@ -45,6 +46,7 @@ enum BindingKind {
Assignment,
ClassDefinition,
Definition,
Builtin,
FutureImportation,
Importation(String),
StarImportation,
@ -408,6 +410,25 @@ impl Checker<'_> {
.push(self.scopes.pop().expect("Attempted to pop without scope."));
}
fn bind_builtins(&mut self) {
for builtin in BUILTINS {
self.add_binding(Binding {
kind: BindingKind::Builtin,
name: builtin.to_string(),
location: Default::default(),
used: None,
})
}
for builtin in MAGIC_GLOBALS {
self.add_binding(Binding {
kind: BindingKind::Builtin,
name: builtin.to_string(),
location: Default::default(),
used: None,
})
}
}
fn add_binding(&mut self, binding: Binding) {
let scope = self.scopes.last_mut().expect("No current scope found.");
@ -532,6 +553,7 @@ impl Checker<'_> {
pub fn check_ast(python_ast: &Suite, settings: &Settings, path: &str) -> Vec<Check> {
let mut checker = Checker::new(settings);
checker.push_scope(Scope::new(Module));
checker.bind_builtins();
for stmt in python_ast {
checker.visit_stmt(stmt);

View file

@ -1,3 +1,4 @@
mod builtins;
mod cache;
pub mod check_ast;
mod check_lines;