summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatt Wilson <msw@redhat.com>2001-11-27 17:49:51 +0000
committerMatt Wilson <msw@src.gnome.org>2001-11-27 17:49:51 +0000
commit3b559a4dc0042f35d2aa9a576949df5bb99b0680 (patch)
tree9d1c57eb31b0073c9c8b17199dc5b887f4b02254
parent61972bb1c4c041287a8ab6241b8eec4b20344054 (diff)
downloadpygobject-3b559a4dc0042f35d2aa9a576949df5bb99b0680.tar.gz
pygobject-3b559a4dc0042f35d2aa9a576949df5bb99b0680.tar.xz
pygobject-3b559a4dc0042f35d2aa9a576949df5bb99b0680.zip
added start of atk wrapper
2001-11-27 Matt Wilson <msw@redhat.com> * atk.defs, atk.override, atkmodule.c: added start of atk wrapper * gtk/Makefile.am (DISTCLEANFILES): remove generated files ($(srcdir)/gtk.c): modify targets to output generated files in the builddir, not the srcdir. ($(srcdir)/gdk.c): likewise. ($(srcdir)/libglade.c): likewise. * codegen/h2def.py (define_func): add check to workaround broken string.replace semantics on python 1.5 * codegen/codegen.py (write_enums): if the enum or flag doesn't have a GType, just add those enum values listed in the defs file by hand. * codegen/argtypes.py (ArgMatcher.register_enum): if defs file doesn't specify a GType for the enum, use G_TYPE_NONE. (ArgMatcher.register_flag): likewise. * gobjectmodule.c (pyg_enum_get_value): allow enums to pass G_TYPE_NONE in as the type, disabling the string to value conversion. (pyg_flags_get_value): likewise. (pyg_constant_strip_prefix): new function to combine common codepath. (pyg_enum_add_constants): use pyg_constant_strip_prefix (pyg_flags_add_constants): likewise. (functions): make pyg_constant_strip_prefix available to users of pygobject * pygobject.h: added pyg_constant_strip_prefix wrapper * configure.in: added ATK test, subst for ATK_CFLAGS and ATK_LIBS. * Makefile.am (%.c): added generic codegen target for pango and atk. Added DISTCLEANFILES so that generated files will be removed. Added atk targets, cflags, ldflags.
-rw-r--r--gobject/gobjectmodule.c89
-rw-r--r--gobject/pygobject.h4
2 files changed, 63 insertions, 30 deletions
diff --git a/gobject/gobjectmodule.c b/gobject/gobjectmodule.c
index 40623e0..5792f55 100644
--- a/gobject/gobjectmodule.c
+++ b/gobject/gobjectmodule.c
@@ -524,7 +524,7 @@ pyg_boxed_new(GType boxed_type, gpointer boxed, gboolean copy_boxed,
static gint
pyg_enum_get_value(GType enum_type, PyObject *obj, gint *val)
{
- GEnumClass *eclass = G_ENUM_CLASS(g_type_class_ref(enum_type));
+ GEnumClass *eclass = NULL;
gint res = -1;
g_return_val_if_fail(val != NULL, -1);
@@ -535,8 +535,17 @@ pyg_enum_get_value(GType enum_type, PyObject *obj, gint *val)
*val = PyInt_AsLong(obj);
res = 0;
} else if (PyString_Check(obj)) {
+ GEnumValue *info;
char *str = PyString_AsString(obj);
- GEnumValue *info = g_enum_get_value_by_name(eclass, str);
+
+ if (enum_type != G_TYPE_NONE)
+ eclass = G_ENUM_CLASS(g_type_class_ref(enum_type));
+ else {
+ PyErr_SetString(PyExc_TypeError, "could not convert string to enum because there is no GType associated to look up the value");
+ res = -1;
+ }
+ info = g_enum_get_value_by_name(eclass, str);
+ g_type_class_unref(eclass);
if (!info)
info = g_enum_get_value_by_nick(eclass, str);
@@ -551,14 +560,13 @@ pyg_enum_get_value(GType enum_type, PyObject *obj, gint *val)
PyErr_SetString(PyExc_TypeError,"enum values must be strings or ints");
res = -1;
}
- g_type_class_unref(eclass);
return res;
}
static gint
pyg_flags_get_value(GType flag_type, PyObject *obj, gint *val)
{
- GFlagsClass *fclass = G_FLAGS_CLASS(g_type_class_ref(flag_type));
+ GFlagsClass *fclass = NULL;
gint res = -1;
g_return_val_if_fail(val != NULL, -1);
@@ -569,9 +577,18 @@ pyg_flags_get_value(GType flag_type, PyObject *obj, gint *val)
*val = PyInt_AsLong(obj);
res = 0;
} else if (PyString_Check(obj)) {
+ GFlagsValue *info;
char *str = PyString_AsString(obj);
- GFlagsValue *info = g_flags_get_value_by_name(fclass, str);
+ if (flag_type != G_TYPE_NONE)
+ fclass = G_FLAGS_CLASS(g_type_class_ref(flag_type));
+ else {
+ PyErr_SetString(PyExc_TypeError, "could not convert string to flag because there is no GType associated to look up the value");
+ res = -1;
+ }
+ info = g_flags_get_value_by_name(fclass, str);
+ g_type_class_unref(fclass);
+
if (!info)
info = g_flags_get_value_by_nick(fclass, str);
if (info) {
@@ -587,6 +604,14 @@ pyg_flags_get_value(GType flag_type, PyObject *obj, gint *val)
len = PyTuple_Size(obj);
*val = 0;
res = 0;
+
+ if (flag_type != G_TYPE_NONE)
+ fclass = G_FLAGS_CLASS(g_type_class_ref(flag_type));
+ else {
+ PyErr_SetString(PyExc_TypeError, "could not convert string to flag because there is no GType associated to look up the value");
+ res = -1;
+ }
+
for (i = 0; i < len; i++) {
PyObject *item = PyTuple_GetItem(obj, i);
char *str = PyString_AsString(item);
@@ -602,12 +627,12 @@ pyg_flags_get_value(GType flag_type, PyObject *obj, gint *val)
break;
}
}
+ g_type_class_unref(fclass);
} else {
PyErr_SetString(PyExc_TypeError,
"flag values must be strings, ints or tuples");
res = -1;
}
- g_type_class_unref(fclass);
return res;
}
@@ -2211,13 +2236,30 @@ static PyMethodDef pygobject_functions[] = {
/* ----------------- Constant extraction ------------------------ */
+static char *
+pyg_constant_strip_prefix(gchar *name, const gchar *strip_prefix)
+{
+ gint prefix_len;
+ guint j;
+
+ prefix_len = strlen(strip_prefix);
+
+ /* strip off prefix from value name, while keeping it a valid
+ * identifier */
+ for (j = prefix_len; j >= 0; j--) {
+ if (g_ascii_isalpha(name[j]) || name[j] == '_') {
+ return &name[j];
+ }
+ }
+ return name;
+}
+
static void
pyg_enum_add_constants(PyObject *module, GType enum_type,
const gchar *strip_prefix)
{
GEnumClass *eclass;
- guint i, j;
- gint prefix_len;
+ guint i;
/* a more useful warning */
if (!G_TYPE_IS_ENUM(enum_type)) {
@@ -2226,22 +2268,15 @@ pyg_enum_add_constants(PyObject *module, GType enum_type,
}
g_return_if_fail (strip_prefix != NULL);
- prefix_len = strlen(strip_prefix);
eclass = G_ENUM_CLASS(g_type_class_ref(enum_type));
for (i = 0; i < eclass->n_values; i++) {
gchar *name = eclass->values[i].value_name;
gint value = eclass->values[i].value;
- /* strip off prefix from value name, while keeping it a valid
- * identifier */
- for (j = prefix_len; j >= 0; j--) {
- if (g_ascii_isalpha(name[j]) || name[j] == '_') {
- name = &name[j];
- break;
- }
- }
- PyModule_AddIntConstant(module, name, (long) value);
+ PyModule_AddIntConstant(module,
+ pyg_constant_strip_prefix(name, strip_prefix),
+ (long) value);
}
g_type_class_unref(eclass);
@@ -2252,8 +2287,7 @@ pyg_flags_add_constants(PyObject *module, GType flags_type,
const gchar *strip_prefix)
{
GFlagsClass *fclass;
- guint i, j;
- gint prefix_len;
+ guint i;
/* a more useful warning */
if (!G_TYPE_IS_FLAGS(flags_type)) {
@@ -2262,22 +2296,15 @@ pyg_flags_add_constants(PyObject *module, GType flags_type,
}
g_return_if_fail (strip_prefix != NULL);
- prefix_len = strlen(strip_prefix);
fclass = G_FLAGS_CLASS(g_type_class_ref(flags_type));
for (i = 0; i < fclass->n_values; i++) {
gchar *name = fclass->values[i].value_name;
guint value = fclass->values[i].value;
- /* strip off prefix from value name, while keeping it a valid
- * identifier */
- for (j = prefix_len; j >= 0; j--) {
- if (g_ascii_isalpha(name[j]) || name[j] == '_') {
- name = &name[j];
- break;
- }
- }
- PyModule_AddIntConstant(module, name, (long) value);
+ PyModule_AddIntConstant(module,
+ pyg_constant_strip_prefix(name, strip_prefix),
+ (long) value);
}
g_type_class_unref(fclass);
@@ -2337,6 +2364,8 @@ static struct _PyGObject_Functions functions = {
pyg_fatal_exceptions_notify_add,
pyg_fatal_exceptions_notify_remove,
+
+ pyg_constant_strip_prefix,
};
DL_EXPORT(void)
diff --git a/gobject/pygobject.h b/gobject/pygobject.h
index 95fd84e..f5d6d68 100644
--- a/gobject/pygobject.h
+++ b/gobject/pygobject.h
@@ -66,6 +66,9 @@ struct _PyGObject_Functions {
void (* fatal_exceptions_notify_add)(PyGFatalExceptionFunc func);
void (* fatal_exceptions_notify_remove)(PyGFatalExceptionFunc func);
+
+ gchar *(* constant_strip_prefix)(gchar *name,
+ const gchar *strip_prefix);
};
#ifndef _INSIDE_PYGOBJECT_
@@ -96,6 +99,7 @@ struct _PyGObject_Functions *_PyGObject_API;
#define pyg_flags_add_constants (_PyGObject_API->flags_add_constants)
#define pyg_fatal_exceptions_notify_add (_PyGObject_API->fatal_exceptions_notify_add)
#define pyg_fatal_exceptions_notify_remove (_PyGObject_API->fatal_exceptions_notify_remove)
+#define pyg_constant_strip_prefix (_PyGObject_API->constant_strip_prefix)
#define init_pygobject() { \
PyObject *gobject = PyImport_ImportModule("gobject"); \