fix(python): preserve existing __init__.py files during code generation (fixes #9229) - #9234
Open
jdymitarai wants to merge 1 commit into
Open
fix(python): preserve existing __init__.py files during code generation (fixes #9229)#9234jdymitarai wants to merge 1 commit into
jdymitarai wants to merge 1 commit into
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #9229.
Problem
flatc --pythoncreates an empty__init__.pyin 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__.pyfiles with an empty file without warning.Solution
Check
if (!FileExists(init_py.c_str()))before writing, ensuring that only missing__init__.pyfiles 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.shthat writes a pre-existing__init__.pyfile with custom content, executesflatc -p, and asserts that the custom content was not overwritten.