diff --git a/lldb/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py b/lldb/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py --- a/lldb/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py +++ b/lldb/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py @@ -845,6 +845,7 @@ def i(sbf): for i in range(10): f = sbf.GetFile() + self.assertEqual(f.mode, "w") yield f sbf = lldb.SBFile.Create(f, borrow=True) yield sbf diff --git a/lldb/source/Host/common/File.cpp b/lldb/source/Host/common/File.cpp --- a/lldb/source/Host/common/File.cpp +++ b/lldb/source/Host/common/File.cpp @@ -310,7 +310,7 @@ if (m_own_stream) { if (::fclose(m_stream) == EOF) error.SetErrorToErrno(); - } else { + } else if (m_options & eOpenOptionWrite) { if (::fflush(m_stream) == EOF) error.SetErrorToErrno(); } diff --git a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h --- a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h +++ b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h @@ -189,6 +189,14 @@ "key not in dict"); } +#if PY_MAJOR_VERSION < 3 +// The python 2 API declares some arguments as char* that should +// be const char *, but it doesn't actually modify them. +inline char *py2_const_cast(const char *s) { return const_cast(s); } +#else +inline const char *py2_const_cast(const char *s) { return s; } +#endif + enum class PyInitialValue { Invalid, Empty }; template struct PythonFormat; @@ -309,16 +317,6 @@ StructuredData::ObjectSP CreateStructuredObject() const; -protected: - -#if PY_MAJOR_VERSION < 3 - // The python 2 API declares some arguments as char* that should - // be const char *, but it doesn't actually modify them. - static char *py2_const_cast(const char *s) { return const_cast(s); } -#else - static const char *py2_const_cast(const char *s) { return s; } -#endif - public: template llvm::Expected CallMethod(const char *name, diff --git a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp --- a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp +++ b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp @@ -1502,12 +1502,19 @@ file_obj = PyFile_FromFd(file.GetDescriptor(), nullptr, mode, -1, nullptr, "ignore", nullptr, 0); #else - // Read through the Python source, doesn't seem to modify these strings - char *cmode = const_cast(mode); // We pass ::flush instead of ::fclose here so we borrow the FILE* -- - // the lldb_private::File still owns it. - file_obj = - PyFile_FromFile(file.GetStream(), const_cast(""), cmode, ::fflush); + // the lldb_private::File still owns it. NetBSD does not allow + // input files to be flushed, so we have to check for that case too. + int (*closer)(FILE *); + auto opts = file.GetOptions(); + if (!opts) + return opts.takeError(); + if (opts.get() & File::eOpenOptionWrite) + closer = ::fflush; + else + closer = [](FILE *) { return 0; }; + file_obj = PyFile_FromFile(file.GetStream(), py2_const_cast(""), + py2_const_cast(mode), closer); #endif if (!file_obj)