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 1 commit
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
Prev Previous commit
Next Next commit
clean up
  • Loading branch information
Berthin Torres authored and Berthin Torres committed Jun 22, 2025
commit 2d6021246e0949eee640f0b74bcdb9aa6711279e
8 changes: 4 additions & 4 deletions Lib/netrc.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ def push_token(self, token):

class netrc:
def __init__(self, file=None):
netrc_envvar = os.environ.get("NETRC", "")
default_netrc = file is None and not bool(netrc_envvar)
if file is None:
file = netrc_envvar or 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
16 changes: 8 additions & 8 deletions Lib/test/test_netrc.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ def __enter__(self):
"""Enter the managed environment."""
self.stack = ExitStack()
self.environ = self.stack.enter_context(
support.os_helper.EnvironmentVarGuard(),
os_helper.EnvironmentVarGuard(),
)
self.tmpdir = self.stack.enter_context(support.os_helper.temp_dir())
self.tmpdir = self.stack.enter_context(os_helper.temp_dir())
return self

def __exit__(self, *ignore_exc):
Expand All @@ -43,7 +43,7 @@ def generate_netrc(
write_mode = "w" if sys.platform != "cygwin" else "wt"
with open(netrc_file, mode=write_mode, encoding=encoding) as fp:
fp.write(textwrap.dedent(content))
if support.os_helper.can_chmod():
if os_helper.can_chmod():
os.chmod(netrc_file, mode=mode)
return netrc_file

Expand Down Expand Up @@ -372,20 +372,20 @@ def test_comment_at_end_of_machine_line_pass_has_hash(self, make_nrc):

@unittest.skipUnless(os.name == 'posix', 'POSIX only test')
@unittest.skipIf(pwd is None, 'security check requires pwd module')
@support.os_helper.skip_unless_working_chmod
@os_helper.skip_unless_working_chmod
def test_security(self):
# This test is incomplete since we are normally not run as root and
# therefore can't test the file ownership being wrong.
d = support.os_helper.TESTFN
d = os_helper.TESTFN
os.mkdir(d)
self.addCleanup(support.os_helper.rmtree, d)
self.addCleanup(os_helper.rmtree, d)
fn = os.path.join(d, '.netrc')
with open(fn, 'wt') as f:
f.write("""\
machine foo.domain.com login bar password pass
default login foo password pass
""")
with support.os_helper.EnvironmentVarGuard() as environ:
with os_helper.EnvironmentVarGuard() as environ:
environ.set('HOME', d)
os.chmod(fn, 0o600)
nrc = netrc.netrc()
Expand All @@ -398,7 +398,7 @@ def test_security(self):
machine foo.domain.com login anonymous password pass
default login foo password pass
""")
with support.os_helper.EnvironmentVarGuard() as environ:
with os_helper.EnvironmentVarGuard() as environ:
environ.set('HOME', d)
os.chmod(fn, 0o600)
nrc = netrc.netrc()
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy