Fix not throwing the same error as CPython in test_pathlib.test_expanduser (#5578)

* Fix not throwing the same error as CPython when trying to expanduser of a non-existent user

Signed-off-by: Hanif Ariffin <hanif.ariffin.4326@gmail.com>

* add pwd test

* Skip pwd test on windows

---------

Signed-off-by: Hanif Ariffin <hanif.ariffin.4326@gmail.com>
Co-authored-by: Jeong YunWon <jeong@youknowone.org>
Co-authored-by: Jeong, YunWon <69878+youknowone@users.noreply.github.com>
This commit is contained in:
Hanif Ariffin
2025-04-05 14:33:33 +08:00
committed by GitHub
parent d800a6bb98
commit 2230d6c751
2 changed files with 13 additions and 1 deletions

View File

@@ -0,0 +1,12 @@
import sys
# windows doesn't support pwd
if sys.platform.startswith("win"):
exit(0)
from testutils import assert_raises
import pwd
with assert_raises(KeyError):
fake_name = 'fake_user'
while pwd.getpwnam(fake_name):
fake_name += '1'

View File

@@ -59,7 +59,7 @@ mod pwd {
if pw_name.contains('\0') {
return Err(exceptions::cstring_error(vm));
}
let user = User::from_name(name.as_str()).map_err(|err| err.into_pyexception(vm))?;
let user = User::from_name(name.as_str()).ok().flatten();
let user = user.ok_or_else(|| {
vm.new_key_error(
vm.ctx