Skip to content

fix(python): preserve existing __init__.py files during code generation (fixes #9229) - #9234

Open
jdymitarai wants to merge 1 commit into
google:masterfrom
jdymitarai:fix/python-preserve-existing-init-py
Open

fix(python): preserve existing __init__.py files during code generation (fixes #9229)#9234
jdymitarai wants to merge 1 commit into
google:masterfrom
jdymitarai:fix/python-preserve-existing-init-py

Conversation

@jdymitarai

Copy link
Copy Markdown

Fixes #9229.

Problem

flatc --python creates an empty __init__.py in every folder of the generated namespace path.
Currently, it unconditionally calls parser_.opts.file_saver->SaveFile(init_py.c_str(), "", false) on every directory, silently overwriting and destroying any pre-existing, hand-written __init__.py files with an empty file without warning.

Solution

Check if (!FileExists(init_py.c_str())) before writing, ensuring that only missing __init__.py files are created while any existing files (e.g. containing user imports, exports, or application code) are preserved intact.

Testing

Added a regression test to tests/PythonTest.sh that writes a pre-existing __init__.py file with custom content, executes flatc -p, and asserts that the custom content was not overwritten.

…on (fixes google#9229)

flatc --python currently unconditionally calls SaveFile(init_py, "", false) for every directory in the namespace path, silently destroying any hand-written __init__.py files that already exist in those directories.

This change checks FileExists(init_py.c_str()) before writing, ensuring that only missing __init__.py files are created.

Fixes google#9229.
@github-actions github-actions Bot added python c++ codegen Involving generating code from schema labels Sep 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ codegen Involving generating code from schema python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Python] flatc --python silently overwrites existing __init__.py files

1 participant