[Lldb-commits] [lldb] [lldb/Interpreter] Make ScriptedInterface Object creation more generic (PR #68052)

2023-10-27 Thread David Spickett via lldb-commits

DavidSpickett wrote:

https://github.com/llvm/llvm-project/commit/02ef12dd808f19191a4d0a9f511c4e5a5dda59b5
 fixes the Windows test. If the behaviour seems suspicious to you, I can check 
more details of the build but I don't know anything about `Scripted...` so I'll 
need you to tell me what to look for.

https://github.com/llvm/llvm-project/pull/68052
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb/Interpreter] Make ScriptedInterface Object creation more generic (PR #68052)

2023-10-26 Thread David Spickett via lldb-commits

DavidSpickett wrote:

Never mind, this could have been the result of using an incompatible Python 
version. I'll get a stacktrace with one I know is used on the bot.

https://github.com/llvm/llvm-project/pull/68052
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb/Interpreter] Make ScriptedInterface Object creation more generic (PR #68052)

2023-10-26 Thread David Spickett via lldb-commits

DavidSpickett wrote:

Somehow it is getting to:
```
  virtual llvm::Expected
  CreatePluginObject(llvm::StringRef class_name, ExecutionContext &exe_ctx,
 StructuredData::DictionarySP args_sp,
 StructuredData::Generic *script_obj = nullptr) {
llvm_unreachable(
"Cannot create an instance of the ScriptedProcessInterface!");
  }
```
Starting to wonder if we're relying on some undefined order of evaluation 
somewhere.

https://github.com/llvm/llvm-project/pull/68052
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb/Interpreter] Make ScriptedInterface Object creation more generic (PR #68052)

2023-10-26 Thread David Spickett via lldb-commits

DavidSpickett wrote:

We still have a failure on Windows after the follow ups:
```
# .---command stderr
# | Cannot create an instance of the ScriptedProcessInterface!
# | UNREACHABLE executed at 
C:\Users\tcwg\david.spickett\llvm-project\lldb\include\lldb/Interpreter/Interfaces/ScriptedProcessInterface.h:29!
# | PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ 
and include the crash backtrace.
# | Stack dump:
# | 0.  Program arguments: 
c:\\users\\tcwg\\david.spickett\\build-llvm\\bin\\lldb-test.exe ir-memory-map 
C:\\Users\\tcwg\\david.spickett\\build-llvm\\tools\\lldb\\test\\Shell\\Expr\\Output\\TestIRMemoryMapWindows.test.tmp
 
C:\\Users\\tcwg\\david.spickett\\llvm-project\\lldb\\test\\Shell\\Expr/Inputs/ir-memory-map-basic
# | 1.  HandleCommand(command = "run")
# | Exception Code: 0xC01D
# |  #0 0x7ff71e493e0c HandleAbort 
C:\Users\tcwg\david.spickett\llvm-project\llvm\lib\Support\Windows\Signals.inc:424:0
# |  #1 0x7ffe20f30ef8 (C:\WINDOWS\SYSTEM32\ucrtbased.dll+0xa0ef8)
# |  #2 0x7ffe20f32a50 (C:\WINDOWS\SYSTEM32\ucrtbased.dll+0xa2a50)
# |  #3 0x7ff71e460358 llvm::llvm_unreachable_internal(char const *, char 
const *, unsigned int) 
C:\Users\tcwg\david.spickett\llvm-project\llvm\lib\Support\ErrorHandling.cpp:212:0
# |  #4 0x7ff71e999fc4 
lldb_private::ScriptedProcessInterface::CreatePluginObject(class 
llvm::StringRef, class lldb_private::ExecutionContext &, class 
std::shared_ptr, class 
lldb_private::StructuredData::Generic *) 
C:\Users\tcwg\david.spickett\llvm-project\lldb\include\lldb\Interpreter\Interfaces\ScriptedProcessInterface.h:28:0
```
I'm looking into it.

https://github.com/llvm/llvm-project/pull/68052
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb/Interpreter] Make ScriptedInterface Object creation more generic (PR #68052)

2023-10-25 Thread Med Ismail Bennani via lldb-commits

https://github.com/medismailben closed 
https://github.com/llvm/llvm-project/pull/68052
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb/Interpreter] Make ScriptedInterface Object creation more generic (PR #68052)

2023-10-25 Thread Jonas Devlieghere via lldb-commits

https://github.com/JDevlieghere approved this pull request.

LGTM

https://github.com/llvm/llvm-project/pull/68052
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb/Interpreter] Make ScriptedInterface Object creation more generic (PR #68052)

2023-10-25 Thread Med Ismail Bennani via lldb-commits


@@ -19,11 +19,11 @@
 namespace lldb_private {
 class ScriptedPlatformInterface : virtual public ScriptedInterface {
 public:
-  StructuredData::GenericSP
+  virtual llvm::Expected
   CreatePluginObject(llvm::StringRef class_name, ExecutionContext &exe_ctx,
  StructuredData::DictionarySP args_sp,
- StructuredData::Generic *script_obj = nullptr) override {
-return {};
+ StructuredData::Generic *script_obj = nullptr) {
+llvm_unreachable("unimplemented!");

medismailben wrote:

This can't be virtual pure because the `ScriptInterpreter` base class 
implements `virtual lldb::Scripted{Process,Platform,Thread}Interface()` methods 
that instantiate each base interface.

https://github.com/llvm/llvm-project/pull/68052
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb/Interpreter] Make ScriptedInterface Object creation more generic (PR #68052)

2023-10-24 Thread Med Ismail Bennani via lldb-commits

https://github.com/medismailben updated 
https://github.com/llvm/llvm-project/pull/68052

>From ef90c8a7f2f555cf312807d2bc83ffda45e8c2af Mon Sep 17 00:00:00 2001
From: Med Ismail Bennani 
Date: Tue, 24 Oct 2023 20:40:43 -0700
Subject: [PATCH] [lldb/Interpreter] Make ScriptedInterface Object creation
 more generic

This patch changes the way plugin objects used with Scripted Interfaces
are created.

Instead of implementing a different SWIG method to create the object for
every scripted interface, this patch makes the creation more generic by
re-using some of the ScriptedPythonInterface templated Dispatch code.

This patch also improves error handling of the object creation by
returning an `llvm::Expected`.

Signed-off-by: Med Ismail Bennani 
---
 lldb/bindings/python/python-wrapper.swig  |  43 ---
 .../Interfaces/ScriptedInterface.h|   5 -
 .../Interfaces/ScriptedPlatformInterface.h|   7 +-
 .../Interfaces/ScriptedProcessInterface.h |   7 +-
 .../Interfaces/ScriptedThreadInterface.h  |   7 +-
 .../Process/scripted/ScriptedProcess.cpp  |   9 +-
 .../Process/scripted/ScriptedThread.cpp   |  12 +-
 .../ScriptedPlatformPythonInterface.cpp   |  26 +
 .../ScriptedPlatformPythonInterface.h |   2 +-
 .../ScriptedProcessPythonInterface.cpp|  26 +
 .../ScriptedProcessPythonInterface.h  |   2 +-
 .../Interfaces/ScriptedPythonInterface.h  | 106 +-
 .../ScriptedThreadPythonInterface.cpp |  36 ++
 .../ScriptedThreadPythonInterface.h   |   2 +-
 .../Python/SWIGPythonBridge.h |   6 -
 .../Python/PythonTestSuite.cpp|  18 +--
 16 files changed, 161 insertions(+), 153 deletions(-)

diff --git a/lldb/bindings/python/python-wrapper.swig 
b/lldb/bindings/python/python-wrapper.swig
index cb54901e66d03c6..17bc7b1f2198709 100644
--- a/lldb/bindings/python/python-wrapper.swig
+++ b/lldb/bindings/python/python-wrapper.swig
@@ -229,49 +229,6 @@ PythonObject 
lldb_private::python::SWIGBridge::LLDBSwigPythonCreateCommandObject
   return pfunc(SWIGBridge::ToSWIGWrapper(std::move(debugger_sp)), dict);
 }
 
-PythonObject 
lldb_private::python::SWIGBridge::LLDBSwigPythonCreateScriptedObject(
-const char *python_class_name, const char *session_dictionary_name,
-lldb::ExecutionContextRefSP exe_ctx_sp,
-const lldb_private::StructuredDataImpl &args_impl,
-std::string &error_string) {
-  if (python_class_name == NULL || python_class_name[0] == '\0' ||
-  !session_dictionary_name)
-return PythonObject();
-
-  PyErr_Cleaner py_err_cleaner(true);
-
-  auto dict = PythonModule::MainModule().ResolveName(
-  session_dictionary_name);
-  auto pfunc = PythonObject::ResolveNameWithDictionary(
-  python_class_name, dict);
-
-  if (!pfunc.IsAllocated()) {
-error_string.append("could not find script class: ");
-error_string.append(python_class_name);
-return PythonObject();
-  }
-
-  llvm::Expected arg_info = pfunc.GetArgInfo();
-  if (!arg_info) {
-llvm::handleAllErrors(
-arg_info.takeError(),
-[&](PythonException &E) { error_string.append(E.ReadBacktrace()); },
-[&](const llvm::ErrorInfoBase &E) {
-  error_string.append(E.message());
-});
-return PythonObject();
-  }
-
-  PythonObject result = {};
-  if (arg_info.get().max_positional_args == 2) {
-  result = pfunc(SWIGBridge::ToSWIGWrapper(exe_ctx_sp), 
SWIGBridge::ToSWIGWrapper(args_impl));
-  } else {
-error_string.assign("wrong number of arguments in __init__, should be 2 "
-"(not including self)");
-  }
-  return result;
-}
-
 PythonObject 
lldb_private::python::SWIGBridge::LLDBSwigPythonCreateScriptedThreadPlan(
 const char *python_class_name, const char *session_dictionary_name,
 const lldb_private::StructuredDataImpl &args_impl,
diff --git a/lldb/include/lldb/Interpreter/Interfaces/ScriptedInterface.h 
b/lldb/include/lldb/Interpreter/Interfaces/ScriptedInterface.h
index 948f763e95ecea4..2406f0f1f9aee27 100644
--- a/lldb/include/lldb/Interpreter/Interfaces/ScriptedInterface.h
+++ b/lldb/include/lldb/Interpreter/Interfaces/ScriptedInterface.h
@@ -25,11 +25,6 @@ class ScriptedInterface {
   ScriptedInterface() = default;
   virtual ~ScriptedInterface() = default;
 
-  virtual StructuredData::GenericSP
-  CreatePluginObject(llvm::StringRef class_name, ExecutionContext &exe_ctx,
- StructuredData::DictionarySP args_sp,
- StructuredData::Generic *script_obj = nullptr) = 0;
-
   StructuredData::GenericSP GetScriptObjectInstance() {
 return m_object_instance_sp;
   }
diff --git 
a/lldb/include/lldb/Interpreter/Interfaces/ScriptedPlatformInterface.h 
b/lldb/include/lldb/Interpreter/Interfaces/ScriptedPlatformInterface.h
index c687cabfe0c1278..dc3630fc75d9e1a 100644
--- a/lldb/include/lldb/Interpreter/Interfaces/ScriptedPlatformInterface.h
+++ b/lldb/include/lldb/Interpreter/Interfaces/Scri

[Lldb-commits] [lldb] [lldb/Interpreter] Make ScriptedInterface Object creation more generic (PR #68052)

2023-10-24 Thread Med Ismail Bennani via lldb-commits

https://github.com/medismailben updated 
https://github.com/llvm/llvm-project/pull/68052

>From 4942cb5209298b5e4a1819885d1f680381c0bb16 Mon Sep 17 00:00:00 2001
From: Med Ismail Bennani 
Date: Tue, 24 Oct 2023 20:39:34 -0700
Subject: [PATCH] [lldb/Interpreter] Make ScriptedInterface Object creation
 more generic

This patch changes the way plugin objects used with Scripted Interfaces
are created.

Instead of implementing a different SWIG method to create the object for
every scripted interface, this patch makes the creation more generic by
re-using some of the ScriptedPythonInterface templated Dispatch code.

This patch also improves error handling of the object creation by
returning an `llvm::Expected`.

Signed-off-by: Med Ismail Bennani 
---
 lldb/bindings/python/python-wrapper.swig  |  43 ---
 .../Interfaces/ScriptedInterface.h|   5 -
 .../Interfaces/ScriptedPlatformInterface.h|   7 +-
 .../Interfaces/ScriptedProcessInterface.h |   7 +-
 .../Interfaces/ScriptedThreadInterface.h  |   7 +-
 .../Process/scripted/ScriptedProcess.cpp  |   9 +-
 .../Process/scripted/ScriptedThread.cpp   |  12 +-
 .../ScriptedPlatformPythonInterface.cpp   |  26 +
 .../ScriptedPlatformPythonInterface.h |   2 +-
 .../ScriptedProcessPythonInterface.cpp|  26 +
 .../ScriptedProcessPythonInterface.h  |   2 +-
 .../Interfaces/ScriptedPythonInterface.h  | 107 +-
 .../ScriptedThreadPythonInterface.cpp |  36 ++
 .../ScriptedThreadPythonInterface.h   |   2 +-
 .../Python/SWIGPythonBridge.h |   6 -
 .../Python/PythonTestSuite.cpp|  18 +--
 16 files changed, 162 insertions(+), 153 deletions(-)

diff --git a/lldb/bindings/python/python-wrapper.swig 
b/lldb/bindings/python/python-wrapper.swig
index cb54901e66d03c6..17bc7b1f2198709 100644
--- a/lldb/bindings/python/python-wrapper.swig
+++ b/lldb/bindings/python/python-wrapper.swig
@@ -229,49 +229,6 @@ PythonObject 
lldb_private::python::SWIGBridge::LLDBSwigPythonCreateCommandObject
   return pfunc(SWIGBridge::ToSWIGWrapper(std::move(debugger_sp)), dict);
 }
 
-PythonObject 
lldb_private::python::SWIGBridge::LLDBSwigPythonCreateScriptedObject(
-const char *python_class_name, const char *session_dictionary_name,
-lldb::ExecutionContextRefSP exe_ctx_sp,
-const lldb_private::StructuredDataImpl &args_impl,
-std::string &error_string) {
-  if (python_class_name == NULL || python_class_name[0] == '\0' ||
-  !session_dictionary_name)
-return PythonObject();
-
-  PyErr_Cleaner py_err_cleaner(true);
-
-  auto dict = PythonModule::MainModule().ResolveName(
-  session_dictionary_name);
-  auto pfunc = PythonObject::ResolveNameWithDictionary(
-  python_class_name, dict);
-
-  if (!pfunc.IsAllocated()) {
-error_string.append("could not find script class: ");
-error_string.append(python_class_name);
-return PythonObject();
-  }
-
-  llvm::Expected arg_info = pfunc.GetArgInfo();
-  if (!arg_info) {
-llvm::handleAllErrors(
-arg_info.takeError(),
-[&](PythonException &E) { error_string.append(E.ReadBacktrace()); },
-[&](const llvm::ErrorInfoBase &E) {
-  error_string.append(E.message());
-});
-return PythonObject();
-  }
-
-  PythonObject result = {};
-  if (arg_info.get().max_positional_args == 2) {
-  result = pfunc(SWIGBridge::ToSWIGWrapper(exe_ctx_sp), 
SWIGBridge::ToSWIGWrapper(args_impl));
-  } else {
-error_string.assign("wrong number of arguments in __init__, should be 2 "
-"(not including self)");
-  }
-  return result;
-}
-
 PythonObject 
lldb_private::python::SWIGBridge::LLDBSwigPythonCreateScriptedThreadPlan(
 const char *python_class_name, const char *session_dictionary_name,
 const lldb_private::StructuredDataImpl &args_impl,
diff --git a/lldb/include/lldb/Interpreter/Interfaces/ScriptedInterface.h 
b/lldb/include/lldb/Interpreter/Interfaces/ScriptedInterface.h
index 948f763e95ecea4..2406f0f1f9aee27 100644
--- a/lldb/include/lldb/Interpreter/Interfaces/ScriptedInterface.h
+++ b/lldb/include/lldb/Interpreter/Interfaces/ScriptedInterface.h
@@ -25,11 +25,6 @@ class ScriptedInterface {
   ScriptedInterface() = default;
   virtual ~ScriptedInterface() = default;
 
-  virtual StructuredData::GenericSP
-  CreatePluginObject(llvm::StringRef class_name, ExecutionContext &exe_ctx,
- StructuredData::DictionarySP args_sp,
- StructuredData::Generic *script_obj = nullptr) = 0;
-
   StructuredData::GenericSP GetScriptObjectInstance() {
 return m_object_instance_sp;
   }
diff --git 
a/lldb/include/lldb/Interpreter/Interfaces/ScriptedPlatformInterface.h 
b/lldb/include/lldb/Interpreter/Interfaces/ScriptedPlatformInterface.h
index c687cabfe0c1278..dc3630fc75d9e1a 100644
--- a/lldb/include/lldb/Interpreter/Interfaces/ScriptedPlatformInterface.h
+++ b/lldb/include/lldb/Interpreter/Interfaces/Scri

[Lldb-commits] [lldb] [lldb/Interpreter] Make ScriptedInterface Object creation more generic (PR #68052)

2023-10-24 Thread Med Ismail Bennani via lldb-commits

https://github.com/medismailben edited 
https://github.com/llvm/llvm-project/pull/68052
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb/Interpreter] Make ScriptedInterface Object creation more generic (PR #68052)

2023-10-24 Thread Med Ismail Bennani via lldb-commits


@@ -32,6 +32,84 @@ class ScriptedPythonInterface : virtual public 
ScriptedInterface {
   ScriptedPythonInterface(ScriptInterpreterPythonImpl &interpreter);
   ~ScriptedPythonInterface() override = default;
 
+  template 
+  llvm::Expected
+  CreatePluginObject(llvm::StringRef class_name,
+ StructuredData::Generic *script_obj, Args... args) {
+using namespace python;
+using Locker = ScriptInterpreterPythonImpl::Locker;
+
+std::string error_string;
+if (class_name.empty() &&
+llvm::StringRef(m_interpreter.GetDictionaryName()).empty() &&
+!script_obj)
+  return llvm::createStringError(
+  llvm::inconvertibleErrorCode(),
+  "ScriptedPythonInterface::CreatePluginObject - missing script class "
+  "name, dictionary or object.");
+
+Locker py_lock(&m_interpreter, Locker::AcquireLock | Locker::NoSTDIN,
+   Locker::FreeLock);
+
+PythonObject result = {};
+
+if (!script_obj) {
+  auto dict =
+  PythonModule::MainModule().ResolveName(
+  m_interpreter.GetDictionaryName());
+  auto pfunc =
+  PythonObject::ResolveNameWithDictionary(
+  class_name, dict);
+
+  if (!pfunc.IsAllocated()) {
+error_string.append("Could not find script class: ");
+error_string.append(class_name);
+return llvm::createStringError(llvm::inconvertibleErrorCode(),
+   error_string);
+  }
+
+  std::tuple original_args = std::forward_as_tuple(args...);
+  auto transformed_args = TransformArgs(original_args);
+
+  llvm::Expected arg_info = pfunc.GetArgInfo();
+  if (!arg_info) {
+llvm::handleAllErrors(
+arg_info.takeError(),
+[&](PythonException &E) { error_string.append(E.ReadBacktrace()); 
},
+[&](const llvm::ErrorInfoBase &E) {
+  error_string.append(E.message());
+});
+return llvm::createStringError(llvm::inconvertibleErrorCode(),
+   error_string);
+  }
+
+  llvm::Expected expected_return_object =
+  llvm::make_error("Not initialized.",
+  llvm::inconvertibleErrorCode());
+
+  std::apply(
+  [&pfunc, &expected_return_object](auto &&...args) {
+llvm::consumeError(expected_return_object.takeError());
+expected_return_object = pfunc(args...);
+  },
+  transformed_args);
+
+  if (llvm::Error e = expected_return_object.takeError())
+return e;
+  result = std::move(expected_return_object.get());
+} else
+  result = PythonObject(PyRefType::Borrowed,
+static_cast(script_obj->GetValue()));
+
+if (!result.IsValid())
+  return llvm::createStringError(llvm::inconvertibleErrorCode(),
+ "python object result is invalid.");

medismailben wrote:

Thanks for the suggestions however I can't get the $name of the called passed a 
`script_obj`. I'll still rephrase it though.

https://github.com/llvm/llvm-project/pull/68052
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb/Interpreter] Make ScriptedInterface Object creation more generic (PR #68052)

2023-10-24 Thread Alex Langford via lldb-commits

https://github.com/bulbazord commented:

Works for me generally. I'm curious to know if some of these can be pure 
virtual function as Jonas mentioned.

https://github.com/llvm/llvm-project/pull/68052
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb/Interpreter] Make ScriptedInterface Object creation more generic (PR #68052)

2023-10-24 Thread Alex Langford via lldb-commits


@@ -32,6 +32,84 @@ class ScriptedPythonInterface : virtual public 
ScriptedInterface {
   ScriptedPythonInterface(ScriptInterpreterPythonImpl &interpreter);
   ~ScriptedPythonInterface() override = default;
 
+  template 
+  llvm::Expected
+  CreatePluginObject(llvm::StringRef class_name,
+ StructuredData::Generic *script_obj, Args... args) {
+using namespace python;
+using Locker = ScriptInterpreterPythonImpl::Locker;
+
+std::string error_string;
+if (class_name.empty() &&
+llvm::StringRef(m_interpreter.GetDictionaryName()).empty() &&
+!script_obj)
+  return llvm::createStringError(
+  llvm::inconvertibleErrorCode(),
+  "ScriptedPythonInterface::CreatePluginObject - missing script class "
+  "name, dictionary or object.");
+
+Locker py_lock(&m_interpreter, Locker::AcquireLock | Locker::NoSTDIN,
+   Locker::FreeLock);
+
+PythonObject result = {};
+
+if (!script_obj) {
+  auto dict =
+  PythonModule::MainModule().ResolveName(
+  m_interpreter.GetDictionaryName());
+  auto pfunc =
+  PythonObject::ResolveNameWithDictionary(
+  class_name, dict);
+
+  if (!pfunc.IsAllocated()) {
+error_string.append("Could not find script class: ");
+error_string.append(class_name);
+return llvm::createStringError(llvm::inconvertibleErrorCode(),
+   error_string);
+  }
+
+  std::tuple original_args = std::forward_as_tuple(args...);
+  auto transformed_args = TransformArgs(original_args);
+
+  llvm::Expected arg_info = pfunc.GetArgInfo();
+  if (!arg_info) {
+llvm::handleAllErrors(
+arg_info.takeError(),
+[&](PythonException &E) { error_string.append(E.ReadBacktrace()); 
},
+[&](const llvm::ErrorInfoBase &E) {
+  error_string.append(E.message());
+});
+return llvm::createStringError(llvm::inconvertibleErrorCode(),
+   error_string);
+  }
+
+  llvm::Expected expected_return_object =
+  llvm::make_error("Not initialized.",
+  llvm::inconvertibleErrorCode());
+
+  std::apply(
+  [&pfunc, &expected_return_object](auto &&...args) {
+llvm::consumeError(expected_return_object.takeError());
+expected_return_object = pfunc(args...);
+  },
+  transformed_args);
+
+  if (llvm::Error e = expected_return_object.takeError())
+return e;
+  result = std::move(expected_return_object.get());
+} else
+  result = PythonObject(PyRefType::Borrowed,
+static_cast(script_obj->GetValue()));
+
+if (!result.IsValid())
+  return llvm::createStringError(llvm::inconvertibleErrorCode(),
+ "python object result is invalid.");

bulbazord wrote:

You could augment this error to say something like "The resulting object from 
the call to $NAME is not a valid Python Object" or something to this effect

https://github.com/llvm/llvm-project/pull/68052
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb/Interpreter] Make ScriptedInterface Object creation more generic (PR #68052)

2023-10-24 Thread Alex Langford via lldb-commits


@@ -32,6 +32,84 @@ class ScriptedPythonInterface : virtual public 
ScriptedInterface {
   ScriptedPythonInterface(ScriptInterpreterPythonImpl &interpreter);
   ~ScriptedPythonInterface() override = default;
 
+  template 
+  llvm::Expected
+  CreatePluginObject(llvm::StringRef class_name,
+ StructuredData::Generic *script_obj, Args... args) {
+using namespace python;
+using Locker = ScriptInterpreterPythonImpl::Locker;
+
+std::string error_string;
+if (class_name.empty() &&
+llvm::StringRef(m_interpreter.GetDictionaryName()).empty() &&
+!script_obj)
+  return llvm::createStringError(
+  llvm::inconvertibleErrorCode(),
+  "ScriptedPythonInterface::CreatePluginObject - missing script class "
+  "name, dictionary or object.");
+
+Locker py_lock(&m_interpreter, Locker::AcquireLock | Locker::NoSTDIN,
+   Locker::FreeLock);
+
+PythonObject result = {};
+
+if (!script_obj) {
+  auto dict =
+  PythonModule::MainModule().ResolveName(
+  m_interpreter.GetDictionaryName());

bulbazord wrote:

Do we need to check the validity of the dictionary?

https://github.com/llvm/llvm-project/pull/68052
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb/Interpreter] Make ScriptedInterface Object creation more generic (PR #68052)

2023-10-24 Thread Alex Langford via lldb-commits


@@ -32,6 +32,84 @@ class ScriptedPythonInterface : virtual public 
ScriptedInterface {
   ScriptedPythonInterface(ScriptInterpreterPythonImpl &interpreter);
   ~ScriptedPythonInterface() override = default;
 
+  template 
+  llvm::Expected
+  CreatePluginObject(llvm::StringRef class_name,
+ StructuredData::Generic *script_obj, Args... args) {
+using namespace python;
+using Locker = ScriptInterpreterPythonImpl::Locker;
+
+std::string error_string;
+if (class_name.empty() &&
+llvm::StringRef(m_interpreter.GetDictionaryName()).empty() &&
+!script_obj)
+  return llvm::createStringError(
+  llvm::inconvertibleErrorCode(),
+  "ScriptedPythonInterface::CreatePluginObject - missing script class "
+  "name, dictionary or object.");
+
+Locker py_lock(&m_interpreter, Locker::AcquireLock | Locker::NoSTDIN,
+   Locker::FreeLock);
+
+PythonObject result = {};
+
+if (!script_obj) {
+  auto dict =
+  PythonModule::MainModule().ResolveName(
+  m_interpreter.GetDictionaryName());
+  auto pfunc =
+  PythonObject::ResolveNameWithDictionary(
+  class_name, dict);
+
+  if (!pfunc.IsAllocated()) {
+error_string.append("Could not find script class: ");
+error_string.append(class_name);
+return llvm::createStringError(llvm::inconvertibleErrorCode(),
+   error_string);
+  }
+
+  std::tuple original_args = std::forward_as_tuple(args...);
+  auto transformed_args = TransformArgs(original_args);
+
+  llvm::Expected arg_info = pfunc.GetArgInfo();
+  if (!arg_info) {
+llvm::handleAllErrors(
+arg_info.takeError(),
+[&](PythonException &E) { error_string.append(E.ReadBacktrace()); 
},
+[&](const llvm::ErrorInfoBase &E) {
+  error_string.append(E.message());
+});
+return llvm::createStringError(llvm::inconvertibleErrorCode(),
+   error_string);
+  }
+
+  llvm::Expected expected_return_object =
+  llvm::make_error("Not initialized.",
+  llvm::inconvertibleErrorCode());
+
+  std::apply(
+  [&pfunc, &expected_return_object](auto &&...args) {
+llvm::consumeError(expected_return_object.takeError());
+expected_return_object = pfunc(args...);
+  },
+  transformed_args);
+
+  if (llvm::Error e = expected_return_object.takeError())
+return e;
+  result = std::move(expected_return_object.get());
+} else
+  result = PythonObject(PyRefType::Borrowed,
+static_cast(script_obj->GetValue()));

bulbazord wrote:

+1

https://github.com/llvm/llvm-project/pull/68052
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb/Interpreter] Make ScriptedInterface Object creation more generic (PR #68052)

2023-10-24 Thread Alex Langford via lldb-commits

https://github.com/bulbazord edited 
https://github.com/llvm/llvm-project/pull/68052
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb/Interpreter] Make ScriptedInterface Object creation more generic (PR #68052)

2023-10-24 Thread Jonas Devlieghere via lldb-commits


@@ -32,6 +32,84 @@ class ScriptedPythonInterface : virtual public 
ScriptedInterface {
   ScriptedPythonInterface(ScriptInterpreterPythonImpl &interpreter);
   ~ScriptedPythonInterface() override = default;
 
+  template 
+  llvm::Expected
+  CreatePluginObject(llvm::StringRef class_name,
+ StructuredData::Generic *script_obj, Args... args) {
+using namespace python;
+using Locker = ScriptInterpreterPythonImpl::Locker;
+
+std::string error_string;
+if (class_name.empty() &&
+llvm::StringRef(m_interpreter.GetDictionaryName()).empty() &&
+!script_obj)
+  return llvm::createStringError(
+  llvm::inconvertibleErrorCode(),
+  "ScriptedPythonInterface::CreatePluginObject - missing script class "
+  "name, dictionary or object.");

JDevlieghere wrote:

Can we make these 3 separate errors? We know which ones are missing. 
Alternatively, you could make them bools and conditionally include the "name", 
"dictionary" and "object" substrings based on that. 

https://github.com/llvm/llvm-project/pull/68052
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb/Interpreter] Make ScriptedInterface Object creation more generic (PR #68052)

2023-10-24 Thread Jonas Devlieghere via lldb-commits


@@ -56,14 +56,17 @@ ScriptedThread::Create(ScriptedProcess &process,
   }
 
   ExecutionContext exe_ctx(process);
-  StructuredData::GenericSP owned_script_object_sp =
-  scripted_thread_interface->CreatePluginObject(
-  thread_class_name, exe_ctx, process.m_scripted_metadata.GetArgsSP(),
-  script_object);
+  auto obj_or_err = scripted_thread_interface->CreatePluginObject(
+  thread_class_name, exe_ctx, process.m_scripted_metadata.GetArgsSP(),
+  script_object);
 
-  if (!owned_script_object_sp)
+  if (!obj_or_err) {
 return llvm::createStringError(llvm::inconvertibleErrorCode(),
"Failed to create script object.");
+  }

JDevlieghere wrote:

No braces around single-line if.

https://github.com/llvm/llvm-project/pull/68052
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb/Interpreter] Make ScriptedInterface Object creation more generic (PR #68052)

2023-10-24 Thread Jonas Devlieghere via lldb-commits


@@ -32,6 +32,84 @@ class ScriptedPythonInterface : virtual public 
ScriptedInterface {
   ScriptedPythonInterface(ScriptInterpreterPythonImpl &interpreter);
   ~ScriptedPythonInterface() override = default;
 
+  template 
+  llvm::Expected
+  CreatePluginObject(llvm::StringRef class_name,
+ StructuredData::Generic *script_obj, Args... args) {
+using namespace python;
+using Locker = ScriptInterpreterPythonImpl::Locker;
+
+std::string error_string;
+if (class_name.empty() &&
+llvm::StringRef(m_interpreter.GetDictionaryName()).empty() &&
+!script_obj)
+  return llvm::createStringError(
+  llvm::inconvertibleErrorCode(),
+  "ScriptedPythonInterface::CreatePluginObject - missing script class "
+  "name, dictionary or object.");
+
+Locker py_lock(&m_interpreter, Locker::AcquireLock | Locker::NoSTDIN,
+   Locker::FreeLock);
+
+PythonObject result = {};
+
+if (!script_obj) {
+  auto dict =
+  PythonModule::MainModule().ResolveName(
+  m_interpreter.GetDictionaryName());
+  auto pfunc =
+  PythonObject::ResolveNameWithDictionary(
+  class_name, dict);
+
+  if (!pfunc.IsAllocated()) {
+error_string.append("Could not find script class: ");
+error_string.append(class_name);
+return llvm::createStringError(llvm::inconvertibleErrorCode(),
+   error_string);

JDevlieghere wrote:

Could you just pass those as twines to `createStringError`? 

https://github.com/llvm/llvm-project/pull/68052
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb/Interpreter] Make ScriptedInterface Object creation more generic (PR #68052)

2023-10-24 Thread Jonas Devlieghere via lldb-commits


@@ -19,11 +19,11 @@
 namespace lldb_private {
 class ScriptedPlatformInterface : virtual public ScriptedInterface {
 public:
-  StructuredData::GenericSP
+  virtual llvm::Expected
   CreatePluginObject(llvm::StringRef class_name, ExecutionContext &exe_ctx,
  StructuredData::DictionarySP args_sp,
- StructuredData::Generic *script_obj = nullptr) override {
-return {};
+ StructuredData::Generic *script_obj = nullptr) {
+llvm_unreachable("unimplemented!");

JDevlieghere wrote:

Could this be a pure virtual method? If not maybe something like "Cannot create 
an instance of an interface" would be a more informative error message.

https://github.com/llvm/llvm-project/pull/68052
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb/Interpreter] Make ScriptedInterface Object creation more generic (PR #68052)

2023-10-24 Thread Jonas Devlieghere via lldb-commits


@@ -32,6 +32,84 @@ class ScriptedPythonInterface : virtual public 
ScriptedInterface {
   ScriptedPythonInterface(ScriptInterpreterPythonImpl &interpreter);
   ~ScriptedPythonInterface() override = default;
 
+  template 
+  llvm::Expected
+  CreatePluginObject(llvm::StringRef class_name,
+ StructuredData::Generic *script_obj, Args... args) {
+using namespace python;
+using Locker = ScriptInterpreterPythonImpl::Locker;
+
+std::string error_string;
+if (class_name.empty() &&
+llvm::StringRef(m_interpreter.GetDictionaryName()).empty() &&
+!script_obj)
+  return llvm::createStringError(
+  llvm::inconvertibleErrorCode(),
+  "ScriptedPythonInterface::CreatePluginObject - missing script class "
+  "name, dictionary or object.");
+
+Locker py_lock(&m_interpreter, Locker::AcquireLock | Locker::NoSTDIN,
+   Locker::FreeLock);
+
+PythonObject result = {};
+
+if (!script_obj) {
+  auto dict =
+  PythonModule::MainModule().ResolveName(
+  m_interpreter.GetDictionaryName());
+  auto pfunc =
+  PythonObject::ResolveNameWithDictionary(
+  class_name, dict);
+
+  if (!pfunc.IsAllocated()) {
+error_string.append("Could not find script class: ");
+error_string.append(class_name);
+return llvm::createStringError(llvm::inconvertibleErrorCode(),
+   error_string);
+  }
+
+  std::tuple original_args = std::forward_as_tuple(args...);
+  auto transformed_args = TransformArgs(original_args);
+
+  llvm::Expected arg_info = pfunc.GetArgInfo();
+  if (!arg_info) {
+llvm::handleAllErrors(
+arg_info.takeError(),
+[&](PythonException &E) { error_string.append(E.ReadBacktrace()); 
},
+[&](const llvm::ErrorInfoBase &E) {
+  error_string.append(E.message());
+});
+return llvm::createStringError(llvm::inconvertibleErrorCode(),
+   error_string);
+  }
+
+  llvm::Expected expected_return_object =
+  llvm::make_error("Not initialized.",
+  llvm::inconvertibleErrorCode());
+
+  std::apply(
+  [&pfunc, &expected_return_object](auto &&...args) {
+llvm::consumeError(expected_return_object.takeError());
+expected_return_object = pfunc(args...);
+  },
+  transformed_args);
+
+  if (llvm::Error e = expected_return_object.takeError())
+return e;
+  result = std::move(expected_return_object.get());
+} else
+  result = PythonObject(PyRefType::Borrowed,
+static_cast(script_obj->GetValue()));

JDevlieghere wrote:

This is mostly personal preference, but since this is simpler than the other 
case, I'd put this one first. That way I can page out the "what if script_obj 
is not NULL" as I'm reading the bigger block above. 

https://github.com/llvm/llvm-project/pull/68052
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] [lldb/Interpreter] Make ScriptedInterface Object creation more generic (PR #68052)

2023-10-23 Thread Med Ismail Bennani via lldb-commits

https://github.com/medismailben edited 
https://github.com/llvm/llvm-project/pull/68052
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits