#include <assert.h>
#include <pthread.h>
#include <time.h>

#include <Python.h>


void *
evil_main (void *_)
{
    PyGILState_STATE gilstate;

    gilstate = PyGILState_Ensure();
    printf("OTHER: acquired GIL\n");

    Py_BEGIN_ALLOW_THREADS
    printf("OTHER: released GIL\n");
    sleep(2);
    printf("OTHER: attempt to acquire GIL...crash!\n");
    Py_END_ALLOW_THREADS

    /* Dead code */
    assert(0);
    PyGILState_Release(gilstate);
    return (NULL);
}


int
main (int argc, char **argv)
{
    int                 rc;
    pthread_t           evil;

    /* Suppress warnings from get_path.c */
    Py_FrozenFlag = 1;

    /* Don't try to import site */
    Py_NoSiteFlag = 1;

    Py_SetPath(L"./Lib");
    Py_Initialize();
    PyEval_InitThreads();

    rc = pthread_create(&evil, NULL, evil_main, NULL);
    assert(rc == 0);

    Py_BEGIN_ALLOW_THREADS
    printf("MAIN: allow other thread to execute\n");
    sleep(1);
    Py_END_ALLOW_THREADS

    Py_Finalize();
    printf("MAIN: interpreter finalized\n");

    sleep(10);
    /* Dead code */
    assert(0);
    return (0);
}
