[Lldb-commits] [lldb] r250525 - Update SWIG typemaps to use `PythonFile`.

Zachary Turner via lldb-commits lldb-commits at lists.llvm.org
Fri Oct 16 09:39:18 PDT 2015


Author: zturner
Date: Fri Oct 16 11:39:18 2015
New Revision: 250525

URL: http://llvm.org/viewvc/llvm-project?rev=250525&view=rev
Log:
Update SWIG typemaps to use `PythonFile`.

Using the Python native C API is non-portable across Python versions,
so this patch changes them to use the `PythonFile` class which hides
the version specific differences behind a single interface.

Modified:
    lldb/trunk/scripts/Python/python-typemaps.swig
    lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
    lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h

Modified: lldb/trunk/scripts/Python/python-typemaps.swig
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/Python/python-typemaps.swig?rev=250525&r1=250524&r2=250525&view=diff
==============================================================================
--- lldb/trunk/scripts/Python/python-typemaps.swig (original)
+++ lldb/trunk/scripts/Python/python-typemaps.swig Fri Oct 16 11:39:18 2015
@@ -503,27 +503,31 @@
 %typemap(in) FILE * {
    if ($input == Py_None)
       $1 = NULL;
-   else if (!PyFile_Check($input)) {
+   else if (!lldb_private::PythonFile::Check($input)) {
       int fd = PyObject_AsFileDescriptor($input);
-      PyObject *py_mode = PyObject_GetAttrString($input, "mode");
-      if (!py_mode) {
-         PyErr_SetString(PyExc_TypeError,"not a file-like object");
-         return NULL;
-      }
-      const char *mode = PyString_AsString(py_mode);
-      if (-1 != fd && mode) {
+      lldb_private::PythonString py_mode(lldb_private::PyRefType::Owned,
+		  PyObject_GetAttrString($input, "mode"));
+
+      if (-1 != fd && py_mode.IsValid()) {
          FILE *f;
-         if ((f = fdopen(fd, mode)))
+         if ((f = fdopen(fd, py_mode.GetString().str().c_str())))
             $1 = f;
          else
             PyErr_SetString(PyExc_TypeError, strerror(errno));
       } else {
          PyErr_SetString(PyExc_TypeError,"not a file-like object");
-         return NULL;
+         return nullptr;
       }
    }
    else
-      $1 = PyFile_AsFile($input);
+   {
+      lldb_private::File file;
+	    lldb_private::PythonFile py_file(lldb_private::PyRefType::Borrowed, $input);
+	    if (!py_file.GetUnderlyingFile(file))
+	      return nullptr;
+
+      $1 = file.GetStream();
+   }
 }
 
 %typemap(out) FILE * {
@@ -539,7 +543,9 @@
    else // if (flags & __SRW)
       mode[i++] = 'a';
 #endif
-   $result = PyFile_FromFile($1, const_cast<char*>(""), mode, fflush);
+   lldb_private::File file($1, false);
+   lldb_private::PythonFile py_file(file, mode);
+   $result = py_file.release();
 }
 
 %typemap(in) (const char* string, int len) {

Modified: lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp?rev=250525&r1=250524&r2=250525&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp (original)
+++ lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp Fri Oct 16 11:39:18 2015
@@ -582,6 +582,14 @@ PythonFile::PythonFile(File &file, const
     Reset(file, mode);
 }
 
+PythonFile::PythonFile(const char *path, const char *mode)
+{
+    FILE *fp = nullptr;
+    fp = fopen(path, mode);
+    lldb_private::File file(fp, true);
+    Reset(file, mode);
+}
+
 PythonFile::PythonFile(PyRefType type, PyObject *o)
 {
     Reset(type, o);
@@ -651,4 +659,18 @@ PythonFile::Reset(File &file, const char
 #endif
 }
 
+bool
+PythonFile::GetUnderlyingFile(File &file) const
+{
+    if (!IsValid())
+        return false;
+
+    file.Close();
+    // We don't own the file descriptor returned by this function, make sure the
+    // File object knows about that.
+    file.SetDescriptor(PyObject_AsFileDescriptor(m_py_obj), false);
+    return file.IsValid();
+}
+
+
 #endif

Modified: lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h?rev=250525&r1=250524&r2=250525&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h (original)
+++ lldb/trunk/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h Fri Oct 16 11:39:18 2015
@@ -323,7 +323,8 @@ public:
 class PythonFile : public PythonObject
 {
   public:
-    explicit PythonFile(File &file, const char *mode);
+    PythonFile(File &file, const char *mode);
+    PythonFile(const char *path, const char *mode);
     PythonFile(PyRefType type, PyObject *o);
     ~PythonFile() override;
 
@@ -333,6 +334,8 @@ class PythonFile : public PythonObject
 
     void Reset(PyRefType type, PyObject *py_obj) override;
     void Reset(File &file, const char *mode);
+
+    bool GetUnderlyingFile(File &file) const;
 };
 
 } // namespace lldb_private




More information about the lldb-commits mailing list