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 12 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
12 changes: 9 additions & 3 deletions Doc/library/netrc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
.. 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.
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,
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 @@ -45,6 +46,11 @@
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
:class:`netrc` try to use the value of the :envvar:`NETRC` environment variable

Check warning on line 50 in Doc/library/netrc.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

'envvar' reference target not found: NETRC [ref.envvar]
if when *file* is not passed as argument, before falling back to the user's
:file:`.netrc` file in the home directory.


.. 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
-----

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


os.path
-------

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

class netrc:
def __init__(self, file=None):
default_netrc = file is None
netrc_envvar = os.environ.get("NETRC", "")
default_netrc = file is None and not bool(netrc_envvar)
if file is None:
file = os.path.join(os.path.expanduser("~"), ".netrc")
file = netrc_envvar or os.path.join(os.path.expanduser("~"), ".netrc")
self.hosts = {}
self.macros = {}
try:
Expand Down
Loading
Loading