-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
278 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import pdb | ||
from builtins import breakpoint | ||
from pdb import set_trace as st | ||
from celery.contrib.rdb import set_trace | ||
|
||
breakpoint() | ||
st() | ||
set_trace() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use fnv::FnvHashMap; | ||
use rustpython_ast::{Expr, ExprKind, Stmt}; | ||
|
||
use crate::ast::types::Range; | ||
use crate::checks::{Check, CheckKind}; | ||
use crate::flake8_debugger::types::{Debugger, DebuggerUsingType}; | ||
|
||
const BREAKPOINT_FN: &str = "breakpoint"; | ||
|
||
const BUILTINS_MODULE: &str = "builtins"; | ||
|
||
const DEBUGGERS: [Debugger; 7] = [ | ||
("pdb", &["set_trace"]), | ||
("pudb", &["set_trace"]), | ||
("ipdb", &["set_trace", "sset_trace"]), | ||
("IPython.terminal.embed", &["InteractiveShellEmbed"]), | ||
("IPython.frontend.terminal.embed", &["InteractiveShellEmbed"]), | ||
("celery.contrib.rdb", &["set_trace"]), | ||
(BUILTINS_MODULE, &[BREAKPOINT_FN]), | ||
]; | ||
|
||
fn get_debugger(module_name: &str) -> Option<&Debugger> { | ||
DEBUGGERS.iter().find(|&d| d.0 == module_name) | ||
} | ||
|
||
fn function_name(func: &Expr) -> Option<&str> { | ||
if let ExprKind::Name { id, .. } = &func.node { | ||
Some(id) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
/// Checks for the presence of a debugger call. | ||
pub fn debugger_call( | ||
expr: &Expr, | ||
func: &Expr, | ||
import_aliases: &FnvHashMap<&str, &str>, | ||
) -> Option<Check> { | ||
let raw_func_name = function_name(func)?; | ||
let func_name = match import_aliases.get(raw_func_name) { | ||
Some(func_name) => func_name, | ||
None => raw_func_name, | ||
}; | ||
|
||
if func_name == BREAKPOINT_FN { | ||
return Some(Check::new( | ||
CheckKind::Debugger(DebuggerUsingType::Call(func_name.to_string())), | ||
Range::from_located(expr), | ||
)); | ||
} | ||
|
||
if let Some(_) = DEBUGGERS.iter().find(|&d| d.1.contains(&func_name)) { | ||
return Some(Check::new( | ||
CheckKind::Debugger(DebuggerUsingType::Call(raw_func_name.to_string())), | ||
Range::from_located(expr), | ||
)); | ||
} | ||
|
||
None | ||
} | ||
|
||
/// Checks for the presence of a debugger import. | ||
pub fn debugger_import(stmt: &Stmt, module: &Option<String>, name: &str) -> Option<Check> { | ||
if let Some(module) = module { | ||
if let Some(debugger) = get_debugger(module) { | ||
if debugger.1.contains(&name) { | ||
return Some(Check::new( | ||
CheckKind::Debugger(DebuggerUsingType::Import), | ||
Range::from_located(stmt), | ||
)); | ||
} | ||
} | ||
} else if name != BUILTINS_MODULE { | ||
if let Some(_) = get_debugger(name) { | ||
return Some(Check::new( | ||
CheckKind::Debugger(DebuggerUsingType::Import), | ||
Range::from_located(stmt), | ||
)); | ||
} | ||
} | ||
|
||
None | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod checks; | ||
pub mod types; |
Oops, something went wrong.