Index: ChangeLog
===================================================================
--- ChangeLog	(revision 1029)
+++ ChangeLog	(working copy)
@@ -1,3 +1,16 @@
+2009-03-22  Mark Lee  <marklee@svn.gnome.org>
+
+	Bug 559001 – Allow setting pytype wrapper class
+
+	initial patch by: John Ehresman  <jpe@wingware.com>
+
+	* gobject/pygtype.c (_wrap_g_type_wrapper__set_pytype): New function.
+
+	* tests/test_gtype.py (GTypeTest.assertPyType),
+	(GTypeTest.setInvalidPyType), (GTypeTest.testPyType),
+	(GTypeTest.testInvalidPyType): Two new testcases to test the above,
+	with accompanying helper methods.
+
 2009-03-22  Gian Mario Tagliaretti  <gianmt@gnome.org>
 
 	* gio/gio.defs: Add g_file_enumerator_get_container, new in 2.18
Index: tests/test_gtype.py
===================================================================
--- tests/test_gtype.py	(revision 1029)
+++ tests/test_gtype.py	(working copy)
@@ -28,6 +28,15 @@
                              'got %r while %r was expected' %
                              (val, expected))
 
+    def assertPyType(self, val, pytype):
+        val.pytype = pytype
+        self.assertEqual(val.pytype, pytype,
+                         'got %r while %r was expected' % (val.pytype, pytype))
+
+    def setInvalidPyType(self):
+        val = GType(gobject.TYPE_INT)
+        val.pytype = 1
+
     def testBool(self):
         self.checkType(gobject.TYPE_BOOLEAN, 'gboolean', bool)
 
@@ -64,6 +73,16 @@
     def testObject(self):
         self.checkType(gobject.TYPE_OBJECT, 'PyObject')
 
+    def testPyType(self):
+        val = GType(gobject.TYPE_INT)
+        self.assertEqual(val.pytype, None,
+                         'got %r while %r was expected' % (val.pytype, None))
+        for t in [int, None, float]:
+            self.assertPyType(val, t)
+
+    def testInvalidPyType(self):
+        self.assertRaises(TypeError, self.setInvalidPyType)
+
     def testValue(self):
         array = [1, "foo", True]
         for i in array:
Index: gobject/pygtype.c
===================================================================
--- gobject/pygtype.c	(revision 1029)
+++ gobject/pygtype.c	(working copy)
@@ -83,6 +83,26 @@
     return py_type;
 }
 
+static int
+_wrap_g_type_wrapper__set_pytype(PyGTypeWrapper *self, PyObject* value, void *closure)
+{
+    PyObject *py_type;
+    
+    py_type = g_type_get_qdata(self->type, pygobject_class_key);
+    Py_CLEAR(py_type);
+    if (value == Py_None)
+	g_type_set_qdata(self->type, pygobject_class_key, NULL);
+    else if (PyType_Check(value)) {
+	Py_INCREF(value);
+	g_type_set_qdata(self->type, pygobject_class_key, value);
+    } else {
+	PyErr_SetString(PyExc_TypeError, "Value must be None or a type object");
+	return -1;
+    }
+
+    return 0;
+}
+
 static PyObject *
 _wrap_g_type_wrapper__get_name(PyGTypeWrapper *self, void *closure)
 {
@@ -143,7 +163,7 @@
 }
 
 static PyGetSetDef _PyGTypeWrapper_getsets[] = {
-    { "pytype", (getter)_wrap_g_type_wrapper__get_pytype, (setter)0 },
+    { "pytype", (getter)_wrap_g_type_wrapper__get_pytype, (setter)_wrap_g_type_wrapper__set_pytype },
     { "name",  (getter)_wrap_g_type_wrapper__get_name, (setter)0 },
     { "fundamental",  (getter)_wrap_g_type_wrapper__get_fundamental, (setter)0 },
     { "parent",  (getter)_wrap_g_type_wrapper__get_parent, (setter)0 },
