Index: codegen/codegen.py
===================================================================
RCS file: /cvs/gnome/gnome-python/pygtk/codegen/codegen.py,v
retrieving revision 1.94
diff -u -p -d -r1.94 codegen.py
--- codegen/codegen.py	20 Jun 2005 22:57:41 -0000	1.94
+++ codegen/codegen.py	5 Jul 2005 22:38:49 -0000
@@ -788,7 +788,10 @@ class GObjectWrapper(Wrapper):
             print >> out, "    if (!pyg_parse_constructor_args(obj_type, arg_names, prop_names,"
             print >> out, "                                    params, &nparams, parsed_args))"
             print >> out, "        return -1;"
-            print >> out, "    self->obj = g_object_newv(obj_type, nparams, params);"
+            print >> out, "    pygobject_push_wrapper((PyObject *) self);"
+            print >> out, "    if (!self->obj)"
+            print >> out, "        self->obj = g_object_newv(obj_type, nparams, params);"
+            print >> out, "    pygobject_pop_wrapper();"
             print >> out, "    for (i = 0; i < nparams; ++i)"
             print >> out, "        g_value_unset(&params[i].value);"
         else:
@@ -802,7 +805,10 @@ class GObjectWrapper(Wrapper):
             print >> out, '    if (!PyArg_ParseTupleAndKeywords(args, kwargs, ":%s.__init__", kwlist))' % classname
             print >> out, "        return -1;"
             print >> out
-            print >> out, "    self->obj = g_object_newv(obj_type, 0, NULL);"
+            print >> out, "    pygobject_push_wrapper((PyObject *) self);"
+            print >> out, "    if (!self->obj)"
+            print >> out, "        self->obj = g_object_newv(obj_type, 0, NULL);"
+            print >> out, "    pygobject_pop_wrapper();"
 
         print >> out, \
               '    if (!self->obj) {\n' \
Index: gobject/gobjectmodule.c
===================================================================
RCS file: /cvs/gnome/gnome-python/pygtk/gobject/gobjectmodule.c,v
retrieving revision 1.177
diff -u -p -d -r1.177 gobjectmodule.c
--- gobject/gobjectmodule.c	5 Jul 2005 17:16:40 -0000	1.177
+++ gobject/gobjectmodule.c	5 Jul 2005 22:38:52 -0000
@@ -41,6 +41,8 @@ GQuark pygenum_class_key;
 GQuark pygflags_class_key;
 GQuark pygpointer_class_key;
 
+
+
 static void pyg_flags_add_constants(PyObject *module, GType flags_type,
 				    const gchar *strip_prefix);
 
@@ -998,6 +1000,74 @@ get_type_name_for_class(PyTypeObject *cl
     return type_name;
 }
 
+
+static GStaticPrivate pygobject_contruction_wrapper = G_STATIC_PRIVATE_INIT;
+
+void
+pygobject_push_wrapper(PyObject *wrapper)
+{
+    GSList *wrapper_list;
+
+    wrapper_list = (GSList *) g_static_private_get(&pygobject_contruction_wrapper);
+    wrapper_list = g_slist_prepend(wrapper_list, wrapper);
+    g_static_private_set(&pygobject_contruction_wrapper, wrapper_list, NULL);
+}
+
+PyObject *
+pygobject_pop_wrapper(void)
+{
+    GSList *wrapper_list;
+    PyObject *retval;
+
+    wrapper_list = (GSList *) g_static_private_get(&pygobject_contruction_wrapper);
+    if (!wrapper_list)
+        return NULL;
+    retval = (PyObject *) wrapper_list->data;
+    wrapper_list = g_slist_delete_link(wrapper_list, wrapper_list);
+    g_static_private_set(&pygobject_contruction_wrapper, wrapper_list, NULL);
+    return retval;
+}
+
+static PyObject *
+pygobject_peek_wrapper(void)
+{
+    GSList *wrapper_list;
+
+    wrapper_list = (GSList *) g_static_private_get(&pygobject_contruction_wrapper);
+    if (!wrapper_list)
+        return NULL;
+    return (PyObject *) wrapper_list->data;
+}
+
+static void
+pygobject__g_instance_init(GTypeInstance   *instance,
+                           gpointer         g_class)
+{
+    GObject *object = (GObject *) instance;
+    PyObject *wrapper, *args, *kwargs;
+ 
+    wrapper = g_object_get_qdata(object, pygobject_wrapper_key); 
+    if (wrapper == NULL) {
+        wrapper = pygobject_peek_wrapper();
+        if (wrapper)
+            g_object_set_qdata(object, pygobject_wrapper_key, wrapper);
+    }
+    if (wrapper == NULL) {
+          /* this looks like a python object created through
+           * g_object_new -> we have no python wrapper, so create it
+           * now */
+          /* FIXME: force pygobject_new to not sink reference here.. */
+        wrapper = pygobject_new(object);
+        /* g_object_set_qdata(object, pygobject_wrapper_key, wrapper); */
+        args = PyTuple_New(0);
+        kwargs = PyDict_New();
+        if (wrapper->ob_type->tp_init(wrapper, args, kwargs))
+            PyErr_Print();
+        Py_DECREF(args);
+        Py_DECREF(kwargs);
+    }
+}
+
 int
 pyg_type_register(PyTypeObject *class, char *type_name)
 {
@@ -1018,7 +1088,7 @@ pyg_type_register(PyTypeObject *class, c
 
 	0,    /* instance_size */
 	0,    /* n_preallocs */
-	(GInstanceInitFunc) NULL
+	(GInstanceInitFunc) pygobject__g_instance_init
     };
 
 
@@ -2491,7 +2561,10 @@ struct _PyGObject_Functions pygobject_ap
   pyg_register_class_init,
   pyg_register_interface_info,
 
-  pyg_closure_set_exception_handler
+  pyg_closure_set_exception_handler,
+  pygobject_push_wrapper,
+  pygobject_pop_wrapper
+
 };
 
 #define REGISTER_TYPE(d, type, name) \
Index: gobject/pygobject-private.h
===================================================================
RCS file: /cvs/gnome/gnome-python/pygtk/gobject/pygobject-private.h,v
retrieving revision 1.34
diff -u -p -d -r1.34 pygobject-private.h
--- gobject/pygobject-private.h	5 Jul 2005 17:16:40 -0000	1.34
+++ gobject/pygobject-private.h	5 Jul 2005 22:38:52 -0000
@@ -52,8 +52,11 @@ extern GQuark pygobject_class_key;
 extern GQuark pygobject_wrapper_key;
 extern GQuark pygpointer_class_key;
 
-void  pyg_destroy_notify (gpointer user_data);
-gboolean pyg_error_check(GError **error);
+void       pyg_destroy_notify     (gpointer   user_data);
+gboolean   pyg_error_check        (GError   **error);
+void       pygobject_push_wrapper (PyObject  *wrapper);
+PyObject * pygobject_pop_wrapper  (void);
+
 
 /* from pygtype.h */
 extern PyTypeObject PyGTypeWrapper_Type;
Index: gobject/pygobject.c
===================================================================
RCS file: /cvs/gnome/gnome-python/pygtk/gobject/pygobject.c,v
retrieving revision 1.47
diff -u -p -d -r1.47 pygobject.c
--- gobject/pygobject.c	5 Jul 2005 17:16:40 -0000	1.47
+++ gobject/pygobject.c	5 Jul 2005 22:38:52 -0000
@@ -598,8 +598,10 @@ pygobject_init(PyGObject *self, PyObject
 	    n_params++;
 	}
     }
-
-    self->obj = g_object_newv(object_type, n_params, params);
+    pygobject_push_wrapper((PyObject *) self);
+    if (!self->obj)
+        self->obj = g_object_newv(object_type, n_params, params);
+    pygobject_pop_wrapper();
     if (self->obj)
 	pygobject_register_wrapper((PyObject *)self);
     else
Index: gobject/pygobject.h
===================================================================
RCS file: /cvs/gnome/gnome-python/pygtk/gobject/pygobject.h,v
retrieving revision 1.52
diff -u -p -d -r1.52 pygobject.h
--- gobject/pygobject.h	10 Jun 2005 00:51:48 -0000	1.52
+++ gobject/pygobject.h	5 Jul 2005 22:38:52 -0000
@@ -163,6 +163,10 @@ struct _PyGObject_Functions {
     void      (*register_class_init) (GType gtype, PyGClassInitFunc class_init);
     void      (*register_interface_info) (GType gtype, const GInterfaceInfo *info);
     void      (*closure_set_exception_handler) (GClosure *closure, PyClosureExceptionHandler handler);
+    void      (*push_wrapper) (PyObject *wrapper);
+    PyObject* (*pop_wrapper) (void);
+
+
 };
 
 #ifndef _INSIDE_PYGOBJECT_
@@ -217,6 +221,9 @@ struct _PyGObject_Functions *_PyGObject_
 #define pyg_enable_threads          (_PyGObject_API->enable_threads)
 #define pyg_register_class_init     (_PyGObject_API->register_class_init)
 #define pyg_register_interface_info (_PyGObject_API->register_interface_info)
+#define pygobject_push_wrapper      (_PyGObject_API->push_wrapper)
+#define pygobject_pop_wrapper       (_PyGObject_API->pop_wrapper)
+
 
 #define pyg_block_threads()   G_STMT_START {   \
     if (_PyGObject_API->block_threads != NULL) \
Index: gtk/gtk.override
===================================================================
RCS file: /cvs/gnome/gnome-python/pygtk/gtk/gtk.override,v
retrieving revision 1.336
diff -u -p -d -r1.336 gtk.override
--- gtk/gtk.override	27 Jun 2005 23:20:39 -0000	1.336
+++ gtk/gtk.override	5 Jul 2005 22:38:54 -0000
@@ -2678,7 +2678,10 @@ _wrap_gtk_dialog_new_with_buttons(PyGObj
         return -1;
     }
     
-    self->obj = g_object_new(pyg_type_from_object((PyObject*) self), NULL);
+    pygobject_push_wrapper((PyObject *) self);
+    if (!self->obj)
+        self->obj = g_object_new(pyg_type_from_object((PyObject*) self), NULL);
+    pygobject_pop_wrapper();
 
     if (!self->obj) {
         PyErr_SetString(PyExc_RuntimeError,
