From 2230d6c751eb8fdd2d7554df529574ea53b906b8 Mon Sep 17 00:00:00 2001 From: Hanif Ariffin Date: Sat, 5 Apr 2025 14:33:33 +0800 Subject: [PATCH] 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 * add pwd test * Skip pwd test on windows --------- Signed-off-by: Hanif Ariffin Co-authored-by: Jeong YunWon Co-authored-by: Jeong, YunWon <69878+youknowone@users.noreply.github.com> --- extra_tests/snippets/stdlib_pwd.py | 12 ++++++++++++ vm/src/stdlib/pwd.rs | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 extra_tests/snippets/stdlib_pwd.py diff --git a/extra_tests/snippets/stdlib_pwd.py b/extra_tests/snippets/stdlib_pwd.py new file mode 100644 index 000000000..6ef3a64d0 --- /dev/null +++ b/extra_tests/snippets/stdlib_pwd.py @@ -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' diff --git a/vm/src/stdlib/pwd.rs b/vm/src/stdlib/pwd.rs index b95910c73..20b4edb44 100644 --- a/vm/src/stdlib/pwd.rs +++ b/vm/src/stdlib/pwd.rs @@ -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