A polyglot header for Python and cmd.exe
After seeing Raymond’s post on polyglot launchers for Perl and JScript with batch files, I decided to present one for Python:
This one simply use the special
-x to ignore the first line, which is somewhat analogous to the
-x Perl flag, but much simpler.
I also have an alternative Perl polyglot header that does not require the special flag
For the curious, Perl’s
-x flag is designed for embedding scripts inside larger files. It tells Perl to ignore every single line in the script until it reaches a line starting with
#! and containing the string
perl. This detail can be found in the
perlrun manpage. Perl also allows for non-code data to appear after the block of code: you can tell Perl to stop parsing the file by putting
__END__ on its own line.
On the other hand, Python’s
-x flag does exactly one thing: ignoring the first line of code. Note that using it would cause line numbers in stack traces to be off by one, which is not ideal. This is stated in the Python documentation.