Index: gobject/gobjectmodule.c
===================================================================
RCS file: /cvs/gnome/gnome-python/pygtk/gobject/gobjectmodule.c,v
retrieving revision 1.163
diff -u -3 -p -r1.163 gobjectmodule.c
--- gobject/gobjectmodule.c	9 May 2005 20:01:40 -0000	1.163
+++ gobject/gobjectmodule.c	16 May 2005 13:48:04 -0000
@@ -410,11 +410,67 @@ pyg_object_get_property (GObject *object
     pyg_gil_state_release(state);
 }
 
+GStaticPrivate __pygobject_creation = G_STATIC_PRIVATE_INIT;
+static GObject *
+pyg_object_class_constructor (GType type, guint n_construct_properties, GObjectConstructParam *construct_properties){
+  int mode = pygtk_get_object_creation_mode();
+  GObject *ret;
+  
+  if (mode) {
+    /* FIXME: This will cause issues if you inherit from a Python object in C and 
+     * inherit from that object in C again. When that happens we'll need to add
+     * some type data to point to the real constructor or so.
+     */
+    GObjectClass *parent = g_type_class_peek (g_type_parent (type));
+    pygtk_set_object_creation_mode (0);
+    while (parent->constructor == pyg_object_class_constructor)
+      parent = g_type_class_peek_parent (parent);
+    return parent->constructor (type, n_construct_properties, construct_properties);
+  } else {
+    PyObject *args, *kwargs, *pyobject;
+    PyTypeObject *typeobject = pygobject_lookup_class (type);
+    guint i;
+
+    if (!typeobject) 
+      g_error ("No Python type, type %s is improperly registered\n",
+	  g_type_name (type));
+    if (n_construct_properties > 0) {
+      kwargs = PyDict_New();
+      for (i = 0; i < n_construct_properties; i++) {
+	PyObject *valobj = pyg_param_gvalue_as_pyobject (construct_properties[i].value,
+	    FALSE, construct_properties[i].pspec);
+	if (!valobj ||
+	    PyDict_SetItemString (kwargs, construct_properties[i].pspec->name, 
+		valobj) != 0)
+	  g_warning ("could not add property %s to construct properties", 
+	      construct_properties[i].pspec->name);
+      }
+    } else {
+      kwargs = NULL;
+    }
+    args = (PyObject *) PyTuple_New (0);
+    pygtk_set_object_creation_mode (1);
+    pyobject = PyObject_Call ((PyObject *) typeobject, args, NULL);
+    if (!pyobject) { 
+      PyErr_Print ();
+      g_error ("object creation for type %s failed", g_type_name (type));
+    }
+    Py_DECREF (args);
+    Py_XDECREF (kwargs);
+    Py_DECREF (pyobject); /* reference is held by the GObject's qdata */
+    ret = pygobject_get (pyobject);
+    /* weird behaviour expected from constructors */
+    g_object_freeze_notify (ret);
+    return ret;
+  }
+}
+
 static void
 pyg_object_class_init(GObjectClass *class, PyObject *py_class)
 {
     class->set_property = pyg_object_set_property;
     class->get_property = pyg_object_get_property;
+    class->constructor = pyg_object_class_constructor;
 }
 
 static gboolean
Index: gobject/pygobject-private.h
===================================================================
RCS file: /cvs/gnome/gnome-python/pygtk/gobject/pygobject-private.h,v
retrieving revision 1.28
diff -u -3 -p -r1.28 pygobject-private.h
--- gobject/pygobject-private.h	9 Feb 2005 11:32:42 -0000	1.28
+++ gobject/pygobject-private.h	16 May 2005 13:48:05 -0000
@@ -38,6 +38,11 @@ extern struct _PyGObject_Functions pygob
             PyEval_RestoreThread(_save);                \
     } G_STMT_END
 
+extern GStaticPrivate __pygobject_creation;
+#define pygtk_set_object_creation_mode(object) \
+  g_static_private_set (&__pygobject_creation, GINT_TO_POINTER (object), NULL)
+#define pygtk_get_object_creation_mode() \
+  GPOINTER_TO_INT (g_static_private_get (&__pygobject_creation))
 
 extern GType PY_TYPE_OBJECT;
 
Index: gobject/pygobject.c
===================================================================
RCS file: /cvs/gnome/gnome-python/pygtk/gobject/pygobject.c,v
retrieving revision 1.39
diff -u -3 -p -r1.39 pygobject.c
--- gobject/pygobject.c	27 Nov 2004 19:12:34 -0000	1.39
+++ gobject/pygobject.c	16 May 2005 13:48:06 -0000
@@ -557,6 +557,7 @@ pygobject_free(PyObject *op)
 
 /* ---------------- PyGObject methods ----------------- */
 
+extern GStaticPrivate __pygobject_creation;
 static int
 pygobject_init(PyGObject *self, PyObject *args, PyObject *kwargs)
 {
@@ -564,6 +565,7 @@ pygobject_init(PyGObject *self, PyObject
     guint n_params = 0, i;
     GParameter *params = NULL;
     GObjectClass *class;
+    gboolean created_by_python;
 
     if (!PyArg_ParseTuple(args, ":GObject.__init__", &object_type))
 	return -1;
@@ -584,6 +586,12 @@ pygobject_init(PyGObject *self, PyObject
 	return -1;
     }
 
+    if (pygtk_get_object_creation_mode ())
+	created_by_python = FALSE;
+    else
+	created_by_python = TRUE;
+    pygtk_set_object_creation_mode (0);
+    
     if (kwargs) {
 	int pos = 0;
 	PyObject *key;
@@ -614,10 +622,27 @@ pygobject_init(PyGObject *self, PyObject
 	}
     }
 
+    /* FIXME: It would be nicer if we only set this when we know that we have
+     * a Python subclasses object to avoid screwups.
+     * That requires a gboolean pygtk_gtype_created_in_python (GType) though
+     */
+    pygtk_set_object_creation_mode (1);
     self->obj = g_object_newv(object_type, n_params, params);
-    if (self->obj)
-	pygobject_register_wrapper((PyObject *)self);
-    else
+    pygtk_set_object_creation_mode (0);
+    if (self->obj) {
+	/* this is basically the same as pygobject_register_wrapper */
+	if (!pygobject_wrapper_key)
+	    pygobject_wrapper_key=g_quark_from_static_string(pygobject_wrapper_id);
+
+	Py_INCREF(self);
+	g_object_set_qdata_full(self->obj, pygobject_wrapper_key, self,
+				pyg_destroy_notify);
+	/* only sink the object if it's ours. C code wants the floating ref */
+	if (created_by_python)
+	    sink_object (self->obj);
+	else
+	    g_object_ref (self->obj);
+    } else
 	PyErr_SetString (PyExc_RuntimeError, "could not create object");
 	   
  cleanup:
