Patchwork D8814: tests: make check-py3-compat.py actually load the specified files correctly

login
register
mail settings
Submitter phabricator
Date July 25, 2020, 12:02 a.m.
Message ID <differential-rev-PHID-DREV-fspjgt6e34t5iq72vb6a-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/46884/
State Superseded
Headers show

Comments

phabricator - July 25, 2020, 12:02 a.m.
spectral created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  For most uses, this change is essentially a no-op, as this script is generally
  only run by test-check-py3-compat.t, which will already put `$TESTDIR/..` in
  `$PYTHONPATH`.
  
  When running outside of tests, however, `$PYTHONPATH` is likely not set, causing
  check-py3-compat.py to parse the file from the repo, but then import the
  installed version, and raise any errors about the installed version, not the one
  currently in the repo.
  
  Additionally, this helps users (like me) who have a strange set up where their
  home directory (and thus their hg repos) happen to be in a subdirectory of
  sys.prefix (which is /usr on my system). Since the '.' entry added to sys.path
  takes precedence over the absolute path of `$TESTDIR/..` in `$PYTHONPATH`, the
  path to the modules that it imports (and that show up in any stack trace) are
  *relative*, meaning that we don't detect them as starting with `sys.prefix`.
  
  Sample non-test invocation, and the difference this change makes (the path for
  'error at <path>:<line>' is correct now)::
  
  Before:
  
    $ python3 contrib/check-py3-compat.py mercurial/win*.py
    mercurial/win32.py: error importing: <ValueError> _type_ 'v' not supported (error at check-py3-compat.py:65)
    mercurial/windows.py: error importing: <ModuleNotFoundError> No module named 'msvcrt' (error at check-py3-compat.py:65)
  
  After:
  
    $ python3 contrib/check-py3-compat.py mercurial/win*.py
    mercurial/win32.py: error importing: <ValueError> _type_ 'v' not supported (error at win32.py:11)
    mercurial/windows.py: error importing: <ModuleNotFoundError> No module named 'msvcrt' (error at windows.py:12)

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D8814

AFFECTED FILES
  contrib/check-py3-compat.py

CHANGE DETAILS




To: spectral, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/contrib/check-py3-compat.py b/contrib/check-py3-compat.py
--- a/contrib/check-py3-compat.py
+++ b/contrib/check-py3-compat.py
@@ -97,6 +97,15 @@ 
     if sys.version_info[0] == 2:
         fn = check_compat_py2
     else:
+        # check_compat_py3 will import every filename we specify as long as it
+        # starts with one of a few prefixes. It does this by converting
+        # specified filenames like 'mercurial/foo.py' to 'mercurial.foo' and
+        # importing that. When running standalone (not as part of a test), this
+        # means we actually import the installed versions, not the files we just
+        # specified. When running as test-check-py3-compat.t, we technically
+        # would import the correct paths, but it's cleaner to have both cases
+        # use the same import logic.
+        sys.path.insert(0, '.')
         fn = check_compat_py3
 
     for f in sys.argv[1:]: