grass
grass copied to clipboard
[Bug] `g.proj` output is not flushed to stdout when redirected in Windows
To Reproduce Steps to reproduce the behavior:
- Run
g.proj -was a command line utility or using Python:gs.start_command + gs.communicate/gs.read_command. - Run the same command but redirect STDOUT to a file
g.proj -w 1> proj.out.txtor using Python:gs.start_command(..., stdout=gs.PIPE).
Expected behavior
When doing 1., it will output the correct projection string to the terminal. When using 2. to retrieve the string produced by the command, the proj.out.txt file (or the result from the gs.communicate call) will be empty.
System description:
- Operating System: Windows 11
- GRASS GIS version 7.8.8 (latest available through OSGeo4W)
Additional context
After some investigation, I found out (from the source of g.proj) that it will print results using fprintf(stdout, ...) but will not call fflush(stdout) upon completion. This leads to a situation in Windows where the OS will flush it automatically when printing to the screen but not when the output is redirected, leading to truncated files. A simple fix would be to add a fflush(stdout) call by the end of all utilities that rely on printing to stdout.
tested here with
System Info
GRASS version: 8.3.2
Code revision: exported
Build date: 2024-05-20
Build platform: x86_64-w64-mingw32
GDAL: 3.9.0
PROJ: 9.4.0
GEOS: 3.12.1
SQLite: 3.45.1
Python: 3.12.4
wxPython: 4.2.1
Platform: Windows-11-10.0.22631-SP0 (OSGeo4W)
and
GRASS version: 8.5.0dev
Code revision: 0b0cffce0
Build date: 2024-06-16
Build platform: x86_64-w64-mingw32
GDAL: 3.9.0
PROJ: 9.4.0
GEOS: 3.12.2
SQLite: 3.45.1
Python: 3.12.4
wxPython: 4.2.1
Platform: Windows-11-10.0.22631-SP0 (OSGeo4W)
g.proj -w 1> proj.out.txt and g.proj -w > proj.out.txt work on the OSGeo4W command shell