Passing `bytes` to `pwd.getpwnam()` raises `TypeError` on CPython
The pwd.getpwnam()
function looks up a user record by username, or raises KeyError
. The username is supposed to be a str
, and CPython raises TypeError
if you pass another type, like bytes
. PyPy 3.6 and 3.7 raise KeyError
in this case. Additionally, PyPy's exception message doesn't use repr()
on the key, so the type issue isn't apparent in the traceback:
builtins.KeyError: 'getpwnam(): name not found: i-am-bytes'
PyPy should check the type to match CPython's behavior. Raising KeyError
is a Python 2 porting hazard, since the caller will usually catch that exception and treat it as if the user record is not present.
Discovered on this Twisted PR. See this failure of this test.