cpython: 8625ce7da152 (original) (raw)
--- a/Lib/netrc.py +++ b/Lib/netrc.py @@ -2,7 +2,7 @@
Module and documentation by Eric S. Raymond, 21 Dec 1998
-import os, shlex +import io, os, shlex all = ["netrc", "NetrcParseError"] @@ -37,12 +37,14 @@ class netrc: lexer.commenters = lexer.commenters.replace('#', '') while 1: # Look for a machine, default, or macdef top-level keyword
saved_lineno = lexer.lineno[](#l1.16) toplevel = tt = lexer.get_token()[](#l1.17) if not tt:[](#l1.18) break[](#l1.19) elif tt[0] == '#':[](#l1.20)
fp.readline();[](#l1.21)
continue;[](#l1.22)
if lexer.lineno == saved_lineno and len(tt) == 1:[](#l1.23)
lexer.instream.readline()[](#l1.24)
continue[](#l1.25) elif tt == 'machine':[](#l1.26) entryname = lexer.get_token()[](#l1.27) elif tt == 'default':[](#l1.28)
@@ -68,8 +70,8 @@ class netrc: self.hosts[entryname] = {} while 1: tt = lexer.get_token()
if (tt=='' or tt == 'machine' or[](#l1.33)
tt == 'default' or tt =='macdef'):[](#l1.34)
if (tt.startswith('#') or[](#l1.35)
tt in {'', 'machine', 'default', 'macdef'}):[](#l1.36) if password:[](#l1.37) self.hosts[entryname] = (login, account, password)[](#l1.38) lexer.push_token(tt)[](#l1.39)
--- a/Lib/test/test_netrc.py +++ b/Lib/test/test_netrc.py @@ -1,54 +1,107 @@ - -import netrc, os, unittest, sys +import netrc, os, unittest, sys, textwrap from test import support -TEST_NETRC = """ -
-#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 = support.TESTFN class NetrcTestCase(unittest.TestCase):
- def setUp(self):
mode = 'w'[](#l2.35)
if sys.platform not in ['cygwin']:[](#l2.36)
mode += 't'[](#l2.37)
fp = open(temp_filename, mode)[](#l2.38)
fp.write(TEST_NETRC)[](#l2.39)
fp.close()[](#l2.40)
self.nrc = netrc.netrc(temp_filename)[](#l2.41)
- def tearDown(self): os.unlink(temp_filename)
- def test_case_1(self):
self.assertEqual(self.nrc.hosts['foo'], ('log1', 'acct1', 'pass1'))[](#l2.47)
self.assertEqual(self.nrc.hosts['default'], ('log2', None, 'pass2'))[](#l2.48)
- def make_nrc(self, test_data):
test_data = textwrap.dedent(test_data)[](#l2.50)
mode = 'w'[](#l2.51)
if sys.platform != 'cygwin':[](#l2.52)
mode += 't'[](#l2.53)
with open(temp_filename, mode) as fp:[](#l2.54)
fp.write(test_data)[](#l2.55)
return netrc.netrc(temp_filename)[](#l2.56)
- def test_default(self):
nrc = self.make_nrc("""\[](#l2.59)
machine host1.domain.com login log1 password pass1 account acct1[](#l2.60)
default login log2 password pass2[](#l2.61)
""")[](#l2.62)
self.assertEqual(nrc.hosts['host1.domain.com'],[](#l2.63)
('log1', 'acct1', 'pass1'))[](#l2.64)
self.assertEqual(nrc.hosts['default'], ('log2', None, 'pass2'))[](#l2.65)
self.assertEqual(self.nrc.macros, {'macro1':['line1\n', 'line2\n'],[](#l2.68)
'macro2':['line3\n', 'line4\n']})[](#l2.69)
nrc = self.make_nrc("""\[](#l2.70)
macdef macro1[](#l2.71)
line1[](#l2.72)
line2[](#l2.73)
macdef macro2[](#l2.75)
line3[](#l2.76)
line4[](#l2.77)
""")[](#l2.78)
self.assertEqual(nrc.macros, {'macro1': ['line1\n', 'line2\n'],[](#l2.79)
'macro2': ['line3\n', 'line4\n']})[](#l2.80)
- def _test_passwords(self, nrc, passwd):
nrc = self.make_nrc(nrc)[](#l2.83)
self.assertEqual(nrc.hosts['host.domain.com'], ('log', 'acct', passwd))[](#l2.84)
- def test_password_with_leading_hash(self):
self._test_passwords("""\[](#l2.87)
machine host.domain.com login log password #pass account acct[](#l2.88)
""", '#pass')[](#l2.89)
- def test_password_with_trailing_hash(self):
self._test_passwords("""\[](#l2.92)
machine host.domain.com login log password pass# account acct[](#l2.93)
""", 'pass#')[](#l2.94)
- def test_password_with_internal_hash(self):
self._test_passwords("""\[](#l2.97)
machine host.domain.com login log password pa#ss account acct[](#l2.98)
""", 'pa#ss')[](#l2.99)
- def _test_comment(self, nrc, passwd='pass'):
nrc = self.make_nrc(nrc)[](#l2.102)
self.assertEqual(nrc.hosts['foo.domain.com'], ('bar', None, passwd))[](#l2.103)
self.assertEqual(nrc.hosts['bar.domain.com'], ('foo', None, 'pass'))[](#l2.104)
- def test_parses_passwords_with_hash_character(self):
self.assertEqual(self.nrc.hosts['bar'], ('log1', 'acct1', 'pass#'))[](#l2.107)
- def test_comment_before_machine_line(self):
self._test_comment("""\[](#l2.109)
# comment[](#l2.110)
machine foo.domain.com login bar password pass[](#l2.111)
machine bar.domain.com login foo password pass[](#l2.112)
""")[](#l2.113)
- def test_comment_before_machine_line_no_space(self):
self._test_comment("""\[](#l2.116)
#comment[](#l2.117)
machine foo.domain.com login bar password pass[](#l2.118)
machine bar.domain.com login foo password pass[](#l2.119)
""")[](#l2.120)
- def test_comment_before_machine_line_hash_only(self):
self._test_comment("""\[](#l2.123)
#[](#l2.124)
machine foo.domain.com login bar password pass[](#l2.125)
machine bar.domain.com login foo password pass[](#l2.126)
""")[](#l2.127)
- def test_comment_at_end_of_machine_line(self):
self._test_comment("""\[](#l2.130)
machine foo.domain.com login bar password pass # comment[](#l2.131)
machine bar.domain.com login foo password pass[](#l2.132)
""")[](#l2.133)
- def test_comment_at_end_of_machine_line_no_space(self):
self._test_comment("""\[](#l2.136)
machine foo.domain.com login bar password pass #comment[](#l2.137)
machine bar.domain.com login foo password pass[](#l2.138)
""")[](#l2.139)
- def test_comment_at_end_of_machine_line_pass_has_hash(self):
self._test_comment("""\[](#l2.142)
machine foo.domain.com login bar password #pass #comment[](#l2.143)
machine bar.domain.com login foo password pass[](#l2.144)
""", '#pass')[](#l2.145)
+ def test_main(): support.run_unittest(NetrcTestCase)
--- a/Misc/ACKS +++ b/Misc/ACKS @@ -564,6 +564,7 @@ Paul Moore Derek Morr James A Morrison Mher Movsisyan +Ruslan Mstoi Sjoerd Mullender Sape Mullender Michael Muller
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -7,6 +7,11 @@ What's New in Python 3.1.4? Release date: 2011-05-XX +Library +------- + +- Issue #12009: Fixed regression in netrc file comment handling. + Extension Modules -----------------