netrc.netrc() emits a syntax error if it encounters a comment after a blank line. Prior to the 3.11, it only did this if the comment was the first non-whitespace thing in the file, but now with 3.11 it does it for all comments.
$ python netrc-test.py Traceback (most recent call last): File "/home/lukeshu/netrc-test.py", line 14, in <module> print(netrc.netrc(filename)) ^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.11/netrc.py", line 31, in __init__ self._parse(file, fp, default_netrc) File "/usr/lib/python3.11/netrc.py", line 66, in _parse raise NetrcParseError(netrc.NetrcParseError: bad toplevel token 'HTTP' (netrctest, line 3)
This issue was made worse by #26330. Prior to that change (prior to 3.11), it only triggered on the if the comment was the first thing in the file. For example, if we remove the leading newline:
I recently found out that ~/.netrc files do not officially support comments and even worse, if you add # prefix to a line, netrc will still load that line and attempt to use that entry!
In other cased it might produce a syntax error but the current behavior is clearly messed badly.
Curl itself seems to treat lines prefixed with # as commented, but not python. Python will somehow load them incorrectly. I tried with a github.com entry and while the entry is correct by default and used by github/python to successfully download, once I add # prefix, curl continues to work (using non authenticated calls), python will fail, likely because is sending an incorrect request to github.
Activity
Merge branch 'main' into pythongh-104306-netrc-comments
Merge branch 'main' into pythongh-104306-netrc-comments
Merge branch 'main' into pythongh-104306-netrc-comments
ssbarnea commented on Jan 4, 2025
I recently found out that ~/.netrc files do not officially support comments and even worse, if you add # prefix to a line, netrc will still load that line and attempt to use that entry!
In other cased it might produce a syntax error but the current behavior is clearly messed badly.
Curl itself seems to treat lines prefixed with # as commented, but not python. Python will somehow load them incorrectly. I tried with a github.com entry and while the entry is correct by default and used by github/python to successfully download, once I add # prefix, curl continues to work (using non authenticated calls), python will fail, likely because is sending an incorrect request to github.
I suspect, it might be fixed by #104511
Merge branch 'main' into pythongh-104306-netrc-comments