[LLDB][Python] remove ArgInfo::count
Summary: This patch updates the last user of ArgInfo::count and deletes it. I also delete `GetNumInitArguments()` and `GetInitArgInfo()`. Classess are callables and `GetArgInfo()` should work on them. On python 3 it already works, of course. `inspect` is good. On python 2 we have to add yet another special case. But hey if python 2 wasn't crufty we wouln't need python 3. I also delete `is_bound_method` becuase it is unused. This path is tested in `TestStepScripted.py` Reviewers: labath, mgorny, JDevlieghere Reviewed By: labath, JDevlieghere Subscribers: lldb-commits Tags: #lldb Differential Revision: https://reviews.llvm.org/D69742
This commit is contained in:
@@ -802,29 +802,11 @@ bool PythonCallable::Check(PyObject *py_obj) {
|
||||
return PyCallable_Check(py_obj);
|
||||
}
|
||||
|
||||
PythonCallable::ArgInfo PythonCallable::GetNumInitArguments() const {
|
||||
auto arginfo = GetInitArgInfo();
|
||||
if (!arginfo) {
|
||||
llvm::consumeError(arginfo.takeError());
|
||||
return ArgInfo{};
|
||||
}
|
||||
return arginfo.get();
|
||||
}
|
||||
|
||||
Expected<PythonCallable::ArgInfo> PythonCallable::GetInitArgInfo() const {
|
||||
if (!IsValid())
|
||||
return nullDeref();
|
||||
auto init = As<PythonCallable>(GetAttribute("__init__"));
|
||||
if (!init)
|
||||
return init.takeError();
|
||||
return init.get().GetArgInfo();
|
||||
}
|
||||
|
||||
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 3
|
||||
static const char get_arg_info_script[] = R"(
|
||||
from inspect import signature, Parameter, ismethod
|
||||
from collections import namedtuple
|
||||
ArgInfo = namedtuple('ArgInfo', ['count', 'has_varargs', 'is_bound_method'])
|
||||
ArgInfo = namedtuple('ArgInfo', ['count', 'has_varargs'])
|
||||
def main(f):
|
||||
count = 0
|
||||
varargs = False
|
||||
@@ -840,7 +822,7 @@ def main(f):
|
||||
pass
|
||||
else:
|
||||
raise Exception(f'unknown parameter kind: {kind}')
|
||||
return ArgInfo(count, varargs, ismethod(f))
|
||||
return ArgInfo(count, varargs)
|
||||
)";
|
||||
#endif
|
||||
|
||||
@@ -856,21 +838,27 @@ Expected<PythonCallable::ArgInfo> PythonCallable::GetArgInfo() const {
|
||||
Expected<PythonObject> pyarginfo = get_arg_info(*this);
|
||||
if (!pyarginfo)
|
||||
return pyarginfo.takeError();
|
||||
result.count = cantFail(As<long long>(pyarginfo.get().GetAttribute("count")));
|
||||
result.has_varargs =
|
||||
long long count =
|
||||
cantFail(As<long long>(pyarginfo.get().GetAttribute("count")));
|
||||
bool has_varargs =
|
||||
cantFail(As<bool>(pyarginfo.get().GetAttribute("has_varargs")));
|
||||
bool is_method =
|
||||
cantFail(As<bool>(pyarginfo.get().GetAttribute("is_bound_method")));
|
||||
result.max_positional_args =
|
||||
result.has_varargs ? ArgInfo::UNBOUNDED : result.count;
|
||||
|
||||
// FIXME emulate old broken behavior
|
||||
if (is_method)
|
||||
result.count++;
|
||||
result.max_positional_args = has_varargs ? ArgInfo::UNBOUNDED : count;
|
||||
|
||||
#else
|
||||
PyObject *py_func_obj;
|
||||
bool is_bound_method = false;
|
||||
PyObject *py_func_obj = m_py_obj;
|
||||
bool is_class = false;
|
||||
|
||||
if (PyType_Check(m_py_obj) || PyClass_Check(m_py_obj)) {
|
||||
auto init = GetAttribute("__init__");
|
||||
if (!init)
|
||||
return init.takeError();
|
||||
py_func_obj = init.get().get();
|
||||
is_class = true;
|
||||
} else {
|
||||
py_func_obj = m_py_obj;
|
||||
}
|
||||
|
||||
if (PyMethod_Check(py_func_obj)) {
|
||||
py_func_obj = PyMethod_GET_FUNCTION(py_func_obj);
|
||||
PythonObject im_self = GetAttributeValue("im_self");
|
||||
@@ -899,11 +887,11 @@ Expected<PythonCallable::ArgInfo> PythonCallable::GetArgInfo() const {
|
||||
if (!code)
|
||||
return result;
|
||||
|
||||
result.count = code->co_argcount;
|
||||
result.has_varargs = !!(code->co_flags & CO_VARARGS);
|
||||
result.max_positional_args = result.has_varargs
|
||||
? ArgInfo::UNBOUNDED
|
||||
: (result.count - (int)is_bound_method);
|
||||
auto count = code->co_argcount;
|
||||
bool has_varargs = !!(code->co_flags & CO_VARARGS);
|
||||
result.max_positional_args =
|
||||
has_varargs ? ArgInfo::UNBOUNDED
|
||||
: (count - (int)is_bound_method) - (int)is_class;
|
||||
|
||||
#endif
|
||||
|
||||
@@ -913,15 +901,6 @@ Expected<PythonCallable::ArgInfo> PythonCallable::GetArgInfo() const {
|
||||
constexpr unsigned
|
||||
PythonCallable::ArgInfo::UNBOUNDED; // FIXME delete after c++17
|
||||
|
||||
PythonCallable::ArgInfo PythonCallable::GetNumArguments() const {
|
||||
auto arginfo = GetArgInfo();
|
||||
if (!arginfo) {
|
||||
llvm::consumeError(arginfo.takeError());
|
||||
return ArgInfo{};
|
||||
}
|
||||
return arginfo.get();
|
||||
}
|
||||
|
||||
PythonObject PythonCallable::operator()() {
|
||||
return PythonObject(PyRefType::Owned, PyObject_CallObject(m_py_obj, nullptr));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user