Skip to content

Commit 1ae9bbf

Browse files
berthinpicnixz
andauthored
Apply suggestions from code review
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
1 parent 2715bda commit 1ae9bbf

3 files changed

Lines changed: 8 additions & 22 deletions

File tree

Doc/library/netrc.rst

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ the Unix :program:`ftp` program and other FTP clients.
1919

2020
A :class:`~netrc.netrc` instance or subclass instance encapsulates data from a netrc
2121
file. The initialization argument, if present, specifies the file to parse. If no
22-
argument is given, it will look for the file path in the :envvar:`!NETRC` environment variable.
23-
If that is not set, it defaults to reading the file :file:`.netrc` in the user's home
22+
argument is given, it will look for the file path in the :envvar:`!NETRC` environment variable,
23+
before defaulting to reading the file :file:`.netrc` in the user's home
2424
directory -- as determined by :func:`os.path.expanduser`. If the file cannot be found,
2525
a :exc:`FileNotFoundError` exception will be raised.
2626
Parse errors will raise :exc:`NetrcParseError` with diagnostic
@@ -47,9 +47,7 @@ the Unix :program:`ftp` program and other FTP clients.
4747
If the login name is anonymous, it won't trigger the security check.
4848

4949
.. versionadded:: next
50-
:class:`netrc` try to use the value of the :envvar:`NETRC` environment variable
51-
if when *file* is not passed as argument, before falling back to the user's
52-
:file:`.netrc` file in the home directory.
50+
Added support for the :envvar:`!NETRC` environment variable.
5351

5452

5553
.. exception:: NetrcParseError

Doc/whatsnew/3.15.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ math
119119
netrc
120120
-----
121121

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

125125

Lib/test/test_netrc.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import netrc, os, unittest, sys, textwrap
2-
3-
from test import support
42
from contextlib import ExitStack
3+
from test import support
4+
from test.support import os_helper
55

66
try:
77
import pwd
@@ -36,24 +36,15 @@ def __exit__(self, *ignore_exc):
3636
self.stack.close()
3737

3838
def generate_netrc(
39-
self,
40-
content,
41-
filename=".netrc",
42-
mode=0o600,
43-
encoding="utf-8",
39+
self, content, filename=".netrc", mode=0o600, encoding=None,
4440
):
4541
"""Create and return the path to a temporary `.netrc` file."""
46-
write_mode = "w"
47-
if sys.platform != "cygwin":
48-
write_mode += "t"
49-
5042
netrc_file = os.path.join(self.tmpdir, filename)
43+
write_mode = "w" if sys.platform != "cygwin" else "wt"
5144
with open(netrc_file, mode=write_mode, encoding=encoding) as fp:
5245
fp.write(textwrap.dedent(content))
53-
5446
if support.os_helper.can_chmod():
5547
os.chmod(netrc_file, mode=mode)
56-
5748
return netrc_file
5849

5950

@@ -70,7 +61,6 @@ def use_default_netrc_in_home(*args, **kwargs):
7061
with NetrcEnvironment() as helper:
7162
helper.environ.unset("NETRC")
7263
helper.environ.set("HOME", helper.tmpdir)
73-
7464
helper.generate_netrc(*args, **kwargs)
7565
return netrc.netrc()
7666

@@ -82,7 +72,6 @@ def use_netrc_envvar(*args, **kwargs):
8272
with NetrcEnvironment() as helper:
8373
netrc_file = helper.generate_netrc(*args, **kwargs)
8474
helper.environ.set("NETRC", netrc_file)
85-
8675
return netrc.netrc()
8776

8877
@staticmethod
@@ -93,7 +82,6 @@ def use_file_argument(*args, **kwargs):
9382
# Just to stress a bit more the test scenario, the NETRC envvar
9483
# will contain rubish information which shouldn't be used
9584
helper.environ.set("NETRC", "not-a-file.netrc")
96-
9785
netrc_file = helper.generate_netrc(*args, **kwargs)
9886
return netrc.netrc(netrc_file)
9987

0 commit comments

Comments
 (0)