Skip to content
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

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion misc/scripts/copyright_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

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

Copy link
Member Author

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.

Copy link
Contributor

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.

Copy link
Member Author

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.

Copy link
Contributor

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.

Copy link
Member Author

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.

line = fileread.readline()

if line.find("/**********") == -1: # Godot header starts this way
Expand Down
Loading