[Tutor] making a string

Jim Mooney cybervigilante at gmail.com
Mon May 27 02:39:13 CEST 2013


On 26 May 2013 17:20, Steven D'Aprano <steve at pearwood.info> wrote:
> On 27/05/13 07:40, Jim Mooney wrote:
>
>> Good to know that compile doesn't check syntax, since I erroneously
>> thought it did.
>
>
> compile does check syntax.

I'm unclear on something. The code below creates bytecode and I don't
see an error message unless I also tack exec code on the end of it. It
has the dumb error of trying to iterate an integer.

from dis import dis

code = compile(
'''a, b = 5, 8
print a
print b
for x in a:
    print x
''','', 'exec'
)

dis(code)

Result below - bytecode but no error message
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)]
Type "help", "copyright", "credits" or "license" for more information.
[evaluate derek.py]
  1           0 LOAD_CONST               3 ((5, 8))
              3 UNPACK_SEQUENCE          2
              6 STORE_NAME               0 (a)
              9 STORE_NAME               1 (b)

  2          12 LOAD_NAME                0 (a)
             15 PRINT_ITEM
             16 PRINT_NEWLINE

  3          17 LOAD_NAME                1 (b)
             20 PRINT_ITEM
             21 PRINT_NEWLINE

  4          22 SETUP_LOOP              19 (to 44)
             25 LOAD_NAME                0 (a)
             28 GET_ITER
        >>   29 FOR_ITER                11 (to 43)
             32 STORE_NAME               2 (x)

  5          35 LOAD_NAME                2 (x)
             38 PRINT_ITEM
             39 PRINT_NEWLINE
             40 JUMP_ABSOLUTE           29
        >>   43 POP_BLOCK
        >>   44 LOAD_CONST               2 (None)
             47 RETURN_VALUE

If I add    exec code    to the end of that program I Do get an error,
after it prints a and b, but that's separate from the compile
statement:

exec code
Traceback (most recent call last):
  File "C:\Program Files (x86)\Wing IDE 101
4.1\src\debug\tserver\_sandbox.py", line 13, in <module>
  File "C:\Program Files (x86)\Wing IDE 101
4.1\src\debug\tserver\_sandbox.py", line 4, in <module>
    #print 'Exiting sandbox process'
TypeError: 'int' object is not iterable


More information about the Tutor mailing list