Issue 28024: fileinput causes RecursionErrors when dealing with large numbers of empty files (original) (raw)
Accidentally discovered this while running code that processes a whole bunch of files, when it turned out the files were empty. The readline method of fileinput.input will make a recursive call to continue processing when it reaches the end of a file. This is fine when files aren't empty, but if you have sufficient (~1000) empty files being processed, this causes recursion errors.
Simple example:
for i in range(10000): ... with open('test{}'.format(i), 'wb'): ... pass ... import fileinput, glob ''.join(fileinput.input(glob.glob('./test*'))) Traceback ... ... (almost all the levels are showing the self.readline() call at the end of readline) ... RecursionError: maximum recursion depth exceeded
Admittedly a niche case, but a relatively simple switch from recursion to iteration would solve it. Same problem appears in all versions of Python.