why does py_compile.compile() miss some errors in sourcefile?

F

Frank Bechmann

where can I find some documentation, which errors will be found by
'py_compile.compile' and which will not be found.

# t1.py
# =====

import os.path
import py_compile

py_compile.compile(os.path.join(os.path.dirname(__file__), "t2.py"),
doraise=True)


# t2.py
# =====
# following line is found as an error:
# Class C: # <== 'SyntaxError: invalid syntax'

class C:
pass

c = CC() # <== not found as an error from py_compile
# but found as an error from python-executable:
# 'NameError: name 'CC' is not defined'

thx in advance
 
B

Bengt Richter

where can I find some documentation, which errors will be found by
'py_compile.compile' and which will not be found.

# t1.py
# =====

import os.path
import py_compile

py_compile.compile(os.path.join(os.path.dirname(__file__), "t2.py"),
doraise=True)


# t2.py
# =====
# following line is found as an error:
# Class C: # <== 'SyntaxError: invalid syntax'

class C:
pass

c = CC() # <== not found as an error from py_compile
# but found as an error from python-executable:
# 'NameError: name 'CC' is not defined'
The difference is between compiling and executing. Syntax errors are found
during complilation and run-time errors during execution ;-) py_compile apparently
does not execute the code it compiles.

c = CC()

compiles ok. It just says, look up CC in the directory associated with the code excution,
call it, and bind the result to c. There is no error until you try to execute it, and
actually try to look up CC. With py_compile you don't have a reasonable way to interfere
before execution, to supply a CC, but if you compile interactively you can, e.g.,
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "", line 1, in ?
NameError: name 'CC' is not defined

But we could have done this before executing:
'CC result'

In general, the compiler can't know if you
are going to fix things up before execution.

If you import or execfile, you will be executing the code (at least on
the first import -- if you're not the first, no re-execution). Import will
create it's own global name space to execute in (and thus to look for CC in),
so CC won't be there, but with execfile you could specify a directory that already
has CC defined in it, and it should succeed. E.g., using your t2.py:

print it to make sure:
===< t2.py >===
# =====
# following line is found as an error:
# Class C: # <== 'SyntaxError: invalid syntax'

class C:
pass

c = CC() # <== not found as an error from py_compile
# but found as an error from python-executable:
# 'NameError: name 'CC' is not defined'

======

We set up a directory
define a funtion ...

bind it to CC in the directory, which will be the global name space for the execution

check on it {'CC': <function foo at 0x008FDF70>}

execute t2.py in it This is foo, bound to CC in dict d

we note the side effect printed
and check on d again (just the names, I know what's coming ;-)
['CC', '__builtins__', 'C', 'c']
The __builtins__ key is put there as a side effect of execfile.

we can check on that "c = CC()" assignment result:
'foo/CC result'

HTH

Regards,
Bengt Richter
 
F

Frank Bechmann

thanks a lot for this substantial explanation. seems that I have to
try much harder to forget my static compiled language experiences :).

if I understood you correctly it is not necessary to try to
'py_compile.compile()' a file once I tried to import it under
pychecker-control because the import already executed the file, so the
import did not only the compile (if necessary) but also the runtime
evaluation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top