Skip to content

How is __name__ loaded into the locals/globals/builtins #17

@ppegusii

Description

@ppegusii
def main():
    print 'Hello World!'

if __name__ == '__main__':
    main()
./doc/nedpycparser.py ./sample_pycs/helloworld.pyc 
magic 03f30d0a
moddate some (time)
code
   argcount 0
   nlocals 0
   stacksize 2
   flags 0040
   code
      6400008400005a00006501006401006b0200721f00650000830000016e00
      0064020053
  1           0 LOAD_CONST               0 (<code object main at 0x7fbead369e30, file "helloworld.py", line 1>)
              3 MAKE_FUNCTION            0
              6 STORE_NAME               0 (main)

  4           9 LOAD_NAME                1 (__name__)
             12 LOAD_CONST               1 ('__main__')
             15 COMPARE_OP               2 (==)
             18 POP_JUMP_IF_FALSE       31

  5          21 LOAD_NAME                0 (main)
             24 CALL_FUNCTION            0
             27 POP_TOP             
             28 JUMP_FORWARD             0 (to 31)
        >>   31 LOAD_CONST               2 (None)
             34 RETURN_VALUE        
   consts
      code
         argcount 0
         nlocals 0
         stacksize 1
         flags 0043
         code 640100474864000053
  2           0 LOAD_CONST               1 ('Hello World!')
              3 PRINT_ITEM          
              4 PRINT_NEWLINE       
              5 LOAD_CONST               0 (None)
              8 RETURN_VALUE        
         consts
            None
            'Hello World!'
         names ()
         varnames ()
         freevars ()
         cellvars ()
         filename 'helloworld.py'
         name 'main'
         firstlineno 1
         lnotab 0001
      '__main__'
      None
   names ('main', '__name__')
   varnames ()
   freevars ()
   cellvars ()
   filename 'helloworld.py'
   name '<module>'
   firstlineno 1
   lnotab 09030c01

Notice that 4 9 LOAD_NAME 1 (__name__) is called before on tuple index 1 before any STORE_* is called from tuple index 1. Therefore, __name__ must be implicitly stored in locals/globals/builtins at some point before LOAD_NAME. I can't figure out where this is happening. So I'm switching pyc files. 😏 Here's the new version of helloworld and its corresponding disassembled pyc file:

def phw():
    print 'Hello World!'

phw()
 ./doc/nedpycparser.py ./sample_pycs/helloworld2.pyc 
magic 03f30d0a
moddate some (time)
code
   argcount 0
   nlocals 0
   stacksize 1
   flags 0040
   code 6400008400005a00006500008300000164010053
  1           0 LOAD_CONST               0 (<code object phw at 0x7f169375de30, file "sample_pycs/helloworld2.py", line 1>)
              3 MAKE_FUNCTION            0
              6 STORE_NAME               0 (phw)

  4           9 LOAD_NAME                0 (phw)
             12 CALL_FUNCTION            0
             15 POP_TOP             
             16 LOAD_CONST               1 (None)
             19 RETURN_VALUE        
   consts
      code
         argcount 0
         nlocals 0
         stacksize 1
         flags 0043
         code 640100474864000053
  2           0 LOAD_CONST               1 ('Hello World!')
              3 PRINT_ITEM          
              4 PRINT_NEWLINE       
              5 LOAD_CONST               0 (None)
              8 RETURN_VALUE        
         consts
            None
            'Hello World!'
         names ()
         varnames ()
         freevars ()
         cellvars ()
         filename 'sample_pycs/helloworld2.py'
         name 'phw'
         firstlineno 1
         lnotab 0001
      None
   names ('phw',)
   varnames ()
   freevars ()
   cellvars ()
   filename 'sample_pycs/helloworld2.py'
   name '<module>'
   firstlineno 1
   lnotab 0903

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions