[pygobject] Remove all Python 2 C code



commit f3cb961528e9e913811ed425bd096fd133880683
Author: Christoph Reiter <reiter christoph gmail com>
Date:   Wed Apr 15 20:15:10 2020 +0200

    Remove all Python 2 C code

 gi/gimodule.c            | 125 +++++++++++++++++-------------
 gi/pygboxed.c            |  16 ++--
 gi/pygenum.c             |  31 ++++----
 gi/pygenum.h             |   4 +-
 gi/pygflags.c            |  61 +++++++--------
 gi/pygflags.h            |   2 +-
 gi/pygi-argument.c       |  10 +--
 gi/pygi-array.c          |   9 +--
 gi/pygi-basictype.c      | 166 ++++++++++++----------------------------
 gi/pygi-boxed.c          |   2 +-
 gi/pygi-cache.c          |   3 +-
 gi/pygi-ccallback.c      |   2 +-
 gi/pygi-enum-marshal.c   |   9 +--
 gi/pygi-error.c          |   3 +-
 gi/pygi-foreign-cairo.c  |  45 +++++++----
 gi/pygi-info.c           |  28 +++----
 gi/pygi-invoke.c         |   6 --
 gi/pygi-object.c         |   7 +-
 gi/pygi-python-compat.h  | 192 -----------------------------------------------
 gi/pygi-repository.c     |   2 +-
 gi/pygi-resulttuple.c    |  26 +++----
 gi/pygi-source.c         |   1 -
 gi/pygi-struct-marshal.c |   3 +-
 gi/pygi-struct.c         |  12 +--
 gi/pygi-type.c           |  32 ++++----
 gi/pygi-util.c           |  61 ++-------------
 gi/pygi-util.h           |  39 ++++------
 gi/pygi-value.c          |  16 ++--
 gi/pyginterface.c        |   2 +-
 gi/pygobject-object.c    |  58 +++++++-------
 gi/pygobject-object.h    |   1 -
 gi/pygoptioncontext.c    |   4 +-
 gi/pygparamspec.c        |  12 +--
 gi/pygpointer.c          |   6 +-
 gi/pygspawn.c            |  20 ++---
 tests/testhelpermodule.c |  45 ++++++++---
 36 files changed, 382 insertions(+), 679 deletions(-)
---
diff --git a/gi/gimodule.c b/gi/gimodule.c
index cf3c5ea2..630135c4 100644
--- a/gi/gimodule.c
+++ b/gi/gimodule.c
@@ -52,7 +52,6 @@
 #include "pygi-property.h"
 #include "pygi-util.h"
 #include "gimodule.h"
-#include "pygi-python-compat.h"
 #include "pygi-basictype.h"
 
 PyObject *PyGIWarning;
@@ -442,7 +441,7 @@ pyg_param_spec_from_object (PyObject *tuple)
     }
 
     item = PyTuple_GetItem(tuple, val_length-1);
-    if (!PYGLIB_PyLong_Check(item)) {
+    if (!PyLong_Check (item)) {
        PyErr_SetString(PyExc_TypeError,
                        "last element in tuple must be an int");
        return NULL;
@@ -565,13 +564,13 @@ add_properties (GObjectClass *klass, PyObject *properties)
 
        /* values are of format (type,nick,blurb, type_specific_args, flags) */
 
-       if (!PYGLIB_PyUnicode_Check(key)) {
+       if (!PyUnicode_Check(key)) {
            PyErr_SetString(PyExc_TypeError,
                            "__gproperties__ keys must be strings");
            ret = FALSE;
            break;
        }
-       prop_name = PYGLIB_PyUnicode_AsString (key);
+       prop_name = PyUnicode_AsUTF8 (key);
 
        if (!PyTuple_Check(value)) {
            PyErr_SetString(PyExc_TypeError,
@@ -604,7 +603,7 @@ add_properties (GObjectClass *klass, PyObject *properties)
            break;
        }
        item = PyTuple_GetItem(value, val_length-1);
-       if (!PYGLIB_PyLong_Check(item)) {
+       if (!PyLong_Check (item)) {
            PyErr_SetString(PyExc_TypeError,
                "last element in __gproperties__ value tuple must be an int");
            ret = FALSE;
@@ -627,14 +626,14 @@ add_properties (GObjectClass *klass, PyObject *properties)
             PyObject *type, *pvalue, *traceback;
            ret = FALSE;
             PyErr_Fetch(&type, &pvalue, &traceback);
-            if (PYGLIB_PyUnicode_Check(pvalue)) {
+            if (PyUnicode_Check(pvalue)) {
                 char msg[256];
                 g_snprintf(msg, 256,
                           "%s (while registering property '%s' for GType '%s')",
-               PYGLIB_PyUnicode_AsString(pvalue),
+               PyUnicode_AsUTF8 (pvalue),
                           prop_name, G_OBJECT_CLASS_NAME(klass));
                 Py_DECREF(pvalue);
-                value = PYGLIB_PyUnicode_FromString(msg);
+                value = PyUnicode_FromString (msg);
             }
             PyErr_Restore(type, pvalue, traceback);
            break;
@@ -682,7 +681,7 @@ _pyg_signal_accumulator(GSignalInvocationHint *ihint,
 
     state = PyGILState_Ensure();
     if (ihint->detail)
-        py_detail = PYGLIB_PyUnicode_FromString(g_quark_to_string(ihint->detail));
+        py_detail = PyUnicode_FromString (g_quark_to_string(ihint->detail));
     else {
         Py_INCREF(Py_None);
         py_detail = Py_None;
@@ -826,17 +825,17 @@ add_signals (GObjectClass *klass, PyObject *signals)
        const gchar *signal_name;
         gchar *signal_name_canon, *c;
 
-       if (!PYGLIB_PyUnicode_Check(key)) {
+       if (!PyUnicode_Check(key)) {
            PyErr_SetString(PyExc_TypeError,
                            "__gsignals__ keys must be strings");
            ret = FALSE;
            break;
        }
-       signal_name = PYGLIB_PyUnicode_AsString (key);
+       signal_name = PyUnicode_AsUTF8 (key);
 
        if (value == Py_None ||
-           (PYGLIB_PyUnicode_Check(value) &&
-            !strcmp(PYGLIB_PyUnicode_AsString(value), "override")))
+           (PyUnicode_Check(value) &&
+            !strcmp(PyUnicode_AsUTF8 (value), "override")))
         {
               /* canonicalize signal name, replacing '-' with '_' */
             signal_name_canon = g_strdup(signal_name);
@@ -1215,8 +1214,8 @@ get_type_name_for_class(PyTypeObject *class)
        g_free(type_name);
        g_snprintf(name_serial_str, 16, "-v%i", name_serial);
        module = PyObject_GetAttrString((PyObject *)class, "__module__");
-       if (module && PYGLIB_PyUnicode_Check(module)) {
-           type_name = g_strconcat(PYGLIB_PyUnicode_AsString(module), ".",
+       if (module && PyUnicode_Check (module)) {
+           type_name = g_strconcat(PyUnicode_AsUTF8 (module), ".",
                                    class->tp_name,
                                    name_serial > 1 ? name_serial_str : NULL,
                                    NULL);
@@ -1934,7 +1933,7 @@ pyg_channel_read(PyObject* self, PyObject *args, PyObject *kwargs)
     }
        
     if (max_count == 0)
-        return PYGLIB_PyBytes_FromString("");
+        return PyBytes_FromString ("");
 
     iochannel = pyg_boxed_get (py_iochannel, GIOChannel);
 
@@ -1953,16 +1952,16 @@ pyg_channel_read(PyObject* self, PyObject *args, PyObject *kwargs)
         }
        
        if ( ret_obj == NULL ) {
-           ret_obj = PYGLIB_PyBytes_FromStringAndSize((char *)NULL, buf_size);
+           ret_obj = PyBytes_FromStringAndSize ((char *)NULL, buf_size);
            if (ret_obj == NULL)
                goto failure;
        }
-       else if (buf_size + total_read > (gsize)PYGLIB_PyBytes_Size(ret_obj)) {
-           if (PYGLIB_PyBytes_Resize(&ret_obj, buf_size + total_read) == -1)
+       else if (buf_size + total_read > (gsize)PyBytes_Size (ret_obj)) {
+           if (_PyBytes_Resize (&ret_obj, buf_size + total_read) == -1)
                goto failure;
        }
        
-        buf = PYGLIB_PyBytes_AsString(ret_obj) + total_read;
+        buf = PyBytes_AsString (ret_obj) + total_read;
 
         Py_BEGIN_ALLOW_THREADS;
         status = g_io_channel_read_chars (iochannel, buf, buf_size, &single_read, &error);
@@ -1974,8 +1973,8 @@ pyg_channel_read(PyObject* self, PyObject *args, PyObject *kwargs)
        total_read += single_read;
     }
        
-    if ( total_read != (gsize)PYGLIB_PyBytes_Size(ret_obj) ) {
-       if (PYGLIB_PyBytes_Resize(&ret_obj, total_read) == -1)
+    if ( total_read != (gsize)PyBytes_Size (ret_obj) ) {
+       if (_PyBytes_Resize (&ret_obj, total_read) == -1)
            goto failure;
     }
 
@@ -2089,7 +2088,7 @@ pyg_add_emission_hook(PyGObject *self, PyObject *args)
     if (!g_signal_parse_name(name, gtype, &sigid, &detail, TRUE)) {
        repr = PyObject_Repr((PyObject*)self);
        PyErr_Format(PyExc_TypeError, "%s: unknown signal name: %s",
-                       PYGLIB_PyUnicode_AsString(repr),
+                       PyUnicode_AsUTF8 (repr),
                     name);
        Py_DECREF(repr);
        return NULL;
@@ -2492,9 +2491,30 @@ pygi_register_version_tuples(PyObject *d)
     return 0;
 }
 
-PYGLIB_MODULE_START(_gi, "_gi")
-{
+static struct PyModuleDef __gimodule = {
+    PyModuleDef_HEAD_INIT,
+    "_gi",
+    NULL,
+    -1,
+    _gi_functions,
+    NULL,
+    NULL,
+    NULL,
+    NULL
+};
+
+#ifdef __GNUC__
+#define PYGI_MODINIT_FUNC __attribute__((visibility("default"))) PyMODINIT_FUNC
+#else
+#define PYGI_MODINIT_FUNC PyMODINIT_FUNC
+#endif
+
+PYGI_MODINIT_FUNC PyInit__gi(void);
+
+PYGI_MODINIT_FUNC PyInit__gi(void) {
+    PyObject *module;
     PyObject *api;
+    module = PyModule_Create(&__gimodule);
     PyObject *module_dict = PyModule_GetDict (module);
 
     /* Always enable Python threads since we cannot predict which GI repositories
@@ -2507,57 +2527,57 @@ PYGLIB_MODULE_START(_gi, "_gi")
     PyModule_AddStringConstant(module, "__package__", "gi._gi");
 
     if (pygi_foreign_init () < 0)
-        return PYGLIB_MODULE_ERROR_RETURN;
+        return NULL;
     if (pygi_error_register_types (module) < 0)
-        return PYGLIB_MODULE_ERROR_RETURN;
+        return NULL;
     if (pygi_repository_register_types (module) < 0)
-        return PYGLIB_MODULE_ERROR_RETURN;
+        return NULL;
     if (pygi_info_register_types (module) < 0)
-        return PYGLIB_MODULE_ERROR_RETURN;
+        return NULL;
     if (pygi_type_register_types (module_dict) < 0)
-        return PYGLIB_MODULE_ERROR_RETURN;
+        return NULL;
     if (pygi_pointer_register_types (module_dict) < 0)
-        return PYGLIB_MODULE_ERROR_RETURN;
+        return NULL;
     if (pygi_struct_register_types (module) < 0)
-        return PYGLIB_MODULE_ERROR_RETURN;
+        return NULL;
     if (pygi_gboxed_register_types (module_dict) < 0)
-        return PYGLIB_MODULE_ERROR_RETURN;
+        return NULL;
     if (pygi_boxed_register_types (module) < 0)
-        return PYGLIB_MODULE_ERROR_RETURN;
+        return NULL;
     if (pygi_ccallback_register_types (module) < 0)
-        return PYGLIB_MODULE_ERROR_RETURN;
+        return NULL;
     if (pygi_resulttuple_register_types (module) < 0)
-        return PYGLIB_MODULE_ERROR_RETURN;
+        return NULL;
 
     if (pygi_spawn_register_types (module_dict) < 0)
-        return PYGLIB_MODULE_ERROR_RETURN;
+        return NULL;
     if (pygi_option_context_register_types (module_dict) < 0)
-        return PYGLIB_MODULE_ERROR_RETURN;
+        return NULL;
     if (pygi_option_group_register_types (module_dict) < 0)
-        return PYGLIB_MODULE_ERROR_RETURN;
+        return NULL;
 
     if (pygi_register_api (module_dict) < 0)
-        return PYGLIB_MODULE_ERROR_RETURN;
+        return NULL;
     if (pygi_register_constants (module) < 0)
-        return PYGLIB_MODULE_ERROR_RETURN;
+        return NULL;
     if (pygi_register_version_tuples (module_dict) < 0)
-        return PYGLIB_MODULE_ERROR_RETURN;
+        return NULL;
     if (pygi_register_warnings (module_dict) < 0)
-        return PYGLIB_MODULE_ERROR_RETURN;
+        return NULL;
     if (pyi_object_register_types (module_dict) < 0)
-        return PYGLIB_MODULE_ERROR_RETURN;
+        return NULL;
     if (pygi_interface_register_types (module_dict) < 0)
-        return PYGLIB_MODULE_ERROR_RETURN;
+        return NULL;
     if (pygi_paramspec_register_types (module_dict) < 0)
-        return PYGLIB_MODULE_ERROR_RETURN;
+        return NULL;
     if (pygi_enum_register_types (module_dict) < 0)
-        return PYGLIB_MODULE_ERROR_RETURN;
+        return NULL;
     if (pygi_flags_register_types (module_dict) < 0)
-        return PYGLIB_MODULE_ERROR_RETURN;
+        return NULL;
 
     PyGIWarning = PyErr_NewException ("gi.PyGIWarning", PyExc_Warning, NULL);
     if (PyGIWarning == NULL)
-        return PYGLIB_MODULE_ERROR_RETURN;
+        return NULL;
 
     PyGIDeprecationWarning = PyErr_NewException("gi.PyGIDeprecationWarning",
                                                 PyExc_DeprecationWarning, NULL);
@@ -2575,8 +2595,9 @@ PYGLIB_MODULE_START(_gi, "_gi")
 
     api = PyCapsule_New ( (void *) &CAPI, "gi._API", NULL);
     if (api == NULL) {
-        return PYGLIB_MODULE_ERROR_RETURN;
+        return NULL;
     }
     PyModule_AddObject (module, "_API", api);
-}
-PYGLIB_MODULE_END
+
+    return module;
+}
\ No newline at end of file
diff --git a/gi/pygboxed.c b/gi/pygboxed.c
index 1f355be3..8f3d0497 100644
--- a/gi/pygboxed.c
+++ b/gi/pygboxed.c
@@ -58,10 +58,10 @@ gboxed_richcompare(PyObject *self, PyObject *other, int op)
     }
 }
 
-static PYGLIB_Py_hash_t
+static Py_hash_t
 gboxed_hash(PyGBoxed *self)
 {
-    return PYGLIB_Py_hash_t_FromVoidPtr (pyg_boxed_get_ptr (self));
+    return (Py_hash_t)(gintptr)(pyg_boxed_get_ptr (self));
 }
 
 static PyObject *
@@ -74,12 +74,12 @@ gboxed_repr(PyGBoxed *boxed)
     if (module == NULL)
         return NULL;
 
-    if (!PYGLIB_PyUnicode_Check (module)) {
+    if (!PyUnicode_Check (module)) {
         Py_DECREF (module);
         return NULL;
     }
 
-    module_str = PYGLIB_PyUnicode_AsString (module);
+    module_str = PyUnicode_AsUTF8 (module);
     namespace = g_strrstr (module_str, ".");
     if (namespace == NULL) {
         namespace = module_str;
@@ -87,10 +87,10 @@ gboxed_repr(PyGBoxed *boxed)
         namespace += 1;
     }
 
-    repr = PYGLIB_PyUnicode_FromFormat ("<%s.%s object at %p (%s at %p)>",
-                                        namespace, Py_TYPE (self)->tp_name,
-                                        self, g_type_name (boxed->gtype),
-                                        pyg_boxed_get_ptr (boxed));
+    repr = PyUnicode_FromFormat ("<%s.%s object at %p (%s at %p)>",
+                                 namespace, Py_TYPE (self)->tp_name,
+                                 self, g_type_name (boxed->gtype),
+                                 pyg_boxed_get_ptr (boxed));
     Py_DECREF (module);
     return repr;
 }
diff --git a/gi/pygenum.c b/gi/pygenum.c
index 69f1cd70..7b3351d3 100644
--- a/gi/pygenum.c
+++ b/gi/pygenum.c
@@ -21,7 +21,6 @@
 
 #include <config.h>
 
-#include "pygi-python-compat.h"
 #include "pygi-type.h"
 #include "pygi-util.h"
 #include "pygi-type.h"
@@ -38,7 +37,7 @@ pyg_enum_val_new(PyObject* subclass, GType gtype, PyObject *intval)
 {
     PyObject *args, *item;
     args = Py_BuildValue("(O)", intval);
-    item =  (&PYGLIB_PyLong_Type)->tp_new((PyTypeObject*)subclass, args, NULL);
+    item =  (&PyLong_Type)->tp_new((PyTypeObject*)subclass, args, NULL);
     Py_DECREF(args);
     if (!item)
        return NULL;
@@ -52,7 +51,7 @@ pyg_enum_richcompare(PyGEnum *self, PyObject *other, int op)
 {
     static char warning[256];
 
-    if (!PYGLIB_PyLong_Check(other)) {
+    if (!PyLong_Check (other)) {
        Py_INCREF(Py_NotImplemented);
        return Py_NotImplemented;
     }
@@ -82,7 +81,7 @@ pyg_enum_repr(PyGEnum *self)
     if (module == NULL)
         return NULL;
 
-    if (!PYGLIB_PyUnicode_Check (module)) {
+    if (!PyUnicode_Check (module)) {
         Py_DECREF (module);
         return NULL;
     }
@@ -90,12 +89,12 @@ pyg_enum_repr(PyGEnum *self)
     enum_class = g_type_class_ref(self->gtype);
     g_assert(G_IS_ENUM_CLASS(enum_class));
 
-    l = PYGLIB_PyLong_AS_LONG(self);
+    l = PyLong_AS_LONG ((PyObject*)self);
     for (index = 0; index < enum_class->n_values; index++)
         if (l == enum_class->values[index].value)
             break;
 
-    module_str = PYGLIB_PyUnicode_AsString (module);
+    module_str = PyUnicode_AsUTF8 (module);
     namespace = g_strrstr (module_str, ".");
     if (namespace == NULL) {
         namespace = module_str;
@@ -108,12 +107,12 @@ pyg_enum_repr(PyGEnum *self)
         sprintf(tmp, "<enum %s of type %s.%s>", value,
                 namespace, Py_TYPE (self)->tp_name);
     else
-        sprintf(tmp, "<enum %ld of type %s.%s>", PYGLIB_PyLong_AS_LONG(self),
+        sprintf(tmp, "<enum %ld of type %s.%s>", PyLong_AS_LONG ((PyObject*)self),
                 namespace, Py_TYPE (self)->tp_name);
     Py_DECREF (module);
     g_type_class_unref(enum_class);
 
-    return PYGLIB_PyUnicode_FromString(tmp);
+    return PyUnicode_FromString (tmp);
 }
 
 static PyObject *
@@ -171,7 +170,7 @@ pyg_enum_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
 
     g_type_class_unref(eclass);
 
-    intvalue = PYGLIB_PyLong_FromLong(value);
+    intvalue = PyLong_FromLong(value);
     ret = PyDict_GetItem(values, intvalue);
     Py_DECREF(intvalue);
     Py_DECREF(values);
@@ -201,11 +200,11 @@ pyg_enum_from_gtype (GType gtype, int value)
     if (!pyclass)
         pyclass = pyg_enum_add(NULL, g_type_name(gtype), NULL, gtype);
     if (!pyclass)
-       return PYGLIB_PyLong_FromLong(value);
+       return PyLong_FromLong(value);
 
     values = PyDict_GetItemString(((PyTypeObject *)pyclass)->tp_dict,
                                  "__enum_values__");
-    intvalue = PYGLIB_PyLong_FromLong(value);
+    intvalue = PyLong_FromLong(value);
     retval = PyDict_GetItem(values, intvalue);
     if (retval) {
        Py_INCREF(retval);
@@ -262,7 +261,7 @@ pyg_enum_add (PyObject *   module,
     if (module)
        PyDict_SetItemString(((PyTypeObject *)stub)->tp_dict,
                             "__module__",
-                            PYGLIB_PyUnicode_FromString(PyModule_GetName(module)));
+                            PyUnicode_FromString (PyModule_GetName(module)));
 
     g_type_set_qdata(gtype, pygenum_class_key, stub);
 
@@ -283,7 +282,7 @@ pyg_enum_add (PyObject *   module,
     for (i = 0; i < eclass->n_values; i++) {
        PyObject *item, *intval;
       
-        intval = PYGLIB_PyLong_FromLong(eclass->values[i].value);
+        intval = PyLong_FromLong(eclass->values[i].value);
        item = pyg_enum_val_new(stub, gtype, intval);
        PyDict_SetItem(values, intval, item);
         Py_DECREF(intval);
@@ -315,7 +314,7 @@ pyg_enum_reduce(PyObject *self, PyObject *args)
     if (!PyArg_ParseTuple(args, ":GEnum.__reduce__"))
         return NULL;
 
-    return Py_BuildValue("(O(i)O)", Py_TYPE(self), PYGLIB_PyLong_AsLong(self),
+    return Py_BuildValue("(O(i)O)", Py_TYPE(self), PyLong_AsLong (self),
                          PyObject_GetAttrString(self, "__dict__"));
 }
 
@@ -384,9 +383,9 @@ pygi_enum_register_types(PyObject *d)
 {
     pygenum_class_key        = g_quark_from_static_string("PyGEnum::class");
 
-    PyGEnum_Type.tp_base = &PYGLIB_PyLong_Type;
+    PyGEnum_Type.tp_base = &PyLong_Type;
     PyGEnum_Type.tp_new = pyg_enum_new;
-    PyGEnum_Type.tp_hash = PYGLIB_PyLong_Type.tp_hash;
+    PyGEnum_Type.tp_hash = PyLong_Type.tp_hash;
     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;
diff --git a/gi/pygenum.h b/gi/pygenum.h
index 5be979a2..5b46d356 100644
--- a/gi/pygenum.h
+++ b/gi/pygenum.h
@@ -20,14 +20,12 @@
 #ifndef __PYGOBJECT_ENUM_H__ 
 #define __PYGOBJECT_ENUM_H__
 
-#include "pygi-python-compat.h"
-
 extern GQuark pygenum_class_key;
 
 #define PyGEnum_Check(x) (PyObject_IsInstance((PyObject *)x, (PyObject *)&PyGEnum_Type) && 
g_type_is_a(((PyGFlags*)x)->gtype, G_TYPE_ENUM))
 
 typedef struct {
-    PYGLIB_PyLongObject parent;
+    PyLongObject parent;
     int zero_pad; /* must always be 0 */
     GType gtype;
 } PyGEnum;
diff --git a/gi/pygflags.c b/gi/pygflags.c
index 01e8a55e..d93fa362 100644
--- a/gi/pygflags.c
+++ b/gi/pygflags.c
@@ -37,7 +37,7 @@ pyg_flags_val_new(PyObject* subclass, GType gtype, PyObject *intval)
     PyObject *args, *item;
     args = Py_BuildValue("(O)", intval);
     g_assert(PyObject_IsSubclass(subclass, (PyObject*) &PyGFlags_Type));
-    item = PYGLIB_PyLong_Type.tp_new((PyTypeObject*)subclass, args, NULL);
+    item = PyLong_Type.tp_new((PyTypeObject*)subclass, args, NULL);
     Py_DECREF(args);
     if (!item)
        return NULL;
@@ -51,7 +51,7 @@ pyg_flags_richcompare(PyGFlags *self, PyObject *other, int op)
 {
     static char warning[256];
 
-    if (!PYGLIB_PyLong_Check(other)) {
+    if (!PyLong_Check (other)) {
         Py_INCREF(Py_NotImplemented);
         return Py_NotImplemented;
     }
@@ -105,18 +105,18 @@ pyg_flags_repr(PyGFlags *self)
     char *tmp, *retval, *module_str, *namespace;
     PyObject *pyretval, *module;
 
-    tmp = generate_repr(self->gtype, (guint)PYGLIB_PyLong_AsUnsignedLong(self));
+    tmp = generate_repr(self->gtype, (guint)PyLong_AsUnsignedLongMask ((PyObject*)self));
 
     module = PyObject_GetAttrString ((PyObject *)self, "__module__");
     if (module == NULL)
         return NULL;
 
-    if (!PYGLIB_PyUnicode_Check (module)) {
+    if (!PyUnicode_Check (module)) {
         Py_DECREF (module);
         return NULL;
     }
 
-    module_str = PYGLIB_PyUnicode_AsString (module);
+    module_str = PyUnicode_AsUTF8 (module);
     namespace = g_strrstr (module_str, ".");
     if (namespace == NULL) {
         namespace = module_str;
@@ -129,12 +129,12 @@ pyg_flags_repr(PyGFlags *self)
                                  namespace, Py_TYPE (self)->tp_name);
     else
         retval = g_strdup_printf("<flags %ld of type %s.%s>",
-                                 PYGLIB_PyLong_AsUnsignedLong (self),
+                                 PyLong_AsUnsignedLongMask ((PyObject*)self),
                                  namespace, Py_TYPE (self)->tp_name);
     g_free(tmp);
     Py_DECREF (module);
 
-    pyretval = PYGLIB_PyUnicode_FromString(retval);
+    pyretval = PyUnicode_FromString (retval);
     g_free(retval);
 
     return pyretval;
@@ -183,7 +183,7 @@ pyg_flags_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
 
     g_type_class_unref(eclass);
 
-    pyint = PYGLIB_PyLong_FromUnsignedLong(value);
+    pyint = PyLong_FromUnsignedLong (value);
     ret = PyDict_GetItem(values, pyint);
     if (!ret) {
         PyErr_Clear();
@@ -206,7 +206,7 @@ pyg_flags_from_gtype (GType gtype, guint value)
     PyObject *pyclass, *values, *retval, *pyint;
 
     if (PyErr_Occurred())
-        return PYGLIB_PyLong_FromUnsignedLong(0);
+        return PyLong_FromUnsignedLong (0);
 
     g_return_val_if_fail(gtype != G_TYPE_INVALID, NULL);
 
@@ -221,11 +221,11 @@ pyg_flags_from_gtype (GType gtype, guint value)
     if (!pyclass)
         pyclass = pyg_flags_add(NULL, g_type_name(gtype), NULL, gtype);
     if (!pyclass)
-       return PYGLIB_PyLong_FromUnsignedLong(value);
+       return PyLong_FromUnsignedLong (value);
 
     values = PyDict_GetItemString(((PyTypeObject *)pyclass)->tp_dict,
                                  "__flags_values__");
-    pyint = PYGLIB_PyLong_FromUnsignedLong(value);
+    pyint = PyLong_FromUnsignedLong (value);
     retval = PyDict_GetItem(values, pyint);
     if (!retval) {
        PyErr_Clear();
@@ -283,7 +283,7 @@ pyg_flags_add (PyObject *   module,
     if (module) {
         PyDict_SetItemString(((PyTypeObject *)stub)->tp_dict,
                              "__module__",
-                             PYGLIB_PyUnicode_FromString(PyModule_GetName(module)));
+                             PyUnicode_FromString (PyModule_GetName(module)));
 
           /* Add it to the module name space */
         PyModule_AddObject(module, (char*)typename, stub);
@@ -302,7 +302,7 @@ pyg_flags_add (PyObject *   module,
     for (i = 0; i < eclass->n_values; i++) {
       PyObject *item, *intval;
       
-      intval = PYGLIB_PyLong_FromUnsignedLong(eclass->values[i].value);
+      intval = PyLong_FromUnsignedLong (eclass->values[i].value);
       g_assert(PyErr_Occurred() == NULL);
       item = pyg_flags_val_new(stub, gtype, intval);
       PyDict_SetItem(values, intval, item);
@@ -334,32 +334,32 @@ static PyObject *
 pyg_flags_and(PyGFlags *a, PyGFlags *b)
 {
        if (!PyGFlags_Check(a) || !PyGFlags_Check(b))
-               return PYGLIB_PyLong_Type.tp_as_number->nb_and((PyObject*)a,
+               return PyLong_Type.tp_as_number->nb_and((PyObject*)a,
                                                       (PyObject*)b);
 
        return pyg_flags_from_gtype(a->gtype,
-                                   (guint)(PYGLIB_PyLong_AsUnsignedLong(a) & 
PYGLIB_PyLong_AsUnsignedLong(b)));
+                                   (guint)(PyLong_AsUnsignedLongMask ((PyObject*)a) & 
PyLong_AsUnsignedLongMask ((PyObject*)b)));
 }
 
 static PyObject *
 pyg_flags_or(PyGFlags *a, PyGFlags *b)
 {
        if (!PyGFlags_Check(a) || !PyGFlags_Check(b))
-               return PYGLIB_PyLong_Type.tp_as_number->nb_or((PyObject*)a,
+               return PyLong_Type.tp_as_number->nb_or((PyObject*)a,
                                                      (PyObject*)b);
 
-       return pyg_flags_from_gtype(a->gtype, (guint)(PYGLIB_PyLong_AsUnsignedLong(a) | 
PYGLIB_PyLong_AsUnsignedLong(b)));
+       return pyg_flags_from_gtype(a->gtype, (guint)(PyLong_AsUnsignedLongMask ((PyObject*)a) | 
PyLong_AsUnsignedLongMask ((PyObject*)b)));
 }
 
 static PyObject *
 pyg_flags_xor(PyGFlags *a, PyGFlags *b)
 {
        if (!PyGFlags_Check(a) || !PyGFlags_Check(b))
-               return PYGLIB_PyLong_Type.tp_as_number->nb_xor((PyObject*)a,
+               return PyLong_Type.tp_as_number->nb_xor((PyObject*)a,
                                                       (PyObject*)b);
 
        return pyg_flags_from_gtype(a->gtype,
-                                   (guint)(PYGLIB_PyLong_AsUnsignedLong(a) ^ 
PYGLIB_PyLong_AsUnsignedLong(b)));
+                                   (guint)(PyLong_AsUnsignedLongMask ((PyObject*)a) ^ 
PyLong_AsUnsignedLongMask ((PyObject*)b)));
 
 }
 
@@ -382,9 +382,9 @@ 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, (guint)PYGLIB_PyLong_AsUnsignedLong(self));
+  flags_value = g_flags_get_first_value(flags_class, (guint)PyLong_AsUnsignedLongMask ((PyObject*)self));
   if (flags_value)
-      retval = PYGLIB_PyUnicode_FromString(flags_value->value_name);
+      retval = PyUnicode_FromString (flags_value->value_name);
   else {
       retval = Py_None;
       Py_INCREF(Py_None);
@@ -404,9 +404,9 @@ 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, (guint)PYGLIB_PyLong_AsUnsignedLong(self));
+  flags_value = g_flags_get_first_value(flags_class, (guint)PyLong_AsUnsignedLongMask ((PyObject*)self));
   if (flags_value)
-      retval = PYGLIB_PyUnicode_FromString(flags_value->value_nick);
+      retval = PyUnicode_FromString (flags_value->value_nick);
   else {
       retval = Py_None;
       Py_INCREF(Py_None);
@@ -430,8 +430,8 @@ pyg_flags_get_value_names(PyGFlags *self, void *closure)
   for (i = 0; i < flags_class->n_values; i++) {
       PyObject *value_name;
 
-      if ((PYGLIB_PyLong_AsUnsignedLong (self) & flags_class->values[i].value) == 
flags_class->values[i].value) {
-        value_name = PYGLIB_PyUnicode_FromString (flags_class->values[i].value_name);
+      if ((PyLong_AsUnsignedLongMask ((PyObject*)self) & flags_class->values[i].value) == 
flags_class->values[i].value) {
+        value_name = PyUnicode_FromString (flags_class->values[i].value_name);
         PyList_Append (retval, value_name);
         Py_DECREF (value_name);
       }
@@ -454,8 +454,8 @@ 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_AsUnsignedLong(self) & flags_class->values[i].value) == 
flags_class->values[i].value) {
-         PyObject *py_nick = PYGLIB_PyUnicode_FromString(flags_class->values[i].value_nick);
+      if ((PyLong_AsUnsignedLongMask ((PyObject*)self) & flags_class->values[i].value) == 
flags_class->values[i].value) {
+         PyObject *py_nick = PyUnicode_FromString (flags_class->values[i].value_nick);
          PyList_Append(retval, py_nick);
          Py_DECREF (py_nick);
       }
@@ -479,9 +479,6 @@ static PyNumberMethods pyg_flags_as_number = {
        (binaryfunc)pyg_flags_warn,             /* nb_multiply */
        (binaryfunc)pyg_flags_warn,             /* nb_divide */
        (binaryfunc)pyg_flags_warn,             /* nb_remainder */
-#if PY_VERSION_HEX < 0x03000000
-        (binaryfunc)pyg_flags_warn,            /* nb_divmod */
-#endif
        (ternaryfunc)pyg_flags_warn,            /* nb_power */
        0,                                      /* nb_negative */
        0,                                      /* nb_positive */
@@ -503,9 +500,9 @@ pygi_flags_register_types(PyObject *d)
 {
     pygflags_class_key = g_quark_from_static_string("PyGFlags::class");
 
-    PyGFlags_Type.tp_base = &PYGLIB_PyLong_Type;
+    PyGFlags_Type.tp_base = &PyLong_Type;
     PyGFlags_Type.tp_new = pyg_flags_new;
-    PyGFlags_Type.tp_hash = PYGLIB_PyLong_Type.tp_hash;
+    PyGFlags_Type.tp_hash = PyLong_Type.tp_hash;
     PyGFlags_Type.tp_repr = (reprfunc)pyg_flags_repr;
     PyGFlags_Type.tp_as_number = &pyg_flags_as_number;
     PyGFlags_Type.tp_str = (reprfunc)pyg_flags_repr;
diff --git a/gi/pygflags.h b/gi/pygflags.h
index 7a8b2c71..7cff7f2f 100644
--- a/gi/pygflags.h
+++ b/gi/pygflags.h
@@ -23,7 +23,7 @@
 extern GQuark pygflags_class_key;
 
 typedef struct {
-    PYGLIB_PyLongObject parent;
+    PyLongObject parent;
     int zero_pad; /* must always be 0 */
     GType gtype;
 } PyGFlags;
diff --git a/gi/pygi-argument.c b/gi/pygi-argument.c
index 76ce8b4b..9b98d9c2 100644
--- a/gi/pygi-argument.c
+++ b/gi/pygi-argument.c
@@ -29,7 +29,6 @@
 #include <pygenum.h>
 #include <pygflags.h>
 
-#include "pygi-python-compat.h"
 #include "pygi-argument.h"
 #include "pygi-info.h"
 #include "pygi-value.h"
@@ -360,9 +359,6 @@ _pygi_argument_from_object (PyObject   *object,
 
             /* Note, strings are sequences, but we cannot accept them here */
             if (!PySequence_Check (object) || 
-#if PY_VERSION_HEX < 0x03000000
-                PyString_Check (object) || 
-#endif
                 PyUnicode_Check (object)) {
                 PyErr_SetString (PyExc_TypeError, "expected sequence");
                 break;
@@ -392,9 +388,9 @@ _pygi_argument_from_object (PyObject   *object,
             }
 
             if (g_type_info_get_tag (item_type_info) == GI_TYPE_TAG_UINT8 &&
-                PYGLIB_PyBytes_Check(object)) {
+                PyBytes_Check (object)) {
 
-                memcpy(array->data, PYGLIB_PyBytes_AsString(object), length);
+                memcpy(array->data, PyBytes_AsString (object), length);
                 array->len = length;
                 goto array_success;
             }
@@ -748,7 +744,7 @@ _pygi_argument_to_object (GIArgument  *arg,
 
             if (item_type_tag == GI_TYPE_TAG_UINT8) {
                 /* Return as a byte array */
-                object = PYGLIB_PyBytes_FromStringAndSize (array->data, array->len);
+                object = PyBytes_FromStringAndSize (array->data, array->len);
             } else {
                 object = PyList_New (array->len);
                 if (object == NULL) {
diff --git a/gi/pygi-array.c b/gi/pygi-array.c
index 890e7c57..735bb419 100644
--- a/gi/pygi-array.c
+++ b/gi/pygi-array.c
@@ -21,7 +21,6 @@
 #include <Python.h>
 #include <glib.h>
 
-#include "pygi-python-compat.h"
 #include "pygi-array.h"
 #include "pygi-info.h"
 #include "pygi-marshal-cleanup.h"
@@ -251,8 +250,8 @@ _pygi_marshal_from_py_array (PyGIInvokeState   *state,
     }
 
     if (sequence_cache->item_cache->type_tag == GI_TYPE_TAG_UINT8 &&
-        PYGLIB_PyBytes_Check (py_arg)) {
-        gchar *data = PYGLIB_PyBytes_AsString (py_arg);
+        PyBytes_Check (py_arg)) {
+        gchar *data = PyBytes_AsString (py_arg);
 
         /* Avoid making a copy if the data
          * is not transferred to the C function
@@ -583,9 +582,9 @@ _pygi_marshal_to_py_array (PyGIInvokeState   *state,
 
     if (seq_cache->item_cache->type_tag == GI_TYPE_TAG_UINT8) {
         if (arg->v_pointer == NULL) {
-            py_obj = PYGLIB_PyBytes_FromString ("");
+            py_obj = PyBytes_FromString ("");
         } else {
-            py_obj = PYGLIB_PyBytes_FromStringAndSize (array_->data, array_->len);
+            py_obj = PyBytes_FromStringAndSize (array_->data, array_->len);
         }
     } else {
         if (arg->v_pointer == NULL) {
diff --git a/gi/pygi-basictype.c b/gi/pygi-basictype.c
index c0e5d1d0..2864d918 100644
--- a/gi/pygi-basictype.c
+++ b/gi/pygi-basictype.c
@@ -19,7 +19,7 @@
  */
 
 #include <Python.h>
-#include "pygi-python-compat.h"
+
 #include "pygi-type.h"
 #include "pygi-basictype.h"
 #include "pygi-argument.h"
@@ -53,7 +53,7 @@ pygi_gpointer_from_py (PyObject *py_arg, gpointer *result)
             return FALSE;
         *result = temp;
         return TRUE;
-    } else if (PYGLIB_PyLong_Check(py_arg) || PyLong_Check(py_arg)) {
+    } else if (PyLong_Check(py_arg)) {
         temp = PyLong_AsVoidPtr (py_arg);
         if (PyErr_Occurred ())
             return FALSE;
@@ -88,13 +88,13 @@ marshal_from_py_void (PyGIInvokeState   *state,
 PyObject *
 pygi_gsize_to_py (gsize value)
 {
-    return PYGLIB_PyLong_FromSize_t (value);
+    return PyLong_FromSize_t (value);
 }
 
 PyObject *
 pygi_gssize_to_py (gssize value)
 {
-    return PYGLIB_PyLong_FromSsize_t (value);
+    return PyLong_FromSsize_t (value);
 }
 
 static PyObject *
@@ -157,7 +157,7 @@ pygi_gfloat_from_py (PyObject *py_arg, gfloat *result)
 
         min = pygi_gfloat_to_py (-G_MAXFLOAT);
         max = pygi_gfloat_to_py (G_MAXFLOAT);
-        pygi_pyerr_format (
+        PyErr_Format (
             PyExc_OverflowError, "%S not in range %S to %S",
             py_float, min, max);
         Py_DECREF (min);
@@ -197,19 +197,8 @@ pygi_gunichar_from_py (PyObject *py_arg, gunichar *result)
        if (!py_bytes)
            return FALSE;
 
-       string_ = g_strdup(PYGLIB_PyBytes_AsString (py_bytes));
+       string_ = g_strdup(PyBytes_AsString (py_bytes));
        Py_DECREF (py_bytes);
-
-#if PY_VERSION_HEX < 0x03000000
-    } else if (PyString_Check (py_arg)) {
-       PyObject *pyuni = PyUnicode_FromEncodedObject (py_arg, "UTF-8", "strict");
-       if (!pyuni)
-           return FALSE;
-
-       size = PyUnicode_GET_SIZE (pyuni);
-       string_ = g_strdup (PyString_AsString(py_arg));
-       Py_DECREF (pyuni);
-#endif
     } else {
        PyErr_Format (PyExc_TypeError, "Must be string, not %s",
                      Py_TYPE (py_arg)->tp_name);
@@ -236,13 +225,13 @@ pygi_gunichar_to_py (gunichar value)
 
     /* Preserve the bidirectional mapping between 0 and "" */
     if (value == 0) {
-        py_obj = PYGLIB_PyUnicode_FromString ("");
+        py_obj = PyUnicode_FromString ("");
     } else if (g_unichar_validate (value)) {
         gchar utf8[6];
         gint bytes;
 
         bytes = g_unichar_to_utf8 (value, utf8);
-        py_obj = PYGLIB_PyUnicode_FromStringAndSize ((char*)utf8, bytes);
+        py_obj = PyUnicode_FromStringAndSize ((char*)utf8, bytes);
     } else {
         /* TODO: Convert the error to an exception. */
         PyErr_Format (PyExc_TypeError,
@@ -288,14 +277,9 @@ pygi_utf8_from_py (PyObject *py_arg, gchar **result)
         if (!pystr_obj)
             return FALSE;
 
-        string_ = g_strdup (PYGLIB_PyBytes_AsString (pystr_obj));
+        string_ = g_strdup (PyBytes_AsString (pystr_obj));
         Py_DECREF (pystr_obj);
     }
-#if PY_VERSION_HEX < 0x03000000
-    else if (PyString_Check (py_arg)) {
-        string_ = g_strdup (PyString_AsString (py_arg));
-    }
-#endif
     else {
         PyErr_Format (PyExc_TypeError, "Must be string, not %s",
                       Py_TYPE (py_arg)->tp_name);
@@ -317,10 +301,10 @@ filename_from_py_unix (PyObject *py_arg, gchar **result)
         return TRUE;
     }
 
-    if (PYGLIB_PyBytes_Check (py_arg)) {
+    if (PyBytes_Check (py_arg)) {
         char *buffer;
 
-        if (PYGLIB_PyBytes_AsStringAndSize (py_arg, &buffer, NULL) == -1)
+        if (PyBytes_AsStringAndSize (py_arg, &buffer, NULL) == -1)
             return FALSE;
 
         filename = g_strdup (buffer);
@@ -328,17 +312,12 @@ filename_from_py_unix (PyObject *py_arg, gchar **result)
         PyObject *bytes;
         char *buffer;
 
-#if PY_VERSION_HEX < 0x03000000
-        bytes = PyUnicode_AsEncodedString (py_arg, Py_FileSystemDefaultEncoding,
-                                           NULL);
-#else
         bytes = PyUnicode_EncodeFSDefault (py_arg);
-#endif
 
         if (!bytes)
             return FALSE;
 
-        if (PYGLIB_PyBytes_AsStringAndSize (bytes, &buffer, NULL) == -1) {
+        if (PyBytes_AsStringAndSize (bytes, &buffer, NULL) == -1) {
             Py_DECREF (bytes);
             return FALSE;
         }
@@ -366,41 +345,12 @@ filename_from_py_win32 (PyObject *py_arg, gchar **result)
         return TRUE;
     }
 
-#if PY_VERSION_HEX < 0x03000000
-    if (PYGLIB_PyBytes_Check (py_arg)) {
-        char *buffer;
-
-        if (PYGLIB_PyBytes_AsStringAndSize (py_arg, &buffer, NULL) == -1)
-            return FALSE;
-
-        filename = g_strdup (buffer);
-    } else if (PyUnicode_Check (py_arg)) {
-        PyObject *bytes;
-        char *buffer;
-
-        bytes = PyUnicode_AsUTF8String (py_arg);
-        if (!bytes)
-            return FALSE;
-
-        if (PYGLIB_PyBytes_AsStringAndSize (bytes, &buffer, NULL) == -1) {
-            Py_DECREF (bytes);
-            return FALSE;
-        }
-
-        filename = g_strdup (buffer);
-        Py_DECREF (bytes);
-    } else {
-        PyErr_Format (PyExc_TypeError, "Must be unicode, not %s",
-                      Py_TYPE (py_arg)->tp_name);
-        return FALSE;
-    }
-#else
-    if (PYGLIB_PyBytes_Check (py_arg)) {
+    if (PyBytes_Check (py_arg)) {
         PyObject *uni_arg;
         gboolean temp_result;
         char *buffer;
 
-        if (PYGLIB_PyBytes_AsStringAndSize (py_arg, &buffer, NULL) == -1)
+        if (PyBytes_AsStringAndSize (py_arg, &buffer, NULL) == -1)
             return FALSE;
 
         uni_arg = PyUnicode_DecodeFSDefault (buffer);
@@ -432,7 +382,7 @@ filename_from_py_win32 (PyObject *py_arg, gchar **result)
         if (!bytes)
             return FALSE;
 
-        if (PYGLIB_PyBytes_AsStringAndSize (bytes, &buffer, NULL) == -1) {
+        if (PyBytes_AsStringAndSize (bytes, &buffer, NULL) == -1) {
             Py_DECREF (bytes);
             return FALSE;
         }
@@ -444,7 +394,6 @@ filename_from_py_win32 (PyObject *py_arg, gchar **result)
                       Py_TYPE (py_arg)->tp_name);
         return FALSE;
     }
-#endif
 
     *result = filename;
     return TRUE;
@@ -471,19 +420,7 @@ base_number_checks (PyObject *object)
         return NULL;
     }
 
-#if PY_MAJOR_VERSION < 3
-    {
-        PyObject *tmp = PyNumber_Int (object);
-        if (tmp) {
-            number = PyNumber_Long (tmp);
-            Py_DECREF (tmp);
-        } else {
-            number = PyNumber_Long (object);
-        }
-    }
-#else
     number = PyNumber_Long (object);
-#endif
 
     if (number == NULL) {
         PyErr_SetString (PyExc_TypeError, "expected int argument");
@@ -568,7 +505,7 @@ pygi_gint_from_py (PyObject *object, gint *result)
     if (number == NULL)
         return FALSE;
 
-    long_value = PYGLIB_PyLong_AsLong (number);
+    long_value = PyLong_AsLong (number);
     if (PyErr_Occurred ()) {
         if (PyErr_ExceptionMatches (PyExc_OverflowError))
             goto overflow;
@@ -584,7 +521,7 @@ pygi_gint_from_py (PyObject *object, gint *result)
 
 overflow:
     PyErr_Clear ();
-    pygi_pyerr_format (
+    PyErr_Format (
         PyExc_OverflowError, "%S not in range %d to %d",
         number, (int)G_MININT, (int)G_MAXINT);
     Py_DECREF (number);
@@ -594,7 +531,7 @@ overflow:
 PyObject *
 pygi_gint_to_py (gint value)
 {
-    return PYGLIB_PyLong_FromLong (value);
+    return PyLong_FromLong (value);
 }
 
 gboolean
@@ -623,7 +560,7 @@ pygi_guint_from_py (PyObject *object, guint *result)
 
 overflow:
     PyErr_Clear ();
-    pygi_pyerr_format (
+    PyErr_Format (
         PyExc_OverflowError, "%S not in range %ld to %lu",
         number, (long)0, (unsigned long)G_MAXUINT);
     Py_DECREF (number);
@@ -634,10 +571,10 @@ PyObject *
 pygi_guint_to_py (guint value)
 {
 #if (G_MAXUINT <= LONG_MAX)
-    return PYGLIB_PyLong_FromLong ((long) value);
+    return PyLong_FromLong ((long) value);
 #else
     if (value <= LONG_MAX)
-        return PYGLIB_PyLong_FromLong ((long) value);
+        return PyLong_FromLong ((long) value);
     return PyLong_FromUnsignedLong (value);
 #endif
 }
@@ -666,7 +603,7 @@ pygi_glong_from_py (PyObject *object, glong *result)
 
 overflow:
     PyErr_Clear ();
-    pygi_pyerr_format (
+    PyErr_Format (
         PyExc_OverflowError, "%S not in range %ld to %ld",
         number, (long)G_MINLONG, (long)G_MAXLONG);
     Py_DECREF (number);
@@ -676,7 +613,7 @@ overflow:
 PyObject *
 pygi_glong_to_py (glong value)
 {
-    return PYGLIB_PyLong_FromLong (value);
+    return PyLong_FromLong (value);
 }
 
 gboolean
@@ -703,7 +640,7 @@ pygi_gulong_from_py (PyObject *object, gulong *result)
 
 overflow:
     PyErr_Clear ();
-    pygi_pyerr_format (
+    PyErr_Format (
         PyExc_OverflowError, "%S not in range %ld to %lu",
         number, (long)0, (unsigned long)G_MAXULONG);
     Py_DECREF (number);
@@ -714,7 +651,7 @@ PyObject *
 pygi_gulong_to_py (gulong value)
 {
     if (value <= LONG_MAX)
-        return PYGLIB_PyLong_FromLong ((long) value);
+        return PyLong_FromLong ((long) value);
     else
         return PyLong_FromUnsignedLong (value);
 }
@@ -725,13 +662,13 @@ pygi_gint8_from_py (PyObject *object, gint8 *result)
     long long_value;
     PyObject *number;
 
-    if (PYGLIB_PyBytes_Check (object)) {
-        if (PYGLIB_PyBytes_Size (object) != 1) {
+    if (PyBytes_Check (object)) {
+        if (PyBytes_Size (object) != 1) {
             PyErr_Format (PyExc_TypeError, "Must be a single character");
             return FALSE;
         }
 
-        *result = (gint8)(PYGLIB_PyBytes_AsString (object)[0]);
+        *result = (gint8)(PyBytes_AsString (object)[0]);
         return TRUE;
     }
 
@@ -755,7 +692,7 @@ pygi_gint8_from_py (PyObject *object, gint8 *result)
 
 overflow:
     PyErr_Clear ();
-    pygi_pyerr_format (
+    PyErr_Format (
         PyExc_OverflowError, "%S not in range %ld to %ld",
         number, (long)G_MININT8, (long)G_MAXINT8);
     Py_DECREF (number);
@@ -765,7 +702,7 @@ overflow:
 PyObject *
 pygi_gint8_to_py (gint8 value)
 {
-    return PYGLIB_PyLong_FromLong (value);
+    return PyLong_FromLong (value);
 }
 
 gboolean
@@ -774,13 +711,13 @@ pygi_guint8_from_py (PyObject *object, guint8 *result)
     long long_value;
     PyObject *number;
 
-    if (PYGLIB_PyBytes_Check (object)) {
-        if (PYGLIB_PyBytes_Size (object) != 1) {
+    if (PyBytes_Check (object)) {
+        if (PyBytes_Size (object) != 1) {
             PyErr_Format (PyExc_TypeError, "Must be a single character");
             return FALSE;
         }
 
-        *result = (guint8)(PYGLIB_PyBytes_AsString (object)[0]);
+        *result = (guint8)(PyBytes_AsString (object)[0]);
         return TRUE;
     }
 
@@ -804,7 +741,7 @@ pygi_guint8_from_py (PyObject *object, guint8 *result)
 
 overflow:
     PyErr_Clear ();
-    pygi_pyerr_format (
+    PyErr_Format (
         PyExc_OverflowError, "%S not in range %ld to %ld",
         number, (long)0, (long)G_MAXUINT8);
     Py_DECREF (number);
@@ -814,7 +751,7 @@ overflow:
 PyObject *
 pygi_guint8_to_py (guint8 value)
 {
-    return PYGLIB_PyLong_FromLong (value);
+    return PyLong_FromLong (value);
 }
 
 static gboolean
@@ -843,7 +780,7 @@ pygi_gint16_from_py (PyObject *object, gint16 *result)
 
 overflow:
     PyErr_Clear ();
-    pygi_pyerr_format (
+    PyErr_Format (
         PyExc_OverflowError, "%S not in range %ld to %ld",
         number, (long)G_MININT16, (long)G_MAXINT16);
     Py_DECREF (number);
@@ -853,7 +790,7 @@ overflow:
 static PyObject *
 pygi_gint16_to_py (gint16 value)
 {
-    return PYGLIB_PyLong_FromLong (value);
+    return PyLong_FromLong (value);
 }
 
 static gboolean
@@ -882,7 +819,7 @@ pygi_guint16_from_py (PyObject *object, guint16 *result)
 
 overflow:
     PyErr_Clear ();
-    pygi_pyerr_format (
+    PyErr_Format (
         PyExc_OverflowError, "%S not in range %ld to %ld",
         number, (long)0, (long)G_MAXUINT16);
     Py_DECREF (number);
@@ -892,7 +829,7 @@ overflow:
 static PyObject *
 pygi_guint16_to_py (guint16 value)
 {
-    return PYGLIB_PyLong_FromLong (value);
+    return PyLong_FromLong (value);
 }
 
 static gboolean
@@ -921,7 +858,7 @@ pygi_gint32_from_py (PyObject *object, gint32 *result)
 
 overflow:
     PyErr_Clear ();
-    pygi_pyerr_format (
+    PyErr_Format (
         PyExc_OverflowError, "%S not in range %ld to %ld",
         number, (long)G_MININT32, (long)G_MAXINT32);
     Py_DECREF (number);
@@ -931,7 +868,7 @@ overflow:
 static PyObject *
 pygi_gint32_to_py (gint32 value)
 {
-    return PYGLIB_PyLong_FromLong (value);
+    return PyLong_FromLong (value);
 }
 
 static gboolean
@@ -960,7 +897,7 @@ pygi_guint32_from_py (PyObject *object, guint32 *result)
 
 overflow:
     PyErr_Clear ();
-    pygi_pyerr_format (
+    PyErr_Format (
         PyExc_OverflowError, "%S not in range %ld to %lu",
         number, (long)0, (unsigned long)G_MAXUINT32);
     Py_DECREF (number);
@@ -971,10 +908,10 @@ PyObject *
 pygi_guint32_to_py (guint32 value)
 {
 #if (G_MAXUINT <= LONG_MAX)
-    return PYGLIB_PyLong_FromLong (value);
+    return PyLong_FromLong (value);
 #else
     if (value <= LONG_MAX)
-        return PYGLIB_PyLong_FromLong((long) value);
+        return PyLong_FromLong((long) value);
     else
         return PyLong_FromLongLong (value);
 #endif
@@ -1008,7 +945,7 @@ overflow:
     PyErr_Clear ();
     min = pygi_gint64_to_py (G_MININT64);
     max = pygi_gint64_to_py (G_MAXINT64);
-    pygi_pyerr_format (
+    PyErr_Format (
         PyExc_OverflowError, "%S not in range %S to %S",
         number, min, max);
     Py_DECREF (number);
@@ -1021,7 +958,7 @@ PyObject *
 pygi_gint64_to_py (gint64 value)
 {
     if (LONG_MIN <= value && value <= LONG_MAX)
-        return PYGLIB_PyLong_FromLong((long) value);
+        return PyLong_FromLong((long) value);
     else
         return PyLong_FromLongLong (value);
 }
@@ -1053,7 +990,7 @@ pygi_guint64_from_py (PyObject *object, guint64 *result)
 overflow:
     PyErr_Clear ();
     max = pygi_guint64_to_py (G_MAXUINT64);
-    pygi_pyerr_format (
+    PyErr_Format (
         PyExc_OverflowError, "%S not in range %ld to %S",
         number, (long)0, max);
     Py_DECREF (number);
@@ -1065,7 +1002,7 @@ PyObject *
 pygi_guint64_to_py (guint64 value)
 {
     if (value <= LONG_MAX)
-        return PYGLIB_PyLong_FromLong((long) value);
+        return PyLong_FromLong((long) value);
     else
         return PyLong_FromUnsignedLongLong (value);
 }
@@ -1195,7 +1132,7 @@ pygi_utf8_to_py (gchar *value)
         Py_RETURN_NONE;
      }
 
-    return PYGLIB_PyUnicode_FromString (value);
+    return PyUnicode_FromString (value);
 }
 
 PyObject *
@@ -1207,16 +1144,11 @@ pygi_filename_to_py (gchar *value)
         Py_RETURN_NONE;
     }
 
-#if PY_VERSION_HEX < 0x03000000
-    /* On PY2 we return str as is */
-    py_obj = PyString_FromString (value);
-#else
 #ifdef G_OS_WIN32
     py_obj = PyUnicode_DecodeUTF8 (value, strlen(value),
                                    "surrogatepass");
 #else
     py_obj = PyUnicode_DecodeFSDefault (value);
-#endif
 #endif
 
     return py_obj;
diff --git a/gi/pygi-boxed.c b/gi/pygi-boxed.c
index e3b487be..41515e07 100644
--- a/gi/pygi-boxed.c
+++ b/gi/pygi-boxed.c
@@ -24,7 +24,7 @@
 #include "pygboxed.h"
 #include "pygi-type.h"
 #include "pygi-basictype.h"
-#include "pygi-python-compat.h"
+#include "pygi-util.h"
 
 #include <girepository.h>
 
diff --git a/gi/pygi-cache.c b/gi/pygi-cache.c
index 667ccf6e..c6630de0 100644
--- a/gi/pygi-cache.c
+++ b/gi/pygi-cache.c
@@ -21,7 +21,6 @@
 #include <Python.h>
 #include <girepository.h>
 
-#include "pygi-python-compat.h"
 #include "pygi-type.h"
 #include "pygi-info.h"
 #include "pygi-cache.h"
@@ -662,7 +661,7 @@ _callable_cache_generate_args_cache_real (PyGICallableCache *callable_cache,
     arg_cache_item = callable_cache->to_py_args;
     while (arg_cache_item) {
         const gchar *arg_name = ((PyGIArgCache *)arg_cache_item->data)->arg_name;
-        PyObject *arg_string = PYGLIB_PyUnicode_FromString (arg_name);
+        PyObject *arg_string = PyUnicode_FromString (arg_name);
         PyList_Append (tuple_names, arg_string);
         Py_DECREF (arg_string);
         arg_cache_item = arg_cache_item->next;
diff --git a/gi/pygi-ccallback.c b/gi/pygi-ccallback.c
index cd003e90..3b825712 100644
--- a/gi/pygi-ccallback.c
+++ b/gi/pygi-ccallback.c
@@ -19,7 +19,7 @@
  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  */
 
-#include "pygi-python-compat.h"
+#include "pygi-util.h"
 #include "pygi-ccallback.h"
 
 #include <girepository.h>
diff --git a/gi/pygi-enum-marshal.c b/gi/pygi-enum-marshal.c
index fc330895..15a489da 100644
--- a/gi/pygi-enum-marshal.c
+++ b/gi/pygi-enum-marshal.c
@@ -21,7 +21,6 @@
 #include <Python.h>
 #include <glib.h>
 
-#include "pygi-python-compat.h"
 #include "pygi-enum-marshal.h"
 #include "pygi-type.h"
 #include "pygenum.h"
@@ -132,13 +131,13 @@ _pygi_marshal_from_py_interface_enum (PyGIInvokeState   *state,
 
     is_instance = PyObject_IsInstance (py_arg, iface_cache->py_type);
 
-    py_long = PYGLIB_PyNumber_Long (py_arg);
+    py_long = PyNumber_Long (py_arg);
     if (py_long == NULL) {
         PyErr_Clear();
         goto err;
     }
 
-    c_long = PYGLIB_PyLong_AsLong (py_long);
+    c_long = PyLong_AsLong (py_long);
     Py_DECREF (py_long);
 
     /* Write c_long into arg */
@@ -201,13 +200,13 @@ _pygi_marshal_from_py_interface_flags (PyGIInvokeState   *state,
 
     is_instance = PyObject_IsInstance (py_arg, iface_cache->py_type);
 
-    py_long = PYGLIB_PyNumber_Long (py_arg);
+    py_long = PyNumber_Long (py_arg);
     if (py_long == NULL) {
         PyErr_Clear ();
         goto err;
     }
 
-    c_ulong = PYGLIB_PyLong_AsUnsignedLong (py_long);
+    c_ulong = PyLong_AsUnsignedLongMask (py_long);
     Py_DECREF (py_long);
 
     /* only 0 or argument of type Flag is allowed */
diff --git a/gi/pygi-error.c b/gi/pygi-error.c
index b0eeccd1..de6b7c19 100644
--- a/gi/pygi-error.c
+++ b/gi/pygi-error.c
@@ -23,7 +23,6 @@
 #include <Python.h>
 #include "pygi-error.h"
 #include "pygi-type.h"
-#include "pygi-python-compat.h"
 #include "pygi-util.h"
 #include "pygi-basictype.h"
 
@@ -355,7 +354,7 @@ pygerror_to_gvalue (GValue *value, PyObject *pyerror)
 int
 pygi_error_register_types (PyObject *module)
 {
-    PyObject *error_module = pygi_import_module ("gi._error");
+    PyObject *error_module = PyImport_ImportModule ("gi._error");
     if (!error_module) {
         return -1;
     }
diff --git a/gi/pygi-foreign-cairo.c b/gi/pygi-foreign-cairo.c
index 399fe75e..aa011571 100644
--- a/gi/pygi-foreign-cairo.c
+++ b/gi/pygi-foreign-cairo.c
@@ -23,13 +23,7 @@
 
 #include <Python.h>
 #include <cairo.h>
-
-#if PY_VERSION_HEX < 0x03000000
-#include <pycairo.h>
-static Pycairo_CAPI_t *Pycairo_CAPI;
-#else
 #include <py3cairo.h>
-#endif
 
 #include <cairo-gobject.h>
 
@@ -38,7 +32,6 @@ static Pycairo_CAPI_t *Pycairo_CAPI;
  * shared library that interacts with PyGI through a PyCapsule API at runtime.
  */
 #include <pygi-foreign-api.h>
-#include "pygi-python-compat.h"
 
 /*
  * cairo_t marshaling
@@ -613,23 +606,43 @@ cairo_matrix_from_gvalue (const GValue *value)
     return PycairoMatrix_FromMatrix (matrix);
 }
 
+#ifdef __GNUC__
+#define PYGI_MODINIT_FUNC __attribute__((visibility("default"))) PyMODINIT_FUNC
+#else
+#define PYGI_MODINIT_FUNC PyMODINIT_FUNC
+#endif
+
 static PyMethodDef _gi_cairo_functions[] = { {0,} };
-PYGLIB_MODULE_START(_gi_cairo, "_gi_cairo")
+
+static struct PyModuleDef __gi_cairomodule = {
+    PyModuleDef_HEAD_INIT,
+    "_gi_cairo",
+    NULL,
+    -1,
+    _gi_cairo_functions,
+    NULL,
+    NULL,
+    NULL,
+    NULL
+};
+
+PYGI_MODINIT_FUNC PyInit__gi_cairo (void);
+
+PYGI_MODINIT_FUNC PyInit__gi_cairo (void)
 {
+    PyObject *module;
+    module = PyModule_Create (&__gi_cairomodule);
+
     PyObject *gobject_mod;
 
-#if PY_VERSION_HEX < 0x03000000
-    Pycairo_IMPORT;
-#else
     import_cairo();
-#endif
 
     if (Pycairo_CAPI == NULL)
-        return PYGLIB_MODULE_ERROR_RETURN;
+        return NULL;
 
     gobject_mod = pygobject_init (3, 13, 2);
     if (gobject_mod == NULL)
-        return PYGLIB_MODULE_ERROR_RETURN;
+        return NULL;
     Py_DECREF (gobject_mod);
 
     pygi_register_foreign_struct ("cairo",
@@ -698,5 +711,5 @@ PYGLIB_MODULE_START(_gi_cairo, "_gi_cairo")
                                cairo_pattern_from_gvalue,
                                cairo_pattern_to_gvalue);
 
-}
-PYGLIB_MODULE_END;
+    return module;
+}
\ No newline at end of file
diff --git a/gi/pygi-info.c b/gi/pygi-info.c
index 0c4824ff..62bc1021 100644
--- a/gi/pygi-info.c
+++ b/gi/pygi-info.c
@@ -20,7 +20,6 @@
  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  */
 
-#include "pygi-python-compat.h"
 #include "pygi-info.h"
 #include "pygi-cache.h"
 #include "pygi-invoke.h"
@@ -40,7 +39,7 @@ _generate_doc_string(PyGIBaseInfo *self)
     static PyObject *_py_generate_doc_string = NULL;
 
     if (_py_generate_doc_string == NULL) {
-        PyObject *mod = pygi_import_module ("gi.docstring");
+        PyObject *mod = PyImport_ImportModule ("gi.docstring");
         if (!mod)
             return NULL;
 
@@ -186,9 +185,9 @@ static PyObject *
 _base_info_repr (PyGIBaseInfo *self)
 {
 
-    return PYGLIB_PyUnicode_FromFormat ("%s(%s)",
-                                        Py_TYPE( (PyObject *) self)->tp_name,
-                                        _safe_base_info_get_name (self->info));
+    return PyUnicode_FromFormat ("%s(%s)",
+                                 Py_TYPE( (PyObject *) self)->tp_name,
+                                 _safe_base_info_get_name (self->info));
 }
 
 static PyObject *
@@ -241,14 +240,7 @@ _pygi_is_python_keyword (const gchar *name)
 {
     /* It may be better to use keyword.iskeyword(); keep in sync with
      * python -c 'import keyword; print(keyword.kwlist)' */
-#if PY_VERSION_HEX < 0x03000000
-    /* Python 2.x */
-    static const gchar* keywords[] = {"and", "as", "assert", "break", "class",
-        "continue", "def", "del", "elif", "else", "except", "exec", "finally",
-        "for", "from", "global", "if", "import", "in", "is", "lambda", "not",
-        "or", "pass", "print", "raise", "return", "try", "while", "with",
-        "yield", NULL};
-#elif PY_VERSION_HEX < 0x04000000
+#if PY_VERSION_HEX < 0x04000000
     /* Python 3.x; note that we explicitly keep "print"; it is not a keyword
      * any more, but we do not want to break API between Python versions */
     static const gchar* keywords[] = {"False", "None", "True", "and", "as",
@@ -376,13 +368,13 @@ _base_info_getattro(PyGIBaseInfo *self, PyObject *name)
 
     static PyObject *docstr;
     if (docstr == NULL) {
-        docstr= PYGLIB_PyUnicode_InternFromString("__doc__");
+        docstr= PyUnicode_InternFromString ("__doc__");
         if (docstr == NULL)
             return NULL;
     }
 
     Py_INCREF (name);
-    PYGLIB_PyUnicode_InternInPlace (&name);
+    PyUnicode_InternInPlace (&name);
 
     if (name == docstr) {
         result = _generate_doc_string (self);
@@ -403,8 +395,8 @@ _base_info_attr_name(PyGIBaseInfo *self, void *closure)
 static PyObject *
 _base_info_attr_module(PyGIBaseInfo *self, void *closure)
 {
-    return PYGLIB_PyUnicode_FromFormat ("gi.repository.%s",
-                                        g_base_info_get_namespace (self->info));
+    return PyUnicode_FromFormat ("gi.repository.%s",
+                                 g_base_info_get_namespace (self->info));
 }
 
 static PyGetSetDef _base_info_getsets[] = {
@@ -600,7 +592,7 @@ _function_info_call (PyGICallableInfo *self, PyObject *args, PyObject *kwargs)
                 py_str_name = tmp;
             }
 
-            str_name = PYGLIB_PyBytes_AsString (py_str_name);
+            str_name = PyBytes_AsString (py_str_name);
             if (strcmp (str_name, _safe_base_info_get_name (container_info))) {
                 PyErr_Format (PyExc_TypeError,
                               "%s constructor cannot be used to create instances of "
diff --git a/gi/pygi-invoke.c b/gi/pygi-invoke.c
index d5956c66..7b822e4b 100644
--- a/gi/pygi-invoke.c
+++ b/gi/pygi-invoke.c
@@ -40,12 +40,6 @@ _check_for_unexpected_kwargs (PyGICallableCache *cache,
     while (PyDict_Next (py_kwargs, &dict_iter_pos, &dict_key, &dict_value)) {
         PyObject *key;
 
-#if PY_VERSION_HEX < 0x03000000
-        if (PyString_Check (dict_key)) {
-            Py_INCREF (dict_key);
-            key = dict_key;
-        } else
-#endif
         {
             key = PyUnicode_AsUTF8String (dict_key);
             if (key == NULL) {
diff --git a/gi/pygi-object.c b/gi/pygi-object.c
index 79170e3f..00b36c1b 100644
--- a/gi/pygi-object.c
+++ b/gi/pygi-object.c
@@ -21,7 +21,6 @@
 #include <Python.h>
 #include <glib.h>
 
-#include "pygi-python-compat.h"
 #include "pygi-object.h"
 #include "pygobject-object.h"
 #include "pygparamspec.h"
@@ -52,7 +51,7 @@ _pygi_marshal_from_py_gobject (PyObject *py_arg, /*in*/
     if (!pygobject_check (py_arg, &PyGObject_Type)) {
         PyObject *repr = PyObject_Repr (py_arg);
         PyErr_Format(PyExc_TypeError, "expected GObject but got %s",
-                     PYGLIB_PyUnicode_AsString (repr));
+                     PyUnicode_AsUTF8 (repr));
         Py_DECREF (repr);
         return FALSE;
     }
@@ -117,7 +116,7 @@ pygi_arg_gobject_out_arg_from_py (PyObject *py_arg, /*in*/
             gchar *msg = g_strdup_printf ("Expecting to marshal a borrowed reference for %s, "
                                           "but nothing in Python is holding a reference to this object. "
                                           "See: https://bugzilla.gnome.org/show_bug.cgi?id=687522";,
-                                          PYGLIB_PyUnicode_AsString(repr));
+                                          PyUnicode_AsUTF8 (repr));
             Py_DECREF (repr);
             if (PyErr_WarnEx (PyExc_RuntimeWarning, msg, 2)) {
                 g_free (msg);
@@ -161,7 +160,7 @@ _pygi_marshal_from_py_interface_object (PyGIInvokeState             *state,
         PyErr_Format (PyExc_TypeError, "argument %s: Expected %s, but got %s%s%s",
                       arg_cache->arg_name ? arg_cache->arg_name : "self",
                       ( (PyGIInterfaceCache *)arg_cache)->type_name,
-                      module ? PYGLIB_PyUnicode_AsString(module) : "",
+                      module ? PyUnicode_AsUTF8 (module) : "",
                       module ? "." : "",
                       Py_TYPE (py_arg)->tp_name);
         if (module)
diff --git a/gi/pygi-repository.c b/gi/pygi-repository.c
index 133f89c3..1dd92d45 100644
--- a/gi/pygi-repository.c
+++ b/gi/pygi-repository.c
@@ -22,7 +22,7 @@
 #include "pygi-repository.h"
 #include "pygi-info.h"
 #include "pygi-basictype.h"
-#include "pygi-python-compat.h"
+#include "pygi-util.h"
 
 PyObject *PyGIRepositoryError;
 
diff --git a/gi/pygi-resulttuple.c b/gi/pygi-resulttuple.c
index 0d060d01..adadd27b 100644
--- a/gi/pygi-resulttuple.c
+++ b/gi/pygi-resulttuple.c
@@ -20,7 +20,7 @@
 #include <Python.h>
 #include <glib.h>
 #include "pygi-resulttuple.h"
-#include "pygi-python-compat.h"
+#include "pygi-util.h"
 
 static char repr_format_key[] = "__repr_format";
 static char tuple_indices_key[] = "__tuple_indices";
@@ -54,12 +54,12 @@ static PyObject*
 resulttuple_repr(PyObject *self) {
     PyObject *format,  *repr, *format_attr;
 
-    format_attr = PYGLIB_PyUnicode_FromString (repr_format_key);
+    format_attr = PyUnicode_FromString (repr_format_key);
     format = PyTuple_Type.tp_getattro (self, format_attr);
     Py_DECREF (format_attr);
     if (format == NULL)
         return NULL;
-    repr = PYGLIB_PyUnicode_Format (format, self);
+    repr = PyUnicode_Format (format, self);
     Py_DECREF (format);
     return repr;
 }
@@ -73,7 +73,7 @@ static PyObject*
 resulttuple_getattro(PyObject *self, PyObject *name) {
     PyObject *mapping, *index, *mapping_attr, *item;
 
-    mapping_attr = PYGLIB_PyUnicode_FromString (tuple_indices_key);
+    mapping_attr = PyUnicode_FromString (tuple_indices_key);
     mapping = PyTuple_Type.tp_getattro (self, mapping_attr);
     Py_DECREF (mapping_attr);
     if (mapping == NULL)
@@ -82,7 +82,7 @@ resulttuple_getattro(PyObject *self, PyObject *name) {
     index = PyDict_GetItem (mapping, name);
 
     if (index != NULL) {
-        item = PyTuple_GET_ITEM (self, PYGLIB_PyLong_AsSsize_t (index));
+        item = PyTuple_GET_ITEM (self, PyLong_AsSsize_t (index));
         Py_INCREF (item);
     } else {
         item = PyTuple_Type.tp_getattro (self, name);
@@ -120,7 +120,7 @@ resulttuple_dir(PyObject *self)
     PyObject *mapping_values = NULL;
     PyObject *result = NULL;
 
-    mapping_attr = PYGLIB_PyUnicode_FromString (tuple_indices_key);
+    mapping_attr = PyUnicode_FromString (tuple_indices_key);
     mapping = PyTuple_Type.tp_getattro (self, mapping_attr);
     Py_DECREF (mapping_attr);
     if (mapping == NULL)
@@ -208,8 +208,8 @@ pygi_resulttuple_new_type(PyObject *tuple_names) {
     format_list = PyList_New (0);
     index_dict = PyDict_New ();
 
-    empty_format = PYGLIB_PyUnicode_FromString ("%r");
-    named_format = PYGLIB_PyUnicode_FromString ("%s=%%r");
+    empty_format = PyUnicode_FromString ("%r");
+    named_format = PyUnicode_FromString ("%s=%%r");
     len = PyList_Size (tuple_names);
     for (i = 0; i < len; i++) {
         PyObject *item, *named_args, *named_build, *index;
@@ -218,11 +218,11 @@ pygi_resulttuple_new_type(PyObject *tuple_names) {
             PyList_Append (format_list, empty_format);
         } else {
             named_args = Py_BuildValue ("(O)", item);
-            named_build = PYGLIB_PyUnicode_Format (named_format, named_args);
+            named_build = PyUnicode_Format (named_format, named_args);
             Py_DECREF (named_args);
             PyList_Append (format_list, named_build);
             Py_DECREF (named_build);
-            index = PYGLIB_PyLong_FromSsize_t (i);
+            index = PyLong_FromSsize_t (i);
             PyDict_SetItem (index_dict, item, index);
             Py_DECREF (index);
         }
@@ -230,12 +230,12 @@ pygi_resulttuple_new_type(PyObject *tuple_names) {
     Py_DECREF (empty_format);
     Py_DECREF (named_format);
 
-    sep = PYGLIB_PyUnicode_FromString (", ");
+    sep = PyUnicode_FromString (", ");
     format_string = PyObject_CallMethod (sep, "join", "O", format_list);
     Py_DECREF (sep);
     Py_DECREF (format_list);
-    paren_format = PYGLIB_PyUnicode_FromString ("(%s)");
-    paren_string = PYGLIB_PyUnicode_Format (paren_format, format_string);
+    paren_format = PyUnicode_FromString ("(%s)");
+    paren_string = PyUnicode_Format (paren_format, format_string);
     Py_DECREF (paren_format);
     Py_DECREF (format_string);
 
diff --git a/gi/pygi-source.c b/gi/pygi-source.c
index c6cdfdaa..c85386d7 100644
--- a/gi/pygi-source.c
+++ b/gi/pygi-source.c
@@ -23,7 +23,6 @@
  * IN THE SOFTWARE.
  */
 
-#include "pygi-python-compat.h"
 #include "pygi-info.h"
 #include "pygi-boxed.h"
 #include "pygi-type.h"
diff --git a/gi/pygi-struct-marshal.c b/gi/pygi-struct-marshal.c
index a7705a53..6daf317c 100644
--- a/gi/pygi-struct-marshal.c
+++ b/gi/pygi-struct-marshal.c
@@ -21,7 +21,6 @@
 #include <Python.h>
 #include <glib.h>
 
-#include "pygi-python-compat.h"
 #include "pygi-struct-marshal.h"
 #include "pygi-struct.h"
 #include "pygi-foreign.h"
@@ -312,7 +311,7 @@ type_error:
         PyErr_Format (PyExc_TypeError, "argument %s: Expected %s, but got %s%s%s",
                       arg_name ? arg_name : "self",
                       type_name,
-                      module ? PYGLIB_PyUnicode_AsString(module) : "",
+                      module ? PyUnicode_AsUTF8(module) : "",
                       module ? "." : "",
                       Py_TYPE (py_arg)->tp_name);
         if (module)
diff --git a/gi/pygi-struct.c b/gi/pygi-struct.c
index 4c05b7c4..fe41f255 100644
--- a/gi/pygi-struct.c
+++ b/gi/pygi-struct.c
@@ -25,7 +25,7 @@
 #include "pygi-type.h"
 #include "pygi-type.h"
 #include "pygpointer.h"
-#include "pygi-python-compat.h"
+#include "pygi-util.h"
 
 #include <girepository.h>
 
@@ -216,11 +216,11 @@ struct_repr(PyGIStruct *self)
     if (info == NULL)
         return NULL;
 
-    repr = PYGLIB_PyUnicode_FromFormat ("<%s.%s object at %p (%s at %p)>",
-                                        g_base_info_get_namespace (info),
-                                        g_base_info_get_name (info),
-                                        self, g_type_name (pointer->gtype),
-                                        pointer->pointer);
+    repr = PyUnicode_FromFormat ("<%s.%s object at %p (%s at %p)>",
+                                 g_base_info_get_namespace (info),
+                                 g_base_info_get_name (info),
+                                 self, g_type_name (pointer->gtype),
+                                 pointer->pointer);
 
     g_base_info_unref (info);
 
diff --git a/gi/pygi-type.c b/gi/pygi-type.c
index 952d3079..f8913753 100644
--- a/gi/pygi-type.c
+++ b/gi/pygi-type.c
@@ -41,7 +41,7 @@ pygi_type_import_by_name (const char *namespace_,
 
     module_name = g_strconcat ("gi.repository.", namespace_, NULL);
 
-    py_module = pygi_import_module (module_name);
+    py_module = PyImport_ImportModule (module_name);
 
     g_free (module_name);
 
@@ -187,7 +187,7 @@ pyg_type_wrapper_repr(PyGTypeWrapper *self)
 
     g_snprintf(buf, sizeof(buf), "<GType %s (%lu)>",
               name?name:"invalid", (unsigned long int) self->type);
-    return PYGLIB_PyUnicode_FromString(buf);
+    return PyUnicode_FromString (buf);
 }
 
 static void
@@ -260,7 +260,7 @@ static PyObject *
 _wrap_g_type_wrapper__get_name(PyGTypeWrapper *self, void *closure)
 {
    const char *name = g_type_name(self->type);
-   return PYGLIB_PyUnicode_FromString(name ? name : "invalid");
+   return PyUnicode_FromString (name ? name : "invalid");
 }
 
 static PyObject *
@@ -501,15 +501,13 @@ pyg_type_from_object_strict(PyObject *obj, gboolean strict)
     if (PyType_Check(obj)) {
        PyTypeObject *tp = (PyTypeObject *)obj;
 
-       if (tp == &PYGLIB_PyLong_Type)
+       if (tp == &PyLong_Type)
            return G_TYPE_INT;
        else if (tp == &PyBool_Type)
            return G_TYPE_BOOLEAN;
-       else if (tp == &PyLong_Type)
-           return G_TYPE_LONG;
        else if (tp == &PyFloat_Type)
            return G_TYPE_DOUBLE;
-       else if (tp == &PYGLIB_PyUnicode_Type)
+       else if (tp == &PyUnicode_Type)
            return G_TYPE_STRING;
        else if (tp == &PyBaseObject_Type)
            return PY_TYPE_OBJECT;
@@ -520,8 +518,8 @@ pyg_type_from_object_strict(PyObject *obj, gboolean strict)
     }
 
     /* handle strings */
-    if (PYGLIB_PyUnicode_Check(obj)) {
-       gchar *name = PYGLIB_PyUnicode_AsString(obj);
+    if (PyUnicode_Check (obj)) {
+       gchar *name = PyUnicode_AsUTF8(obj);
 
        type = g_type_from_name(name);
        if (type != 0) {
@@ -594,7 +592,7 @@ pyg_enum_get_value(GType enum_type, PyObject *obj, gint *val)
     if (!obj) {
        *val = 0;
        res = 0;
-    } else if (PYGLIB_PyLong_Check(obj)) {
+    } else if (PyLong_Check (obj)) {
        if (!pygi_gint_from_py (obj, val))
            res = -1;
        else
@@ -617,9 +615,9 @@ pyg_enum_get_value(GType enum_type, PyObject *obj, gint *val)
                      g_type_name(enum_type),
                      g_type_name(((PyGEnum *) obj)->gtype));
        }
-    } else if (PYGLIB_PyUnicode_Check(obj)) {
+    } else if (PyUnicode_Check (obj)) {
        GEnumValue *info;
-       char *str = PYGLIB_PyUnicode_AsString(obj);
+       char *str = PyUnicode_AsUTF8 (obj);
 
        if (enum_type != G_TYPE_NONE)
            eclass = G_ENUM_CLASS(g_type_class_ref(enum_type));
@@ -671,15 +669,15 @@ pyg_flags_get_value(GType flag_type, PyObject *obj, guint *val)
     if (!obj) {
        *val = 0;
        res = 0;
-    } else if (PYGLIB_PyLong_Check(obj)) {
+    } else if (PyLong_Check (obj)) {
        if (pygi_guint_from_py (obj, val))
            res = 0;
     } else if (PyLong_Check(obj)) {
        if (pygi_guint_from_py (obj, val))
            res = 0;
-    } else if (PYGLIB_PyUnicode_Check(obj)) {
+    } else if (PyUnicode_Check (obj)) {
        GFlagsValue *info;
-       char *str = PYGLIB_PyUnicode_AsString(obj);
+       char *str = PyUnicode_AsUTF8 (obj);
 
        if (flag_type != G_TYPE_NONE)
            fclass = G_FLAGS_CLASS(g_type_class_ref(flag_type));
@@ -715,7 +713,7 @@ pyg_flags_get_value(GType flag_type, PyObject *obj, guint *val)
 
        for (i = 0; i < len; i++) {
            PyObject *item = PyTuple_GetItem(obj, i);
-           char *str = PYGLIB_PyUnicode_AsString(item);
+           char *str = PyUnicode_AsUTF8 (item);
            GFlagsValue *info = g_flags_get_value_by_name(fclass, str);
 
            if (!info)
@@ -1250,7 +1248,7 @@ object_doc_descr_get(PyObject *self, PyObject *obj, PyObject *type)
         g_array_free(parents, TRUE);
     }
 
-    pystring = PYGLIB_PyUnicode_FromStringAndSize(string->str, string->len);
+    pystring = PyUnicode_FromStringAndSize (string->str, string->len);
     g_string_free(string, TRUE);
     return pystring;
 }
diff --git a/gi/pygi-util.c b/gi/pygi-util.c
index c02a53c0..3a297f40 100644
--- a/gi/pygi-util.c
+++ b/gi/pygi-util.c
@@ -18,38 +18,6 @@
 
 #include "pygi-util.h"
 
-/**
- * Like PyErr_Format, but supports the format syntax of
- * PyUnicode_FromFormat also under Python 2.
- * Note: Python 2 doesn't support %lld and %llo there.
- */
-PyObject*
-pygi_pyerr_format (PyObject *exception, const char *format, ...)
-{
-    PyObject *text;
-    va_list argp;
-    va_start(argp, format);
-    text = PyUnicode_FromFormatV (format, argp);
-    va_end(argp);
-
-    if (text != NULL) {
-#if PY_MAJOR_VERSION < 3
-        PyObject *str;
-        str = PyUnicode_AsUTF8String (text);
-        Py_DECREF (text);
-        if (str) {
-            PyErr_SetObject (exception, str);
-            Py_DECREF (str);
-        }
-#else
-        PyErr_SetObject (exception, text);
-        Py_DECREF (text);
-#endif
-    }
-
-    return NULL;
-}
-
 gboolean
 pygi_guint_from_pyssize (Py_ssize_t pyval, guint *result)
 {
@@ -64,23 +32,6 @@ pygi_guint_from_pyssize (Py_ssize_t pyval, guint *result)
     return TRUE;
 }
 
-/* Better alternative to PyImport_ImportModule which tries to import from
- * sys.modules first */
-PyObject *
-pygi_import_module (const char *name)
-{
-#if PY_VERSION_HEX < 0x03000000 && !defined(PYPY_VERSION)
-    /* see PyImport_ImportModuleNoBlock
-     * https://github.com/python/cpython/blob/2.7/Python/import.c#L2166-L2206 */
-    PyObject *result = PyImport_ImportModuleNoBlock(name);
-    if (result)
-        return result;
-
-    PyErr_Clear();
-#endif
-    return PyImport_ImportModule(name);
-}
-
 PyObject *
 pyg_integer_richcompare(PyObject *v, PyObject *w, int op)
 {
@@ -88,12 +39,12 @@ pyg_integer_richcompare(PyObject *v, PyObject *w, int op)
     gboolean t;
 
     switch (op) {
-    case Py_EQ: t = PYGLIB_PyLong_AS_LONG(v) == PYGLIB_PyLong_AS_LONG(w); break;
-    case Py_NE: t = PYGLIB_PyLong_AS_LONG(v) != PYGLIB_PyLong_AS_LONG(w); break;
-    case Py_LE: t = PYGLIB_PyLong_AS_LONG(v) <= PYGLIB_PyLong_AS_LONG(w); break;
-    case Py_GE: t = PYGLIB_PyLong_AS_LONG(v) >= PYGLIB_PyLong_AS_LONG(w); break;
-    case Py_LT: t = PYGLIB_PyLong_AS_LONG(v) <  PYGLIB_PyLong_AS_LONG(w); break;
-    case Py_GT: t = PYGLIB_PyLong_AS_LONG(v) >  PYGLIB_PyLong_AS_LONG(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();
     }
 
diff --git a/gi/pygi-util.h b/gi/pygi-util.h
index 2f9847e6..e16add13 100644
--- a/gi/pygi-util.h
+++ b/gi/pygi-util.h
@@ -3,20 +3,30 @@
 
 #include <Python.h>
 #include <glib.h>
-#include "pygobject-internal.h"
-#include "pygi-python-compat.h"
 
 G_BEGIN_DECLS
 
 PyObject * pyg_integer_richcompare(PyObject *v, PyObject *w, int op);
 PyObject * pyg_ptr_richcompare(void* a, void *b, int op);
 const gchar * pyg_constant_strip_prefix(const gchar *name, const gchar *strip_prefix);
-PyObject * pygi_import_module (const char *name);
-PyObject * pygi_pyerr_format (PyObject *exception, const char *format, ...);
 
 gboolean pygi_guint_from_pyssize (Py_ssize_t pyval, guint *result);
 
-#if PY_VERSION_HEX >= 0x03000000
+#define PYGLIB_DEFINE_TYPE(typename, symbol, csymbol)  \
+PyTypeObject symbol = {                                 \
+    PyVarObject_HEAD_INIT(NULL, 0)                      \
+    typename,                                           \
+    sizeof(csymbol)                                     \
+};
+
+#define PYGLIB_REGISTER_TYPE(d, type, name)                \
+    if (!type.tp_alloc)                                 \
+           type.tp_alloc = PyType_GenericAlloc;            \
+    if (!type.tp_new)                                   \
+           type.tp_new = PyType_GenericNew;                \
+    if (PyType_Ready(&type))                            \
+           return -1;                                         \
+    PyDict_SetItemString(d, name, (PyObject *)&type);
 
 #define _PyGI_ERROR_PREFIX(format, ...) G_STMT_START { \
     PyObject *py_error_prefix; \
@@ -37,25 +47,6 @@ gboolean pygi_guint_from_pyssize (Py_ssize_t pyval, guint *result);
     } \
 } G_STMT_END
 
-#else
-
-#define _PyGI_ERROR_PREFIX(format, ...) G_STMT_START { \
-    PyObject *py_error_prefix; \
-    py_error_prefix = PyString_FromFormat(format, ## __VA_ARGS__); \
-    if (py_error_prefix != NULL) { \
-        PyObject *py_error_type, *py_error_value, *py_error_traceback; \
-        PyErr_Fetch(&py_error_type, &py_error_value, &py_error_traceback); \
-        if (PyString_Check(py_error_value)) { \
-            PyString_ConcatAndDel(&py_error_prefix, py_error_value); \
-            if (py_error_prefix != NULL) { \
-                py_error_value = py_error_prefix; \
-            } \
-        } \
-        PyErr_Restore(py_error_type, py_error_value, py_error_traceback); \
-    } \
-} G_STMT_END
-
-#endif
 
 G_END_DECLS
 
diff --git a/gi/pygi-value.c b/gi/pygi-value.c
index 0c54c3e3..e7ee1e06 100644
--- a/gi/pygi-value.c
+++ b/gi/pygi-value.c
@@ -19,7 +19,6 @@
 #include <Python.h>
 #include "pygi-value.h"
 #include "pygi-struct.h"
-#include "pygi-python-compat.h"
 #include "pygi-basictype.h"
 #include "pygobject-object.h"
 #include "pygi-type.h"
@@ -536,12 +535,13 @@ pyg_value_from_pyobject_with_error(GValue *value, PyObject *obj)
         else if (PySequence_Check(obj) &&
                 G_VALUE_HOLDS(value, G_TYPE_ARRAY))
             return pyg_array_from_pyobject(value, obj);
-        else if (PYGLIB_PyUnicode_Check(obj) &&
+        else if (PyUnicode_Check (obj) &&
                 G_VALUE_HOLDS(value, G_TYPE_GSTRING)) {
             GString *string;
             char *buffer;
             Py_ssize_t len;
-            if (PYGLIB_PyUnicode_AsStringAndSize(obj, &buffer, &len))
+            buffer = PyUnicode_AsUTF8AndSize (obj, &len);
+            if (buffer == NULL)
                 return -1;
             string = g_string_new_len(buffer, len);
             g_value_set_boxed(value, string);
@@ -655,9 +655,9 @@ pygi_value_to_py_basic_type (const GValue *value, GType fundamental, gboolean *h
     *handled = TRUE;
     switch (fundamental) {
         case G_TYPE_CHAR:
-            return PYGLIB_PyLong_FromLong (g_value_get_schar (value));
+            return PyLong_FromLong (g_value_get_schar (value));
         case G_TYPE_UCHAR:
-            return PYGLIB_PyLong_FromLong (g_value_get_uchar (value));
+            return PyLong_FromLong (g_value_get_uchar (value));
         case G_TYPE_BOOLEAN:
             return pygi_gboolean_to_py (g_value_get_boolean (value));
         case G_TYPE_INT:
@@ -747,7 +747,7 @@ value_to_py_structured_type (const GValue *value, GType fundamental, gboolean co
             return ret;
         } else if (G_VALUE_HOLDS(value, G_TYPE_GSTRING)) {
             GString *string = (GString *) g_value_get_boxed(value);
-            PyObject *ret = PYGLIB_PyUnicode_FromStringAndSize(string->str, string->len);
+            PyObject *ret = PyUnicode_FromStringAndSize (string->str, string->len);
             return ret;
         }
         bm = pyg_type_lookup(G_VALUE_TYPE(value));
@@ -816,10 +816,10 @@ pyg_value_as_pyobject (const GValue *value, gboolean copy_boxed)
      * See: https://bugzilla.gnome.org/show_bug.cgi?id=733893 */
     if (fundamental == G_TYPE_CHAR) {
         gint8 val = g_value_get_schar(value);
-        return PYGLIB_PyUnicode_FromStringAndSize ((char *)&val, 1);
+        return PyUnicode_FromStringAndSize ((char *)&val, 1);
     } else if (fundamental == G_TYPE_UCHAR) {
         guint8 val = g_value_get_uchar(value);
-        return PYGLIB_PyBytes_FromStringAndSize ((char *)&val, 1);
+        return PyBytes_FromStringAndSize ((char *)&val, 1);
     }
 
     pyobj = pygi_value_to_py_basic_type (value, fundamental, &handled);
diff --git a/gi/pyginterface.c b/gi/pyginterface.c
index a80b40ff..db15587c 100644
--- a/gi/pyginterface.c
+++ b/gi/pyginterface.c
@@ -23,7 +23,7 @@
 #include <Python.h>
 #include <glib-object.h>
 
-#include "pygi-python-compat.h"
+#include "pygi-util.h"
 #include "pyginterface.h"
 #include "pygi-type.h"
 
diff --git a/gi/pygobject-object.c b/gi/pygobject-object.c
index 76b18ccc..8163947d 100644
--- a/gi/pygobject-object.c
+++ b/gi/pygobject-object.c
@@ -263,7 +263,7 @@ build_parameter_list(GObjectClass *class)
        name = g_strdup(g_param_spec_get_name(props[i]));
        /* hyphens cannot belong in identifiers */
        g_strdelimit(name, "-", '_');
-       prop_str = PYGLIB_PyUnicode_FromString(name);
+       prop_str = PyUnicode_FromString (name);
        
        PyList_SetItem(props_list, i, prop_str);
        g_free(name);
@@ -282,7 +282,7 @@ PyGProps_getattro(PyGProps *self, PyObject *attr)
     GObjectClass *class;
     GParamSpec *pspec;
 
-    attr_name = PYGLIB_PyUnicode_AsString(attr);
+    attr_name = PyUnicode_AsUTF8 (attr);
     if (!attr_name) {
         PyErr_Clear();
         return PyObject_GenericGetAttr((PyObject *)self, attr);
@@ -336,7 +336,7 @@ set_property_from_pspec(GObject *obj,
         PyObject *pvalue_str = PyObject_Repr(pvalue);
        PyErr_Format(PyExc_TypeError,
                     "could not convert %s to type '%s' when setting property '%s.%s'",
-                    PYGLIB_PyUnicode_AsString(pvalue_str),
+                    PyUnicode_AsUTF8 (pvalue_str),
                     g_type_name(G_PARAM_SPEC_VALUE_TYPE(pspec)),
                     G_OBJECT_TYPE_NAME(obj),
                     pspec->name);
@@ -368,7 +368,7 @@ PyGProps_setattro(PyGProps *self, PyObject *attr, PyObject *pvalue)
        return -1;
     }
 
-    attr_name = PYGLIB_PyUnicode_AsString(attr);
+    attr_name = PyUnicode_AsUTF8 (attr);
     if (!attr_name) {
         PyErr_Clear();
         return PyObject_GenericSetAttr((PyObject *)self, attr, pvalue);
@@ -579,7 +579,7 @@ pygobject_register_class(PyObject *dict, const gchar *type_name,
      */
     s = strrchr(type->tp_name, '.');
     if (s != NULL) {
-       mod_name = PYGLIB_PyUnicode_FromStringAndSize(type->tp_name, (int)(s - type->tp_name));
+       mod_name = PyUnicode_FromStringAndSize (type->tp_name, (int)(s - type->tp_name));
        PyDict_SetItemString(type->tp_dict, "__module__", mod_name);
        Py_DECREF(mod_name);
     }
@@ -795,7 +795,7 @@ pygobject_new_with_interfaces(GType gtype)
 
     /* Something special to point out that it's not accessible through
      * gi.repository */
-    o = PYGLIB_PyUnicode_FromString ("__gi__");
+    o = PyUnicode_FromString ("__gi__");
     PyDict_SetItemString (dict, "__module__", o);
     Py_DECREF (o);
 
@@ -870,17 +870,11 @@ static void
 pygobject_inherit_slots(PyTypeObject *type, PyObject *bases, gboolean check_for_present)
 {
     static int slot_offsets[] = { offsetof(PyTypeObject, tp_richcompare),
-#if PY_VERSION_HEX < 0x03000000
-                                  offsetof(PyTypeObject, tp_compare),
-#endif
                                   offsetof(PyTypeObject, tp_richcompare),
                                   offsetof(PyTypeObject, tp_hash),
                                   offsetof(PyTypeObject, tp_iter),
                                   offsetof(PyTypeObject, tp_repr),
                                   offsetof(PyTypeObject, tp_str),
-#if PY_VERSION_HEX < 0x03000000
-                                  offsetof(PyTypeObject, tp_print),
-#endif
     };
     gsize i;
 
@@ -1163,10 +1157,10 @@ pygobject_richcompare(PyObject *self, PyObject *other, int op)
                                op);
 }
 
-static PYGLIB_Py_hash_t
+static Py_hash_t
 pygobject_hash(PyGObject *self)
 {
-    return PYGLIB_Py_hash_t_FromVoidPtr (self->obj);
+    return (Py_hash_t)(gintptr)(self->obj);
 }
 
 static PyObject *
@@ -1179,12 +1173,12 @@ pygobject_repr(PyGObject *self)
     if (module == NULL)
         return NULL;
 
-    if (!PYGLIB_PyUnicode_Check (module)) {
+    if (!PyUnicode_Check (module)) {
         Py_DECREF (module);
         return NULL;
     }
 
-    module_str = PYGLIB_PyUnicode_AsString (module);
+    module_str = PyUnicode_AsUTF8 (module);
     namespace = g_strrstr (module_str, ".");
     if (namespace == NULL) {
         namespace = module_str;
@@ -1192,10 +1186,10 @@ pygobject_repr(PyGObject *self)
         namespace += 1;
     }
 
-    repr = PYGLIB_PyUnicode_FromFormat ("<%s.%s object at %p (%s at %p)>",
-                                        namespace, Py_TYPE (self)->tp_name, self,
-                                        self->obj ? G_OBJECT_TYPE_NAME (self->obj) : "uninitialized",
-                                        self->obj);
+    repr = PyUnicode_FromFormat ("<%s.%s object at %p (%s at %p)>",
+                                 namespace, Py_TYPE (self)->tp_name, self,
+                                 self->obj ? G_OBJECT_TYPE_NAME (self->obj) : "uninitialized",
+                                 self->obj);
     Py_DECREF (module);
     return repr;
 }
@@ -1277,7 +1271,7 @@ pygobject_prepare_construct_properties(GObjectClass *class, PyObject *kwargs,
             GParamSpec *pspec;
             GValue *gvalue = &(*values)[*n_properties];
 
-            const gchar *key_str = PYGLIB_PyUnicode_AsString(key);
+            const gchar *key_str = PyUnicode_AsUTF8 (key);
 
             pspec = g_object_class_find_property(class, key_str);
             if (!pspec) {
@@ -1401,13 +1395,13 @@ pygobject_get_properties(PyGObject *self, PyObject *args)
         gchar *property_name;
         PyObject *item;
 
-        if (!PYGLIB_PyUnicode_Check(py_property)) {
+        if (!PyUnicode_Check (py_property)) {
             PyErr_SetString(PyExc_TypeError,
                             "Expected string argument for property.");
             goto fail;
         }
 
-        property_name = PYGLIB_PyUnicode_AsString(py_property);
+        property_name = PyUnicode_AsUTF8 (py_property);
         item = pygi_get_property_value_by_name (self, property_name);
         PyTuple_SetItem (tuple, i, item);
     }
@@ -1474,7 +1468,7 @@ pygobject_set_properties(PyGObject *self, PyObject *args, PyObject *kwargs)
     pos = 0;
 
     while (kwargs && PyDict_Next (kwargs, &pos, &key, &value)) {
-       gchar *key_str = PYGLIB_PyUnicode_AsString(key);
+       gchar *key_str = PyUnicode_AsUTF8 (key);
        GParamSpec *pspec;
        int ret = -1;
 
@@ -1658,8 +1652,8 @@ pygobject_bind_property(PyGObject *self, PyObject *args)
                source_repr = PyObject_Repr((PyObject*)self);
                target_repr = PyObject_Repr(target);
                PyErr_Format(PyExc_TypeError, "Cannot create binding from %s.%s to %s.%s",
-                            PYGLIB_PyUnicode_AsString(source_repr), source_name,
-                            PYGLIB_PyUnicode_AsString(target_repr), target_name);
+                            PyUnicode_AsUTF8 (source_repr), source_name,
+                            PyUnicode_AsUTF8 (target_repr), target_name);
                Py_DECREF(source_repr);
                Py_DECREF(target_repr);
                return NULL;
@@ -1681,7 +1675,7 @@ connect_helper(PyGObject *self, gchar *name, PyObject *callback, PyObject *extra
                             &sigid, &detail, TRUE)) {
        PyObject *repr = PyObject_Repr((PyObject*)self);
        PyErr_Format(PyExc_TypeError, "%s: unknown signal name: %s",
-                    PYGLIB_PyUnicode_AsString(repr),
+                    PyUnicode_AsUTF8 (repr),
                     name);
        Py_DECREF(repr);
        return NULL;
@@ -1889,7 +1883,7 @@ pygobject_emit(PyGObject *self, PyObject *args)
                             &signal_id, &detail, TRUE)) {
        repr = PyObject_Repr((PyObject*)self);
        PyErr_Format(PyExc_TypeError, "%s: unknown signal name: %s",
-                    PYGLIB_PyUnicode_AsString(repr),
+                    PyUnicode_AsUTF8 (repr),
                     name);
        Py_DECREF(repr);
        return NULL;
@@ -2102,7 +2096,7 @@ pygobject_disconnect_by_func(PyGObject *self, PyObject *args)
     if (!closure) {
        repr = PyObject_Repr((PyObject*)pyfunc);
        PyErr_Format(PyExc_TypeError, "nothing connected to %s",
-                    PYGLIB_PyUnicode_AsString(repr));
+                    PyUnicode_AsUTF8 (repr));
        Py_DECREF(repr);
        return NULL;
     }
@@ -2136,7 +2130,7 @@ pygobject_handler_block_by_func(PyGObject *self, PyObject *args)
     if (!closure) {
        repr = PyObject_Repr((PyObject*)pyfunc);
        PyErr_Format(PyExc_TypeError, "nothing connected to %s",
-                    PYGLIB_PyUnicode_AsString(repr));
+                    PyUnicode_AsUTF8 (repr));
        Py_DECREF(repr);
        return NULL;
     }
@@ -2170,7 +2164,7 @@ pygobject_handler_unblock_by_func(PyGObject *self, PyObject *args)
     if (!closure) {
        repr = PyObject_Repr((PyObject*)pyfunc);
        PyErr_Format(PyExc_TypeError, "nothing connected to %s",
-                    PYGLIB_PyUnicode_AsString(repr));
+                    PyUnicode_AsUTF8 (repr));
        Py_DECREF(repr);
        return NULL;
     }
@@ -2473,7 +2467,7 @@ pyi_object_register_types(PyObject *d)
     descr = PyObject_New(PyObject, &PyGPropsDescr_Type);
     PyDict_SetItemString(PyGObject_Type.tp_dict, "props", descr);
     PyDict_SetItemString(PyGObject_Type.tp_dict, "__module__",
-                        o=PYGLIB_PyUnicode_FromString("gi._gi"));
+                        o=PyUnicode_FromString ("gi._gi"));
     Py_DECREF(o);
 
     /* GPropsIter */
diff --git a/gi/pygobject-object.h b/gi/pygobject-object.h
index 580939a2..95e95aad 100644
--- a/gi/pygobject-object.h
+++ b/gi/pygobject-object.h
@@ -3,7 +3,6 @@
 
 #include <Python.h>
 #include <glib-object.h>
-#include "pygi-python-compat.h"
 #include "pygobject-internal.h"
 
 /* Data that belongs to the GObject instance, not the Python wrapper */
diff --git a/gi/pygoptioncontext.c b/gi/pygoptioncontext.c
index 61ecb763..f9f4a652 100644
--- a/gi/pygoptioncontext.c
+++ b/gi/pygoptioncontext.c
@@ -151,7 +151,7 @@ pyg_option_context_parse(PyGOptionContext *self,
     for (pos = 0; pos < argv_length; pos++)
     {
         arg = PyList_GetItem(argv, pos);
-        argv_content[pos] = g_strdup(PYGLIB_PyUnicode_AsString(arg));
+        argv_content[pos] = g_strdup(PyUnicode_AsUTF8 (arg));
         if (argv_content[pos] == NULL)
         {
             g_strfreev(argv_content);
@@ -179,7 +179,7 @@ pyg_option_context_parse(PyGOptionContext *self,
     new_argv = PyList_New(g_strv_length(argv_content));
     for (pos = 0; pos < argv_length; pos++)
     {
-        arg = PYGLIB_PyUnicode_FromString(argv_content[pos]);
+        arg = PyUnicode_FromString (argv_content[pos]);
         PyList_SetItem(new_argv, pos, arg);
     }
 
diff --git a/gi/pygparamspec.c b/gi/pygparamspec.c
index 2c59181d..133119c0 100644
--- a/gi/pygparamspec.c
+++ b/gi/pygparamspec.c
@@ -46,10 +46,10 @@ pyg_param_spec_richcompare(PyObject *self, PyObject *other, int op)
     }
 }
 
-static PYGLIB_Py_hash_t
+static Py_hash_t
 pyg_param_spec_hash(PyGParamSpec *self)
 {
-    return PYGLIB_Py_hash_t_FromVoidPtr (pyg_param_spec_get (self));
+    return (Py_hash_t)(gintptr)(pyg_param_spec_get (self));
 }
 
 static PyObject *
@@ -60,7 +60,7 @@ pyg_param_spec_repr(PyGParamSpec *self)
     g_snprintf(buf, sizeof(buf), "<%s '%s'>",
               G_PARAM_SPEC_TYPE_NAME (pyg_param_spec_get (self)),
               g_param_spec_get_name (pyg_param_spec_get (self)));
-    return PYGLIB_PyUnicode_FromString(buf);
+    return PyUnicode_FromString (buf);
 }
 
 static void
@@ -136,7 +136,7 @@ pyg_param_spec_getattr(PyGParamSpec *self, const gchar *attr)
 
     if (G_IS_PARAM_SPEC_CHAR(pspec)) {
        if (!strcmp(attr, "default_value")) {
-           return PYGLIB_PyUnicode_FromFormat(
+           return PyUnicode_FromFormat(
                "%c", G_PARAM_SPEC_CHAR(pspec)->default_value);
        } else if (!strcmp(attr, "minimum")) {
            return pygi_gint8_to_py (G_PARAM_SPEC_CHAR(pspec)->minimum);
@@ -145,7 +145,7 @@ pyg_param_spec_getattr(PyGParamSpec *self, const gchar *attr)
        }
     } else if (G_IS_PARAM_SPEC_UCHAR(pspec)) {
        if (!strcmp(attr, "default_value")) {
-           return PYGLIB_PyUnicode_FromFormat(
+           return PyUnicode_FromFormat(
                "%c", G_PARAM_SPEC_UCHAR(pspec)->default_value);
        } else if (!strcmp(attr, "minimum")) {
            return pygi_guint8_to_py (G_PARAM_SPEC_UCHAR(pspec)->minimum);
@@ -206,7 +206,7 @@ pyg_param_spec_getattr(PyGParamSpec *self, const gchar *attr)
        }
     } else if (G_IS_PARAM_SPEC_UNICHAR(pspec)) {
        if (!strcmp(attr, "default_value")) {
-           return PYGLIB_PyUnicode_FromFormat(
+           return PyUnicode_FromFormat(
                "%c", G_PARAM_SPEC_UNICHAR(pspec)->default_value);
        }
     } else if (G_IS_PARAM_SPEC_ENUM(pspec)) {
diff --git a/gi/pygpointer.c b/gi/pygpointer.c
index 8e13374b..7818221f 100644
--- a/gi/pygpointer.c
+++ b/gi/pygpointer.c
@@ -51,10 +51,10 @@ pyg_pointer_richcompare(PyObject *self, PyObject *other, int op)
     }
 }
 
-static PYGLIB_Py_hash_t
+static Py_hash_t
 pyg_pointer_hash(PyGPointer *self)
 {
-    return PYGLIB_Py_hash_t_FromVoidPtr (pyg_pointer_get_ptr (self));
+    return (Py_hash_t)(gintptr)(pyg_pointer_get_ptr (self));
 }
 
 static PyObject *
@@ -65,7 +65,7 @@ pyg_pointer_repr(PyGPointer *self)
     g_snprintf(buf, sizeof(buf), "<%s at 0x%" G_GUINTPTR_FORMAT ">",
                g_type_name(self->gtype),
                (guintptr)pyg_pointer_get_ptr (self));
-    return PYGLIB_PyUnicode_FromString(buf);
+    return PyUnicode_FromString (buf);
 }
 
 static int
diff --git a/gi/pygspawn.c b/gi/pygspawn.c
index 9457c6de..b2825637 100644
--- a/gi/pygspawn.c
+++ b/gi/pygspawn.c
@@ -22,7 +22,7 @@
 #include <Python.h>
 #include <glib.h>
 
-#include "pygi-python-compat.h"
+#include "pygi-util.h"
 #include "pygi-basictype.h"
 #include "pygspawn.h"
 #include "pygi-error.h"
@@ -32,7 +32,7 @@ struct _PyGChildSetupData {
     PyObject *data;
 };
 
-PYGLIB_DEFINE_TYPE("gi._gi.Pid", PyGPid_Type, PYGLIB_PyLongObject)
+PYGLIB_DEFINE_TYPE("gi._gi.Pid", PyGPid_Type, PyLongObject)
 
 static GPid
 pyg_pid_get_pid (PyObject *self)
@@ -40,7 +40,7 @@ pyg_pid_get_pid (PyObject *self)
 #ifdef G_OS_WIN32
     return (GPid)PyLong_AsVoidPtr (self);
 #else
-    return (GPid)PYGLIB_PyLong_AsLong (self);
+    return (GPid)PyLong_AsLong (self);
 #endif
 }
 
@@ -61,7 +61,7 @@ static void
 pyg_pid_free(PyObject *gpid)
 {
     g_spawn_close_pid(pyg_pid_get_pid (gpid));
-    PYGLIB_PyLong_Type.tp_free((void *) gpid);
+    PyLong_Type.tp_free((void *) gpid);
 }
 
 static int
@@ -158,7 +158,7 @@ pyglib_spawn_async(PyObject *object, PyObject *args, PyObject *kwargs)
     argv = g_new0(char *, len + 1);
     for (i = 0; i < len; ++i) {
         PyObject *tmp = PySequence_ITEM(pyargv, i);
-        if (tmp == NULL || !PYGLIB_PyUnicode_Check(tmp)) {
+        if (tmp == NULL || !PyUnicode_Check (tmp)) {
             PyErr_SetString(PyExc_TypeError,
                             "gi._gi.spawn_async: "
                            "first argument must be a sequence of strings");
@@ -166,7 +166,7 @@ pyglib_spawn_async(PyObject *object, PyObject *args, PyObject *kwargs)
             Py_XDECREF(tmp);
             return NULL;
         }
-        argv[i] = PYGLIB_PyUnicode_AsString(tmp);
+        argv[i] = PyUnicode_AsUTF8 (tmp);
         Py_DECREF(tmp);
     }
 
@@ -183,7 +183,7 @@ pyglib_spawn_async(PyObject *object, PyObject *args, PyObject *kwargs)
         envp = g_new0(char *, len + 1);
         for (i = 0; i < len; ++i) {
             PyObject *tmp = PySequence_ITEM(pyenvp, i);
-            if (tmp == NULL || !PYGLIB_PyUnicode_Check(tmp)) {
+            if (tmp == NULL || !PyUnicode_Check (tmp)) {
                 PyErr_SetString(PyExc_TypeError,
                                 "gi._gi.spawn_async: "
                                "second argument must be a sequence of strings");
@@ -192,7 +192,7 @@ pyglib_spawn_async(PyObject *object, PyObject *args, PyObject *kwargs)
                g_free(argv);
                 return NULL;
             }
-            envp[i] = PYGLIB_PyUnicode_AsString(tmp);
+            envp[i] = PyUnicode_AsUTF8 (tmp);
             Py_DECREF(tmp);
         }
     }
@@ -266,12 +266,12 @@ pyglib_spawn_async(PyObject *object, PyObject *args, PyObject *kwargs)
 int
 pygi_spawn_register_types(PyObject *d)
 {
-    PyGPid_Type.tp_base = &PYGLIB_PyLong_Type;
+    PyGPid_Type.tp_base = &PyLong_Type;
     PyGPid_Type.tp_flags = Py_TPFLAGS_DEFAULT;
     PyGPid_Type.tp_methods = pyg_pid_methods;
     PyGPid_Type.tp_init = pyg_pid_tp_init;
     PyGPid_Type.tp_free = (freefunc)pyg_pid_free;
-    PyGPid_Type.tp_new = PYGLIB_PyLong_Type.tp_new;
+    PyGPid_Type.tp_new = PyLong_Type.tp_new;
     PYGLIB_REGISTER_TYPE(d, PyGPid_Type, "Pid");
 
     return 0;
diff --git a/tests/testhelpermodule.c b/tests/testhelpermodule.c
index 82b0399e..848d8203 100644
--- a/tests/testhelpermodule.c
+++ b/tests/testhelpermodule.c
@@ -5,7 +5,12 @@
 #include "test-unknown.h"
 #include "test-floating.h"
 
-#include "pygi-python-compat.h"
+#define PYGLIB_DEFINE_TYPE(typename, symbol, csymbol)  \
+PyTypeObject symbol = {                                 \
+    PyVarObject_HEAD_INIT(NULL, 0)                      \
+    typename,                                           \
+    sizeof(csymbol)                                     \
+};
 
 static PyObject * _wrap_TestInterface__do_iface_method(PyObject *cls,
                                                       PyObject *args,
@@ -82,7 +87,7 @@ _wrap_test_g_object_new (PyObject * self)
     PyObject *rv;
 
     obj = g_object_new(g_type_from_name("PyGObject"), NULL);
-    rv = PYGLIB_PyLong_FromLong(obj->ref_count); /* should be == 2 at this point */
+    rv = PyLong_FromLong(obj->ref_count); /* should be == 2 at this point */
     g_object_unref(obj);
     return rv;
 }
@@ -583,7 +588,7 @@ _wrap_constant_strip_prefix(PyObject *self, PyObject *args)
         return NULL;
 
     result = pyg_constant_strip_prefix (name, strip_prefix);
-    return PYGLIB_PyUnicode_FromString (result);
+    return PyUnicode_FromString (result);
 }
 
 static PyObject *
@@ -676,7 +681,7 @@ _wrap_test_parse_constructor_args (PyObject *self, PyObject *args)
         return NULL;
     }
 
-    return PYGLIB_PyLong_FromLong (nparams);
+    return PyLong_FromLong (nparams);
 }
 
 G_GNUC_END_IGNORE_DEPRECATIONS
@@ -693,7 +698,7 @@ _wrap_test_to_unichar_conv (PyObject * self, PyObject *args)
     if (!pyg_pyobj_to_unichar_conv (obj, &result))
         return NULL;
 
-    return PYGLIB_PyLong_FromLong (result);
+    return PyLong_FromLong (result);
 }
 
 static PyMethodDef testhelper_functions[] = {
@@ -715,11 +720,32 @@ static PyMethodDef testhelper_functions[] = {
     { NULL, NULL }
 };
 
-PYGLIB_MODULE_START(testhelper, "testhelper")
-{
+static struct PyModuleDef _testhelpermodule = {
+    PyModuleDef_HEAD_INIT,
+    "testhelper",
+    NULL,
+    -1,
+    testhelper_functions,
+    NULL,
+    NULL,
+    NULL,
+    NULL
+};
+
+#ifdef __GNUC__
+#define PYGI_MODINIT_FUNC __attribute__((visibility("default"))) PyMODINIT_FUNC
+#else
+#define PYGI_MODINIT_FUNC PyMODINIT_FUNC
+#endif
+
+PYGI_MODINIT_FUNC PyInit_testhelper(void);
+
+PYGI_MODINIT_FUNC PyInit_testhelper(void) {
+  PyObject *module;
   PyObject *gobject_module;
   PyObject *m, *d;
 
+  module = PyModule_Create(&_testhelpermodule);
 
   if ((gobject_module = pygobject_init(-1, -1, -1)) == NULL)
     return NULL;
@@ -730,7 +756,7 @@ PYGLIB_MODULE_START(testhelper, "testhelper")
   if ((m = PyImport_ImportModule("gi.repository.GObject")) == NULL) {
     PyErr_SetString(PyExc_ImportError,
                    "could not import gobject");
-    return PYGLIB_MODULE_ERROR_RETURN;
+    return NULL;
   }
 
   /* TestInterface */
@@ -779,6 +805,7 @@ PYGLIB_MODULE_START(testhelper, "testhelper")
                            &PyTestFloatingAndSunk_Type,
                            Py_BuildValue("(O)",
                            &PyGObject_Type));
+
+  return module;
 }
-PYGLIB_MODULE_END
 



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