diff options
| author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2023-11-15 10:57:50 +0100 |
|---|---|---|
| committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2023-11-16 16:18:40 +0100 |
| commit | 9f7b808f64be9c01a17153839c368626edac32ed (patch) | |
| tree | 7226dfca205e8d3c9f10ef144e1ae4e3b63dc4ee /sources/shiboken6/libshiboken/helper.cpp | |
| parent | ef38449d27508e25ce45810f77e6b619089420ae (diff) | |
libshiboken: Extend the PyObject debug operator to format code objects
This is helpful for debugging signal connections.
Task-number: PYSIDE-2524
Pick-to: 6.6
Change-Id: I4b051de4a5b1583f1cba56864b24be403b4bc96d
Reviewed-by: Shyamnath Premnadh <Shyamnath.Premnadh@qt.io>
Diffstat (limited to 'sources/shiboken6/libshiboken/helper.cpp')
| -rw-r--r-- | sources/shiboken6/libshiboken/helper.cpp | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/sources/shiboken6/libshiboken/helper.cpp b/sources/shiboken6/libshiboken/helper.cpp index abe4a3166..d2203fb43 100644 --- a/sources/shiboken6/libshiboken/helper.cpp +++ b/sources/shiboken6/libshiboken/helper.cpp @@ -9,6 +9,7 @@ #include "pep384impl.h" #include <algorithm> +#include <optional> #include <iomanip> #include <iostream> @@ -22,6 +23,25 @@ # include <pthread.h> #endif +static std::optional<std::string> getStringAttr(PyObject *obj, const char *what) +{ + if (PyObject_HasAttrString(obj, what) != 0) { // Check first to suppress error. + Shiboken::AutoDecRef result(PyObject_GetAttrString(obj, what)); + if (PyUnicode_Check(result.object()) != 0) + return _PepUnicode_AsString(result.object()); + } + return std::nullopt; +} + +static std::optional<int> getIntAttr(PyObject *obj, const char *what) +{ + if (PyObject_HasAttrString(obj, what) != 0) { // Check first to suppress error. + Shiboken::AutoDecRef result(PyObject_GetAttrString(obj, what)); + if (PyLong_Check(result.object()) != 0) + return PyLong_AsLong(result.object()); + } + return std::nullopt; +} static void formatTypeTuple(PyObject *t, const char *what, std::ostream &str); @@ -268,6 +288,27 @@ static void formatPyMethod(PyObject *obj, std::ostream &str) str << ", instance=" << PyMethod_Self(obj); } +static void formatPyCodeObject(PyObject *obj, std::ostream &str) +{ + if (auto name = getStringAttr(obj, "co_name")) + str << '"' << name.value() << '"'; + if (auto qualName = getStringAttr(obj, "co_qualname")) + str << ", co_qualname=\"" << qualName.value() << '"'; + if (auto flags = getIntAttr(obj, "co_flags")) + str << ", flags=0x" << std::hex << flags.value() << std::dec; + if (auto c = getIntAttr(obj, "co_argcount")) + str << ", co_argcounts=" << c.value(); + if (auto c = getIntAttr(obj, "co_posonlyargcount")) + str << ", co_posonlyargcount=" << c.value(); + if (auto c = getIntAttr(obj, "co_kwonlyargcount")) + str << ", co_kwonlyargcount=" << c.value(); + if (auto fileName = getStringAttr(obj, "co_filename")) { + str << " @" << fileName.value(); + if (auto l = getIntAttr(obj, "co_firstlineno")) + str << ':'<< l.value(); + } +} + static void formatPyObjectHelper(PyObject *obj, std::ostream &str) { str << ", "; @@ -301,6 +342,8 @@ static void formatPyObjectHelper(PyObject *obj, std::ostream &str) formatPyFunction(obj, str); else if (PyMethod_Check(obj) != 0) formatPyMethod(obj, str); + else if (PepCode_Check(obj) != 0) + formatPyCodeObject(obj, str); else if (PySequence_Check(obj)) formatPySequence(obj, str); else if (PyDict_Check(obj)) |
