From 9c40264fdadbb229ce311c9bda898981d7ac8eae Mon Sep 17 00:00:00 2001 From: Zachary Turner Date: Thu, 15 Oct 2015 19:35:48 +0000 Subject: Introduce a `PythonFile` object, and use it everywhere. Python file handling got an overhaul in Python 3, and it affects the way we have to interact with files. Notably: 1) `PyFile_FromFile` no longer exists, and instead we have to use `PyFile_FromFd`. This means having a way to get an fd from a FILE*. For this we reuse the lldb_private::File class to convert between FILE*s and fds, since there are some subtleties regarding ownership rules when FILE*s and fds refer to the same file. 2) PyFile is no longer a builtin type, so there is no such thing as `PyFile_Check`. Instead, files in Python 3 are just instances of `io.IOBase`. So the logic for checking if something is a file in Python 3 is to check if it is a subclass of that module. Additionally, some unit tests are added to verify that `PythonFile` works as expected on Python 2 and Python 3, and `ScriptInterpreterPython` is updated to use `PythonFile` instead of manual calls to the various `PyFile_XXX` methods. llvm-svn: 250444 --- .../ScriptInterpreter/Python/PythonDataObjects.h | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h') diff --git a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h index 7d95d576945..34549d97f3d 100644 --- a/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h +++ b/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h @@ -19,6 +19,7 @@ #include "lldb/Core/ConstString.h" #include "lldb/Core/StructuredData.h" #include "lldb/Core/Flags.h" +#include "lldb/Host/File.h" #include "lldb/Interpreter/OptionValue.h" namespace lldb_private { @@ -67,7 +68,8 @@ enum class PyObjectType Integer, Dictionary, List, - String + String, + File }; enum class PyRefType @@ -196,6 +198,9 @@ public: return *this; } + bool + HasAttribute(llvm::StringRef attribute) const; + bool IsValid() const; @@ -315,6 +320,21 @@ public: StructuredData::DictionarySP CreateStructuredDictionary() const; }; +class PythonFile : public PythonObject +{ + public: + explicit PythonFile(File &file, const char *mode); + PythonFile(PyRefType type, PyObject *o); + ~PythonFile() override; + + static bool Check(PyObject *py_obj); + + using PythonObject::Reset; + + void Reset(PyRefType type, PyObject *py_obj) override; + void Reset(File &file, const char *mode); +}; + } // namespace lldb_private #endif // LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_PYTHONDATAOBJECTS_H -- cgit v1.2.3