[Lldb-commits] [lldb] [lldb] use the Py_REFCNT() macro instead of directly accessing member (PR #188161)

via lldb-commits lldb-commits at lists.llvm.org
Mon Mar 23 19:30:48 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lldb

Author: Charlie Li (vishwin)

<details>
<summary>Changes</summary>

[PyObject members are not to be accessed directly](https://docs.python.org/3/c-api/structures.html#c.PyObject), but rather through macros, in this case `Py_REFCNT()`.

In most, ie Global Interpreter Lock-enabled, CPython cases, `Py_REFCNT()` expands to accessing `ob_refcnt` anyway. However, in a free-threaded CPython, combined with disabling the limited API (since it requires the GIL for now), the direct member does not exist, causing the build to fail. The macro expands to the correct access method in the free-threaded configuration.

---
Full diff: https://github.com/llvm/llvm-project/pull/188161.diff


1 Files Affected:

- (modified) lldb/bindings/python/python-wrapper.swig (+1-1) 


``````````diff
diff --git a/lldb/bindings/python/python-wrapper.swig b/lldb/bindings/python/python-wrapper.swig
index bf59569920470..72f90f1b23c29 100644
--- a/lldb/bindings/python/python-wrapper.swig
+++ b/lldb/bindings/python/python-wrapper.swig
@@ -134,7 +134,7 @@ bool lldb_private::python::SWIGBridge::LLDBSwigPythonCallTypeScript(
 #endif
       ) {
     pfunc_impl = (PyObject *)(*pyfunct_wrapper);
-    if (pfunc_impl->ob_refcnt == 1) {
+    if (Py_REFCNT(pfunc_impl) == 1) {
       Py_XDECREF(pfunc_impl);
       pfunc_impl = NULL;
     }

``````````

</details>


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


More information about the lldb-commits mailing list