mirror of
https://github.com/github/codeql.git
synced 2026-05-05 21:55:19 +02:00
Python: Only alert on Python 2 code
since - Python 3 is ok from 3.7 onwards - support for Python 3.6 was just dropped - we do not actually know the minor version of the analysed code (only of the extractor)
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
| test.py:5:15:5:22 | ControlFlowNode for next() | Call to next() in a generator |
|
||||
| test.py:10:20:10:27 | ControlFlowNode for next() | Call to next() in a generator |
|
||||
@@ -0,0 +1 @@
|
||||
Exceptions/UnguardedNextInGenerator.ql
|
||||
64
python/ql/test/2/query-tests/Exceptions/generators/test.py
Normal file
64
python/ql/test/2/query-tests/Exceptions/generators/test.py
Normal file
@@ -0,0 +1,64 @@
|
||||
#Unguarded calls to next()
|
||||
|
||||
def bad1(it):
|
||||
while True:
|
||||
yield next(it)
|
||||
|
||||
def bad2(seq):
|
||||
it = iter(seq)
|
||||
#Not OK as seq may be empty
|
||||
raise KeyError(next(it))
|
||||
yield 0
|
||||
|
||||
def ok1(seq):
|
||||
#Not a generator
|
||||
it = iter(seq)
|
||||
#Not OK as seq may be empty
|
||||
raise KeyError(next(it))
|
||||
|
||||
def ok2(seq):
|
||||
if seq:
|
||||
it = iter(seq)
|
||||
#OK seq is non-empty so next(it) will not raise StopIteration
|
||||
raise KeyError(next(it))
|
||||
yield 0
|
||||
|
||||
def explicit_raise_stop_iter(seq):
|
||||
for i in seq:
|
||||
yield seq
|
||||
raise StopIteration()
|
||||
|
||||
def ok3(seq):
|
||||
it = iter(seq)
|
||||
try:
|
||||
yield next(iter)
|
||||
except StopIteration:
|
||||
return
|
||||
|
||||
def ok4(seq, ctx):
|
||||
try:
|
||||
with ctx:
|
||||
yield next(iter)
|
||||
except StopIteration:
|
||||
return
|
||||
|
||||
#ODASA-6536
|
||||
def next_in_comp(seq, fields):
|
||||
seq_iter = iter(seq)
|
||||
values = [ next(seq_iter) if f.attname in NAMES else DEFAULT for f in fields ]
|
||||
return values
|
||||
|
||||
def ok5(seq):
|
||||
yield next(iter([]), 'foo')
|
||||
|
||||
def ok6(seq):
|
||||
yield next(iter([]), default='foo')
|
||||
|
||||
# Handling for multiple exception types, one of which is `StopIteration`
|
||||
# Reported as a false positive in github/codeql#6227
|
||||
def ok7(seq, ctx):
|
||||
try:
|
||||
with ctx:
|
||||
yield next(iter)
|
||||
except (StopIteration, MemoryError):
|
||||
return
|
||||
Reference in New Issue
Block a user