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

Random832 random832 at fastmail.com
Thu Jul 28 16:46:33 EDT 2016


On Thu, Jul 28, 2016, at 11:47, Enjoys Math wrote:
> So what's the proper way to get the return value of an exec call when
> there is one?

Exec calls do not have return values.

If you need to pass an object out of the exec call to the surrounding
context, you can wrap it in an exception and throw it.

class ObjectWrapperException(Exception):
  def __init__(self, obj):
    self.obj = obj
    super().__init__()

code = 'obj = ' + objType + '(self)\nraise ObjectWrapperException(obj)'
try:
    exec(code, None, _locals)
except ObjectWrapperException as e
    obj = e.obj



More information about the Python-list mailing list