1

I was looking at the codetype object, and in particular the co_flags attribute.

I wrote a tiny function

def f(a,b,c,*args):
   print a,b,c,args

Which was compiled to a codetype object. examining the codetype object, the container (the outer codetype) co_flag is set to 64, the function (the inner codetype) is 71

According to the docs 0x40 (64) is set if the function has variable args (eg *args) and that bit is set on the outer and inner codetype objects.

I'm just wondering

  • why the container is set to 64 as all it does is set functions up eg there is no variable arguments used?
  • what the other bits in the function codetype co_flags do?

Is there a comprehensive list of the various flags as the documentation alludes to 'reserved for internal use' but nothing else is mentioned?

I'm using python 2.7.10

5
  • It's not clear how you're getting two code objects here. What do you mean by "container"? Could you provide the expressions you used to access these two code objects? Commented Nov 12, 2015 at 23:22
  • And what docs are you reading? As far as I'm aware, 0x4 is the varargs flag, not 0x40. Commented Nov 12, 2015 at 23:25
  • @user2357112 official docs and using the compile command the code is encloses in triple " Commented Nov 13, 2015 at 0:25
  • Can you link the docs you're reading? Commented Nov 13, 2015 at 0:27
  • @user2357112 not from my current device, but will do tomorrow, you are you right it is 0x4 - my bad Commented Nov 13, 2015 at 0:54

1 Answer 1

6

what the other bits in the function codetype co_flags do?

From dis.COMPILER_FLAG_NAMES:

  1 OPTIMIZED
  2 NEWLOCALS
  4 VARARGS
  8 VARKEYWORDS
 16 NESTED
 32 GENERATOR
 64 NOFREE
128 COROUTINE
256 ITERABLE_COROUTINE

It is from Python 3 but flags that are available in Python 2.7 have kept the same values from Include/code.h. They are documented only by Jython.

the function (the inner codetype) is 71

71 = 1 + 2 + 4 + 64:

  1 OPTIMIZED
  2 NEWLOCALS
  4 VARARGS
 64 NOFREE

Example (in Python 3):

>>> import dis
>>> def f(a, b, c, *args):
...     print(a, b, c, args)
... 
>>> dis.show_code(f)
Name:              f
Filename:          <stdin>
Argument count:    3
Kw-only arguments: 0
Number of locals:  4
Stack size:        5
Flags:             OPTIMIZED, NEWLOCALS, VARARGS, NOFREE
Constants:
   0: None
Names:
   0: print
Variable names:
   0: a
   1: b
   2: c
   3: args

why the container is set to 64 as all it does is set functions up eg there is no variable arguments used?

As I understand "the container" is a module (check co_name). NOFREE just means that there are no freevars, cellvars that is (always?) true for a module. On pypy co_flags is 0 (the flags are the same there).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.