[cfe-commits] [PATCH 1/4] [clang.py] Store reference to TranslationUnit in Cursor and Type

Manuel Klimek klimek at google.com
Sun May 13 01:00:21 PDT 2012


+    @property
+    def translation_unit(self):
+        """Returns the TranslationUnit to which this Cursor belongs."""
+        return getattr(self, '_tu', None)

What's the reason for the default value? Do we expect that people create
cursers via the lowlevel calls? Why would just return tu_ not work?

+        # Store a reference to the TU in the Python object so it won't get
GC'd
+        # before the Cursor.
+        tu = None
+        for arg in args:
+            if isinstance(arg, TranslationUnit):
+                tu = arg
+                break
+
+            if hasattr(arg, 'translation_unit'):
+                tu = arg.translation_unit
+                break
+
+        assert tu is not None
+
+        res._tu = tu

That seems - odd. I can't find docs what from_result is supposed to do, or
what "args" are provided. But having to search through them for a TU seems
wrong - shouldn't they all have a TU?

Also, regarding the test - would it be possible to create a test that drops
the reference, triggers the gc and then makes sure the cursor is still
valid (as that is what we really care about)?

Cheers,
/Manuel

On Sun, May 13, 2012 at 7:20 AM, Gregory Szorc <gregory.szorc at gmail.com>wrote:

> This patch stores a reference to the underlying TranslationUnit in
> Cursor and Type instances. We want to keep this reference so garbage
> collection doesn't collect the TranslationUnit instance, which would
> free the backing memory and possibly cause a segfault if the Cursor or
> Type instance makes a C API call against a dead TU.
>
> Other types in the module (like File) also need this treatment. Those
> will be addressed in a later patch.
>
> ---
>  bindings/python/clang/cindex.py             |   46
> ++++++++++++++++++++++++++-
>  bindings/python/tests/cindex/test_cursor.py |    3 ++
>  bindings/python/tests/cindex/test_type.py   |    1 +
>  3 files changed, 49 insertions(+), 1 deletion(-)
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20120513/deaca922/attachment.html>


More information about the cfe-commits mailing list