Repository navigation
Fix import crash on Python 3.13+ when getpass.getuser() has no OS user entry - #1078
cristianchiriac wants to merge 3 commits into
Conversation
…r entry Fixes aio-libs#1053. As of Python 3.13, getpass.getuser() raises OSError instead of KeyError when there's no entry in the OS user database for the current uid (e.g. an arbitrary uid in a container). The module-level fallback only caught KeyError, so importing aiomysql crashed outright in that environment. Catch OSError too, matching the fix PyMySQL applied for the same underlying getpass change (referenced in the issue).
|
The test's cleanup reloaded aiomysql.connection before monkeypatch undid the getpass.getuser patch, so the reload still observed the broken getuser and left DEFAULT_USER stuck at "unknown" afterwards. The reload also left aiomysql.Connection/aiomysql.connect pointing at the pre-reload objects while aiomysql.connection.Connection/connect were replaced, breaking identity checks for any test that runs after this one. Undo the monkeypatch before reloading, and reload the parent aiomysql package too so its re-exported names match the reloaded submodule again.
|
Good catch — pushed a fix: undo the monkeypatch before reloading so the second reload picks the real |
| raise exc_type("no such user") | ||
|
|
||
| monkeypatch.setattr("getpass.getuser", raise_exc) | ||
| importlib.reload(aiomysql_connection) |
There was a problem hiding this comment.
woah, this doesn't appear safe..
There was a problem hiding this comment.
Agreed, reloading modules during the test run isn't safe. The test now does the import in a separate Python process (f242454), so nothing in the test session is touched. It still fails without the fix.
Fixes #1053.
Bug
As of Python 3.13,
getpass.getuser()raisesOSErrorinstead ofKeyErrorwhen there's no entry in the OS user database for the current uid (e.g. an arbitrary uid set inside a Docker container). The module-level fallback inaiomysql/connection.pyonly caughtKeyError:so on Python 3.13+ in that environment,
import aiomysqlcrashes outright with an unhandledOSError.PyMySQL hit the identical issue and fixed it the same way (referenced in the issue): PyMySQL/PyMySQL@a1ac823
Fix
Catch
OSErroralongsideKeyError.Test plan
test_default_user_falls_back_when_getuser_fails, parametrized over bothKeyErrorandOSError, monkeypatchinggetpass.getuserand reloadingaiomysql.connectionto exercise the module-level fallback directly.OSErroragainst the unpatched code, and passes after the fix (both exception types).tests/conftest.pyimportsuvloop, which is Unix-only) — verified the fix and the new test's logic manually via directimportlib.reloadoutside pytest, and confirmedflake8is clean on the changed file.