[Lldb-commits] [lldb] [lldb] Check for abstract methods implementation in Scripted Plugin Objects (PR #71260)

Alex Langford via lldb-commits lldb-commits at lists.llvm.org
Fri Nov 3 18:08:52 PDT 2023


================
@@ -103,24 +139,79 @@ class ScriptedPythonInterface : virtual public ScriptedInterface {
                                   "Resulting object is not initialized.");
 
       std::apply(
-          [&method, &expected_return_object](auto &&...args) {
+          [&init, &expected_return_object](auto &&...args) {
             llvm::consumeError(expected_return_object.takeError());
-            expected_return_object = method(args...);
+            expected_return_object = init(args...);
           },
           transformed_args);
 
-      if (llvm::Error e = expected_return_object.takeError())
-        return std::move(e);
-      result = std::move(expected_return_object.get());
+      if (!expected_return_object)
+        return expected_return_object.takeError();
+      result = expected_return_object.get();
     }
 
-    if (!result.IsValid())
+    if (!result.IsValid() || !result.HasAttribute("__class__"))
+      return llvm::createStringError(
+          llvm::inconvertibleErrorCode(),
+          "Resulting object is not a valid Python Object.");
+
+    PythonObject obj_class = result.GetAttributeValue("__class__");
+
+    PythonString obj_class_name =
+        obj_class.GetAttributeValue("__name__").AsType<PythonString>();
+
+    PythonObject object_class_mapping_proxy =
+        obj_class.GetAttributeValue("__dict__");
+
+    PythonCallable dict_converter = PythonModule::BuiltinsModule()
+                                        .ResolveName("dict")
+                                        .AsType<PythonCallable>();
+    if (!dict_converter.IsAllocated())
+      return llvm::createStringError(
+          llvm::inconvertibleErrorCode(),
+          "Resulting object is not a valid Python Object.");
+
+    PythonDictionary object_class_dict =
+        dict_converter(object_class_mapping_proxy).AsType<PythonDictionary>();
+    if (!object_class_dict.IsAllocated())
       return llvm::createStringError(
           llvm::inconvertibleErrorCode(),
           "Resulting object is not a valid Python Object.");
 
+    auto checker_or_err = CheckAbstractMethodImplementation(object_class_dict);
+    if (!checker_or_err)
+      return checker_or_err.takeError();
+
+#define LOG_ABSTRACT_METHOD_STATUS(state)                                      \
+  LLDB_LOG(GetLog(LLDBLog::Script), "Abstract method {0}.{1} " #state ".",     \
+           obj_class_name.GetString(), method_checker.first)
+
+    for (const auto &method_checker : *checker_or_err)
+      switch (method_checker.second) {
+      case AbstractMethodCheckerCases::eNotImplemented:
+        LOG_ABSTRACT_METHOD_STATUS(not implemented);
+        break;
+      case AbstractMethodCheckerCases::eNotAllocated:
+        LOG_ABSTRACT_METHOD_STATUS(not allocated);
+        break;
+      case AbstractMethodCheckerCases::eNotCallable:
+        LOG_ABSTRACT_METHOD_STATUS(not callable);
+        break;
+      case AbstractMethodCheckerCases::eValid:
+        LOG_ABSTRACT_METHOD_STATUS(implemented);
+        break;
+      }
+#undef LOG_ABSTRACT_METHOD_STATUS
+
+    for (const auto &method_checker : *checker_or_err) {
+      if (method_checker.second != AbstractMethodCheckerCases::eValid)
+        return llvm::createStringError(llvm::inconvertibleErrorCode(),
+                                       "Abstract method {0}.{1} missing ❌.");
----------------
bulbazord wrote:

Love the emoji

https://github.com/llvm/llvm-project/pull/71260


More information about the lldb-commits mailing list