GeezerVison is showing github.com — Obscure netrc parser "bug" · Issue #78313 · python/cpython
Obscure netrc parser "bug" #78313Description
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee = None
closed_at = None
created_at = <Date 2018-07-16.23:13:33.685>
labels = ['3.7', '3.8', 'type-bug', 'library']
title = 'Obscure netrc parser "bug"'
updated_at = <Date 2018-07-21.00:54:36.328>
user = 'https://github.com/smontanaro'
bugs.python.org fields:
activity = <Date 2018-07-21.00:54:36.328>
actor = 'bbayles'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2018-07-16.23:13:33.685>
creator = 'skip.montanaro'
dependencies = []
files = ['47696', '47697', '47699']
hgrepos = []
issue_num = 34132
keywords = ['patch']
message_count = 4.0
messages = ['321779', '321810', '321975', '322053']
nosy_count = 4.0
nosy_names = ['skip.montanaro', 'xiang.zhang', 'bbayles', 'xtreak']
pr_nums = ['8320', '8360']
priority = 'normal'
resolution = None
stage = 'patch review'
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue34132'
versions = ['Python 3.6', 'Python 3.7', 'Python 3.8']
Activitysmontanaro commented on Jul 16, 2018 ContributorAuthor More actions 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)tirkarthi commented on 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
rc = netrc.netrc(file="netrc-comment-blank")
rc = netrc.netrc(file="netrc-blank-comment")
➜ cpython git:(master) ✗ git reset --quiet HEAD . && git checkout . && git checkout be19ed7 Lib/netrc.py && ./python netrc_foo.py
netrc_foo.py:2: ResourceWarning: unclosed file <_io.TextIOWrapper name='netrc-comment-blank' mode='r' encoding='UTF-8'>
rc = netrc.netrc(file="netrc-comment-blank")
netrc_foo.py:3: ResourceWarning: unclosed file <_io.TextIOWrapper name='netrc-blank-comment' mode='r' encoding='UTF-8'>
rc = netrc.netrc(file="netrc-blank-comment")
➜ cpython git:(master) ✗ git reset --quiet HEAD . && git checkout . && git checkout c12a813 Lib/netrc.py && ./python netrc_foo.py
➜ cpython git:(master) ✗ git reset --quiet HEAD . && git checkout . && git checkout 78a1a15 Lib/netrc.py && ./python netrc_foo.py
➜ cpython git:(master) ✗ git reset --quiet HEAD . && git checkout . && git checkout 1df0f21 Lib/netrc.py && ./python netrc_foo.py
Traceback (most recent call last):
File "netrc_foo.py", line 3, in <module>
rc = netrc.netrc(file="netrc-blank-comment")
File "/home/cpython/Lib/netrc.py", line 32, in __init__
self._parse(file, fp)
File "/home/cpython/Lib/netrc.py", line 65, in _parse
"bad toplevel token %r" % tt, file, lexer.lineno)
netrc.NetrcParseError: bad toplevel token 'Comment' (netrc-blank-comment, line 2)
I hope the above approach is correct.
Thanks bbayles commented on 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 commented on 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 commented on Jun 9, 2026 Hello everyone. Is this still an issue in newer versions? If not, I suggest closing it. hugovk commented on Oct 25, 2026
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|