diff -r 932de36903e7 Lib/netrc.py --- a/Lib/netrc.py Tue May 17 13:22:30 2011 +0200 +++ b/Lib/netrc.py Tue May 17 17:28:18 2011 +0300 @@ -41,8 +41,11 @@ if not tt: break elif tt[0] == '#': - fp.readline(); - continue; + # seek the beginning of the comment + pos = len(tt) + 1 + lexer.instream.seek(-pos, 1) + lexer.instream.readline() + continue elif tt == 'machine': entryname = lexer.get_token() elif tt == 'default': @@ -68,7 +71,7 @@ self.hosts[entryname] = {} while 1: tt = lexer.get_token() - if (tt=='' or tt == 'machine' or + if (tt=='' or tt == 'machine' or tt[0] == "#" or tt == 'default' or tt =='macdef'): if password: self.hosts[entryname] = (login, account, password) diff -r 932de36903e7 Lib/test/test_netrc.py --- a/Lib/test/test_netrc.py Tue May 17 13:22:30 2011 +0200 +++ b/Lib/test/test_netrc.py Tue May 17 17:28:18 2011 +0300 @@ -21,6 +21,16 @@ default login log2 password pass2 +machine host.domain.com login username password somet#hing + +# comment1 +machine host2.domain.com login username2 password something2 #comment at end + +#comment2 +machine host3.domain.com login username3 password something3 # comment at end with space + +# +machine host4.domain.com login username4 password something4 """ temp_filename = test_support.TESTFN @@ -49,6 +59,14 @@ def test_parses_passwords_with_hash_character(self): self.assertEqual(self.nrc.hosts['bar'], ('log1', 'acct1', 'pass#')) + self.assertEqual(self.nrc.hosts['host.domain.com'], ('username', None, 'somet#hing')) + + def test_comments(self): + '''Verifies that comment lines do not cause the following entries to be + skipped''' + self.assertEqual(self.nrc.hosts['host2.domain.com'], ('username2', None, 'something2')) + self.assertEqual(self.nrc.hosts['host3.domain.com'], ('username3', None, 'something3')) + self.assertEqual(self.nrc.hosts['host4.domain.com'], ('username4', None, 'something4')) def test_main(): test_support.run_unittest(NetrcTestCase)