Skip to content

Using Python 3.13.0a3+ on Windows when USERNAME env var is masked/unset, get_user() leaks OSError and breaks (at least) tmp_path usage #11874

@rmartin16

Description

@rmartin16

Fetching the username for temporary paths is failing for Windows on Python 3.13a3 when the USERNAME is not set. This is a common situation with tox, for instance, since it masks all the env vars.

getpass.getuser() was updated to always raise OSError instead of the ImportError it was leaking before.

Additionally catching OSError would presumably resolve this.

Example:

_______________ ERROR at setup of test_bundle_path[debian-bullseye] _______________

    def getuser():
        """Get the username from the environment or password database.

        First try various environment variables, then the password
        database.  This works on Windows as long as USERNAME is set.
        Any failure to find a username raises OSError.

        .. versionchanged:: 3.13
            Previously, various exceptions beyond just :exc:`OSError`
            were raised.
        """

        for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
            user = os.environ.get(name)
            if user:
                return user

        try:
>           import pwd
E           ModuleNotFoundError: No module named 'pwd'

..\..\..\.pyenv\pyenv-win\versions\3.13.0a3\Lib\getpass.py:172: ModuleNotFoundError

The above exception was the direct cause of the following exception:

    def getuser():
        """Get the username from the environment or password database.

        First try various environment variables, then the password
        database.  This works on Windows as long as USERNAME is set.
        Any failure to find a username raises OSError.

        .. versionchanged:: 3.13
            Previously, various exceptions beyond just :exc:`OSError`
            were raised.
        """

        for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
            user = os.environ.get(name)
            if user:
                return user

        try:
            import pwd
            return pwd.getpwuid(os.getuid())[0]
        except (ImportError, KeyError) as e:
>           raise OSError('No username set in the environment') from e
E           OSError: No username set in the environment

..\..\..\.pyenv\pyenv-win\versions\3.13.0a3\Lib\getpass.py:175: OSError

CI example: https://github.com/beeware/briefcase/actions/runs/7688404294/job/20949599385

python -m pip list
❯ python -m pip list
Package                     Version                 Editable project location
--------------------------- ----------------------- -------------------------------------------------
arrow                       1.3.0
binaryornot                 0.4.4
briefcase                   0.3.17.dev246+g22bc0a0b C:\Users\user\github\beeware\briefcase
briefcase-automation        0.3.17.dev117+gd2187c76
build                       1.0.3
cachetools                  5.3.2
certifi                     2023.11.17
cfgv                        3.4.0
chardet                     5.2.0
charset-normalizer          3.3.2
click                       8.1.7
colorama                    0.4.6
cookiecutter                2.5.0
coverage                    7.4.1
coverage-conditional-plugin 0.9.0
distlib                     0.3.8
execnet                     2.0.2
filelock                    3.13.1
gitdb                       4.0.11
GitPython                   3.1.41
identify                    2.5.33
idna                        3.6
iniconfig                   2.0.0
Jinja2                      3.1.3
markdown-it-py              3.0.0
MarkupSafe                  2.1.4
mdurl                       0.1.2
nodeenv                     1.8.0
packaging                   23.2
pip                         23.3.2
platformdirs                4.1.0
pluggy                      1.4.0
pre-commit                  3.6.0
psutil                      5.9.8
Pygments                    2.17.2
pyproject-api               1.6.1
pyproject_hooks             1.0.0
pytest                      8.0.0
pytest-xdist                3.5.0
python-dateutil             2.8.2
python-slugify              8.0.2
PyYAML                      6.0.1
requests                    2.31.0
rich                        13.7.0
setuptools                  69.0.3
setuptools-scm              8.0.4
six                         1.16.0
smmap                       5.0.1
text-unidecode              1.3
tomli_w                     1.0.0
tox                         4.12.1
types-python-dateutil       2.8.19.20240106
typing_extensions           4.9.0
urllib3                     2.1.0
virtualenv                  20.25.0
wheel                       0.42.0
x-briefcase-automation      0.3.17.dev246+g22bc0a0b C:\Users\user\github\beeware\briefcase\automation

OS: Windows 11, Server 2022
Python: 3.13.0a3
pytest: 8.0.0

  • a detailed description of the bug or problem you are having
  • output of pip list from the virtual environment you are using
  • pytest and operating system versions
  • minimal example if possible

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions