

#include "Python.h"


static PyObject*
import_from(char *modulename, char *fromname, PyObject *scope) {
	PyObject *fromlist, *name, *module;

	fromlist = PyTuple_New(1);
	if (fromlist == NULL) return NULL;

	name = PyString_FromString(fromname);
	if (name == NULL) {
		Py_DECREF(fromlist);
		return NULL;
	}
  	Py_INCREF(name);
	PyTuple_SET_ITEM(fromlist, 0, name);

	module = PyImport_ImportModuleEx(modulename, scope, scope, fromlist);
	Py_DECREF(fromlist);
	if (module == NULL) {
		Py_DECREF(name);
		return NULL;
	}

	fromlist = PyObject_GetAttr(module, name);
	Py_DECREF(module);
	Py_DECREF(name);
	return fromlist;
}


int
main (int argc, char *argv[])
{
	static char *python_prog_name="test";
	static char *python_args[]={ "" };

	PyObject *main_module, *main_locals;
	PyObject *gtk, *wt;

	Py_SetProgramName(python_prog_name);
	Py_Initialize();
	PySys_SetArgv(1, python_args);

	main_module = PyImport_AddModule("__main__");
	if (main_module == NULL) return 1;

	main_locals = PyModule_GetDict(main_module);
	if (main_locals == NULL) return 2;

	/* from gi.repository import Gtk */
	gtk = import_from("gi.repository", "Gtk", main_locals);
	if (gtk == NULL) return 3;

	/* wt = Gtk.WindowType */
	wt = PyObject_GetAttrString(gtk, "WindowType");
	if (wt == NULL) return 4;

	/* print(repr(wt)) */
	PyObject_Print(wt, stdout, 0);

	return 0;
}
