[Python-ideas] Augment dis.dis to autocompile strings

Terry Reedy tjreedy at udel.edu
Fri Jul 17 21:07:04 CEST 2009


Steven D'Aprano wrote:
> On Fri, 17 Jul 2009 04:35:40 am Terry Reedy wrote:
> 
>> Proposal: if the input is a string, do the above (do what I mean)
>> instead of raising
>> TypeError: don't know how to disassemble str objects
>>
>> Any negatives before I submit a feature request?
> 
> +1 on the idea, but how do you determine which of the following are 
> required?
> 
> compile('x+1', '', 'eval')
> compile('x = x+1', '', 'single')
> compile('while x < 42: x += 1', '', 'exec')
> 
> 
> Or do you just assume 'exec'?

Good question. Let us see what difference it makes in 3.1

 >>> dis(compile('x+1','', 'eval'))
   1           0 LOAD_NAME                0 (x)
               3 LOAD_CONST               0 (1)
               6 BINARY_ADD
               7 RETURN_VALUE

 >>> dis(compile('x+1', '', 'exec'))
   1           0 LOAD_NAME                0 (x)
               3 LOAD_CONST               0 (1)
               6 BINARY_ADD
               7 POP_TOP
               8 LOAD_CONST               1 (None)
              11 RETURN_VALUE

Adds an extra POP_TOP and LOAD_CONST None

 >>> dis(compile('x = x+1', '', 'single'))
   1           0 LOAD_NAME                0 (x)
               3 LOAD_CONST               0 (1)
               6 BINARY_ADD
               7 STORE_NAME               0 (x)
              10 LOAD_CONST               1 (None)
              13 RETURN_VALUE

 >>> dis(compile('x = x+1', '', 'exec'))
   1           0 LOAD_NAME                0 (x)
               3 LOAD_CONST               0 (1)
               6 BINARY_ADD
               7 STORE_NAME               0 (x)
              10 LOAD_CONST               1 (None)
              13 RETURN_VALUE

No difference

I decided to submit this to the tracker.
http://bugs.python.org/issue6507

Terry Jan Reedy





More information about the Python-ideas mailing list