diff --git a/src/idl_gen_python.cpp b/src/idl_gen_python.cpp index cb25fbc55e..fa88f43561 100644 --- a/src/idl_gen_python.cpp +++ b/src/idl_gen_python.cpp @@ -2957,7 +2957,9 @@ class PythonGenerator : public BaseGenerator { i != std::string::npos; i = directories.find(kPathSeparator, i + 1)) { const std::string init_py = directories.substr(0, i) + kPathSeparator + "__init__.py"; - parser_.opts.file_saver->SaveFile(init_py.c_str(), "", false); + if (!FileExists(init_py.c_str())) { + parser_.opts.file_saver->SaveFile(init_py.c_str(), "", false); + } } const std::string filename = directories + defname; diff --git a/tests/PythonTest.sh b/tests/PythonTest.sh index e7806bd429..7a57a96576 100755 --- a/tests/PythonTest.sh +++ b/tests/PythonTest.sh @@ -30,6 +30,16 @@ ${test_dir}/../flatc -p -o ${gen_code_path} -I include_test nested_union_test.fb ${test_dir}/../flatc -p -o ${gen_code_path} -I include_test service_test.fbs --grpc --grpc-python-typed-handlers --python-typing --no-python-gen-numpy --gen-onefile ${test_dir}/../flatc -p -o ${gen_code_path} union_name_test.fbs --gen-object-api +# Verify that flatc --python preserves existing __init__.py files (#9229) +mkdir -p ${gen_code_path}/preserve_test +echo "# HANDWRITTEN CONTENT" > ${gen_code_path}/preserve_test/__init__.py +${test_dir}/../flatc -p -o ${gen_code_path}/preserve_test -I include_test monster_test.fbs +if ! grep -q "# HANDWRITTEN CONTENT" ${gen_code_path}/preserve_test/__init__.py; then + echo "FAIL: flatc overwrote existing __init__.py" + exit 1 +fi +rm -rf ${gen_code_path}/preserve_test + # Syntax: run_tests # interpreters_tested=()