[issue20164] Undocumented KeyError from os.path.expanduser

Yury V. Zaytsev report at bugs.python.org
Tue Jan 7 17:38:31 CET 2014


Yury V. Zaytsev added the comment:

Amusingly, it's also the case on BG/Q compute nodes. Only this morning, I cooked up the following patch:

--- a/Lib/posixpath.py
+++ b/Lib/posixpath.py
@@ -237,7 +237,11 @@ def expanduser(path):
     if i == 1:
         if 'HOME' not in os.environ:
             import pwd
-            userhome = pwd.getpwuid(os.getuid()).pw_dir
+            try:
+                userhome = pwd.getpwuid(os.getuid()).pw_dir
+            except KeyError:
+                import warnings
+                warnings.warn('Unable to auto-detect $HOME, export it or functionality such as user-installed modules will not work!', RuntimeWarning)
         else:
             userhome = os.environ['HOME']
     else:

I think it's a bit harsh to fail completely, but returning silently also doesn't sound like a good idea, so I decided to issue a warning instead; the wording should be probably tweaked by a native speaker of English though.

What do you think?

----------
nosy: +zaytsev
type:  -> behavior
versions: +Python 3.4

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue20164>
_______________________________________


More information about the Python-bugs-list mailing list