#define PY_SSIZE_T_CLEAN
#include <Python.h>

int
main(int argc, char *argv[])
{
    Py_Initialize();

    PyThreadState* _main = PyThreadState_Get();

    PyGILState_STATE state = PyGILState_Ensure();

    PyThreadState *interpreter1 = Py_NewInterpreter();

    char *filename = "subinterpreter_ssl_issue.py";
    FILE *fp = fopen(filename, "r");
    PyRun_SimpleFile(fp, filename);

    PyThreadState_Swap(_main);

    PyThreadState *interpreter2 = Py_NewInterpreter();

    PyRun_String("import _ssl", Py_file_input, PyDict_New(), NULL);

    PyThreadState_Swap(interpreter1);

    PyObject *evMain, *evDict, *evFunc;
    evMain = PyImport_AddModule((char *) "__main__");
    evDict = PyModule_GetDict(evMain);
    evFunc = PyDict_GetItemString(evDict, "read_socket");
    PyObject_CallFunction(evFunc, NULL);

    Py_EndInterpreter(interpreter1);

    PyThreadState_Swap(interpreter2);
    Py_EndInterpreter(interpreter2);

    PyThreadState_Swap(_main);

    PyGILState_Release(state);

    if (Py_FinalizeEx() < 0) {
        exit(120);
    }
    return 0;
}
