`exec`-based routine crashes app upon migration from 3.4.3 to python 3.5.2.

Chris Angelico rosuav at gmail.com
Thu Jul 28 14:40:53 EDT 2016


On Fri, Jul 29, 2016 at 1:47 AM, Enjoys Math <enjoysmath at gmail.com> wrote:
> I've manually set breakpoints and traced this app crash back to this
> function:
>
>     def loadLSobjsOfType(self, objType, listJ):
>         if listJ != None:
>             for objJ in listJ:
>                 _locals = locals()
>                 exec('obj = ' + objType + '(self)', None, _locals)
>                 obj = _locals['obj']
>                 obj.loadJson(objJ)
>                 self.addLSobjOfType(objType, obj)
>
> when breakpoints are set on `obj=_locals['obj']` my wingware debugger kicks
> in and throws a KeyError exeption.
>
> So what's the proper way to get the return value of an exec call when there
> is one?

(Side point: Be aware that it's perfectly legal for a Python
interpreter to make mutations to locals() actually significant. You're
safe here, but just make sure that can't ever bite you.)

By the look of things, you're executing code using a locals but no
globals, and it's executing code at the global scope. Can you simply
remove the 'None' parameter, and thus make _locals be the exec'd
code's locals AND globals both?

Incidentally, what exactly is objType? By the name, I'd suspect that
it's a simple identifier, in which case I would strongly recommend not
using exec at all, but just looking up that name in some dictionary
(maybe globals()??) and calling the result. It'd be a lot simpler and
more efficient.

ChrisA



More information about the Python-list mailing list