This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author acdha
Recipients acdha
Date 2014-01-07.16:15:26
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1389111327.1.0.12442170212.issue20164@psf.upfronthosting.co.za>
In-reply-to
Content
This is a more general version of #10496: os.path.expanduser is documented as returning the unmodified string if it cannot be expanded (http://docs.python.org/3/library/os.path.html#os.path.expanduser) but there's one edge case where this can fail: when running without HOME in the environment:

https://github.com/python/cpython/blob/3.3/Lib/posixpath.py#L259-L263

In this case, pwd.getpwuid is called and although it's not documented as such, it raises a KeyError anytime the underlying POSIX getpwuid() call fails:

https://github.com/python/cpython/blob/3.3/Modules/pwdmodule.c#L119-L120

Reproducing this deliberately:

cadams@Io:~ $ sudo -H python
Python 2.7.5 (default, Aug 25 2013, 00:04:04) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import posix
>>> posix.setuid(1001)
>>> posix.seteuid(1001)
>>> os.environ.pop('HOME')
'/var/root'
>>> os.path.expanduser("~")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.py", line 269, in expanduser
    userhome = pwd.getpwuid(os.getuid()).pw_dir
KeyError: 'getpwuid(): uid not found: 1001'

Context:
* https://github.com/toastdriven/django-haystack/issues/924
* https://github.com/kennethreitz/requests/issues/1846.
History
Date User Action Args
2014-01-07 16:15:27acdhasetrecipients: + acdha
2014-01-07 16:15:27acdhasetmessageid: <1389111327.1.0.12442170212.issue20164@psf.upfronthosting.co.za>
2014-01-07 16:15:27acdhalinkissue20164 messages
2014-01-07 16:15:26acdhacreate