void state_tracker_free(state_tracker_t *tracker) { free(tracker->info); #ifdef HAVE_PYTHON py_state_free(tracker->py); #endif free(tracker); }
py_state_t *py_state_new(const char *script, unsigned is_file, const char *pyclass) { py_state_t *handle; PyObject *hook; RARCH_LOG("Initializing Python runtime ...\n"); PyImport_AppendInittab("rarch", &PyInit_Retro); Py_Initialize(); RARCH_LOG("Initialized Python runtime.\n"); handle = (py_state_t*)calloc(1, sizeof(*handle)); hook = NULL; handle->main = PyImport_AddModule("__main__"); if (!handle->main) goto error; Py_INCREF(handle->main); if (is_file) { /* Have to hack around the fact that the FILE struct * isn't standardized across environments. * PyRun_SimpleFile() breaks on Windows because it's * compiled with MSVC. */ ssize_t len; char *script_ = NULL; bool ret = retro_read_file (script, (void**)&script_, &len); if (!ret || len < 0) { RARCH_ERR("Python: Failed to read script\n"); goto error; } PyRun_SimpleString(script_); free(script_); } else { char *script_ = align_program(script); if (script_) { PyRun_SimpleString(script_); free(script_); } } RARCH_LOG("Python: Script loaded.\n"); handle->dict = PyModule_GetDict(handle->main); if (!handle->dict) { RARCH_ERR("Python: PyModule_GetDict() failed.\n"); goto error; } Py_INCREF(handle->dict); hook = PyDict_GetItemString(handle->dict, pyclass); if (!hook) { RARCH_ERR("Python: PyDict_GetItemString() failed.\n"); goto error; } handle->inst = PyObject_CallFunction(hook, NULL); if (!handle->inst) { RARCH_ERR("Python: PyObject_CallFunction() failed.\n"); goto error; } Py_INCREF(handle->inst); return handle; error: PyErr_Print(); PyErr_Clear(); py_state_free(handle); return NULL; }