Patchwork D8302: hg: make _local() behave consistently on Python 3.8

login
register
mail settings
Submitter phabricator
Date March 18, 2020, 7:20 p.m.
Message ID <differential-rev-PHID-DREV-t4ut62rejpwasrp3bumj-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/45822/
State Superseded
Headers show

Comments

phabricator - March 18, 2020, 7:20 p.m.
durin42 created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  Python 3.8 makes os.path.isfile quietly eat "path invalid" errors and
  return False instead of allowing the exception to propagate. Given
  that this is a change from 2018 (sigh) and it's mentioned in the
  release notes (double sigh) we're definitely too late to complain to
  Python about the behavior change, so open-code part of
  os.path.isfile() in this method so we can catch invalid-path errors
  and handle them appropriately. I confirmed that posixpath and ntpath
  both delegate to genericpath, which uses os.stat() under the covers.

REPOSITORY
  rHG Mercurial

BRANCH
  default

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

AFFECTED FILES
  mercurial/hg.py

CHANGE DETAILS




To: durin42, #hg-reviewers
Cc: mercurial-devel

Patch

diff --git a/mercurial/hg.py b/mercurial/hg.py
--- a/mercurial/hg.py
+++ b/mercurial/hg.py
@@ -60,12 +60,19 @@ 
     path = util.expandpath(util.urllocalpath(path))
 
     try:
-        isfile = os.path.isfile(path)
-    # Python 2 raises TypeError, Python 3 ValueError.
+        # we use os.stat() directly here instead of os.path.isfile()
+        # because the latter started returning `False` on invalid path
+        # exceptions starting in 3.8 and we care about handling
+        # invalid paths specially here.
+        st = os.stat(path)
+        isfile = stat.S_ISREG(st.st_mode)
+        # Python 2 raises TypeError, Python 3 ValueError.
     except (TypeError, ValueError) as e:
         raise error.Abort(
             _(b'invalid path %s: %s') % (path, pycompat.bytestr(e))
         )
+    except OSError:
+        isfile = False
 
     return isfile and bundlerepo or localrepo