I am using some old code base that was initially written in python2. The devs say they have ported it to python3 however I found a all the code that uses exec is completely broken. Below you can find an example like the one I am trying to fix:
def function_with_exec(table):
(sess, tgt) = 3, None
try:
exec("ec = some_object.{}_get_entry_count(sess_hdl=sess, dev_tgt=tgt)".
format(table))
except AttributeError:
return []
ec = some_function(ec)
When running this code, I get: `UnboundLocalError: local variable 'ec' referenced before assignment.
I have been reading that a way to modify the global ec variable from the exec function is to pass the globals() as second argument. However, when I do that, then the variables sess and tgt which are also used in the expression, become undefined too.
I really do not find a way to solve this.