@@ -184,3 +184,39 @@ def test_duplicate_keys(self):
184184
185185 self .assertEqual (str (ctx .exception ),
186186 "Duplicate dict key 'bar' in file None on line 3" )
187+
188+
189+ class TestManyComments (unittest .TestCase ):
190+ """
191+ Regression tests for RecursionError with many consecutive comment/blank
192+ lines.
193+ """
194+
195+ def _make_config_with_comments (self , n ):
196+ lines = ["# comment %d" % i for i in range (n )]
197+ lines .append ("x = 1" )
198+ return "\n " .join (lines )
199+
200+ def test_many_comments_from_string (self ):
201+ cfg = self ._make_config_with_comments (1000 )
202+ conf = PyConfigParser ()
203+ conf .load_from_string (cfg )
204+ self .assertEqual (conf ["x" ], 1 )
205+
206+ def test_many_comments_from_file (self ):
207+ cfg = self ._make_config_with_comments (1000 )
208+ conf = PyConfigParser ()
209+ with tempfile .NamedTemporaryFile (mode = "w" , suffix = ".conf" , delete = False ) as f :
210+ f .write (cfg )
211+ path = f .name
212+ try :
213+ conf .load_from_file (path )
214+ self .assertEqual (conf ["x" ], 1 )
215+ finally :
216+ os .unlink (path )
217+
218+ def test_many_blank_lines_from_string (self ):
219+ cfg = "\n " * 1000 + "x = 1\n "
220+ conf = PyConfigParser ()
221+ conf .load_from_string (cfg )
222+ self .assertEqual (conf ["x" ], 1 )
0 commit comments