Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #73232

Asymmetry in globals __getitem__/__setitem__

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <robert@robertlehmann.de>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.103
X-Spam-Level *
X-Spam-Evidence '*H*': 0.80; '*S*': 0.00; 'interpreter': 0.05; 'def': 0.12; 'globals': 0.16; 'globals.': 0.16; 'key)': 0.16; 'key):': 0.16; 'slight': 0.16; '\xc2\xa0i': 0.16; 'ignore': 0.16; 'all,': 0.19; 'commit': 0.19; 'seems': 0.21; 'cheers,': 0.24; 'subject:/': 0.26; 'fixed': 0.29; 'robert': 0.30; 'message-id:@mail.gmail.com': 0.30; 'fine,': 0.31; 'loading': 0.31; 'loads': 0.31; 'class': 0.32; 'url:python': 0.33; 'noticed': 0.34; 'skip:_ 10': 0.34; 'received:209.85': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'doing': 0.36; 'url:org': 0.36; 'should': 0.36; 'received:209': 0.37; 'expected': 0.38; 'skip:& 10': 0.38; 'mapping': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'show': 0.63; 'skip:n 10': 0.64; 'from:addr:mail': 0.83; 'bare': 0.84; 'dict,': 0.84; 'dispatching': 0.84; 'ps.': 0.84; 'silently': 0.84; 'url:cpython': 0.84; 'url:rev': 0.84
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:sender:date:message-id:subject:from :to:content-type; bh=HOkogBVMH47pqakIMaGsukub+5IDPhDzPIn25OK0yTI=; b=hFldA/Ny+VxNvHyo9Rf6/tKviknM21Rs/hNuuBU3nNkZaebBWU3wehaqu8TuMka2A+ IuwBdaJ2GR6eskyPD5QgPhxO07pCUXsX+B88rw6VQ+QgKyQCVpHFztZ4HAog8vSQwCh+ 8ZDQsbvtf0ATpvSH5u+kd/Vxr3L1LL3dkxMjvwrQtFa8ud5xcmAKoUxjWET7DzJ5Xn7d FbCLwXzOtVqO+f0BQp1R0J/mMShCA/23BdbBa9n5q1QVmghCS+k7o3sGDL+jHBnftlNy 3Pn4F7IvUDIjaRMaA4WBkufj986TDZuyOj0FeubxTJ3z0oIAdAr/2WQe5an9wzDIuANz ptCw==
X-Gm-Message-State ALoCoQlxd9kuG2rGAoJ03WfTu6WgPUZoFsuSyAbdHkEVMAqiSKh6uugKg++Uc943/SSdGHxXnS3H
MIME-Version 1.0
X-Received by 10.236.47.201 with SMTP id t49mr6182412yhb.123.1402597109496; Thu, 12 Jun 2014 11:18:29 -0700 (PDT)
Sender robert@robertlehmann.de
Date Thu, 12 Jun 2014 20:18:29 +0200
X-Google-Sender-Auth 3bPMDSc85zqLcQrS3B6ew__q8lw
Subject Asymmetry in globals __getitem__/__setitem__
From Robert Lehmann <mail@robertlehmann.de>
To python-list@python.org
Content-Type multipart/alternative; boundary=089e01681de47996e904fba796c8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.11039.1402597117.18130.python-list@python.org> (permalink)
Lines 73
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1402597117 news.xs4all.nl 2908 [2001:888:2000:d::a6]:55785
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:73232

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

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?

Cheers,
Robert

PS.  I found a 3.3.x commit (e3ab8aa
<http://hg.python.org/cpython/rev/e3ab8aa0216c>) which fixed the
LOAD_GLOBAL opcode to support other types than dict, but STORE_GLOBAL seems
to use bare PyDict_SetItem instead of dispatching to PyObject_SetItem.

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Asymmetry in globals __getitem__/__setitem__ Robert Lehmann <mail@robertlehmann.de> - 2014-06-12 20:18 +0200
  Re: Asymmetry in globals __getitem__/__setitem__ Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-06-13 12:38 +1200
    Re: Asymmetry in globals __getitem__/__setitem__ Marko Rauhamaa <marko@pacujo.net> - 2014-06-13 09:07 +0300
      Re: Asymmetry in globals __getitem__/__setitem__ robert@robertlehmann.de - 2014-06-13 03:13 -0700
        Re: Asymmetry in globals __getitem__/__setitem__ Peter Otten <__peter__@web.de> - 2014-06-13 12:53 +0200
        Re: Asymmetry in globals __getitem__/__setitem__ Paul Sokolovsky <pmiscml@gmail.com> - 2014-06-13 14:28 +0300
          Re: Asymmetry in globals __getitem__/__setitem__ Marko Rauhamaa <marko@pacujo.net> - 2014-06-13 15:32 +0300

csiph-web