| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -32,7 +32,9 @@ module Builtins { | |||
| 32 | 32 | "UnicodeDecodeError", "UnicodeEncodeError", "UnicodeError", "UnicodeTranslateError", | |
| 33 | 33 | "UnicodeWarning", "UserWarning", "ValueError", "Warning", "ZeroDivisionError", | |
| 34 | 34 | // Added for compatibility | |
| 35 | - "exec" | ||
| 35 | + "exec", | ||
| 36 | + // Added by the `site` module (available by default unless `-S` is used) | ||
| 37 | + "copyright", "credits", "exit", "quit" | ||
| 36 | 38 | ] | |
| 37 | 39 | or | |
| 38 | 40 | // Built-in constants shared between Python 2 and 3 | |
@@ -51,8 +53,8 @@ module Builtins { | |||
| 51 | 53 | or | |
| 52 | 54 | // Python 2 only | |
| 53 | 55 | result in [ | |
| 54 | - "basestring", "cmp", "execfile", "file", "long", "raw_input", "reduce", "reload", "unichr", | ||
| 55 | - "unicode", "xrange" | ||
| 56 | + "apply", "basestring", "cmp", "execfile", "file", "long", "raw_input", "reduce", "reload", | ||
| 57 | + "unichr", "unicode", "xrange" | ||
| 56 | 58 | ] | |
| 57 | 59 | } | |
| 58 | 60 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,9 +10,10 @@ | |||
| 10 | 10 | */ | |
| 11 | 11 | ||
| 12 | 12 | import python | |
| 13 | - private import LegacyPointsTo | ||
| 14 | - private import semmle.python.types.Builtins | ||
| 13 | + private import semmle.python.ApiGraphs | ||
| 15 | 14 | ||
| 16 | - from CallNode call, ControlFlowNodeWithPointsTo func | ||
| 17 | - where major_version() = 2 and call.getFunction() = func and func.pointsTo(Value::named("apply")) | ||
| 15 | + from CallNode call | ||
| 16 | + where | ||
| 17 | + major_version() = 2 and | ||
| 18 | + call = API::builtin("apply").getACall().asCfgNode() | ||
| 18 | 19 | select call, "Call to the obsolete builtin function 'apply'." | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -11,7 +11,7 @@ | |||
| 11 | 11 | */ | |
| 12 | 12 | ||
| 13 | 13 | import python | |
| 14 | - private import LegacyPointsTo | ||
| 14 | + private import semmle.python.ApiGraphs | ||
| 15 | 15 | ||
| 16 | 16 | /** | |
| 17 | 17 | * Holds if the module `name` was deprecated in Python version `major`.`minor`, | |
@@ -80,7 +80,7 @@ where | |||
| 80 | 80 | name = imp.getName() and | |
| 81 | 81 | deprecated_module(name, instead, _, _) and | |
| 82 | 82 | not exists(Try try, ExceptStmt except | except = try.getAHandler() | | |
| 83 | - except.getType().(ExprWithPointsTo).pointsTo(ClassValue::importError()) and | ||
| 83 | + except.getType() = API::builtin("ImportError").getAValueReachableFromSource().asExpr() and | ||
| 84 | 84 | except.containsInScope(imp) | |
| 85 | 85 | ) | |
| 86 | 86 | select imp, deprecation_message(name) + replacement_message(name) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,7 +13,7 @@ | |||
| 13 | 13 | */ | |
| 14 | 14 | ||
| 15 | 15 | import python | |
| 16 | - private import LegacyPointsTo | ||
| 16 | + private import semmle.python.ApiGraphs | ||
| 17 | 17 | ||
| 18 | 18 | predicate func_with_side_effects(Expr e) { | |
| 19 | 19 | exists(string name | name = e.(Attribute).getName() or name = e.(Name).getId() | | |
@@ -24,11 +24,11 @@ predicate func_with_side_effects(Expr e) { | |||
| 24 | 24 | } | |
| 25 | 25 | ||
| 26 | 26 | predicate call_with_side_effect(Call e) { | |
| 27 | - e.getAFlowNode() = Value::named("subprocess.call").getACall() | ||
| 28 | - or | ||
| 29 | - e.getAFlowNode() = Value::named("subprocess.check_call").getACall() | ||
| 30 | - or | ||
| 31 | - e.getAFlowNode() = Value::named("subprocess.check_output").getACall() | ||
| 27 | + e.getAFlowNode() = | ||
| 28 | + API::moduleImport("subprocess") | ||
| 29 | + .getMember(["call", "check_call", "check_output"]) | ||
| 30 | + .getACall() | ||
| 31 | + .asCfgNode() | ||
| 32 | 32 | } | |
| 33 | 33 | ||
| 34 | 34 | predicate probable_side_effect(Expr e) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,7 +13,7 @@ | |||
| 13 | 13 | */ | |
| 14 | 14 | ||
| 15 | 15 | import python | |
| 16 | - private import LegacyPointsTo | ||
| 16 | + private import semmle.python.ApiGraphs | ||
| 17 | 17 | ||
| 18 | 18 | predicate isInsideLoop(AstNode node) { | |
| 19 | 19 | node.getParentNode() instanceof While | |
@@ -33,9 +33,9 @@ where | |||
| 33 | 33 | not isInsideLoop(del) and | |
| 34 | 34 | // False positive: calling `sys.exc_info` within a function results in a | |
| 35 | 35 | // reference cycle, and an explicit call to `del` helps break this cycle. | |
| 36 | - not exists(FunctionValue ex | | ||
| 37 | - ex = Value::named("sys.exc_info") and | ||
| 38 | - ex.getACall().getScope() = f | ||
| 36 | + not exists(API::CallNode call | | ||
| 37 | + call = API::moduleImport("sys").getMember("exc_info").getACall() and | ||
| 38 | + call.getScope() = f | ||
| 39 | 39 | ) | |
| 40 | 40 | select del, "Unnecessary deletion of local variable $@ in function $@.", e, e.toString(), f, | |
| 41 | 41 | f.getName() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,7 +12,7 @@ | |||
| 12 | 12 | */ | |
| 13 | 13 | ||
| 14 | 14 | import python | |
| 15 | - private import LegacyPointsTo | ||
| 15 | + private import semmle.python.ApiGraphs | ||
| 16 | 16 | import Definition | |
| 17 | 17 | ||
| 18 | 18 | predicate is_increment(Stmt s) { | |
@@ -41,23 +41,16 @@ predicate one_item_only(For f) { | |||
| 41 | 41 | ) | |
| 42 | 42 | } | |
| 43 | 43 | ||
| 44 | - predicate points_to_call_to_range(ControlFlowNode f) { | ||
| 45 | - /* (x)range is a function in Py2 and a class in Py3, so we must treat it as a plain object */ | ||
| 46 | - exists(Value range | | ||
| 47 | - range = Value::named("range") or | ||
| 48 | - range = Value::named("xrange") | ||
| 49 | - | | ||
| 50 | - f = range.getACall() | ||
| 51 | - ) | ||
| 44 | + /** Holds if `node` is a call to `range`, `xrange`, or `list(range(...))`. */ | ||
| 45 | + predicate call_to_range(DataFlow::Node node) { | ||
| 46 | + node = API::builtin(["range", "xrange"]).getACall() | ||
| 52 | 47 | or | |
| 53 | - /* In case points-to fails due to 'from six.moves import range' or similar. */ | ||
| 54 | - exists(string range | f.getNode().(Call).getFunc().(Name).getId() = range | | ||
| 55 | - range = "range" or range = "xrange" | ||
| 56 | - ) | ||
| 48 | + /* Handle 'from six.moves import range' or similar. */ | ||
| 49 | + node = API::moduleImport("six").getMember("moves").getMember(["range", "xrange"]).getACall() | ||
| 57 | 50 | or | |
| 58 | 51 | /* Handle list(range(...)) and list(list(range(...))) */ | |
| 59 | - f.(CallNode).(ControlFlowNodeWithPointsTo).pointsTo().getClass() = ClassValue::list() and | ||
| 60 | - points_to_call_to_range(f.(CallNode).getArg(0)) | ||
| 52 | + node = API::builtin("list").getACall() and | ||
| 53 | + call_to_range(node.(DataFlow::CallCfgNode).getArg(0)) | ||
| 61 | 54 | } | |
| 62 | 55 | ||
| 63 | 56 | /** Whether n is a use of a variable that is a not effectively a constant. */ | |
@@ -102,8 +95,8 @@ from For f, Variable v, string msg | |||
| 102 | 95 | where | |
| 103 | 96 | f.getTarget() = v.getAnAccess() and | |
| 104 | 97 | not f.getAStmt().contains(v.getAnAccess()) and | |
| 105 | - not points_to_call_to_range(f.getIter().getAFlowNode()) and | ||
| 106 | - not points_to_call_to_range(get_comp_iterable(f)) and | ||
| 98 | + not call_to_range(DataFlow::exprNode(f.getIter())) and | ||
| 99 | + not call_to_range(DataFlow::exprNode(get_comp_iterable(f).getNode())) and | ||
| 107 | 100 | not name_acceptable_for_unused_variable(v) and | |
| 108 | 101 | not f.getScope().getName() = "genexpr" and | |
| 109 | 102 | not empty_loop(f) and | |
| Back | FazBrowse Home | New Git URL |
0 commit comments