Dodgy indeed.
From the doc:
If two separate objects are given as globals and locals, the code will be executed as if it were embedded in a class definition.
(Note that when you run exec(..., globals(), locals())
at module level, this does not apply, because globals() is locals()
, i.e. not two separate objects).
This indicates you can simply reproduce the problem by running this script:
class A(object):
items = (
'foo/bar',
'foo/baz',
'foof',
'barf/fizz',
)
whitelist = (
'foo/',
)
for key in items:
try:
# Not terribly efficient, but who cares; computers are fast.
next(True for prefix in whitelist if key.startswith(prefix))
# Found!
print(key)
except StopIteration:
pass
"Ok, but why do I get the error here?"
So glad you asked. The answer is here.