Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #29512

Re: 'indent'ing Python in windows bat

From Terry Reedy <tjreedy@udel.edu>
Subject Re: 'indent'ing Python in windows bat
Date 2012-09-19 14:18 -0400
References (2 earlier) <k38vl8$u02$2@ger.gmane.org> <505803A9.4000409@davea.name> <mailman.870.1347973403.27098.python-list@python.org> <k3c2st$l6l$1@r03.glglgl.gl> <5059BA28.2090803@invtools.com>
Newsgroups comp.lang.python
Message-ID <mailman.932.1348078726.27098.python-list@python.org> (permalink)

Show all headers | View raw


On 9/19/2012 8:27 AM, David Smith wrote:

> but not:
> print('hi');if 1: print('hi')
>
> Chokes on the 'if'. On the surface, this is not consistent.

Yes it is. ; can only be followed by simple statements. The keyword for 
compound statememts must be the first non-indent token on a line. That 
is why I suggested at the beginning of the thread to insert '\n', 
stating correctly that it works for exec().

 >>> exec("print('hi');if 1: print('hi')")
Traceback (most recent call last):
   File "<pyshell#0>", line 1, in <module>
     exec("print('hi');if 1: print('hi')")
   File "<string>", line 1
     print('hi');if 1: print('hi')
                  ^
SyntaxError: invalid syntax
 >>> exec("print('hi');\nif 1: print('hi')")
hi
hi
 >>> exec("print('hi')\nif 1: print('hi')")
hi
hi

Someone raised the issue of whether the bat interpreter passes along the 
quoted string unchanged or if it interprets '\' or '\n' itself and in 
the latter case whether one to do anything so that python will see '\n' 
after any fiddling by the bat interpreter. It seems that \ is not 
interpreted within strngs by bat, but the problem is that the string is 
then seen by python as code, not as a string literal, and so python does 
not 'cook' it either. Running tem.bat from a command line (which echoes 
line from .bat), so I see the output, I get (Win7)

C:\Programs\Python33>python -c "print(1)\nif 1: print(2)"
   File "<string>", line 1
     print(1)\nif 1: print(2)
                            ^
SyntaxError: unexpected character after line continuation character

One gets the same response interactively from
 >>> print('hi')\nif 1: print('hi')
or
 >>> exec("print('hi')\\nif 1: print('hi')")

The fix is to quote and pass the exact code that worked above in the 
python shell, keeping in mind that the outer quotes must be the double 
quote characters recognized by windows.

C:\Programs\Python33>python -c "exec('print(1)\nif 1: print(2)')"
1
2

I did check that windows % interpolation of .bat args works within '' 
quoted strings. Change tem.bat to
python -c "exec('print(%1)\nif 1: print(2)')"
and calling 'tem 3' prints
3
2

That said, if you have many multiline statements, putting them in a 
separate file or files may be a good idea.

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: 'indent'ing Python in windows bat David Smith <davids@invtools.com> - 2012-09-18 09:03 -0400
  Re: 'indent'ing Python in windows bat Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-09-19 11:22 +0200
    Re: Re: 'indent'ing Python in windows bat David Smith <davids@invtools.com> - 2012-09-19 08:27 -0400
    Re: 'indent'ing Python in windows bat Terry Reedy <tjreedy@udel.edu> - 2012-09-19 14:18 -0400
    Re: Re: 'indent'ing Python in windows bat David Smith <davids@invtools.com> - 2012-09-19 16:09 -0400

csiph-web