#include <Python.h>
#include <compile.h>
#include <eval.h>

const char *py_source =
    "def fn():\n"
    "   yield 1\n";

int main ()
{
  Py_Initialize();

  printf("debug: %d\n", Py_DebugFlag);

  PyObject *globals = PyDict_New();

  // insert function code into interpreter
  PyObject *code = PyRun_String(py_source, Py_file_input, globals, NULL);
  Py_DECREF(code);

  // compile call to the function
  code = Py_CompileString("fn()", "<string>", Py_eval_input);

  // do call
  PyObject *gen = PyEval_EvalCode((PyCodeObject *)code, globals, NULL);

  // iterate result
  PyObject *item;
  while ((item = PyIter_Next(gen))) {
      printf("> %ld\n", PyInt_AsLong(item));
      Py_DECREF(item);
  }

  Py_DECREF(gen);
  Py_DECREF(code);
  Py_DECREF(globals);

  Py_Finalize();
  return 0;
}

 	  	 

