by tyingq · 5y ago · 0 comments
Lacking that, there's no way to write a (single file) script where a Python 2 interpreter would just get a "You need Python 3" error instead of a syntax error. Because testing for the interpreter version happens too late...after the syntax error already happened.
Perl's "BEGIN {}" block lets you easily test for versions or language features before the script itself is parsed.
0_0 # Python >= 3.6 is required
I have a collection of such incantations for many Python versions here: 0_0
syntactically okay? I typed it into an AST visualizer (I'm on mobile) and if I'm not mistaken this is equivalent to `0`. But why?Your question about 0_0 is just 00 which is 0.
(This is really common in other modern languages. Readable example: 1_000_000 vs 1000000)
You can also catch a SyntaxError like any other exception.
Yes, people are able to write backwards compatible scripts... by avoiding some features, or by using more than a single file.
What I'm describing was just a desire for a very simple "this script requires python version >= X" error, with the ability to write unfettered python version X code below it, in a single file script.
import sys
if sys.version_info[0] < 3:
print("Needs Python 3+")
sys.exit(1)
else:
prog = r'''
def help():
print("Help: ...")
def do_spam(*args):
print("spamming")
def main(args):
match args:
case [subcommand, *args]:
return globals()[f'do_{subcommand}'](*args)
case _:
return help()
if __name__ == '__main__':
try:
status = main(sys.argv[1:])
sys.exit(int(status if status is not None else 0))
except Exception as e:
import traceback
traceback.print_exc(e)
sys.exit(1)
'''
compile(prog, '', 'exec')
exec(prog) import sys
if sys.version_info.major < 3:
raise Exception("this script requires python version >= 3")
plus an extra line or two if you're worried about minor versions. > cat test.py
import sys
if sys.version_info.major < 3:
raise Exception("this script requires python version >= 3")
print(f"test")
> python2 test.py
File "test.py", line 5
print(f"test")
^
SyntaxError: invalid syntax
Doesn't seem that simpleprint(f"whee")
And run it with python2, or a python3 < 3.6.
Nowaday, I use shiv to produce the zipapp:
https://shiv.readthedocs.io/en/latest/
It's handy because you can also now embed 3rd party dependencies, even for small quick scripts if you wish.
# -*- coding: ... -*-
The trick is that encoding names come from an extensible registry of codecs (https://docs.python.org/3/library/codecs.html#codecs.registe...), so you can register your own. And when you look at codec API, you get the raw byte stream as input, and spit out strings - so it's basically as powerful as reader macros.The problem is getting that registration code to run before your script starts. Easy if you have multiple scripts, but it requires .pth hacks if you go for single-file.
Therefore, I suspect it is not possible to write a test in the BEGIN block which avoids a version-dependent syntax error elsewhere in favor of terminating with a graceful error.
(It's only Standards Track at the moment)
I imagine it would have been more popular than 17% if it were available when v2 -> v3 pain was at the high point.
The whole reason this thought popped in my head is that I still have people asking today: "Tried to run your script, and I got a syntax error...is it broken?". When the issue is that the default python on their box is 2.x.