aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2023-11-15 10:57:50 +0100
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2023-11-16 19:10:02 +0000
commit213684902b5ef2f45827c8935dc8af68d6a5ab42 (patch)
tree37187aecfe81172d3c152f12ed39b653197246c6
parenta54a8ca33b5e70757eadb88826fb5b4237288571 (diff)
libshiboken: Extend the PyObject debug operator to format code objects
This is helpful for debugging signal connections. Task-number: PYSIDE-2524 Change-Id: I4b051de4a5b1583f1cba56864b24be403b4bc96d Reviewed-by: Shyamnath Premnadh <Shyamnath.Premnadh@qt.io> (cherry picked from commit 9f7b808f64be9c01a17153839c368626edac32ed) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--sources/shiboken6/libshiboken/helper.cpp43
-rw-r--r--sources/shiboken6/libshiboken/pep384impl.cpp6
-rw-r--r--sources/shiboken6/libshiboken/pep384impl.h3
3 files changed, 51 insertions, 1 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))
diff --git a/sources/shiboken6/libshiboken/pep384impl.cpp b/sources/shiboken6/libshiboken/pep384impl.cpp
index 35726924b..0d2981508 100644
--- a/sources/shiboken6/libshiboken/pep384impl.cpp
+++ b/sources/shiboken6/libshiboken/pep384impl.cpp
@@ -504,6 +504,12 @@ PepCode_Get(PepCodeObject *co, const char *name)
}
return ret;
}
+
+int PepCode_Check(PyObject *o)
+{
+ return o != nullptr && std::strcmp(Py_TYPE(o)->tp_name, "code") == 0 ? 1 : 0;
+}
+
#endif // Py_LIMITED_API
/*****************************************************************************
diff --git a/sources/shiboken6/libshiboken/pep384impl.h b/sources/shiboken6/libshiboken/pep384impl.h
index f993d1747..5a3157446 100644
--- a/sources/shiboken6/libshiboken/pep384impl.h
+++ b/sources/shiboken6/libshiboken/pep384impl.h
@@ -391,6 +391,7 @@ LIBSHIBOKEN_API PyObject *PyMethod_Self(PyObject *);
typedef struct _code PepCodeObject;
LIBSHIBOKEN_API int PepCode_Get(PepCodeObject *co, const char *name);
+LIBSHIBOKEN_API int PepCode_Check(PyObject *o);
# define PepCode_GET_FLAGS(o) PepCode_Get(o, "co_flags")
# define PepCode_GET_ARGCOUNT(o) PepCode_Get(o, "co_argcount")
@@ -408,7 +409,7 @@ LIBSHIBOKEN_API int PepCode_Get(PepCodeObject *co, const char *name);
# define PepCodeObject PyCodeObject
# define PepCode_GET_FLAGS(o) ((o)->co_flags)
# define PepCode_GET_ARGCOUNT(o) ((o)->co_argcount)
-
+# define PepCode_Check PyCode_Check
#endif
/*****************************************************************************