pygobject r885 - in trunk: . glib gobject



Author: johan
Date: Sun Jul 27 09:49:13 2008
New Revision: 885
URL: http://svn.gnome.org/viewvc/pygobject?rev=885&view=rev

Log:
2008-07-27  Johan Dahlin  <johan gnome org>

    * glib/pyglib-python-compat.h:
    Add a Py_TYPE macro for accessing ob_type.
    * glib/glibmodule.c (pyglib_register_constants):
    * gobject/gobjectmodule.c (pygobject__g_instance_init),
    (pyg_integer_richcompare):
    * gobject/pygenum.c (pyg_enum_repr), (pyg_enum_from_gtype),
    (pyg_enum_add), (pyg_enum_get_value_name),
    (pyg_enum_get_value_nick):
    * gobject/pygflags.c (pyg_flags_repr), (pyg_flags_from_gtype),
    (pyg_flags_add), (pyg_flags_and), (pyg_flags_or), (pyg_flags_xor),
    (pyg_flags_get_first_value_name), (pyg_flags_get_first_value_nick),
    (pyg_flags_get_value_names), (pyg_flags_get_value_nicks):
    Use Py_TYPE and PyLong macros to access struct fields



Modified:
   trunk/ChangeLog
   trunk/glib/glibmodule.c
   trunk/glib/pyglib-python-compat.h
   trunk/gobject/gobjectmodule.c
   trunk/gobject/pygenum.c
   trunk/gobject/pygflags.c

Modified: trunk/glib/glibmodule.c
==============================================================================
--- trunk/glib/glibmodule.c	(original)
+++ trunk/glib/glibmodule.c	Sun Jul 27 09:49:13 2008
@@ -519,7 +519,6 @@
     return Py_None;
 }
 
-
 static PyMethodDef _glib_functions[] = {
     { "spawn_async",
       (PyCFunction)pyglib_spawn_async, METH_VARARGS|METH_KEYWORDS },
@@ -623,7 +622,6 @@
 static void
 pyglib_register_constants(PyObject *m)
 {
-
     PyModule_AddIntConstant(m, "SPAWN_LEAVE_DESCRIPTORS_OPEN",
 			    G_SPAWN_LEAVE_DESCRIPTORS_OPEN);
     PyModule_AddIntConstant(m, "SPAWN_DO_NOT_REAP_CHILD",

Modified: trunk/glib/pyglib-python-compat.h
==============================================================================
--- trunk/glib/pyglib-python-compat.h	(original)
+++ trunk/glib/pyglib-python-compat.h	Sun Jul 27 09:49:13 2008
@@ -28,7 +28,7 @@
 
 /* Compilation on Python 2.x */
 #if PY_VERSION_HEX < 0x03000000
-
+#define RO READONLY
 #define _PyUnicode_Check PyString_Check 
 #define _PyUnicode_AsString PyString_AsString
 #define _PyUnicode_AsStringAndSize PyString_AsStringAndSize
@@ -42,9 +42,10 @@
 #define _PyLong_Check PyInt_Check
 #define _PyLong_FromLong PyInt_FromLong
 #define _PyLong_AsLong  PyInt_AsLong
-#define RO READONLY
 #define _PyLongObject PyIntObject
 #define _PyLong_Type PyInt_Type
+#define _PyLong_AS_LONG PyInt_AS_LONG
+#define Py_TYPE(ob) (ob->ob_type)
 #else
 #undef PYGLIB_MODULE_START
 #undef PYGLIB_MODULE_END
@@ -96,6 +97,7 @@
 #define _PyLong_Check PyLong_Check
 #define _PyLong_FromLong PyLong_FromLong
 #define _PyLong_AsLong PyLong_AsLong
+#define _PyLong_AS_LONG PyLong_AS_LONG
 #define _PyLongObject PyLongObject
 #define _PyLong_Type PyLong_Type
 #endif

Modified: trunk/gobject/gobjectmodule.c
==============================================================================
--- trunk/gobject/gobjectmodule.c	(original)
+++ trunk/gobject/gobjectmodule.c	Sun Jul 27 09:49:13 2008
@@ -1045,7 +1045,7 @@
         wrapper = pygobject_new_full(object, FALSE, g_class);
         args = PyTuple_New(0);
         kwargs = PyDict_New();
-        if (wrapper->ob_type->tp_init(wrapper, args, kwargs))
+        if (Py_TYPE(wrapper)->tp_init(wrapper, args, kwargs))
             PyErr_Print();
         Py_DECREF(args);
         Py_DECREF(kwargs);
@@ -2306,7 +2306,6 @@
     g_type_set_qdata(type, pygobject_has_updated_constructor_key, GINT_TO_POINTER(1));
 }
 
-#define GET_INT(x) (((_PyLongObject*)x)->ob_ival)
 PyObject *
 pyg_integer_richcompare(PyObject *v, PyObject *w, int op)
 {
@@ -2314,12 +2313,12 @@
     gboolean t;
 
     switch (op) {
-    case Py_EQ: t = GET_INT(v) == GET_INT(w); break;
-    case Py_NE: t = GET_INT(v) != GET_INT(w); break;
-    case Py_LE: t = GET_INT(v) <= GET_INT(w); break;
-    case Py_GE: t = GET_INT(v) >= GET_INT(w); break;
-    case Py_LT: t = GET_INT(v) <  GET_INT(w); break;
-    case Py_GT: t = GET_INT(v) >  GET_INT(w); break;
+    case Py_EQ: t = _PyLong_AS_LONG(v) == _PyLong_AS_LONG(w); break;
+    case Py_NE: t = _PyLong_AS_LONG(v) != _PyLong_AS_LONG(w); break;
+    case Py_LE: t = _PyLong_AS_LONG(v) <= _PyLong_AS_LONG(w); break;
+    case Py_GE: t = _PyLong_AS_LONG(v) >= _PyLong_AS_LONG(w); break;
+    case Py_LT: t = _PyLong_AS_LONG(v) <  _PyLong_AS_LONG(w); break;
+    case Py_GT: t = _PyLong_AS_LONG(v) >  _PyLong_AS_LONG(w); break;
     default: g_assert_not_reached();
     }
 
@@ -2327,7 +2326,6 @@
     Py_INCREF(result);
     return result;
 }
-#undef GET_INT
 
 static void
 _log_func(const gchar *log_domain,

Modified: trunk/gobject/pygenum.c
==============================================================================
--- trunk/gobject/pygenum.c	(original)
+++ trunk/gobject/pygenum.c	Sun Jul 27 09:49:13 2008
@@ -64,13 +64,13 @@
   g_assert(G_IS_ENUM_CLASS(enum_class));
 
   for (index = 0; index < enum_class->n_values; index++)
-      if (self->parent.ob_ival == enum_class->values[index].value)
+      if (_PyLong_AS_LONG(self) == enum_class->values[index].value)
           break;
   value = enum_class->values[index].value_name;
   if (value)
       sprintf(tmp, "<enum %s of type %s>", value, g_type_name(self->gtype));
   else
-      sprintf(tmp, "<enum %ld of type %s>", self->parent.ob_ival, g_type_name(self->gtype));
+      sprintf(tmp, "<enum %ld of type %s>", _PyLong_AS_LONG(self), g_type_name(self->gtype));
 
   g_type_class_unref(enum_class);
 
@@ -168,7 +168,11 @@
 	retval = ((PyTypeObject *)pyclass)->tp_alloc((PyTypeObject *)pyclass, 0);
 	g_assert(retval != NULL);
 	
+#if PY_VERSION_HEX >= 0x03000000
+#   warning "FIXME: figure out how to subclass long"    
+#else
 	((_PyLongObject*)retval)->ob_ival = value;
+#endif    
 	((PyGFlags*)retval)->gtype = gtype;
 	//return _PyLong_FromLong(value);
     }
@@ -236,7 +240,11 @@
 	PyObject *item, *intval;
       
 	item = ((PyTypeObject *)stub)->tp_alloc((PyTypeObject *)stub, 0);
+#if PY_VERSION_HEX >= 0x03000000
+#   warning "FIXME: figure out how to subclass long"    
+#else
 	((_PyLongObject*)item)->ob_ival = eclass->values[i].value;
+#endif    
 	((PyGEnum*)item)->gtype = gtype;
 	
         intval = _PyLong_FromLong(eclass->values[i].value);
@@ -284,7 +292,7 @@
   enum_class = g_type_class_ref(self->gtype);
   g_assert(G_IS_ENUM_CLASS(enum_class));
   
-  enum_value = g_enum_get_value(enum_class, self->parent.ob_ival);
+  enum_value = g_enum_get_value(enum_class, _PyLong_AS_LONG(self));
 
   retval = _PyUnicode_FromString(enum_value->value_name);
   g_type_class_unref(enum_class);
@@ -302,7 +310,7 @@
   enum_class = g_type_class_ref(self->gtype);
   g_assert(G_IS_ENUM_CLASS(enum_class));
   
-  enum_value = g_enum_get_value(enum_class, self->parent.ob_ival);
+  enum_value = g_enum_get_value(enum_class, _PyLong_AS_LONG(self));
 
   retval = _PyUnicode_FromString(enum_value->value_nick);
   g_type_class_unref(enum_class);

Modified: trunk/gobject/pygflags.c
==============================================================================
--- trunk/gobject/pygflags.c	(original)
+++ trunk/gobject/pygflags.c	Sun Jul 27 09:49:13 2008
@@ -33,8 +33,6 @@
 
 PYGLIB_DEFINE_TYPE("gobject.GFlags", PyGFlags_Type, PyGFlags);
 
-#define GET_INT_VALUE(x) (((_PyLongObject*)x)->ob_ival)
-
 static PyObject *
 pyg_flags_richcompare(PyGFlags *self, PyObject *other, int op)
 {
@@ -94,13 +92,13 @@
     char *tmp, *retval;
     PyObject *pyretval;
   
-    tmp = generate_repr(self->gtype, self->parent.ob_ival);
+    tmp = generate_repr(self->gtype, _PyLong_AS_LONG(self));
 
     if (tmp)
         retval = g_strdup_printf("<flags %s of type %s>", tmp,
                                  g_type_name(self->gtype));
     else
-        retval = g_strdup_printf("<flags %ld of type %s>", self->parent.ob_ival,
+        retval = g_strdup_printf("<flags %ld of type %s>", _PyLong_AS_LONG(self),
                                  g_type_name(self->gtype));
     g_free(tmp);
     
@@ -192,7 +190,11 @@
 	retval = ((PyTypeObject *)pyclass)->tp_alloc((PyTypeObject *)pyclass, 0);
 	g_assert(retval != NULL);
 	
+#if PY_VERSION_HEX >= 0x03000000
+#   warning "FIXME: figure out how to subclass long"    
+#else
 	((_PyLongObject*)retval)->ob_ival = value;
+#endif    
 	((PyGFlags*)retval)->gtype = gtype;
     } else {
 	Py_INCREF(retval);
@@ -257,7 +259,11 @@
       PyObject *item, *intval;
       
       item = ((PyTypeObject *)stub)->tp_alloc((PyTypeObject *)stub, 0);
+#if PY_VERSION_HEX >= 0x03000000
+#   warning "FIXME: figure out how to subclass long"    
+#else
       ((_PyLongObject*)item)->ob_ival = eclass->values[i].value;
+#endif    
       ((PyGFlags*)item)->gtype = gtype;
             
       intval = _PyLong_FromLong(eclass->values[i].value);
@@ -294,7 +300,7 @@
 						       (PyObject*)b);
 	
 	return pyg_flags_from_gtype(a->gtype,
-				    GET_INT_VALUE(a) & GET_INT_VALUE(b));
+				    _PyLong_AS_LONG(a) & _PyLong_AS_LONG(b));
 }
 
 static PyObject *
@@ -304,7 +310,7 @@
 		return _PyLong_Type.tp_as_number->nb_or((PyObject*)a,
 						      (PyObject*)b);
 
-	return pyg_flags_from_gtype(a->gtype, GET_INT_VALUE(a) | GET_INT_VALUE(b));
+	return pyg_flags_from_gtype(a->gtype, _PyLong_AS_LONG(a) | _PyLong_AS_LONG(b));
 }
 
 static PyObject *
@@ -315,7 +321,7 @@
 						       (PyObject*)b);
 
 	return pyg_flags_from_gtype(a->gtype,
-				    GET_INT_VALUE(a) ^ GET_INT_VALUE(b));
+				    _PyLong_AS_LONG(a) ^ _PyLong_AS_LONG(b));
 
 }
 
@@ -338,7 +344,7 @@
   
   flags_class = g_type_class_ref(self->gtype);
   g_assert(G_IS_FLAGS_CLASS(flags_class));
-  flags_value = g_flags_get_first_value(flags_class, self->parent.ob_ival);
+  flags_value = g_flags_get_first_value(flags_class, _PyLong_AS_LONG(self));
   if (flags_value)
       retval = _PyUnicode_FromString(flags_value->value_name);
   else {
@@ -360,7 +366,7 @@
   flags_class = g_type_class_ref(self->gtype);
   g_assert(G_IS_FLAGS_CLASS(flags_class));
 
-  flags_value = g_flags_get_first_value(flags_class, self->parent.ob_ival);
+  flags_value = g_flags_get_first_value(flags_class, _PyLong_AS_LONG(self));
   if (flags_value)
       retval = _PyUnicode_FromString(flags_value->value_nick);
   else {
@@ -384,7 +390,7 @@
   
   retval = PyList_New(0);
   for (i = 0; i < flags_class->n_values; i++)
-      if ((self->parent.ob_ival & flags_class->values[i].value) == flags_class->values[i].value)
+      if ((_PyLong_AS_LONG(self) & flags_class->values[i].value) == flags_class->values[i].value)
 	  PyList_Append(retval, _PyUnicode_FromString(flags_class->values[i].value_name));
 
   g_type_class_unref(flags_class);
@@ -404,7 +410,7 @@
   
   retval = PyList_New(0);
   for (i = 0; i < flags_class->n_values; i++)
-      if ((self->parent.ob_ival & flags_class->values[i].value) == flags_class->values[i].value)
+      if ((_PyLong_AS_LONG(self) & flags_class->values[i].value) == flags_class->values[i].value)
 	  PyList_Append(retval, _PyUnicode_FromString(flags_class->values[i].value_nick));
 
   g_type_class_unref(flags_class);



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