8000 gh-135788: Support NETRC environment variable in `netrc` by berthin · Pull Request #135802 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-135788: Support NETRC environment variable in netrc 8000 #135802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 19 commits into
base: main
Choose a base branch
from

Conversation

berthin
Copy link
@berthin berthin commented Jun 21, 2025
  • This change adds support for reading the NETRC environment variable when no file is passed as argument to the constructor of netrc, which takes precedence over the user's home directory .netrc file.
  • Test cases are updated to check all initialisation scenarios.
  • The documentation for netrc has been updated to reflect this behavior.

📚 Documentation preview 📚: https://cpython-previews--135802.org.readthedocs.build/

@bedevere-app
Copy link
bedevere-app bot commented Jun 21, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@bedevere-app
Copy link
bedevere-app bot commented Jun 21, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@bedevere-app
Copy link
bedevere-app bot commented Jun 21, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@bedevere-app
Copy link
bedevere-app bot commented Jun 22, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

Copy link
Member
@picnixz picnixz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid verbose comments in tests in general unless they are needed. Most of the time, the code is small enough to understand what happens without the needs of a docstring.

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
@python-cla-bot
Copy link
python-cla-bot bot commented Jun 22, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

Berthin Torres added 2 commits June 22, 2025 10:22
envvar_netrc = os.environ.get("NETRC", "")
home_netrc = os.path.join(os.path.expanduser("~"), ".netrc")
file = file or envvar_netrc or home_netrc
default_netrc = (file == home_netrc)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The paths may be different but resolve to the same file.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please elaborate more? what's the expected behavior in that case?

@picnixz picnixz changed the title gh-135788: Support NETRC environment variable in netrc and update documentation gh-135788: Support NETRC environment variable in netrc Jun 22, 2025
@picnixz
Copy link
Member
picnixz commented Jun 22, 2025

So there is some WASI issue because of some missing OS function. I think it's because we previously never tested some code path that used that function, hence the issue. For now just ignore this failure.

Copy link
Member
@picnixz picnixz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the WASI issue is in netrc.py itself and the default_netrc stuff.

I suspect that the if os.name == 'posix' check is not sufficient and we need if hasattr(os, "getuid"). We should have a separate PR and an other issue just for that, so for now you can skip the failing test on WASI (namely only the "use_default_netrc_in_home", not the others) and you should also document that this is not supported on WASI (when the PR fixing the WASI handling lands, we'll update that note, but until then the docs should be consistent and say that NETRC envvar is not supported on WASI)

@picnixz
Copy link
Member
picnixz commented Jun 22, 2025

I'll work on the WASI failure here #135815. If my PR lands before yours, then you won't need to change the test. I suggest not to change anything for now as I think I can fix it by the end of the day.

@picnixz
Copy link
Member
picnixz commented Jun 22, 2025

The WASI test should be fixed. You can now resolve the conflicts (sorry for them!)

@berthin
Copy link
Author
berthin commented Jun 22, 2025

The WASI test should be fixed. You can now resolve the conflicts (sorry for them!)

No worries, and thanks for fixing that issue!

@berthin
Copy link
Author
berthin commented Jun 22, 2025

I don't get why I get the Result: ENV CHANGED then ENV CHANGED in Ubuntu (free-threading) / build and test (ubuntu-24.04), will take a look later during the week.

@picnixz
Copy link
Member
picnixz commented Jun 22, 2025

I've restarted the CI job. It might be a false positive as it's under free-threading and we've had similar failures

@cmaloney
Copy link
Contributor

Commented on the issue, adding for visibility here: Multiple http libraries which use CPython's netrc implement a security feature trust_env which indicates whether environment variables (and sometimes files in home directories) should be trusted, default reading environment variables in an underlying library seems likely to bypass that, so I think this needs to be opt-in for things that use netrc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0