Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed5.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'cpython': 0.05; 'defaults': 0.07; 'raises': 0.07; 'subject:when': 0.07; 'foo,': 0.09; 'variations': 0.09; 'am,': 0.12; 'def': 0.14; 'this:': 0.15; '(lambda': 0.16; 'conclusion:': 0.16; 'expected,': 0.16; 'fascinating!': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'lambda': 0.16; 'opcode': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'arguments': 0.18; 'received:209.85.210.174': 0.18; 'received:mail-iy0-f174.google.com': 0.18; "doesn't": 0.23; 'header:In-Reply-To:1': 0.23; 'code': 0.25; 'looks': 0.28; 'message-id:@mail.gmail.com': 0.29; 'unable': 0.30; 'nov': 0.31; 'to:addr:python-list': 0.32; 'normally': 0.34; 'setting': 0.34; '(a)': 0.34; 'file': 0.36; 'fri,': 0.36; 'variables': 0.36; 'but': 0.37; 'received:google.com': 0.38; 'some': 0.38; 'received:209.85': 0.38; 'either': 0.39; "it's": 0.39; 'subject:: ': 0.39; 'to:addr:python.org': 0.39; 'received:209': 0.40; 'did': 0.40; '2011': 0.62; 'theme': 0.67; 'show': 0.67; 'have.': 0.77; '256': 0.84; 'presumably': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=5mPtMGHgBinjwg5c60T7lsXjDxED9N+rrD42ainm2EE=; b=x9FFxATwjEqDBHIRQzXJr/GDl62th6kBTA0BIMMUXZT17gmkmdxQmgH5AsxxUzvo3v wChIx4Cz+jf457/A1/KZvC//GBH9TpubQMStBVc51C/9LyI0ror30Zqz5Vu8uj0OLcNe Wdc+3JwjLgRDO0BqQd7sTANAufQ4hQm0dOFJ0= MIME-Version: 1.0 In-Reply-To: References: Date: Fri, 4 Nov 2011 10:51:46 +1100 Subject: Re: SystemError when defining From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 50 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1320364309 news.xs4all.nl 6944 [2001:888:2000:d::a6]:40946 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:15312 On Fri, Nov 4, 2011 at 9:24 AM, Joshua Landau wrote: > Non-lambdas work as expected, but a lambda inside a non-lambda still > exhibits this behaviour. > Conclusion: > When setting defaults to keyword-only arguments in lambdas which are inside > non-global scopes, cPython is unable to access the global scope and raises > SystemError. Fascinating! I did some introspection with this: >>> def foo(): a=1 # to make sure local variables do exist return (lambda *,keyword_only=global_variable: None)() >>> dis.dis(foo) 2 0 LOAD_CONST 1 (1) 3 STORE_FAST 0 (a) 3 6 LOAD_CONST 2 ('keyword_only') 9 LOAD_NAME 0 (global_variable) 12 LOAD_CONST 3 ( at 0x00FC7340, file "", line 3>) 15 MAKE_FUNCTION 256 18 CALL_FUNCTION 0 21 RETURN_VALUE >>> def foo(): a=1 return (lambda argument=global_variable: None)() >>> dis.dis(foo) 2 0 LOAD_CONST 1 (1) 3 STORE_FAST 0 (a) 3 6 LOAD_GLOBAL 0 (global_variable) 9 LOAD_CONST 2 ( at 0x00F95E30, file "", line 3>) 12 MAKE_FUNCTION 1 15 CALL_FUNCTION 0 18 RETURN_VALUE Variations on the theme show that during compilation of foo, the name is normally cemented as either a global or a local - but if it's keyword-only, then a LOAD_NAME opcode is emitted. It's the code for LOAD_NAME that looks for locals, which presumably a lambda doesn't have. ChrisA