When working on the replacement of PySys_GetObject() I noticed a large number of sites (23) that call the flush() method of a file, check the result for error, and decref the returned object. It takes 7 lines of code and requires a temporary variable. There are many variations in writing the same code, so you need to read it carefully to recognize the pattern and ensure that it is correct.
I propose to add convenient function _PyFile_Flush() which calls the method and returns -1 on error and 0 on success. It allows to get rid of temporary variable and write the same code in 3 lines, including the closing }. It is also more recognizable.
Linked PRs
When working on the replacement of
PySys_GetObject()I noticed a large number of sites (23) that call the flush() method of a file, check the result for error, and decref the returned object. It takes 7 lines of code and requires a temporary variable. There are many variations in writing the same code, so you need to read it carefully to recognize the pattern and ensure that it is correct.I propose to add convenient function
_PyFile_Flush()which calls the method and returns -1 on error and 0 on success. It allows to get rid of temporary variable and write the same code in 3 lines, including the closing}. It is also more recognizable.Linked PRs