diff --git a/mercurial/windows.py b/mercurial/windows.py --- a/mercurial/windows.py +++ b/mercurial/windows.py @@ -581,7 +581,13 @@ def username(uid=None): If uid is None, return the name of the current user.""" if not uid: - return pycompat.fsencode(getpass.getuser()) + try: + return pycompat.fsencode(getpass.getuser()) + except ModuleNotFoundError: + # getpass.getuser() checks for a few environment variables first, + # but if those aren't set, imports pwd and calls getpwuid(), none of + # which exists on Windows. + pass return None diff --git a/tests/test-issue1102.t b/tests/test-issue1102.t --- a/tests/test-issue1102.t +++ b/tests/test-issue1102.t @@ -14,4 +14,18 @@ tip 3:a49829c4fc11 t1 0:f7b1eb17ad24 +Ensure that the username access fails gracefully if assumptions about the +environment made by python do not hold. + +#if windows + >>> import os + >>> from mercurial import util + >>> os.environ.pop('LOGNAME', None) and None + >>> os.environ.pop('USER', None) and None + >>> os.environ.pop('LNAME', None) and None + >>> os.environ.pop('USERNAME', None) and None + >>> print(util.username()) + None +#endif + $ cd ..