Skip to content

Commit ff86c1d

Browse files
committed
Python: Port NonIteratorInForLoop.ql
Same comment as for the preceding commit. We lose one test result due to the fact that we don't know what to do about `for ... in 1` (because `1` is an instance of a built-in). I'm going to defer addressing this until we get some modelling of built-in types.
1 parent 34837ea commit ff86c1d

File tree

3 files changed

+12
-14
lines changed

3 files changed

+12
-14
lines changed

python/ql/src/Statements/NonIteratorInForLoop.ql

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,15 @@
1212
*/
1313

1414
import python
15-
private import LegacyPointsTo
15+
private import semmle.python.dataflow.new.internal.DataFlowDispatch
1616

17-
from For loop, ControlFlowNodeWithPointsTo iter, Value v, ClassValue t, ControlFlowNode origin
17+
from For loop, Expr iter, Class cls
1818
where
19-
loop.getIter().getAFlowNode() = iter and
20-
iter.pointsTo(_, v, origin) and
21-
v.getClass() = t and
22-
not t.isIterable() and
23-
not t.failedInference(_) and
24-
not v = Value::named("None") and
25-
not t.isDescriptorType()
26-
select loop, "This for-loop may attempt to iterate over a $@ of class $@.", origin,
27-
"non-iterable instance", t, t.getName()
19+
iter = loop.getIter() and
20+
classInstanceTracker(cls).asExpr() = iter and
21+
not DuckTyping::isIterable(cls) and
22+
not DuckTyping::isDescriptor(cls) and
23+
not (loop.isAsync() and DuckTyping::hasMethod(cls, "__aiter__")) and
24+
not DuckTyping::hasUnresolvedBase(getADirectSuperclass*(cls))
25+
select loop, "This for-loop may attempt to iterate over a $@ of class $@.", iter,
26+
"non-iterable instance", cls, cls.getName()
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
| async_iterator.py:26:11:26:34 | For | This for-loop may attempt to iterate over a $@ of class $@. | async_iterator.py:26:20:26:33 | ControlFlowNode for MissingAiter() | non-iterable instance | async_iterator.py:13:1:13:19 | class MissingAiter | MissingAiter |
2-
| statements_test.py:34:5:34:19 | For | This for-loop may attempt to iterate over a $@ of class $@. | statements_test.py:34:18:34:18 | ControlFlowNode for IntegerLiteral | non-iterable instance | file://:0:0:0:0 | builtin-class int | int |
1+
| async_iterator.py:26:11:26:34 | For | This for-loop may attempt to iterate over a $@ of class $@. | async_iterator.py:26:20:26:33 | MissingAiter() | non-iterable instance | async_iterator.py:13:1:13:19 | Class MissingAiter | MissingAiter |
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
| test.py:50:1:50:23 | For | This for-loop may attempt to iterate over a $@ of class $@. | test.py:50:10:50:22 | ControlFlowNode for NonIterator() | non-iterable instance | test.py:45:1:45:26 | class NonIterator | NonIterator |
1+
| test.py:50:1:50:23 | For | This for-loop may attempt to iterate over a $@ of class $@. | test.py:50:10:50:22 | NonIterator() | non-iterable instance | test.py:45:1:45:26 | Class NonIterator | NonIterator |

0 commit comments

Comments
 (0)