-
-
Notifications
You must be signed in to change notification settings - Fork 21.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix infinite while loop on empty added file. #90588
Fix infinite while loop on empty added file. #90588
Conversation
I'm not familiar with the way python does line reading, I'm assuming this preserves the original behaviour, and that "empty" lines are actually |
yes empty lines at least have the "\n" except if they are the last line. So effectively "" is only reached in two cases :
those two cases clearly indicates we have reached the end of the file and there is nothing more to skip (because there is nothing more at all) |
@@ -73,7 +73,7 @@ | |||
line = fileread.readline() | |||
header_done = False | |||
|
|||
while line.strip() == "": # Skip empty lines at the top | |||
while line.strip() == "" and line != "": # Skip empty lines at the top |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the recommended way to parse files in Python is
while fileread:
not
while fileread.readline() != "":
I'd recommend changing the code to something like
while fileread:
line = fileread.readline()
if line == "":
continue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not how file works in python the fileread
variable will not be considered as false when end of file is reached. This means while fileread:
will be an infinite loop. Moreover here we want to remove only the empty lines at the start of the file which is exactly what it was doing. Code was ok just didn't think of empty file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, this should probably be rewritten to use for loops instead of while loops. It looks like for line in file
is the recommended way to iterate through a file line by line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fact I agree that for loop are more idiomatic in file reading in pythonbut both can be found, moreover here changing the while loop wouldn't be recommended because we are first skipping lines until we find a non empty line then skipping until we find a non header line. Those until are for me a sign that while loops are more appropriate. Moreover I don't think a refactor of the file would be a good idea when the fix is simply modifying one line and therefore has no link with the refactor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough. I tend to get scared of while
loops that don't have some guaranteed termination condition, but it's hard to argue with the simplicity of a one line fix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the line != ""
is in fact the guaranteed termination because we are sure it will evaluate to false at some point because the file as a finite number of lines and each loop will consume one line hence it will either terminate the loop because of the other condition or reach the end of the file and terminate the loop because of this condition.
Thanks! |
In case an empty file was added in a commit
copyright_headers
would enter in an infinite loop as the file only contains empty lines which the scripts try to remove. Added a check to make sure that it would exit the loop it cases it reaches the last line.this happened in #82198