[libpeas] Python: add a PeasActivatable test to check reference counts.



commit 21be89bf572efa80769ebb43e353b38f730b71a3
Author: Steve Frécinaux <code istique net>
Date:   Mon Mar 7 11:48:44 2011 +0100

    Python: add a PeasActivatable test to check reference counts.
    
    The python bindings used to leak references on the object instance
    we put in the property value. So let's check it doesn't happen again
    since it was preventing eog to shut down.

 tests/libpeas/extension-python.c                   |   46 +++++++++++++++++++-
 .../plugins/extension-python/extension-python.py   |   16 ++++++-
 2 files changed, 60 insertions(+), 2 deletions(-)
---
diff --git a/tests/libpeas/extension-python.c b/tests/libpeas/extension-python.c
index 27e91a2..f2ddd92 100644
--- a/tests/libpeas/extension-python.c
+++ b/tests/libpeas/extension-python.c
@@ -2,7 +2,7 @@
  * extension-python.c
  * This file is part of libpeas
  *
- * Copyright (C) 2011 - Garrett Regier
+ * Copyright (C) 2011 - Steve Frécinaux, Garrett Regier
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU Library General Public License as published by
@@ -25,6 +25,8 @@
 
 #include "testing/testing-extension.h"
 #include "introspection/introspection-callable.h"
+#include <libpeas/peas-activatable.h>
+
 #include <pygobject.h>
 
 static void
@@ -57,6 +59,47 @@ extension_python_instance_refcount (PeasEngine *engine)
   g_object_unref (extension);
 }
 
+static void
+extension_python_activatable_subject_refcount (PeasEngine *engine)
+{
+  PeasPluginInfo *info;
+  PeasExtension *extension;
+  GObject *object;
+  PyObject *wrapper;
+
+  info = peas_engine_get_plugin_info (engine, "extension-python");
+  g_assert (peas_engine_load_plugin (engine, info));
+
+  /* Create the 'object' property value, to be similar to a GtkWindow
+   * instance: a sunk GInitiallyUnowned object. */
+  object = g_object_new (G_TYPE_INITIALLY_UNOWNED, NULL);
+  g_object_ref_sink (object);
+  g_assert_cmpint (object->ref_count, ==, 1);
+
+  /* we pre-create the wrapper to make it easier to check reference count */
+  extension = peas_engine_create_extension (engine, info,
+                                            PEAS_TYPE_ACTIVATABLE,
+                                            "object", object,
+                                            NULL);
+  g_assert (PEAS_IS_EXTENSION (extension));
+
+  /* The python wrapper created around our dummy object should have increased
+   * its refcount by 1.
+   */
+  g_assert_cmpint (object->ref_count, ==, 2);
+
+  /* Ensure the python wrapper is only reffed once, by the extension */
+  wrapper = g_object_get_data (object, "PyGObject::wrapper");
+  g_assert_cmpint (wrapper->ob_refcnt, ==, 1);
+
+  g_assert_cmpint (((GObject *)extension)->ref_count, ==, 1);
+  g_object_unref (extension);
+
+  /* We unreffed the extension, so it should have been destroyed and our dummy
+   * object refcount should be back to 1. */
+  g_assert_cmpint (object->ref_count, ==, 1);
+}
+
 int
 main (int   argc,
       char *argv[])
@@ -67,6 +110,7 @@ main (int   argc,
   EXTENSION_TESTS ("python");
 
   EXTENSION_TEST_ADD ("python", "instance-refcount", extension_python_instance_refcount);
+  EXTENSION_TEST_ADD ("python", "activatable-subject-refcount", extension_python_activatable_subject_refcount);
 
   return testing_run_tests ();
 }
diff --git a/tests/libpeas/plugins/extension-python/extension-python.py b/tests/libpeas/plugins/extension-python/extension-python.py
index 7a49647..e4e73e9 100644
--- a/tests/libpeas/plugins/extension-python/extension-python.py
+++ b/tests/libpeas/plugins/extension-python/extension-python.py
@@ -2,7 +2,7 @@
 # ex:set ts=4 et sw=4 ai:
 
 import gobject
-from gi.repository import Introspection
+from gi.repository import Introspection, Peas
 
 class CallablePythonPlugin(gobject.GObject, Introspection.Callable):
     __gtype_name__ = "CallablePythonPlugin"
@@ -29,3 +29,17 @@ class PropertiesPythonPlugin(gobject.GObject, Introspection.Properties):
     write_only = gobject.property(type=str)
 
     readwrite = gobject.property(type=str, default="readwrite")
+
+class ActivatablePythonExtension(gobject.GObject, Peas.Activatable):
+    __gtype_name__ = "ActivatablePythonExtension"
+
+    object = gobject.property(type=gobject.GObject)
+
+    def do_activate(self):
+        pass
+
+    def do_deactivate(self):
+        pass
+
+    def update_state(self):
+        pass



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]