You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Not sure I can really call this a bug, however there is a behavioral change between 2.7 and at least 3.6 and 3.7 (probably earlier versions of the 3.x series as well). There is no spec for .netrc files that I can find, certainly nothing which mentions comment or blank lines. Still, Python's netrc file parser seems happy with both.
However, in 3.x a blank line followed immediately by a comment line containing actual comment text causes the parser to raise a parse error. I've attached two netrc files, netrc-comment-blank, and netrc-blank-comment, identical save for the ordering of a blank line and a comment line. Here's what a 2.7.14 session looks like:
Python 2.7.14 |Anaconda, Inc.| (default, Mar 27 2018, 17:29:31)
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import netrc
>>> rc = netrc.netrc(file="/home/skip/tmp/netrc-comment-blank")
>>> rc = netrc.netrc(file="/home/skip/tmp/netrc-blank-comment")
Here's 3.7.0:
Python 3.7.0 (default, Jun 28 2018, 13:15:42)
[GCC 7.2.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import netrc
>>> rc = netrc.netrc(file="/home/skip/tmp/netrc-comment-blank")
>>> rc = netrc.netrc(file="/home/skip/tmp/netrc-blank-comment")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/skip/miniconda3/envs/python3/lib/python3.7/netrc.py", line 30, in __init__
self._parse(file, fp, default_netrc)
File "/home/skip/miniconda3/envs/python3/lib/python3.7/netrc.py", line 63, in _parse
"bad toplevel token %r" % tt, file, lexer.lineno)
netrc.NetrcParseError: bad toplevel token 'Comment' (/home/skip/tmp/netrc-blank-comment, line 2)
This seems to have been introduced with commit : 1df0f21 before which it was working fine.
Testing shows the error from 1df0f21 and above. I have checked out only versions of Lib/netrc.py and I think this doesn't require a rebuild of Python.
➜ cpython git:(master) $ git log --oneline --format="%h" Lib/netrc.py > commits.txt # Get all commits to test the changes
➜ cpython git:(master) ✗ ./python netrc_foo_tester.py
netrc_foo_tester.py is attached with the message which basically does the below using Python for every commit. netrc_foo.py contains the actual test of parsing two files.
➜ cpython git:(master) ✗ ./python
Python 3.8.0a0 (heads/master:35c0809, Jul 16 2018, 10:29:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
I took a shot at this, but closed my PR when I found that it introduced a different regression.
After playing with it for a bit, I think that this commit [1] is a bit problematic because it uses lexer.instream.readline(), which can make the line number in lexer.lineno incorrect.
I may take another look if I have time, but someone else is free to tackle this.
Activity
smontanaro commentedon Jul 16, 2018
Not sure I can really call this a bug, however there is a behavioral change between 2.7 and at least 3.6 and 3.7 (probably earlier versions of the 3.x series as well). There is no spec for .netrc files that I can find, certainly nothing which mentions comment or blank lines. Still, Python's netrc file parser seems happy with both.
However, in 3.x a blank line followed immediately by a comment line containing actual comment text causes the parser to raise a parse error. I've attached two netrc files, netrc-comment-blank, and netrc-blank-comment, identical save for the ordering of a blank line and a comment line. Here's what a 2.7.14 session looks like:
Here's 3.7.0:
tirkarthi commentedon Jul 17, 2018
This seems to have been introduced with commit : 1df0f21 before which it was working fine.
Testing shows the error from 1df0f21 and above. I have checked out only versions of Lib/netrc.py and I think this doesn't require a rebuild of Python.
➜ cpython git:(master) $ git log --oneline --format="%h" Lib/netrc.py > commits.txt # Get all commits to test the changes
➜ cpython git:(master) ✗ ./python netrc_foo_tester.py
netrc_foo_tester.py is attached with the message which basically does the below using Python for every commit. netrc_foo.py contains the actual test of parsing two files.
➜ cpython git:(master) ✗ ./python
Python 3.8.0a0 (heads/master:35c0809, Jul 16 2018, 10:29:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
➜ cpython git:(master) ✗ cat netrc_foo.py
import netrc
I hope the above approach is correct.
Thanks
bbayles commentedon Jul 20, 2018
I took a shot at this, but closed my PR when I found that it introduced a different regression.
After playing with it for a bit, I think that this commit [1] is a bit problematic because it uses lexer.instream.readline(), which can make the line number in lexer.lineno incorrect.
I may take another look if I have time, but someone else is free to tackle this.
[1] 1df0f21#diff-e6896f6d68ca3fd8094a933533f8b2ed
bbayles commentedon Jul 21, 2018
I realized that the Python 2 solution was adapt-able after all; I've re-submitted as GitHub PR 8360.
#8360
diegogimenez04 commentedon Jun 9, 2026
Hello everyone. Is this still an issue in newer versions? If not, I suggest closing it.
hugovk commentedon Oct 25, 2026
Likely duplicate of #79089, fixed by #26330.