-1

Python is an interpreted language so it execute the code line by line so when I am running

import csv,re,sys

print len(sys.argv)
if(len(sys.argv)!=2):
    sys.exit(0)

filename= #from command line argument

it doesn't execute even a single line and give syntax error.

Now my question is that last line of the code has error but python interpreter execute the code line by line so the code up to the last line is correct so it should execute the code upto the last line but it is giving me the below error and not printing length of sys.argv that I have defined in line 2

File "trace-analysis.py", line 45
filename = # from command line argument
SyntaxError: invalid syntax

I am not getting this behaviour.... please someone explain this ...

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Jatin Khurana
  • 1,155
  • 8
  • 15

1 Answers1

7

python interpreter execute the code line by line

This is false!!!

Python reads the whole file, compiles it to bytecode and then executes the bytecode. If there is a syntax error anywhere in the file no instruction is run because the interpreter will first try to parse the whole contents of the file and realize it's not a well-formed program.

Python is not bash.


Just ot be clear what I mean with the last statement:

$echo 'print("Hello, World!")
> $(
> ' > test.py
$python test.py   # NOTE: no Hello, World in the output
  File "test.py", line 2
    $(
    ^
SyntaxError: invalid syntax
$echo 'echo "Hello, World!"
$(
' > test.sh
$bash test.sh    # NOTE: there's a Hello, World => bash execute the first statement!
Hello, World!
test.sh: riga 2: EOF non atteso durante la ricerca di ")"
test.sh: riga 4: errore di sintassi: EOF non atteso

My locale is italian. The error is just a standard error message saying that it found an unexpected EOF.

Hence bash does not parse the whole file before starting execution. Quod est demostrandum

Bakuriu
  • 98,325
  • 22
  • 197
  • 231
  • bash actually doesn't do strict line-by-line parsing and execution either. – Charles Duffy Oct 09 '14 at 18:52
  • Demonstrably false: It's lookahead in the parser that was responsible for one of the more interesting security bugs of late. – Charles Duffy Oct 09 '14 at 18:54
  • @MarkReed That was my point. Write a script that starts with `echo "Hello, World"` and then contains crap and bash will happily output `Hello, World`. Python doesn't. – Bakuriu Oct 09 '14 at 18:55
  • yes... thanks a lot to correcting me ..... My concept was full wrong.... but Really I didn't know the concept that's why put the question here.... I don't know why people give negative rating... kk no worry.. don't bother about rating.. atleast I have learned something new.... again thanks a lot – Jatin Khurana Oct 09 '14 at 18:57
  • @CharlesDuffy Could you point out where did I state that bash executes line by line? My statement is *Python isn't bash* and my whole answer **never** mentions "line-by-line" behaviour. I believe the point I wanted to make is perfectly clear, but if you want an example I can provide one. – Bakuriu Oct 09 '14 at 18:58
  • @CharlesDuffy I used the wrong term. It doesn't have forward declarations - you can't call a function before you declare it. (You can declare a function containing calls to as-yet-undeclared functions, though.) – Mark Reed Oct 09 '14 at 18:58
  • And they both do (mostly) line-by-line at the REPL. :) – Mark Reed Oct 09 '14 at 19:00
  • 1
    @MarkReed, that's... dependent on how one defines things; you can certainly define your functions out-of-order (which many explicitly single-pass-compiled languages _don't_ allow), and can certainly define stubs and replace them with full definitions later. To be sure, there's no implicit reordering as you get with proper two-pass compilation, and the only reason one can get away with some of the above is the lack of any kind of reference checking at definition time. – Charles Duffy Oct 09 '14 at 19:03
  • 2
    @Bakuriu, "Python is not bash" as a refutation to a claim that Python parses and executes code in a strictly line-by-line manner reads to me as an unambiguous implication that bash executes code line-by-line. Which is close, but not _quite_ unambiguously true. – Charles Duffy Oct 09 '14 at 19:04
  • 2
    Re: recent edits -- I never claimed that bash _did_ parse an entire file as code before running it, by the way. If it did, quite a number of tools (shar archives, for instance) would be useless. – Charles Duffy Oct 09 '14 at 19:06