Posts: 4,647
Threads: 1,494
Joined: Sep 2016
an editing error exposed this to me which i did not find in the reference manual. on a with statement i omitted the "with_item", that is, the as followed by the variable name. but it still worked. i was using the variable f. so, i left it out, and removed it in another with statement. the script still works. maybe, some day, it will break.
is this supposed to happen? is this a Cpython glitch?
#!/usr/bin/env python3
import os,sys
a='~/.'+(sys.argv[0].rsplit('/',1)[1])
try:
with open(os.path.expanduser(a+'-pre.py')):
p=[x[:-1] for x in f]
except:
p="""
import os
from subprocess import call,DEVNULL,PIPE,Popen,run
from sys import stderr,stdin,stdout,version_info
from time import sleep,time as secs
""".splitlines()
try:
with open(os.path.expanduser(a+'-suf.py')):
s=[x[:-1] for x in f]
except:
s=[]
c='\n'.join(p+sys.argv[1:]+s)
if len(sys.argv)>1:os.execvp('/usr/bin/env',['env','python3','-c',c])
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 1,838
Threads: 2
Joined: Apr 2017
May-12-2020, 04:51 AM
(This post was last modified: May-12-2020, 04:51 AM by ndc85430.)
I'm not sure there's a bug really as it would be quite a serious one that you'd expect to have been noticed by now. In any case, I can't reproduce the problem. I suspect what's happening is that you have some older bytecode around, that was generated when the variable existed and that's what's being run. If you remove the .pyc files and try again, does this still happen?
Posts: 8,160
Threads: 160
Joined: Sep 2016
May-12-2020, 05:03 AM
(This post was last modified: May-12-2020, 05:03 AM by buran.)
(May-11-2020, 08:13 PM)Skaperen Wrote: an editing error exposed this to me which i did not find in the reference manual. on a with statement i omitted the "with_item", that is, the as followed by the variable name. but it still worked. i was using the variable f. so, i left it out, and removed it in another with statement. the script still works. maybe, some day, it will break.
is this supposed to happen? is this a Cpython glitch?
#!/usr/bin/env python3
import os,sys
a='~/.'+(sys.argv[0].rsplit('/',1)[1])
try:
with open(os.path.expanduser(a+'-pre.py')):
p=[x[:-1] for x in f]
except:
p="""
import os
from subprocess import call,DEVNULL,PIPE,Popen,run
from sys import stderr,stdin,stdout,version_info
from time import sleep,time as secs
""".splitlines()
try:
with open(os.path.expanduser(a+'-suf.py')):
s=[x[:-1] for x in f]
except:
s=[]
c='\n'.join(p+sys.argv[1:]+s)
if len(sys.argv)>1:os.execvp('/usr/bin/env',['env','python3','-c',c])
well, with all-catching except what else do you expect? It throws an error and the except block is executed always.
By the way, how do you read this code with single space indentation per level? It's a nightmare
Posts: 4,647
Threads: 1,494
Joined: Sep 2016
May-12-2020, 11:43 PM
(This post was last modified: May-12-2020, 11:56 PM by Skaperen.)
that was code that was not intended to be read. it was processed by another script. i should have fixed it before posting. and you are right about the except. but i expected it to throw syntax errors in the compile phase. i guess it compiled the raise in place of them.
this should be better:
#!/usr/bin/env python3
import os,sys
a='~/.'+(sys.argv[0].rsplit('/',1)[1])
try:
with open(os.path.expanduser(a+'-pre.py')) as f:
p=[x[:-1] for x in f]
except:
p="""
import os
from subprocess import call,DEVNULL,PIPE,Popen,run
from sys import stderr,stdin,stdout,version_info
from time import sleep,time as secs
""".splitlines()
try:
with open(os.path.expanduser(a+'-suf.py')) as f:
s=[x[:-1] for x in f]
except:
s=[]
c='\n'.join(p+sys.argv[1:]+s)
if len(sys.argv)>1:
os.execvp('/usr/bin/env',['env','python3','-c',c])
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 8,160
Threads: 160
Joined: Sep 2016
(May-12-2020, 11:43 PM)Skaperen Wrote: i expected it to throw syntax errors in the compile phase. i guess it compiled the raise in place of them.
look at docs for Compound statements: the with statement
Quote:with_stmt ::= "with" with_item ("," with_item)* ":" suite
with_item ::= expression ["as" target]
....
5. If a target was included in the with statement, the return value from __enter__() is assigned to it.
providing target is optional and there is no SyntaxError. Your snippet raise NameError and then the except catch it.
Also in PEP343, Specification: The 'with' Statement:
Quote:with EXPR as VAR:
BLOCK ...
The "as VAR" part is optional.
...
If the "as VAR" part of the syntax is omitted, the "VAR =" part of the translation is omitted (but mgr.__enter__() is still called).
|