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: 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 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 --089e01681de47996e904fba796c8 Content-Type: text/plain; charset=UTF-8 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 ) 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. --089e01681de47996e904fba796c8 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Hi all,

I have noticed there is a sligh= t asymmetry in the way the interpreter (v3.3.5, reproduced also in v3.5.x) = loads and stores globals. =C2=A0While loading globals from a custom mapping= triggers __getitem__ just fine, writing seems to silently ignore __setitem= __.

class Namespace(d= ict):
=C2=A0 =C2=A0 = def __getitem__(self, key):
=C2=A0 =C2=A0 =C2=A0 =C2=A0 print("getitem", key)<= /div>
=C2=A0 =C2=A0 def __setitem__(se= lf, key, value):
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 print("setitem", key, value)

def fun():
=C2=A0 =C2=A0 global x, y
=C2=A0 =C2=A0 x =C2=A0= # should call globals.__getitem__
=C2=A0 =C2=A0 y =3D 1 =C2=A0# sh= ould call globals.__setitem__

exec(= fun.__code__, Namespace())
# =3D> getitem x
=

I would have expected "setitem y 1" to show u= p as well, but to no avail. =C2=A0Am I doing something wrong? =C2=A0Is this= on purpose?

Cheers,
Robert

PS. = =C2=A0I found a 3.3.x commit (e3ab8aa) which fixed the LOAD_GLOBAL opcode to support oth= er types than dict, but STORE_GLOBAL seems to use bare PyDict_SetItem inste= ad of dispatching to PyObject_SetItem.

--089e01681de47996e904fba796c8--