diff --git a/gi/_glib/pyglib-python-compat.h b/gi/_glib/pyglib-python-compat.h
index 96a20c0..92bd5a3 100644
--- a/gi/_glib/pyglib-python-compat.h
+++ b/gi/_glib/pyglib-python-compat.h
@@ -225,6 +225,9 @@ PyTypeObject symbol = {                                 \
 #define PYGLIB_PyLongObject PyLongObject
 #define PYGLIB_PyLong_Type PyLong_Type
 
+#define PYGLIB_PyLong_FromUnsignedLong PyLong_FromUnsignedLong
+#define PYGLIB_PyLong_AsUnsignedLong(o) PyLong_AsUnsignedLongMask((PyObject*)(o))
+
 #define PYGLIB_PyBytes_FromString PyBytes_FromString
 #define PYGLIB_PyBytes_FromStringAndSize PyBytes_FromStringAndSize
 #define PYGLIB_PyBytes_Resize(o, len) _PyBytes_Resize(o, len)
diff --git a/gi/_gobject/gobjectmodule.c b/gi/_gobject/gobjectmodule.c
index 9fb7564..c49b412 100644
--- a/gi/_gobject/gobjectmodule.c
+++ b/gi/_gobject/gobjectmodule.c
@@ -572,7 +572,7 @@ create_property (const gchar  *prop_name,
 		return NULL;
 
 	    if (pyg_flags_get_value(prop_type, pydefault,
-				    (gint *)&default_value))
+				    &default_value))
 		return NULL;
 
 	    pspec = g_param_spec_flags (prop_name, nick, blurb,
diff --git a/gi/_gobject/pygflags.c b/gi/_gobject/pygflags.c
index bad32c6..b7c714c 100644
--- a/gi/_gobject/pygflags.c
+++ b/gi/_gobject/pygflags.c
@@ -44,7 +44,7 @@ pyg_flags_val_new(PyObject* subclass, GType gtype, PyObject *intval)
     Py_DECREF(args);
     if (!item)
 	return NULL;
-    ((PyGEnum*)item)->gtype = gtype;
+    ((PyGFlags*)item)->gtype = gtype;
 
     return item;
 }
@@ -70,7 +70,7 @@ pyg_flags_richcompare(PyGFlags *self, PyObject *other, int op)
 }
 
 static char *
-generate_repr(GType gtype, int value)
+generate_repr(GType gtype, guint value)
 {
     GFlagsClass *flags_class;
     char *retval = NULL, *tmp;
@@ -108,13 +108,13 @@ pyg_flags_repr(PyGFlags *self)
     char *tmp, *retval;
     PyObject *pyretval;
 
-    tmp = generate_repr(self->gtype, PYGLIB_PyLong_AS_LONG(self));
+    tmp = generate_repr(self->gtype, PYGLIB_PyLong_AsUnsignedLong(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>", PYGLIB_PyLong_AS_LONG(self),
+        retval = g_strdup_printf("<flags %ld of type %s>", PYGLIB_PyLong_AsUnsignedLong(self),
                                  g_type_name(self->gtype));
     g_free(tmp);
 
@@ -128,7 +128,7 @@ static PyObject *
 pyg_flags_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
 {
     static char *kwlist[] = { "value", NULL };
-    long value;
+    guint value;
     PyObject *pytc, *values, *ret, *pyint;
     GType gtype;
     GFlagsClass *eclass;
@@ -167,7 +167,7 @@ pyg_flags_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
 
     g_type_class_unref(eclass);
 
-    pyint = PYGLIB_PyLong_FromLong(value);
+    pyint = PYGLIB_PyLong_FromUnsignedLong(value);
     ret = PyDict_GetItem(values, pyint);
     if (!ret) {
         PyErr_Clear();
@@ -185,10 +185,13 @@ pyg_flags_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
 }
 
 PyObject*
-pyg_flags_from_gtype (GType gtype, int value)
+pyg_flags_from_gtype (GType gtype, guint value)
 {
     PyObject *pyclass, *values, *retval, *pyint;
 
+    if (PyErr_Occurred())
+        return PYGLIB_PyLong_FromUnsignedLong(0);
+
     g_return_val_if_fail(gtype != G_TYPE_INVALID, NULL);
 
     /* Get a wrapper class by:
@@ -202,11 +205,11 @@ pyg_flags_from_gtype (GType gtype, int value)
     if (!pyclass)
         pyclass = pyg_flags_add(NULL, g_type_name(gtype), NULL, gtype);
     if (!pyclass)
-	return PYGLIB_PyLong_FromLong(value);
+	return PYGLIB_PyLong_FromUnsignedLong(value);
 
     values = PyDict_GetItemString(((PyTypeObject *)pyclass)->tp_dict,
 				  "__flags_values__");
-    pyint = PYGLIB_PyLong_FromLong(value);
+    pyint = PYGLIB_PyLong_FromUnsignedLong(value);
     retval = PyDict_GetItem(values, pyint);
     if (!retval) {
 	PyErr_Clear();
@@ -277,8 +280,10 @@ pyg_flags_add (PyObject *   module,
     for (i = 0; i < eclass->n_values; i++) {
       PyObject *item, *intval;
       
-      intval = PYGLIB_PyLong_FromLong(eclass->values[i].value);
+      intval = PYGLIB_PyLong_FromUnsignedLong(eclass->values[i].value);
+      g_assert(PyErr_Occurred() == NULL);
       item = pyg_flags_val_new(stub, gtype, intval);
+      printf("pyg_flags_add %s value: %X pyval %lX item %lX\n", typename, eclass->values[i].value, PYGLIB_PyLong_AsUnsignedLong(intval), PYGLIB_PyLong_AsUnsignedLong(item));
       PyDict_SetItem(values, intval, item);
       Py_DECREF(intval);
 
@@ -312,7 +317,7 @@ pyg_flags_and(PyGFlags *a, PyGFlags *b)
 						       (PyObject*)b);
 
 	return pyg_flags_from_gtype(a->gtype,
-				    PYGLIB_PyLong_AS_LONG(a) & PYGLIB_PyLong_AS_LONG(b));
+				    PYGLIB_PyLong_AsUnsignedLong(a) & PYGLIB_PyLong_AsUnsignedLong(b));
 }
 
 static PyObject *
@@ -322,7 +327,7 @@ pyg_flags_or(PyGFlags *a, PyGFlags *b)
 		return PYGLIB_PyLong_Type.tp_as_number->nb_or((PyObject*)a,
 						      (PyObject*)b);
 
-	return pyg_flags_from_gtype(a->gtype, PYGLIB_PyLong_AS_LONG(a) | PYGLIB_PyLong_AS_LONG(b));
+	return pyg_flags_from_gtype(a->gtype, PYGLIB_PyLong_AsUnsignedLong(a) | PYGLIB_PyLong_AsUnsignedLong(b));
 }
 
 static PyObject *
@@ -333,8 +338,19 @@ pyg_flags_xor(PyGFlags *a, PyGFlags *b)
 						       (PyObject*)b);
 
 	return pyg_flags_from_gtype(a->gtype,
-				    PYGLIB_PyLong_AS_LONG(a) ^ PYGLIB_PyLong_AS_LONG(b));
+				    PYGLIB_PyLong_AsUnsignedLong(a) ^ PYGLIB_PyLong_AsUnsignedLong(b));
+
+}
+
+static PyObject *
+pyg_flags_int(PyGFlags *a)
+{
+        printf("pyg_flags_int XXXX\n");
+        abort();
+	if (!PyGFlags_Check(a))
+		return PYGLIB_PyLong_Type.tp_as_number->nb_int((PyObject*)a);
 
+	return pyg_flags_from_gtype(a->gtype, PYGLIB_PyLong_AsUnsignedLong(a));
 }
 
 static PyObject *
@@ -356,7 +372,7 @@ pyg_flags_get_first_value_name(PyGFlags *self, void *closure)
 
   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, PYGLIB_PyLong_AS_LONG(self));
+  flags_value = g_flags_get_first_value(flags_class, PYGLIB_PyLong_AsUnsignedLong(self));
   if (flags_value)
       retval = PYGLIB_PyUnicode_FromString(flags_value->value_name);
   else {
@@ -378,7 +394,7 @@ pyg_flags_get_first_value_nick(PyGFlags *self, void *closure)
   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, PYGLIB_PyLong_AS_LONG(self));
+  flags_value = g_flags_get_first_value(flags_class, PYGLIB_PyLong_AsUnsignedLong(self));
   if (flags_value)
       retval = PYGLIB_PyUnicode_FromString(flags_value->value_nick);
   else {
@@ -402,7 +418,7 @@ pyg_flags_get_value_names(PyGFlags *self, void *closure)
 
   retval = PyList_New(0);
   for (i = 0; i < flags_class->n_values; i++)
-      if ((PYGLIB_PyLong_AS_LONG(self) & flags_class->values[i].value) == flags_class->values[i].value)
+      if ((PYGLIB_PyLong_AsUnsignedLong(self) & flags_class->values[i].value) == flags_class->values[i].value)
 	  PyList_Append(retval, PYGLIB_PyUnicode_FromString(flags_class->values[i].value_name));
 
   g_type_class_unref(flags_class);
@@ -422,7 +438,7 @@ pyg_flags_get_value_nicks(PyGFlags *self, void *closure)
 
   retval = PyList_New(0);
   for (i = 0; i < flags_class->n_values; i++)
-      if ((PYGLIB_PyLong_AS_LONG(self) & flags_class->values[i].value) == flags_class->values[i].value)
+      if ((PYGLIB_PyLong_AsUnsignedLong(self) & flags_class->values[i].value) == flags_class->values[i].value)
 	  PyList_Append(retval, PYGLIB_PyUnicode_FromString(flags_class->values[i].value_nick));
 
   g_type_class_unref(flags_class);
@@ -458,6 +474,9 @@ static PyNumberMethods pyg_flags_as_number = {
 	(binaryfunc)pyg_flags_and,		/* nb_and */
 	(binaryfunc)pyg_flags_xor,		/* nb_xor */
 	(binaryfunc)pyg_flags_or,		/* nb_or */
+        0,					/* nb_coerce */
+        (unaryfunc)pyg_flags_int,		/* nb_int */
+        (unaryfunc)pyg_flags_int,		/* nb_long */
 };
 
 void
diff --git a/gi/_gobject/pygobject-private.h b/gi/_gobject/pygobject-private.h
index 0d1aca0..814783b 100644
--- a/gi/_gobject/pygobject-private.h
+++ b/gi/_gobject/pygobject-private.h
@@ -107,7 +107,7 @@ GType     pyg_type_from_object_strict (PyObject *obj, gboolean strict);
 GType     pyg_type_from_object (PyObject *obj);
 
 gint pyg_enum_get_value  (GType enum_type, PyObject *obj, gint *val);
-gint pyg_flags_get_value (GType flag_type, PyObject *obj, gint *val);
+gint pyg_flags_get_value (GType flag_type, PyObject *obj, guint *val);
 int pyg_pyobj_to_unichar_conv (PyObject* py_obj, void* ptr);
 
 typedef PyObject *(* fromvaluefunc)(const GValue *value);
@@ -194,7 +194,7 @@ extern PyObject * pyg_flags_add        (PyObject *   module,
 					const char * strip_prefix,
 					GType        gtype);
 extern PyObject * pyg_flags_from_gtype (GType        gtype,
-					int          value);
+					guint        value);
 
 /* pygenum */
 #define PyGEnum_Check(x) (PyObject_IsInstance((PyObject *)x, (PyObject *)&PyGEnum_Type) && g_type_is_a(((PyGFlags*)x)->gtype, G_TYPE_ENUM))
diff --git a/gi/_gobject/pygobject.h b/gi/_gobject/pygobject.h
index 92dc030..cfd88d7 100644
--- a/gi/_gobject/pygobject.h
+++ b/gi/_gobject/pygobject.h
@@ -103,7 +103,7 @@ struct _PyGObject_Functions {
     PyObject *(* type_wrapper_new)(GType type);
 
     gint (* enum_get_value)(GType enum_type, PyObject *obj, gint *val);
-    gint (* flags_get_value)(GType flag_type, PyObject *obj, gint *val);
+    gint (* flags_get_value)(GType flag_type, PyObject *obj, guint *val);
     void (* register_gtype_custom)(GType gtype,
 			    PyObject *(* from_func)(const GValue *value),
 			    int (* to_func)(GValue *value, PyObject *obj));
@@ -168,7 +168,7 @@ struct _PyGObject_Functions {
 			   const char *type_name_,
 			   const char *strip_prefix,
 			   GType gtype);
-    PyObject* (*flags_from_gtype)(GType gtype, int value);
+    PyObject* (*flags_from_gtype)(GType gtype, guint value);
 
     gboolean threads_enabled;
     int       (*enable_threads) (void);
diff --git a/gi/_gobject/pygtype.c b/gi/_gobject/pygtype.c
index 227178f..94014ff 100644
--- a/gi/_gobject/pygtype.c
+++ b/gi/_gobject/pygtype.c
@@ -538,7 +538,7 @@ pyg_enum_get_value(GType enum_type, PyObject *obj, gint *val)
  * Returns: 0 on success or -1 on failure
  */
 gint
-pyg_flags_get_value(GType flag_type, PyObject *obj, gint *val)
+pyg_flags_get_value(GType flag_type, PyObject *obj, guint *val)
 {
     GFlagsClass *fclass = NULL;
     gint res = -1;
@@ -548,7 +548,7 @@ pyg_flags_get_value(GType flag_type, PyObject *obj, gint *val)
 	*val = 0;
 	res = 0;
     } else if (PYGLIB_PyLong_Check(obj)) {
-	*val = PYGLIB_PyLong_AsLong(obj);
+	*val = PYGLIB_PyLong_AsUnsignedLong(obj);
 	res = 0;
     } else if (PyLong_Check(obj)) {
         *val = PyLong_AsLongLong(obj);
@@ -944,7 +944,7 @@ pyg_value_from_pyobject(GValue *value, PyObject *obj)
 	break;
     case G_TYPE_FLAGS:
 	{
-	    gint val = 0;
+	    guint val = 0;
 	    if (pyg_flags_get_value(G_VALUE_TYPE(value), obj, &val) < 0) {
 		PyErr_Clear();
 		return -1;
diff --git a/tests/test_overrides_gdk.py b/tests/test_overrides_gdk.py
index 070acb8..261e4b4 100644
--- a/tests/test_overrides_gdk.py
+++ b/tests/test_overrides_gdk.py
@@ -117,3 +117,14 @@ class TestGdk(unittest.TestCase):
 
         self.assertNotEqual(c, None)
         self.assertRaises(ValueError, Gdk.Cursor, 1, 2, 3)
+
+    def test_flags(self):
+        self.assertEqual(Gdk.ModifierType.META_MASK | 0, 0x10000000)
+        self.assertEqual(hex(Gdk.ModifierType.META_MASK), '0x10000000')
+        self.assertEqual(str(Gdk.ModifierType.META_MASK),
+                         '<flags GDK_META_MASK of type GdkModifierType>')
+
+        self.assertEqual(Gdk.ModifierType.RELEASE_MASK | 0, 0x40000000)
+        self.assertEqual(hex(Gdk.ModifierType.RELEASE_MASK), '0x40000000')
+        self.assertEqual(str(Gdk.ModifierType.RELEASE_MASK),
+                         '<flags GDK_RELEASE_MASK of type GdkModifierType>')
