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

Skip to content

gh-135788: Support NETRC environment variable in netrc #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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Doc/library/netrc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ the Unix :program:`ftp` program and other FTP clients.
.. class:: netrc([file])

A :class:`~netrc.netrc` instance or subclass instance encapsulates data from a netrc
file. The initialization argument, if present, specifies the file to parse. If
no argument is given, the file :file:`.netrc` in the user's home directory --
as determined by :func:`os.path.expanduser` -- will be read. Otherwise,
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,
before defaulting 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,
a :exc:`FileNotFoundError` exception will be raised.
Parse errors will raise :exc:`NetrcParseError` with diagnostic
information including the file name, line number, and terminating token.
Expand All @@ -47,6 +48,9 @@ the Unix :program:`ftp` program and other FTP clients.
can contain arbitrary characters, like whitespace and non-ASCII characters.
If the login name is anonymous, it won't trigger the security check.

.. versionadded:: next
Added support for the :envvar:`!NETRC` environment variable.


.. exception:: NetrcParseError

Expand Down
7 changes: 7 additions & 0 deletions Doc/whatsnew/3.15.rst
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ math
(Contributed by Sergey B Kirpichev in :gh:`132908`.)


netrc
-----

* Added support for the :envvar:`!NETRC` environment variable in :func:`netrc.netrc`.
(Contributed by Berthin Torres in :gh:`135788`.)


os.path
-------

Expand Down
7 changes: 4 additions & 3 deletions Lib/netrc.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ def push_token(self, token):

class netrc:
def __init__(self, file=None):
default_netrc = file is None
if file is None:
file = os.path.join(os.path.expanduser("~"), ".netrc")
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?

self.hosts = {}
self.macros = {}
try:
Expand Down
Loading
Loading