-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
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
base: main
Are you sure you want to change the base?
Conversation
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 |
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 |
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 |
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 |
There was a problem hiding this 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 |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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.
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, |
There was a problem hiding this comment.
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()) |
There was a problem hiding this comment.
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`, |
There was a problem hiding this comment.
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.
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. |
There was a problem hiding this comment.
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.
""" | ||
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. | ||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
""" | |
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 |
There was a problem hiding this comment.
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.
📚 Documentation preview 📚: https://cpython-previews--135802.org.readthedocs.build/