Skip to content

Write to File Object Created by argparse Without Context Manager Fails on Windows #14446

Open
@Doekin

Description

@Doekin

Describe the Bug

I encountered an issue when building p11-kit on Windows. The build process fails due to an empty header file being generated.

Upon investigation, it seems that writing to a file object without using a with outfile: context manager does not work with meson runpython on Windows.

To Reproduce

This issue can be reproduced by building p11-kit on Windows, or by running the following example header generator:

# file: writefile.py
import argparse

def write(outfile):
    outfile.write("Done")

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--outfile", type=argparse.FileType("w"), default="foo.txt")
    args = parser.parse_args()
    write(args.outfile)
  • On Windows:

    • Running python writefile.py correctly writes "Done" to foo.txt.
    • Running meson runpython writefile.py results in an empty foo.txt.
  • On Linux:

    • Both methods work as expected, writing "Done" to foo.txt.

Expected Behavior

Aligning the behavior with direct Python execution would be beneficial. This would allow scripts to function as expected on Windows.

System Parameters

  • Native build
  • Windows 10
  • Python 3.13.2
  • Meson 1.7.1
  • Ninja 1.12.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions