Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.011 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'interpreter': 0.05; "'',": 0.07; 'def': 0.12; 'bug': 0.12; 'globals': 0.16; 'globals.': 0.16; 'key)': 0.16; 'key):': 0.16; 'lehmann': 0.16; 'slight': 0.16; 'ignore': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'thu,': 0.19; 'seems': 0.21; '>>>': 0.22; 'subject:/': 0.26; 'header:In-Reply- To:1': 0.27; 'robert': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'fine,': 0.31; 'loading': 0.31; 'loads': 0.31; 'class': 0.32; 'noticed': 0.34; 'skip:_ 10': 0.34; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'doing': 0.36; 'should': 0.36; 'expected': 0.38; 'mapping': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'does': 0.39; '12,': 0.39; 'to:addr:python.org': 0.39; 'show': 0.63; 'skip:n 10': 0.64; 'silently': 0.84; 'not:': 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 :content-type; bh=ySGUg1jy5fe4VbmslAc8USFo9xQdxjsIXDK0HVzi/2Q=; b=gn03mMjDsb3QK+GJtS/glhuEAyHHih2Nnyq+c9ZeDHtIrCYr0ocI7wGysPG8qiv3Oh uegPl6SFzDvGHKJLeTZVADEuF+58luYQdwJnr3lHJ/EEEO2ONfUDybY6ECH/HIPJabQe NTkz7j596pja9cecQz02kAoLb20mCIV1W6lNp5quTFXQczGx48wpYIBOCgDDR5vAK9wQ p8vPdIVX8AMVaoy46YK+LCqP+PqkiSIJkndibhaARkrGIe9ZwrhcmyrUwKGzeFUb5uoy hOxEnBjXtNzZh6T+f4BqL7wYGMWS+WIf7Him1ftpxsL5PAsE67cbf4zzq2VdYkwEXedi ILiA== X-Received: by 10.236.229.133 with SMTP id h5mr18329533yhq.64.1402598968709; Thu, 12 Jun 2014 11:49:28 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Thu, 12 Jun 2014 12:48:48 -0600 Subject: Re: Asymmetry in globals __getitem__/__setitem__ To: Python Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 46 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1402598976 news.xs4all.nl 2863 [2001:888:2000:d::a6]:32834 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:73234 On Thu, Jun 12, 2014 at 12:18 PM, Robert Lehmann wrote: > Hi all, > > I have noticed there is a slight asymmetry in the way the interpreter > (v3.3.5, reproduced also in v3.5.x) loads and stores globals. While loading > globals from a custom mapping triggers __getitem__ just fine, writing seems > to silently ignore __setitem__. > > class Namespace(dict): > def __getitem__(self, key): > print("getitem", key) > def __setitem__(self, key, value): > print("setitem", key, value) > > def fun(): > global x, y > x # should call globals.__getitem__ > y = 1 # should call globals.__setitem__ > > exec(fun.__code__, Namespace()) > # => getitem x > > I would have expected "setitem y 1" to show up as well, but to no avail. Am > I doing something wrong? Is this on purpose? Seems like a bug to me. I note that the STORE_NAME opcode does call __setitem__: >>> code = compile('x = 1', '', 'exec') >>> dis.dis(code) 1 0 LOAD_CONST 0 (1) 3 STORE_NAME 0 (x) 6 LOAD_CONST 1 (None) 9 RETURN_VALUE >>> exec(code, Namespace()) setitem x 1 But STORE_GLOBAL does not: >>> code = compile('global x; x = 1', '', 'exec') >>> dis.dis(code) 1 0 LOAD_CONST 0 (1) 3 STORE_GLOBAL 0 (x) 6 LOAD_CONST 1 (None) 9 RETURN_VALUE >>> exec(code, Namespace())