[Lldb-commits] [lldb] 6e450d1 - [lldb] Use SWIG_fail in python-typemaps.swig (NFC)

Dave Lee via lldb-commits lldb-commits at lists.llvm.org
Fri Sep 16 08:25:15 PDT 2022


Author: Dave Lee
Date: 2022-09-16T08:25:01-07:00
New Revision: 6e450d134755c41bd4ac0ba356e31fc37ba88757

URL: https://github.com/llvm/llvm-project/commit/6e450d134755c41bd4ac0ba356e31fc37ba88757
DIFF: https://github.com/llvm/llvm-project/commit/6e450d134755c41bd4ac0ba356e31fc37ba88757.diff

LOG: [lldb] Use SWIG_fail in python-typemaps.swig (NFC)

When attempting to use SWIG's `-builtin` flag, there were a few compile
failures caused by a mismatch between return type and return value. In those
cases, the return type was `int` but many of the type maps assume returning
`NULL`/`nullptr` (only the latter caused compile failures).

This fix abstracts failure paths to use the `SWIG_fail` macro, which performs
`goto fail;`. Each of the generated functions contain a `fail` label, which
performs any resource cleanup and returns the appropriate failure value.

This change isn't strictly necessary at this point, but seems like the right
thing to do, and for anyone who tries `-builtin` later, it resolves those
issues.

Differential Revision: https://reviews.llvm.org/D133961

Added: 
    

Modified: 
    lldb/bindings/python/python-typemaps.swig

Removed: 
    


################################################################################
diff  --git a/lldb/bindings/python/python-typemaps.swig b/lldb/bindings/python/python-typemaps.swig
index bf3de66b91bf1..203be803d2ebd 100644
--- a/lldb/bindings/python/python-typemaps.swig
+++ b/lldb/bindings/python/python-typemaps.swig
@@ -18,7 +18,7 @@
       if (!py_str.IsAllocated()) {
         PyErr_SetString(PyExc_TypeError, "list must contain strings");
         free($1);
-        return nullptr;
+        SWIG_fail;
       }
 
       $1[i] = const_cast<char *>(py_str.GetString().data());
@@ -28,7 +28,7 @@
     $1 = NULL;
   } else {
     PyErr_SetString(PyExc_TypeError, "not a list");
-    return NULL;
+    SWIG_fail;
   }
 }
 
@@ -70,7 +70,7 @@
   PythonObject obj = Retain<PythonObject>($input);
   lldb::tid_t value = unwrapOrSetPythonException(As<unsigned long long>(obj));
   if (PyErr_Occurred())
-    return nullptr;
+    SWIG_fail;
   $1 = value;
 }
 
@@ -79,10 +79,10 @@
   unsigned long long state_type_value =
       unwrapOrSetPythonException(As<unsigned long long>(obj));
   if (PyErr_Occurred())
-    return nullptr;
+    SWIG_fail;
   if (state_type_value > lldb::StateType::kLastStateType) {
     PyErr_SetString(PyExc_ValueError, "Not a valid StateType value");
-    return nullptr;
+    SWIG_fail;
   }
   $1 = static_cast<lldb::StateType>(state_type_value);
 }
@@ -93,12 +93,12 @@
 %typemap(in) (char *dst, size_t dst_len) {
   if (!PyInt_Check($input)) {
     PyErr_SetString(PyExc_ValueError, "Expecting an integer");
-    return NULL;
+    SWIG_fail;
   }
   $2 = PyInt_AsLong($input);
   if ($2 <= 0) {
     PyErr_SetString(PyExc_ValueError, "Positive integer expected");
-    return NULL;
+    SWIG_fail;
   }
   $1 = (char *)malloc($2);
 }
@@ -129,12 +129,12 @@
 %typemap(in) (char *dst_or_null, size_t dst_len) {
   if (!PyInt_Check($input)) {
     PyErr_SetString(PyExc_ValueError, "Expecting an integer");
-    return NULL;
+    SWIG_fail;
   }
   $2 = PyInt_AsLong($input);
   if ($2 <= 0) {
     PyErr_SetString(PyExc_ValueError, "Positive integer expected");
-    return NULL;
+    SWIG_fail;
   }
   $1 = (char *)malloc($2);
 }
@@ -166,7 +166,7 @@
     $2 = bytes.GetSize();
   } else {
     PyErr_SetString(PyExc_ValueError, "Expecting a string");
-    return NULL;
+    SWIG_fail;
   }
 }
 // For SBProcess::WriteMemory, SBTarget::GetInstructions and SBDebugger::DispatchInput.
@@ -186,7 +186,7 @@
     $2 = bytes.GetSize();
   } else {
     PyErr_SetString(PyExc_ValueError, "Expecting a buffer");
-    return NULL;
+    SWIG_fail;
   }
 }
 
@@ -199,11 +199,11 @@
     $2 = PyLong_AsLong($input);
   } else {
     PyErr_SetString(PyExc_ValueError, "Expecting an integer or long object");
-    return NULL;
+    SWIG_fail;
   }
   if ($2 <= 0) {
     PyErr_SetString(PyExc_ValueError, "Positive integer expected");
-    return NULL;
+    SWIG_fail;
   }
   $1 = (void *)malloc($2);
 }
@@ -287,12 +287,12 @@ template <> bool SetNumberFromPyObject<double>(double &number, PyObject *obj) {
       if (!SetNumberFromPyObject($1[i], o)) {
         PyErr_SetString(PyExc_TypeError, "list must contain numbers");
         free($1);
-        return NULL;
+        SWIG_fail;
       }
 
       if (PyErr_Occurred()) {
         free($1);
-        return NULL;
+        SWIG_fail;
       }
     }
   } else if ($input == Py_None) {
@@ -300,7 +300,7 @@ template <> bool SetNumberFromPyObject<double>(double &number, PyObject *obj) {
     $2 = 0;
   } else {
     PyErr_SetString(PyExc_TypeError, "not a list");
-    return NULL;
+    SWIG_fail;
   }
 }
 
@@ -353,7 +353,7 @@ template <> bool SetNumberFromPyObject<double>(double &number, PyObject *obj) {
   if (!($input == Py_None ||
         PyCallable_Check(reinterpret_cast<PyObject *>($input)))) {
     PyErr_SetString(PyExc_TypeError, "Need a callable object or None!");
-    return NULL;
+    SWIG_fail;
   }
 
   // FIXME (filcab): We can't currently check if our callback is already
@@ -377,11 +377,11 @@ template <> bool SetNumberFromPyObject<double>(double &number, PyObject *obj) {
   PythonFile py_file(PyRefType::Borrowed, $input);
   if (!py_file) {
     PyErr_SetString(PyExc_TypeError, "not a file");
-    return nullptr;
+    SWIG_fail;
   }
   auto sp = unwrapOrSetPythonException(py_file.ConvertToFile());
   if (!sp)
-    return nullptr;
+    SWIG_fail;
   $1 = sp;
 }
 
@@ -389,12 +389,12 @@ template <> bool SetNumberFromPyObject<double>(double &number, PyObject *obj) {
   PythonFile py_file(PyRefType::Borrowed, $input);
   if (!py_file) {
     PyErr_SetString(PyExc_TypeError, "not a file");
-    return nullptr;
+    SWIG_fail;
   }
   auto sp = unwrapOrSetPythonException(
       py_file.ConvertToFileForcingUseOfScriptingIOMethods());
   if (!sp)
-    return nullptr;
+    SWIG_fail;
   $1 = sp;
 }
 
@@ -402,12 +402,12 @@ template <> bool SetNumberFromPyObject<double>(double &number, PyObject *obj) {
   PythonFile py_file(PyRefType::Borrowed, $input);
   if (!py_file) {
     PyErr_SetString(PyExc_TypeError, "not a file");
-    return nullptr;
+    SWIG_fail;
   }
   auto sp =
       unwrapOrSetPythonException(py_file.ConvertToFile(/*borrowed=*/true));
   if (!sp)
-    return nullptr;
+    SWIG_fail;
   $1 = sp;
 }
 
@@ -415,12 +415,12 @@ template <> bool SetNumberFromPyObject<double>(double &number, PyObject *obj) {
   PythonFile py_file(PyRefType::Borrowed, $input);
   if (!py_file) {
     PyErr_SetString(PyExc_TypeError, "not a file");
-    return nullptr;
+    SWIG_fail;
   }
   auto sp = unwrapOrSetPythonException(
       py_file.ConvertToFileForcingUseOfScriptingIOMethods(/*borrowed=*/true));
   if (!sp)
-    return nullptr;
+    SWIG_fail;
   $1 = sp;
 }
 
@@ -439,7 +439,7 @@ template <> bool SetNumberFromPyObject<double>(double &number, PyObject *obj) {
   if (sp) {
     PythonFile pyfile = unwrapOrSetPythonException(PythonFile::FromFile(*sp));
     if (!pyfile.IsValid())
-      return nullptr;
+      SWIG_fail;
     $result = pyfile.release();
   }
   if (!$result) {
@@ -468,7 +468,7 @@ template <> bool SetNumberFromPyObject<double>(double &number, PyObject *obj) {
     py_str.release();
   } else {
     PyErr_SetString(PyExc_TypeError, "not a string-like object");
-    return NULL;
+    SWIG_fail;
   }
 }
 


        


More information about the lldb-commits mailing list