Skip to content

Commit f4e5079

Browse files
authored
Merge pull request github#2991 from BekaValentine/python-objectapi-to-valueapi-unguardednextingenerator
Python: ObjectAPI to ValueAPI: UnguardedNextInGenerator
2 parents a309545 + 06f0947 commit f4e5079

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

python/ql/src/Exceptions/UnguardedNextInGenerator.ql

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212

1313
import python
1414

15-
FunctionObject iter() {
16-
result = Object::builtin("iter")
15+
FunctionValue iter() {
16+
result = Value::named("iter")
1717
}
1818

19-
BuiltinFunctionObject next() {
20-
result = Object::builtin("next")
19+
BuiltinFunctionValue next() {
20+
result = Value::named("next")
2121
}
2222

2323
predicate call_to_iter(CallNode call, EssaVariable sequence) {
@@ -28,6 +28,10 @@ predicate call_to_next(CallNode call, ControlFlowNode iter) {
2828
iter = next().getArgumentForCall(call, 0)
2929
}
3030

31+
predicate call_to_next_has_default(CallNode call) {
32+
exists(call.getArg(1)) or exists(call.getArgByName("default"))
33+
}
34+
3135
predicate guarded_not_empty_sequence(EssaVariable sequence) {
3236
sequence.getDefinition() instanceof EssaEdgeRefinement
3337
}
@@ -43,12 +47,13 @@ predicate iter_not_exhausted(EssaVariable iterator) {
4347
predicate stop_iteration_handled(CallNode call) {
4448
exists(Try t |
4549
t.containsInScope(call.getNode()) and
46-
t.getAHandler().getType().refersTo(theStopIterationType())
50+
t.getAHandler().getType().pointsTo(ClassValue::stopIteration())
4751
)
4852
}
4953

5054
from CallNode call
5155
where call_to_next(call, _) and
56+
not call_to_next_has_default(call) and
5257
not exists(EssaVariable iterator |
5358
call_to_next(call, iterator.getAUse()) and
5459
iter_not_exhausted(iterator)
@@ -58,4 +63,3 @@ not exists(Comp comp | comp.contains(call.getNode())) and
5863
not stop_iteration_handled(call)
5964

6065
select call, "Call to next() in a generator"
61-

python/ql/test/query-tests/Exceptions/generators/test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,9 @@ def next_in_comp(seq, fields):
4747
seq_iter = iter(seq)
4848
values = [ next(seq_iter) if f.attname in NAMES else DEFAULT for f in fields ]
4949
return values
50+
51+
def ok5(seq):
52+
yield next(iter([]), 'foo')
53+
54+
def ok6(seq):
55+
yield next(iter([]), default='foo')

0 commit comments

Comments
 (0)