Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: `exec`-based routine crashes app upon migration from 3.4.3 to python 3.5.2. Date: Fri, 29 Jul 2016 04:40:53 +1000 Lines: 35 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de V1bCPGb/qf9STsDQbuNOvw1i+Jve6W+BP22GfnOUxF7w== 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; 'cc:addr:python-list': 0.09; 'debugger': 0.09; 'enjoys': 0.09; 'globals': 0.09; 'identifier,': 0.09; 'obj': 0.09; 'throws': 0.09; 'python': 0.10; 'def': 0.13; 'subject:python': 0.14; 'interpreter': 0.15; 'result.': 0.15; 'subject: \n ': 0.15; '2016': 0.16; 'breakpoints': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 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; 'skip:` 20': 0.18; 'all,': 0.20; 'math': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'exec': 0.22; 'function:': 0.22; 'simpler': 0.22; 'am,': 0.23; 'thus': 0.24; 'header:In-Reply-To:1': 0.24; "i've": 0.25; 'fri,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'executing': 0.27; '(maybe': 0.29; 'crash': 0.29; 'dictionary': 0.29; 'code': 0.30; 'strongly': 0.30; "i'd": 0.31; "can't": 0.32; 'skip:_ 10': 0.32; 'received:google.com': 0.35; 'but': 0.36; 'there': 0.36; 'sure': 0.39; 'subject:from': 0.39; 'some': 0.40; 'ever': 0.60; 'back': 0.62; 'more': 0.63; 'safe': 0.63; 'legal': 0.66; 'jul': 0.72; 'bite': 0.84; 'chrisa': 0.84; "it'd": 0.84; 'locals': 0.84; 'you.)': 0.84; 'to:none': 0.91; '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:cc; bh=44TkD5whhsgQ/aCbdv/Jece/YUBt5entDczcko3LRas=; b=Ziju9psjy+hSSjwKPpP4bgg0Z1ZHJErOY+VjMOUsE0MQj1DwwXl6UGK33mPDmU0rvl 6rDs5yW1+cnW+eLnqtrSP6pRQbH5kBUCu1FmZJUSUdtxCU5cSntE3OamNgtcJVrWIc1H y88xc0rYlknnXhO80mgYIuLUyrvdxX+BY90CU+SJS0lpubtem7nz+QQGMl+jLNH1IzT9 Cypq+w/TDEgGg8wB+R+rMJ+/xlVxkMz0+bdzlGL9H5D4G7NTSexxH5kjEO5llwwlI104 YfSxWahZEqbJNjuU+Na6C4n4ZtmVvvlYJ9BHfAVsucaeVr+HOtJrfpWbuPkbTh9WZ8AB a6+A== 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:cc; bh=44TkD5whhsgQ/aCbdv/Jece/YUBt5entDczcko3LRas=; b=ZitbxrKoxmq2eq0avFV/XkpOSaqj2L5gIyAY4jcR5qCrZFde/C9g7aprnQSz862BXH +dOeQQ2vFjZvZh6dN+hnl2F/8RNIGOt2kNOF+nB+FJRFEgy9np9AQos4YsdsdPRhdlyk HJB65cq3+5PBFaQh54tDEl7UQJolwh2TgQZfkWAANkrV872UMZTJ5g3BKQBNcgCdaTln 6XpWE3bMVBfAASnyT+T1ZR2oqHJrfuS1wv5MPyEVi/CV1uSLs/eodBambadA/YqnI9PO xatU9iLozEtNekLrijRu/aNKY2+4aBolGzol8ERaOVIC6dd60a8BeoEE3PriHT4G2K55 fHYQ== X-Gm-Message-State: AEkooutMumo7bpXuUp0t5PflwQxYF+wsHgvdVamD/mUvQr/qChFzxN0iPq6PxuXdsk7X53UADnDl4Vl4FjbLSg== X-Received: by 10.28.45.69 with SMTP id t66mr37959102wmt.41.1469731254050; Thu, 28 Jul 2016 11:40:54 -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:111990 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? 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