This is an archive of the discontinued Mercurial Phabricator instance.

global: use python3 in shebangs
ClosedPublic

Authored by indygreg on Nov 6 2020, 5:27 PM.

Details

Summary

Python 3 is the future. We want Python scripts to be using Python 3
by default.

This change updates all #!/usr/bin/env python shebangs to use
python3.

Does this mean all scripts use or require Python 3: no.

In the test environment, the PATH environment variable in tests is
updated to guarantee that the Python executable used to run
run-tests.py is used. Since test scripts all now use
#!/usr/bin/env python3, we had to update this code to install
a python3 symlink instead of python.

It is possible there are some random scripts now executed with the
incorrect Python interpreter in some contexts. However, I would argue
that this was a pre-existing bug: we should almost always be executing
new Python processes using the sys.executable from the originating
Python script, as python or python3 won't guarantee we'll use the
same interpreter.

Diff Detail

Repository
rHG Mercurial
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.

Event Timeline

indygreg created this revision.Nov 6 2020, 5:27 PM

test-parseindex2.py fails for me with this patch:

@@ -0,0 +1,11 @@
+FAIL: testversiondetection (__main__.parseindex2tests)
+
+Traceback (most recent call last):
+  File "/usr/local/google/home/martinvonz/hg/tests/test-parseindex2.py", line 201, in testversiondetection
+    self.assertversionokay(1, makehex(major, minor, micro))
+  File "/usr/local/google/home/martinvonz/hg/tests/test-parseindex2.py", line 170, in assertversionokay
+    self.assertFalse(
+AssertionError: b'Traceback (most recent call last):\n  File "<string>", line 1, in <module>\nImportError: No module named parsers\n' is not false : FAILED: version test #1 with Python sys.version_info(major=3, minor=8, micro=5, releaselevel='final', serial=0) and patched sys.hexversion 50857216 ('0x3080500'):
+ Expected no stdout but got:
+-->'b'Traceback (most recent call last):\n  File "<string>", line 1, in <module>\nImportError: No module named parsers\n''
+

I've sent D9286 to fix that. That patch needs to go in before this one. I'll add it as a parent here in phabricator.

martinvonz accepted this revision.Nov 10 2020, 12:01 AM
This revision is now accepted and ready to land.Nov 10 2020, 12:01 AM
This revision was automatically updated to reflect the committed changes.

This causes problems on Windows (since python3 == python.exe)- for example, all test-check-* but one are skipped because hghave is failing, complaining python3 is missing. The only way I can think of to work around this is to use $PYTHON to run the scripts instead of directly executing them. Before I do that, is there some other simpler mechanism that I'm overlooking? I tried to add alias python3="py -3" to the script that gets generated when MSYSTEM is in the environment around run-tests.py:1760, but that didn't help (likely env doesn't know about it).

This causes problems on Windows (since python3 == python.exe)- for example, all test-check-* but one are skipped because hghave is failing, complaining python3 is missing. The only way I can think of to work around this is to use $PYTHON to run the scripts instead of directly executing them. Before I do that, is there some other simpler mechanism that I'm overlooking? I tried to add alias python3="py -3" to the script that gets generated when MSYSTEM is in the environment around run-tests.py:1760, but that didn't help (likely env doesn't know about it).

@indygreg I don't have any idea what to do about this (other than to roll back this patch). What do you think?