diff -r 932de36903e7 Lib/netrc.py --- a/Lib/netrc.py Tue May 17 13:22:30 2011 +0200 +++ b/Lib/netrc.py Mon May 23 11:01:19 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,8 +71,8 @@ self.hosts[entryname] = {} while 1: tt = lexer.get_token() - if (tt=='' or tt == 'machine' or - tt == 'default' or tt =='macdef'): + if (tt.startswith('#') or + tt in {'', 'machine', 'default', 'macdef'}): if password: self.hosts[entryname] = (login, account, password) lexer.push_token(tt) 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 Mon May 23 11:01:19 2011 +0300 @@ -1,54 +1,78 @@ -import netrc, os, unittest, sys +import netrc, os, unittest, sys, textwrap from test import test_support -TEST_NETRC = """ - - #this is a comment -#this is a comment -# this is a comment - -machine foo login log1 password pass1 account acct1 -machine bar login log1 password pass# account acct1 - -macdef macro1 -line1 -line2 - -macdef macro2 -line3 -line4 - -default login log2 password pass2 - -""" - temp_filename = test_support.TESTFN class NetrcTestCase(unittest.TestCase): - def setUp (self): - mode = 'w' - if sys.platform not in ['cygwin']: - mode += 't' - fp = open(temp_filename, mode) - fp.write(TEST_NETRC) - fp.close() - self.nrc = netrc.netrc(temp_filename) - - def tearDown (self): + def tearDown(self): os.unlink(temp_filename) - def test_case_1(self): - self.assertEqual(self.nrc.hosts['foo'], ('log1', 'acct1', 'pass1')) - self.assertEqual(self.nrc.hosts['default'], ('log2', None, 'pass2')) + def make_nrc(self, test_data): + test_data = textwrap.dedent(test_data) + mode = 'w' + if sys.platform != 'cygwin': + mode += 't' + with open(temp_filename, mode) as fp: + fp.write(test_data) + fp.close() + return netrc.netrc(temp_filename) + + def test_default(self): + nrc = self.make_nrc("""\ + machine host1.domain.com login log1 password pass1 account acct1 + default login log2 password pass2 + """) + self.assertEqual(('log1', 'acct1', 'pass1'), nrc.hosts['host1.domain.com']) + self.assertEqual(('log2', None, 'pass2'), nrc.hosts['default']) def test_macros(self): - self.assertEqual(self.nrc.macros, {'macro1':['line1\n', 'line2\n'], - 'macro2':['line3\n', 'line4\n']}) + nrc = self.make_nrc("""\ + macdef macro1 + line1 + line2 + + macdef macro2 + line3 + line4 + """) + self.assertEqual({'macro1':['line1\n', 'line2\n'], + 'macro2':['line3\n', 'line4\n']}, nrc.macros) - def test_parses_passwords_with_hash_character(self): - self.assertEqual(self.nrc.hosts['bar'], ('log1', 'acct1', 'pass#')) + def test_passwords_with_hash_character(self): + nrc = self.make_nrc("""\ + #this is a comment + #this is a comment + # this is a comment + + machine host1.domain.com login log1 password #pass1 account acct1 + machine host2.domain.com login log2 password pass2# account acct2 + machine host3.domain.com login log3 password pa#ss3 account acct3 + """) + self.assertEqual(('log1', 'acct1', '#pass1'), nrc.hosts['host1.domain.com']) + self.assertEqual(('log2', 'acct2', 'pass2#'), nrc.hosts['host2.domain.com']) + self.assertEqual(('log3', 'acct3', 'pa#ss3'), nrc.hosts['host3.domain.com']) + + def test_comments(self): + '''Verifies that comment lines do not cause the following entries to be + skipped''' + nrc = self.make_nrc("""\ + machine host1.domain.com login log1 password pass1 + + # comment1 + machine host2.domain.com login log2 password pass2 #comment at end + + #comment2 + machine host3.domain.com login log3 password pass3 # comment at end with space + + # + machine host4.domain.com login log4 password pass4 + """) + self.assertEqual(('log1', None, 'pass1'), nrc.hosts['host1.domain.com']) + self.assertEqual(('log2', None, 'pass2'), nrc.hosts['host2.domain.com']) + self.assertEqual(('log3', None, 'pass3'), nrc.hosts['host3.domain.com']) + self.assertEqual(('log4', None, 'pass4'), nrc.hosts['host4.domain.com']) def test_main(): test_support.run_unittest(NetrcTestCase)