summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohn Ehresman <jpe@wingware.com>2010-04-15 17:11:30 -0400
committerJohn Ehresman <jpe@wingware.com>2010-04-15 17:14:07 -0400
commit882652743e7b15bcf756e1e6bc9456a84942630d (patch)
treecd3f4f5a17985d11c909c9b7e72d62df5dc27ea7
parente0729eac9d52f8bc0adce4fcbbb7ada5918bfb81 (diff)
downloadpygobject-882652743e7b15bcf756e1e6bc9456a84942630d.tar.gz
pygobject-882652743e7b15bcf756e1e6bc9456a84942630d.tar.xz
pygobject-882652743e7b15bcf756e1e6bc9456a84942630d.zip
Initial working enum/flags/pid subclasses of long
-rw-r--r--glib/pygspawn.c5
-rw-r--r--gobject/pygenum.c57
-rw-r--r--gobject/pygflags.c52
3 files changed, 67 insertions, 47 deletions
diff --git a/glib/pygspawn.c b/glib/pygspawn.c
index cded501..3471b4a 100644
--- a/glib/pygspawn.c
+++ b/glib/pygspawn.c
@@ -65,11 +65,12 @@ PyObject *
pyg_pid_new(GPid pid)
{
_PyLongObject *pygpid;
- pygpid = PyObject_NEW(_PyLongObject, &PyGPid_Type);
#if PY_VERSION_HEX >= 0x03000000
-# warning "FIXME: figure out how to subclass long"
+ return PyObject_CallMethod((PyObject*)&PyLong_Type, "__new__", "Oi",
+ &PyGPid_Type, pygpid);
#else
+ pygpid = PyObject_NEW(_PyLongObject, &PyGPid_Type);
pygpid->ob_ival = pid;
#endif
return (PyObject *) pygpid;
diff --git a/gobject/pygenum.c b/gobject/pygenum.c
index 027dbd4..633c7eb 100644
--- a/gobject/pygenum.c
+++ b/gobject/pygenum.c
@@ -35,6 +35,23 @@ GQuark pygenum_class_key;
PYGLIB_DEFINE_TYPE("gobject.GEnum", PyGEnum_Type, PyGEnum);
static PyObject *
+pyg_enum_val_new(PyObject* subclass, GType gtype, PyObject *intval)
+{
+ PyObject *item;
+
+#if PY_VERSION_HEX >= 0x03000000
+ item = PyObject_CallMethod((PyObject*)&PyLong_Type, "__new__", "OO",
+ subclass, intval);
+#else
+ item = ((PyTypeObject *)stub)->tp_alloc((PyTypeObject *)subclass, 0);
+ ((_PyLongObject*)item)->ob_ival = PyInt_AS_LONG(intval);
+#endif
+ ((PyGEnum*)item)->gtype = gtype;
+
+ return item;
+}
+
+static PyObject *
pyg_enum_richcompare(PyGEnum *self, PyObject *other, int op)
{
static char warning[256];
@@ -170,22 +187,16 @@ pyg_enum_from_gtype (GType gtype, int value)
"__enum_values__");
intvalue = _PyLong_FromLong(value);
retval = PyDict_GetItem(values, intvalue);
- Py_DECREF(intvalue);
- if (!retval) {
+ if (retval) {
+ Py_INCREF(retval);
+ }
+ else {
PyErr_Clear();
- 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);
+ retval = pyg_enum_val_new(pyclass, gtype, intvalue);
}
-
- Py_INCREF(retval);
+ Py_DECREF(intvalue);
+
return retval;
}
@@ -247,15 +258,8 @@ pyg_enum_add (PyObject * module,
for (i = 0; i < eclass->n_values; i++) {
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);
+ item = pyg_enum_val_new(stub, gtype, intval);
PyDict_SetItem(values, intval, item);
Py_DECREF(intval);
@@ -343,14 +347,19 @@ pygobject_enum_register_types(PyObject *d)
{
pygenum_class_key = g_quark_from_static_string("PyGEnum::class");
- PyGEnum_Type.tp_base = &_PyLong_Type;
+#if PY_VERSION_HEX < 0x03000000
+ PyGEnum_Type.tp_base = PyInt_Type;
+ PyGEnum_Type.tp_new = pyg_enum_new;
+#else
+ PyGEnum_Type.tp_base = &PyLong_Type;
+ PyGEnum_Type.tp_new = _PyLong_Type.tp_new;
+#endif
+
PyGEnum_Type.tp_repr = (reprfunc)pyg_enum_repr;
PyGEnum_Type.tp_str = (reprfunc)pyg_enum_repr;
PyGEnum_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
PyGEnum_Type.tp_richcompare = (richcmpfunc)pyg_enum_richcompare;
PyGEnum_Type.tp_methods = pyg_enum_methods;
PyGEnum_Type.tp_getset = pyg_enum_getsets;
- PyGEnum_Type.tp_new = pyg_enum_new;
PYGOBJECT_REGISTER_GTYPE(d, PyGEnum_Type, "GEnum", G_TYPE_ENUM);
-
}
diff --git a/gobject/pygflags.c b/gobject/pygflags.c
index abac423..a71a980 100644
--- a/gobject/pygflags.c
+++ b/gobject/pygflags.c
@@ -36,6 +36,23 @@ GQuark pygflags_class_key;
PYGLIB_DEFINE_TYPE("gobject.GFlags", PyGFlags_Type, PyGFlags);
static PyObject *
+pyg_flags_val_new(PyObject* subclass, GType gtype, PyObject *intval)
+{
+ PyObject *item;
+
+#if PY_VERSION_HEX >= 0x03000000
+ item = PyObject_CallMethod((PyObject*)&PyLong_Type, "__new__", "OO",
+ subclass, intval);
+#else
+ item = ((PyTypeObject *)stub)->tp_alloc((PyTypeObject *)subclass, 0);
+ ((_PyLongObject*)item)->ob_ival = PyInt_AS_LONG(intval);
+#endif
+ ((PyGFlags*)item)->gtype = gtype;
+
+ return item;
+}
+
+static PyObject *
pyg_flags_richcompare(PyGFlags *self, PyObject *other, int op)
{
static char warning[256];
@@ -189,23 +206,16 @@ pyg_flags_from_gtype (GType gtype, int value)
"__flags_values__");
pyint = _PyLong_FromLong(value);
retval = PyDict_GetItem(values, pyint);
- Py_DECREF(pyint);
-
if (!retval) {
PyErr_Clear();
- retval = ((PyTypeObject *)pyclass)->tp_alloc((PyTypeObject *)pyclass, 0);
+ retval = pyg_flags_val_new(pyclass, gtype, pyint);
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);
}
+ Py_DECREF(pyint);
+
return retval;
}
@@ -265,15 +275,8 @@ pyg_flags_add (PyObject * module,
for (i = 0; i < eclass->n_values; i++) {
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);
+ item = pyg_flags_val_new(stub, gtype, intval);
PyDict_SetItem(values, intval, item);
Py_DECREF(intval);
@@ -281,11 +284,11 @@ pyg_flags_add (PyObject * module,
char *prefix;
prefix = g_strdup(pyg_constant_strip_prefix(eclass->values[i].value_name, strip_prefix));
+ Py_INCREF(item);
PyModule_AddObject(module, prefix, item);
g_free(prefix);
-
- Py_INCREF(item);
}
+ Py_DECREF(item);
}
PyDict_SetItemString(((PyTypeObject *)stub)->tp_dict,
@@ -458,6 +461,14 @@ pygobject_flags_register_types(PyObject *d)
{
pygflags_class_key = g_quark_from_static_string("PyGFlags::class");
+#if PY_VERSION_HEX < 0x03000000
+ PyGFlags_Type.tp_base = PyInt_Type;
+ PyGFlags_Type.tp_new = pyg_enum_new;
+#else
+ PyGFlags_Type.tp_base = &PyLong_Type;
+ PyGFlags_Type.tp_new = _PyLong_Type.tp_new;
+#endif
+
PyGFlags_Type.tp_base = &_PyLong_Type;
PyGFlags_Type.tp_repr = (reprfunc)pyg_flags_repr;
PyGFlags_Type.tp_as_number = &pyg_flags_as_number;
@@ -465,6 +476,5 @@ pygobject_flags_register_types(PyObject *d)
PyGFlags_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
PyGFlags_Type.tp_richcompare = (richcmpfunc)pyg_flags_richcompare;
PyGFlags_Type.tp_getset = pyg_flags_getsets;
- PyGFlags_Type.tp_new = pyg_flags_new;
PYGOBJECT_REGISTER_GTYPE(d, PyGFlags_Type, "GFlags", G_TYPE_FLAGS);
}