Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: eryk sun Newsgroups: comp.lang.python Subject: Re: `exec`-based routine crashes app upon migration from 3.4.3 to python 3.5.2. Date: Thu, 28 Jul 2016 21:18:45 +0000 Lines: 48 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de hLbyNdC/WqXa4ctBuB+AwAOoyHYHeWyGSeJRPRVf3Wsg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'none,': 0.05; 'none:': 0.05; 'one?': 0.05; 'assignment': 0.07; 'debugger': 0.09; 'enjoys': 0.09; 'executes': 0.09; 'globals': 0.09; 'obj': 0.09; 'pointless': 0.09; 'throws': 0.09; 'example:': 0.10; 'python': 0.10; 'def': 0.13; 'subject:python': 0.14; 'interpreter': 0.15; 'subject: \n ': 0.15; 'thu,': 0.15; '2016': 0.16; 'breakpoints': 0.16; 'keyerror': 0.16; 'parameter,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'scope.': 0.16; 'subject:based': 0.16; 'wingware': 0.16; 'wrote:': 0.16; 'app': 0.16; 'module,': 0.18; 'skip:` 20': 0.18; '>>>': 0.20; 'math': 0.20; 'to:name:python- list@python.org': 0.20; 'exec': 0.22; 'function:': 0.22; 'parameter': 0.22; 'am,': 0.23; 'code,': 0.23; 'passing': 0.23; 'sets': 0.23; 'thus': 0.24; 'header:In-Reply-To:1': 0.24; "i've": 0.25; 'chris': 0.26; 'fri,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'executing': 0.27; 'fine': 0.28; 'crash': 0.29; 'declared': 0.29; 'instruction': 0.29; 'code': 0.30; "can't": 0.32; 'skip:_ 10': 0.32; 'class': 0.33; 'problem': 0.33; 'received:google.com': 0.35; 'could': 0.35; 'but': 0.36; 'there': 0.36; 'to:addr:python- list': 0.36; 'pm,': 0.36; 'sure': 0.39; 'subject:from': 0.39; 'to:addr:python.org': 0.40; 'ever': 0.60; 'back': 0.62; 'here.': 0.62; 'safe': 0.63; 'legal': 0.66; 'jul': 0.72; 'bite': 0.84; 'compiles': 0.84; 'dict.': 0.84; 'locals': 0.84; "op's": 0.84; 'surface': 0.84; 'you.)': 0.84; '1:47': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=vlZgN7C6DkGSKwukYROasKRIjJ0/Ie7cEpDVpbeFw0o=; b=iG3/GVfqEVj/jBexQc1+uEhtsKz9Yu+BUIs/0qJj6dFvS9i+A7xsC1J3Af22d7Ip3h QMlUT9o+iA+IGLYhE3m68NdQ1tPWwwPYYp+mVQ2sVhvTB4pPOzUiXNZay8iDB9FiV2+f qInJIi0POfZULuGErpAnOgaCq2pSuFyfp+pTTbEGMe5jB3EOPHA475RiX6XFdj9NCHog 3n613oRRQBtkHZnt7pD0lNeuIfSw21sW2qI8mkrBcopufB8KBNNW8ZtMsZBPQFmTwlut PG3PkVeb288BJLrlQmBPeIYIIey6WnhvPBbaGb+0aaRQmJmQ/P5yOl71t5FYc9Wpy813 p9yQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=vlZgN7C6DkGSKwukYROasKRIjJ0/Ie7cEpDVpbeFw0o=; b=RBW5zoONIxA39N1uAFMUstNLWFAoIyysJtmj9hP176KOt5jVe752mx1MUqrmLGAj+m fng5uG1JK1yWeQdZCg45eUi6TFpPNVLBKnJPVGLop4R0THDfecm6aOT43p39NbzIUtHj FWjITzHO1QlpFvxGOefHOy8q6wA/x77ueT9tj34hDUxnj6wK28LBAKI4kvERR01rNMKy AFTbHhOVbqVmBGfzgLJTH0LRHMlv5SLTrTa9UwAF93owVamp/ExudveZ94EZHFi29mSg MGnEla1EHibvvr7g7ZPczEEtT2gEbcrTnzxs2/IW5oLsiFNHhqAonTGBbHGps5yn/8sj Mugw== X-Gm-Message-State: AEkoouvHifbMbhVde77+1I2gEt7/XTdPtBZfqk7hvCI8XHcoGw6iuMDGG93AtQ8oHHcEbpqOQKntl0fTYKRWfw== X-Received: by 10.107.20.206 with SMTP id 197mr45592056iou.103.1469740765293; Thu, 28 Jul 2016 14:19:25 -0700 (PDT) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: Xref: csiph.com comp.lang.python:111995 On Thu, Jul 28, 2016 at 6:40 PM, Chris Angelico wrote: > On Fri, Jul 29, 2016 at 1:47 AM, Enjoys Math 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? Passing `None` for the globals parameter executes the code using the current globals(). This is pointless since the OP could just use exec() without passing either globals or locals to use the current globals and locals dicts. exec() compiles code like a module, so assignment uses the STORE_NAME instruction unless the name is declared `global`. STORE_NAME sets the value in the frame's f_locals dict. That much is fine with the OP's code, so on the surface I don't see the problem here. For example: class C: def method(self): exec('obj = int(self)') return locals()['obj'] def __int__(self): return 42 >>> C().method() 42