Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '2.7': 0.04; 'subject:code': 0.07; 'subject:object': 0.07; '%s"': 0.09; 'subject:2.7': 0.09; 'tuple': 0.09; 'variables.': 0.09; 'def': 0.15; "'w',": 0.16; '(name,': 0.16; '**kwargs):': 0.16; '*args,': 0.16; '2.7:': 0.16; 'attributes:': 0.16; 'incomplete': 0.16; 'subject:between': 0.16; 'superfluous': 0.16; 'variables;': 0.16; 'thanks!': 0.16; '>>>': 0.18; 'arguments': 0.18; 'to:name:python- list': 0.18; 'seems': 0.20; 'update.': 0.24; 'code': 0.25; 'guess': 0.26; 'up.': 0.26; 'tried': 0.26; 'function': 0.27; 'version,': 0.28; 'message-id:@mail.gmail.com': 0.29; 'module': 0.30; '3.x': 0.30; 'arguments.': 0.30; 'url:dev': 0.30; 'url:library': 0.31; 'list': 0.32; 'anyone': 0.32; 'actually': 0.33; 'probably': 0.33; 'there': 0.33; 'to:addr:python-list': 0.33; 'wondering': 0.33; 'difference': 0.34; 'however,': 0.34; 'doc': 0.34; 'exercises': 0.34; 'object': 0.35; 'url:python': 0.36; 'several': 0.37; 'but': 0.37; 'could': 0.38; 'received:google.com': 0.38; 'url:org': 0.38; 'received:209.85': 0.38; 'url:docs': 0.39; 'listing': 0.39; 'why': 0.39; 'to:addr:python.org': 0.39; 'description': 0.39; "it's": 0.40; 'results': 0.61; 'closed': 0.62; 'free': 0.63; 'here': 0.65; 'received:209.85.215.174': 0.67; 'received:mail- ey0-f174.google.com': 0.67; 'skip:b 50': 0.67; 'care': 0.71; '[3]': 0.73; '3.3': 0.84; 'compose': 0.84; 'differences,': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; bh=LquQyQjY7Fp0A+nHhyXmbZKeh1jXw8ShQdDN53JeEx8=; b=UKSh5nBGoe9knfScbvFfk54IH3iM0O9NjLkmlvJuhBfJzX+gh+czkrGpSC7M6Ksn1g 5AxrXDj/qHzwPaYBmiuOkSwqTY8Cm0KCInCs29rOA9X8rpItxQfpx0izdi7oFHT/fEH2 /hdCje7ccfIuaaLH8BS5tgDpIDB5vtzFHdwJY= MIME-Version: 1.0 Date: Wed, 10 Aug 2011 18:38:17 -0600 Subject: code object differences between 2.7 and 3.3a From: Eric Snow To: python-list 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: 79 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1313023099 news.xs4all.nl 23923 [2001:888:2000:d::a6]:39672 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:11160 Specifically, I am wondering why there is a difference for co_names. Here is a function that exercises the different code object pieces[1]: def g(y=5): a = 7 def f(x, w=y, z=4, *args, **kwargs): b = a c = global_x return f f1 = g() Here are the results for 2.7: >>> for name in dir(f1.func_code): print("%s -> %s" % (name, getattr(f1.func_code, name))) ... co_argcount -> 3 co_cellvars -> () co_code -> }t}dS co_consts -> (None,) co_filename -> co_firstlineno -> 3 co_flags -> 15 co_freevars -> ('a',) co_lnotab -> co_name -> f co_names -> ('a', 'b', 'global_x', 'c') co_nlocals -> 7 co_stacksize -> 1 co_varnames -> ('x', 'w', 'z', 'args', 'kwargs', 'c', 'b') And for 3.3: >>> for name in dir(f1.__code__): print("%s -> %s" % (name, getattr(f1.__code__, name))) ... co_argcount -> 3 co_cellvars -> () co_code -> b'\x88\x00\x00}\x05\x00t\x00\x00}\x06\x00d\x00\x00S' co_consts -> (None,) co_filename -> co_firstlineno -> 3 co_flags -> 31 co_freevars -> ('a',) co_kwonlyargcount -> 0 co_lnotab -> b'\x00\x01\x06\x01' co_name -> f co_names -> ('global_x',) co_nlocals -> 7 co_stacksize -> 1 co_varnames -> ('x', 'w', 'z', 'args', 'kwargs', 'b', 'c') While there are several differences, the one I care about is co_name. For 2.7 it's what I would expect. However, for 3.3 it's not[2][3]. It is actually nicer for my application this way, but I want to verify the situation before I get me hopes up. :) Before I go email-list-diving or digging through PyEval_EvalCodeEx, I wanted to see if anyone has any insight about this change in co_names. Thanks! -eric [1] yes, 3.x also supports keyword-only arguments. I tried this on 3.3 with extra kw-only arguments and it was the same outcome. [2] The documentation for the inspect module gives an incomplete listing of the code object attributes: The description of co_names there ("tuple of names of local variables") seems inconsistent with what I am seeing. It's probably just that I am misinterpreting that list or the doc needs an update. [3] My guess is that co_names was seen as bloated and the superfluous items removed, leaving only the un-closed free variables; co_freevars is the closed free variables. To get the same tuple as the 2.7 version, you could compose it from co_freevars, co_names, and the end of co_varnames.