Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-135788: Support NETRC environment variable in netrc and update documentation #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 6 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.

@@ -65,9 +65,10 @@ def push_token(self, token):

class netrc:
def __init__(self, file=None):
default_netrc = file is None
Copy link
Member

Choose a reason for hiding this comment

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

This variable is not used so you can drop it.

"""
self.stack.close()

def generate_netrc(self, content, filename=".netrc", mode=0o600, encoding="utf-8") -> str:
Copy link
Member

Choose a reason for hiding this comment

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

Please avoid type hints in the standard library.

Comment on lines +21 to +24
file. The initialization argument, if present, specifies the file to parse. If no
argument is given, it will look for the file path in the :envvar:`!NETRC` environment variable.
If that is not set, it defaults to reading the file :file:`.netrc` in the user's home
directory -- as determined by :func:`os.path.expanduser`. If the file cannot be found,
Copy link
Member

Choose a reason for hiding this comment

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

Please add a versionadded:: next entry below indicating that we now support the NETRC environment variable. Also, add a NEWS entry and update Doc/whatsnew/3.15.rst. TiA.

"""
self.stack = ExitStack()
self.environ = self.stack.enter_context(support.os_helper.EnvironmentVarGuard())
self.tmpdir = self.stack.enter_context(tempfile.TemporaryDirectory())
Copy link
Member

Choose a reason for hiding this comment

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

Avoid using tempfile.TemporaryDirectory but prefer using a test-only temporary directory via test.support.os_helper.temp_dir


def __exit__(self, *ignore_exc) -> None:
"""
Exits the managed environment and performs cleanup. This method closes the `ExitStack`,
Copy link
Member

Choose a reason for hiding this comment

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

Wrap all lines under 80 chars.

Comment on lines +40 to +48
Args:
content (str): The content to write into the `.netrc` file.
filename (str, optional): The name of the file to write. Defaults to ".netrc".
mode (int, optional): File permission bits to set after writing. Defaults to `0o600`. Mode
is set only if the platform supports `chmod`.
encoding (str, optional): The encoding used to write the file. Defaults to "utf-8".
Returns:
str: The full path to the generated `.netrc` file.
Copy link
Member

Choose a reason for hiding this comment

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

The parameters are self-explanatory no need for them.

Comment on lines +37 to +49
"""
Creates a `.netrc` file in the temporary directory with the given content and permissions.
Args:
content (str): The content to write into the `.netrc` file.
filename (str, optional): The name of the file to write. Defaults to ".netrc".
mode (int, optional): File permission bits to set after writing. Defaults to `0o600`. Mode
is set only if the platform supports `chmod`.
encoding (str, optional): The encoding used to write the file. Defaults to "utf-8".
Returns:
str: The full path to the generated `.netrc` file.
"""
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"""
Creates a `.netrc` file in the temporary directory with the given content and permissions.
Args:
content (str): The content to write into the `.netrc` file.
filename (str, optional): The name of the file to write. Defaults to ".netrc".
mode (int, optional): File permission bits to set after writing. Defaults to `0o600`. Mode
is set only if the platform supports `chmod`.
encoding (str, optional): The encoding used to write the file. Defaults to "utf-8".
Returns:
str: The full path to the generated `.netrc` file.
"""
"""Create and return the path to a temporary `.netrc` file."""

('anonymous', '', 'pass'))
os.chmod(fn, 0o622)
content = """\
machine foo.domain.com login anonymous password pass
Copy link
Member

Choose a reason for hiding this comment

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

Keep the old test; we'll add an other test in a follow-up PR for anonymous entry without a default.

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.

2 participants