void PyInviwo::initPythonCInterface() { if (isInit_) return; isInit_ = true; LogInfo("Python version: " + toString(Py_GetVersion())); wchar_t programName[] = L"PyInviwo"; Py_SetProgramName(programName); #ifdef WIN32 Py_NoSiteFlag = 1; #endif Py_InitializeEx(false); if (!Py_IsInitialized()) { LogError("Python is not Initialized"); return; } PyEval_InitThreads(); mainDict_ = PyDict_New(); modulesDict_ = PyImport_GetModuleDict(); importModule("builtins"); importModule("sys"); importModule("os"); importModule("glob"); importModule("random"); addModulePath(InviwoApplication::getPtr()->getBasePath() + "/modules/python3/scripts"); initDefaultInterfaces(); initOutputRedirector(); }
PyObject * AerospikeGeospatial_DoDumps(PyObject *geo_data, as_error *err) { PyObject *initresult = NULL; PyObject* sysmodules = PyImport_GetModuleDict(); PyObject* json_module = NULL; if (PyMapping_HasKeyString(sysmodules, "json")) { json_module = PyMapping_GetItemString(sysmodules, "json"); } else { json_module = PyImport_ImportModule("json"); } if (!json_module) { /* insert error handling here! and exit this function */ as_error_update(err, AEROSPIKE_ERR_CLIENT, "Unable to load json module"); } else { PyObject *py_funcname = PyString_FromString("dumps"); Py_INCREF(json_module); initresult = PyObject_CallMethodObjArgs(json_module, py_funcname, geo_data, NULL); Py_DECREF(json_module); Py_DECREF(py_funcname); } return initresult; }
PyObject *BPY_rna_props(void) { PyObject *submodule; PyObject *submodule_dict; submodule= PyModule_Create(&props_module); PyDict_SetItemString(PyImport_GetModuleDict(), props_module.m_name, submodule); /* INCREF since its its assumed that all these functions return the * module with a new ref like PyDict_New, since they are passed to * PyModule_AddObject which steals a ref */ Py_INCREF(submodule); /* api needs the PyObjects internally */ submodule_dict= PyModule_GetDict(submodule); #define ASSIGN_STATIC(_name) pymeth_##_name= PyDict_GetItemString(submodule_dict, #_name) ASSIGN_STATIC(BoolProperty); ASSIGN_STATIC(BoolVectorProperty); ASSIGN_STATIC(IntProperty); ASSIGN_STATIC(IntVectorProperty); ASSIGN_STATIC(FloatProperty); ASSIGN_STATIC(FloatVectorProperty); ASSIGN_STATIC(StringProperty); ASSIGN_STATIC(EnumProperty); ASSIGN_STATIC(PointerProperty); ASSIGN_STATIC(CollectionProperty); ASSIGN_STATIC(RemoveProperty); return submodule; }
void PyInviwo::initPythonCInterface(Python3Module* module) { if (isInit_) return; isInit_ = true; LogInfo("Python version: " + toString(Py_GetVersion())); wchar_t programName[] = L"PyInviwo"; Py_SetProgramName(programName); Py_InitializeEx(false); if (!Py_IsInitialized()) { LogError("Python is not Initialized"); return; } PyEval_InitThreads(); importModule("builtins"); importModule("sys"); dict_ = PyImport_GetModuleDict(); registerPyModule(&Inviwo_Internals_Module_Def, "inviwo_internal"); registerPyModule(&Inviwo_Module_Def, "inviwo"); addModulePath(module->getPath() + "/scripts"); initOutputRedirector(module); }
PyObject* loadModule_(const QString& name) { PyObject* module_dict = PyImport_GetModuleDict(); if(!module_dict) { Log.error() << "Could not obtain module dictionary" << std::endl; return 0; } PyObject* mod_name = PyString_FromString(name.toAscii().data()); PyObject* module = 0; if(PyDict_Contains(module_dict, mod_name)) { module = PyDict_GetItem(module_dict, mod_name); } else { //This could leak the imported module. Need to check... module = PyImport_ImportModule(name.toAscii().data()); } Py_XDECREF(mod_name); if(!module || PyErr_Occurred()) { PyErr_Print(); return 0; } return module; }
bool uninstall_py_dlls(void) { PyObject *modules = PyImport_GetModuleDict(); int i = 0; while(true) { if(g_funs[i] == NULL) break; DEBUG_MSG(fmt::format("Script::uninstall_py_dlls(): {}\n", g_sfuns[i])); PyObject * m = g_importedModules[i++]; if(m == NULL) { return false; } Py_DECREF(m); struct PyModuleDef *def; def = PyModule_GetDef(m); if (!def) { PyErr_BadInternalCall(); return false; } if (PyState_RemoveModule(def) < 0 || PyDict_DelItemString(modules, def->m_name) < 0) return false; } return true; }
PyObject *GPU_initPython(void) { PyObject *module = PyInit_gpu(); PyModule_AddObject(module, "export_shader", (PyObject *)PyCFunction_New(meth_export_shader, NULL)); PyDict_SetItemString(PyImport_GetModuleDict(), "gpu", module); return module; }
PyObject* AUD_initPython() { PyObject* module = PyInit_aud(); PyModule_AddObject(module, "device", (PyObject*)PyCFunction_New(meth_getcdevice, NULL)); PyModule_AddObject(module, "_sound_from_pointer", (PyObject*)PyCFunction_New(meth_sound_from_pointer, NULL)); PyDict_SetItemString(PyImport_GetModuleDict(), "aud", module); return module; }
PyObject * _PyImport_LoadDynamicModule(char *name, char *pathname, FILE *fp) { PyObject *m, *d, *s; char *lastdot, *shortname, *packagecontext; dl_funcptr p; if ((m = _PyImport_FindExtension(name, pathname)) != NULL) { Py_INCREF(m); return m; } lastdot = strrchr(name, '.'); if (lastdot == NULL) { packagecontext = NULL; shortname = name; } else { packagecontext = name; shortname = lastdot+1; } p = _PyImport_GetDynLoadFunc(name, shortname, pathname, fp); if (PyErr_Occurred()) return NULL; if (p == NULL) { PyErr_Format(PyExc_ImportError, "dynamic module does not define init function (init%.200s)", shortname); return NULL; } _Py_PackageContext = packagecontext; (*p)(); _Py_PackageContext = NULL; if (PyErr_Occurred()) return NULL; if (_PyImport_FixupExtension(name, pathname) == NULL) return NULL; m = PyDict_GetItemString(PyImport_GetModuleDict(), name); if (m == NULL) { PyErr_SetString(PyExc_SystemError, "dynamic module not initialized properly"); return NULL; } /* Remember the filename as the __file__ attribute */ d = PyModule_GetDict(m); s = PyString_FromString(pathname); if (s == NULL || PyDict_SetItemString(d, "__file__", s) != 0) PyErr_Clear(); /* Not important enough to report */ Py_XDECREF(s); if (Py_VerboseFlag) PySys_WriteStderr( "import %s # dynamically loaded from %s\n", name, pathname); Py_INCREF(m); return m; }
static PyObject* init (PyObject* self) { PyObject *allmodules, *moduleslist, *dict, *func, *result, *mod; int loop, num; int success=0, fail=0; if (!CheckSDLVersions ()) return NULL; /*nice to initialize timer, so startup time will reflec init() time*/ sdl_was_init = SDL_Init ( #if defined(WITH_THREAD) && !defined(MS_WIN32) && defined(SDL_INIT_EVENTTHREAD) SDL_INIT_EVENTTHREAD | #endif SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE) == 0; /* initialize all pygame modules */ allmodules = PyImport_GetModuleDict (); moduleslist = PyDict_Values (allmodules); if (!allmodules || !moduleslist) return Py_BuildValue ("(ii)", 0, 0); if (PyGame_Video_AutoInit ()) ++success; else ++fail; num = PyList_Size (moduleslist); for (loop = 0; loop < num; ++loop) { mod = PyList_GET_ITEM (moduleslist, loop); if (!mod || !PyModule_Check (mod)) continue; dict = PyModule_GetDict (mod); func = PyDict_GetItemString (dict, "__PYGAMEinit__"); if(func && PyCallable_Check (func)) { result = PyObject_CallObject (func, NULL); if (result && PyObject_IsTrue (result)) ++success; else { PyErr_Clear (); ++fail; } Py_XDECREF (result); } } Py_DECREF (moduleslist); return Py_BuildValue ("(ii)", success, fail); }
static void import_init(PyInterpreterState *interp, PyObject *sysmod) { PyObject *importlib; PyObject *impmod; PyObject *sys_modules; PyObject *value; /* Import _importlib through its frozen version, _frozen_importlib. */ if (PyImport_ImportFrozenModule("_frozen_importlib") <= 0) { Py_FatalError("Py_Initialize: can't import _frozen_importlib"); } else if (Py_VerboseFlag) { PySys_FormatStderr("import _frozen_importlib # frozen\n"); } importlib = PyImport_AddModule("_frozen_importlib"); if (importlib == NULL) { Py_FatalError("Py_Initialize: couldn't get _frozen_importlib from " "sys.modules"); } interp->importlib = importlib; Py_INCREF(interp->importlib); interp->import_func = PyDict_GetItemString(interp->builtins, "__import__"); if (interp->import_func == NULL) Py_FatalError("Py_Initialize: __import__ not found"); Py_INCREF(interp->import_func); /* Import the _imp module */ impmod = PyInit_imp(); if (impmod == NULL) { Py_FatalError("Py_Initialize: can't import _imp"); } else if (Py_VerboseFlag) { PySys_FormatStderr("import _imp # builtin\n"); } sys_modules = PyImport_GetModuleDict(); if (Py_VerboseFlag) { PySys_FormatStderr("import sys # builtin\n"); } if (PyDict_SetItemString(sys_modules, "_imp", impmod) < 0) { Py_FatalError("Py_Initialize: can't save _imp to sys.modules"); } /* Install importlib as the implementation of import */ value = PyObject_CallMethod(importlib, "_install", "OO", sysmod, impmod); if (value == NULL) { PyErr_Print(); Py_FatalError("Py_Initialize: importlib install failed"); } Py_DECREF(value); Py_DECREF(impmod); _PyImportZip_Init(); }
/* * Remove the function module from sys.modules * * >>> del sys.modules[func.oidstr] * * -1 on error. */ int PyPgFunction_RemoveModule(PyObj func) { int rv; PyObj modules; modules = PyImport_GetModuleDict(); rv = PySequence_Contains(modules, PyPgFunction_GetPyUnicodeOid(func)); if (rv == 1) rv = PyObject_DelItem(modules, PyPgFunction_GetPyUnicodeOid(func)); return(rv); }
/* returns borrowed reference */ _DLL_EXPORT PyObject *getJavaModule(PyObject *module, const char *parent, const char *name) { PyObject *modules = PyImport_GetModuleDict(); PyObject *parent_module, *full_name; if (parent[0] == '\0') { parent_module = NULL; full_name = PyString_FromString(name); } else if ((parent_module = PyDict_GetItemString(modules, parent)) == NULL) { PyErr_Format(PyExc_ValueError, "Parent module '%s' not found", parent); return NULL; } else full_name = PyString_FromFormat("%s.%s", parent, name); PyObject *child_module = PyDict_GetItem(modules, full_name); if (child_module == NULL) { child_module = PyModule_New(PyString_AS_STRING(full_name)); if (child_module != NULL) { if (parent_module != NULL) PyDict_SetItemString(PyModule_GetDict(parent_module), name, child_module); PyDict_SetItem(modules, full_name, child_module); Py_DECREF(child_module); /* borrow reference */ } } Py_DECREF(full_name); /* During __install__ pass, __file__ is not yet set on module. * During __initialize__ pass, __file__ is passed down to child_module. */ if (child_module != NULL) { PyObject *__file__ = PyString_FromString("__file__"); PyObject *file = PyDict_GetItem(PyModule_GetDict(module), __file__); if (file != NULL) PyDict_SetItem(PyModule_GetDict(child_module), __file__, file); Py_DECREF(__file__); } return child_module; }
void _PyStackless_Init(void) { PyObject *dict; PyObject *modules; char *name = "stackless"; PySlpModuleObject *m; if (init_slpmoduletype()) return; /* record the thread state for thread support */ slp_initial_tstate = PyThreadState_GET(); /* smuggle an instance of our module type into modules */ /* this is a clone of PyImport_AddModule */ modules = PyImport_GetModuleDict(); slp_module = slpmodule_new(name); if (slp_module == NULL || PyDict_SetItemString(modules, name, slp_module)) { Py_DECREF(slp_module); return; } Py_DECREF(slp_module); /* Yes, it still exists, in modules! */ /* Create the module and add the functions */ slp_module = Py_InitModule3("stackless", stackless_methods, stackless__doc__); if (slp_module == NULL) return; /* errors handled by caller */ if (init_prickelpit()) return; dict = PyModule_GetDict(slp_module); #define INSERT(name, object) \ if (PyDict_SetItemString(dict, name, (PyObject*)object) < 0) return INSERT("slpmodule", PySlpModule_TypePtr); INSERT("cframe", &PyCFrame_Type); INSERT("cstack", &PyCStack_Type); INSERT("bomb", &PyBomb_Type); INSERT("tasklet", &PyTasklet_Type); INSERT("channel", &PyChannel_Type); INSERT("stackless", slp_module); m = (PySlpModuleObject *) slp_module; slpmodule_set__tasklet__(m, &PyTasklet_Type, NULL); slpmodule_set__channel__(m, &PyChannel_Type, NULL); }
void PyInviwo::importModule(const std::string& moduleName) { auto mainDict = PyImport_GetModuleDict(); const static std::string __key__ = "__"; char* key = new char[moduleName.size() + 5]; sprintf(key, "__%s__", moduleName.c_str()); if (PyDict_GetItemString(mainDict, key) == nullptr) { PyObject* pMod = PyImport_ImportModule(moduleName.c_str()); if (nullptr != pMod) { PyDict_SetItemString(mainDict, key, pMod); } else { LogWarn("Failed to import python module: " << moduleName); } } delete[] key; }
static PlannerPythonEnv * planner_python_env_new (const gchar *filename) { PlannerPythonEnv *env; PyObject *pDict, *pMain; env = g_new0 (PlannerPythonEnv,1); env->filename = g_strdup (filename); pDict = PyImport_GetModuleDict (); pMain = PyDict_GetItemString (pDict, "__main__"); pDict = PyModule_GetDict (pMain); env->globals = PyDict_Copy (pDict); return env; }
PyObject * PyImport_ImportModuleEx(char *name) { PyObject *modules; PyObject *m; PyImport_ImportFrozenModule(name); modules = PyImport_GetModuleDict(); m = PyDict_GetItemString(modules, name); if (m == NULL) { printf("module %s not properly initialized\n", name); return NULL; } Py_INCREF(m); return m; }
PyObject * reload_module(PyObject *m) { PyObject *modules = PyImport_GetModuleDict(); PyObject *path = NULL; char *name, *subname; if (m == NULL || !PyModule_Check(m)) { PyErr_SetString(PyExc_TypeError, "reload_module() argument must be module"); return NULL; } name = PyModule_GetName(m); if (name == NULL) return NULL; if (m != PyDict_GetItemString(modules, name)) { PyErr_Format(PyExc_ImportError, "reload(): module %.200s not in sys.modules", name); return NULL; } subname = strrchr(name, '.'); if (subname == NULL) subname = name; else { PyObject *parentname, *parent; parentname = PyString_FromStringAndSize(name, (subname-name)); if (parentname == NULL) return NULL; parent = PyDict_GetItem(modules, parentname); Py_DECREF(parentname); if (parent == NULL) { PyErr_Format(PyExc_ImportError, "reload(): parent %.200s not in sys.modules", name); return NULL; } subname++; path = PyObject_GetAttrString(parent, "__path__"); if (path == NULL) PyErr_Clear(); } m = call_find_load(name, subname, path); Py_XDECREF(path); return m; }
static bool checkStartupSuccess() { PyObject* mods = PyImport_GetModuleDict(); if(!mods) return false; PyObject* m = PyDict_GetItemString(mods, "main"); if(!m) return false; PyObject* successObj = PyObject_GetAttrString(m, "successStartup"); bool success = false; if(successObj) success = PyObject_IsTrue(successObj); Py_XDECREF(successObj); mods = NULL; // borrowed m = NULL; // borrowed return success; }
/** Initialize the vcl module. This creates all types need by PyVcl and the vcl object, * which is placed in the list of imported modules. * This way you can write "import vcl" instead of "from PyVcl import vcl". */ PyObject* InitPyVcl() { PyObject *PyVclModule = PyModule_Create(&PyVclModuleDef); if(PyType_Ready(&GlobalVclType) < 0 || PyType_Ready(&VclMethodType) < 0 || PyType_Ready(&VclObjectType) < 0 || PyType_Ready(&VclFunctionType) < 0 || PyType_Ready(&VclArrayPropertyType) < 0 || PyType_Ready(&VclRefType) < 0) return NULL; PyVclException = PyErr_NewException("vcl.VclError", NULL, NULL); PyModule_AddObject(PyVclModule, "VclError", PyVclException); PyObject *vcl = GlobalVcl_Create(); PyModule_AddObject(PyVclModule, "vcl", vcl); PyObject *Modules = PyImport_GetModuleDict(); PyDict_SetItemString(Modules, "vcl", vcl); Py_DECREF(vcl); return PyVclModule; }
PyObject *GPU_initPython(void) { PyObject *module; PyObject *submodule; PyObject *sys_modules = PyThreadState_GET()->interp->modules; module = PyInit_gpu(); PyModule_AddObject(module, "export_shader", (PyObject *)PyCFunction_New(meth_export_shader, NULL)); /* gpu.offscreen */ PyModule_AddObject(module, "offscreen", (submodule = BPyInit_gpu_offscreen())); PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule); Py_INCREF(submodule); PyDict_SetItem(PyImport_GetModuleDict(), PyModule_GetNameObject(module), module); return module; }
/* Returns a new reference. A NULL return value can mean false or an error. */ static PyObject * get_warnings_attr(const char *attr, int try_import) { static PyObject *warnings_str = NULL; PyObject *all_modules; PyObject *warnings_module, *obj; if (warnings_str == NULL) { warnings_str = PyUnicode_InternFromString("warnings"); if (warnings_str == NULL) return NULL; } /* don't try to import after the start of the Python finallization */ if (try_import && _Py_Finalizing == NULL) { warnings_module = PyImport_Import(warnings_str); if (warnings_module == NULL) { /* Fallback to the C implementation if we cannot get the Python implementation */ PyErr_Clear(); return NULL; } } else { all_modules = PyImport_GetModuleDict(); warnings_module = PyDict_GetItem(all_modules, warnings_str); if (warnings_module == NULL) return NULL; Py_INCREF(warnings_module); } if (!PyObject_HasAttrString(warnings_module, attr)) { Py_DECREF(warnings_module); return NULL; } obj = PyObject_GetAttrString(warnings_module, attr); Py_DECREF(warnings_module); return obj; }
PyObject *BPY_utils_units(void) { PyObject *submodule, *item; submodule = PyModule_Create(&bpyunits_module); PyDict_SetItemString(PyImport_GetModuleDict(), bpyunits_module.m_name, submodule); Py_INCREF(submodule); /* Finalize our unit systems and types structseq definitions! */ /* bpy.utils.units.system */ item = py_structseq_from_strings(&BPyUnitsSystemsType, &bpyunits_systems_desc, bpyunits_usystem_items); PyModule_AddObject(submodule, "systems", item); /* steals ref */ /* bpy.utils.units.categories */ item = py_structseq_from_strings(&BPyUnitsCategoriesType, &bpyunits_categories_desc, bpyunits_ucategorie_items); PyModule_AddObject(submodule, "categories", item); /* steals ref */ return submodule; }
static PyObject * create_newmodule(char *name) { PyObject *mod, *moddict; mod = PyModule_New(name); if (mod == NULL) return NULL; /* Register into module dict to allow such codes like: * from freebsd.const import * */ moddict = PyImport_GetModuleDict(); if (moddict == NULL) { Py_DECREF(mod); return NULL; } PyDict_SetItemString(moddict, name, mod); return mod; }
PyObject * PyImport_AddModule(char *name) { LOG("> PyImport_AddModule\n"); { PyObject *modules = PyImport_GetModuleDict(); PyObject *m; if ((m = PyDict_GetItemString(modules, name)) != NULL && PyModule_Check(m)) return m; m = PyModule_New(name); if (m == NULL) return NULL; if (PyDict_SetItemString(modules, name, m) != 0) { Py_DECREF(m); return NULL; } Py_DECREF(m); LOG("< PyImport_AddModule\n"); return m; }}
static PyObject * import_submodule(PyObject *mod, char *subname, char *fullname) { PyObject *modules = PyImport_GetModuleDict(); PyObject *m; /* Require: if mod == None: subname == fullname else: mod.__name__ + "." + subname == fullname */ if ((m = PyDict_GetItemString(modules, fullname)) != NULL) { Py_INCREF(m); } else { PyObject *path; if (mod == Py_None) path = NULL; else { path = PyObject_GetAttrString(mod, "__path__"); if (path == NULL) { PyErr_Clear(); Py_INCREF(Py_None); return Py_None; } } m = call_find_load(fullname, subname, path); if (m != NULL && mod != Py_None) { if (PyObject_SetAttrString(mod, subname, m) < 0) { Py_DECREF(m); m = NULL; } } } return m; }
static bool loadCSA () { PYGILSTATE_ENSURE (gstate); PyObject* pModule = PyMapping_GetItemString (PyImport_GetModuleDict (), (char*)"csa"); pMask = PyObject_GetAttrString (pModule, "Mask"); if (pMask == NULL) { Py_DECREF (pModule); PYGILSTATE_RELEASE (gstate); error ("Couldn't find the Mask class in the CSA library"); return false; } pConnectionSet = PyObject_GetAttrString (pModule, "ConnectionSet"); if (pConnectionSet == NULL) { Py_DECREF (pModule); PYGILSTATE_RELEASE (gstate); error ("Couldn't find the ConnectionSet class in the CSA library"); return false; } pArity = PyObject_GetAttrString (pModule, "arity"); pCross = PyObject_GetAttrString (pModule, "cross"); pPartition = PyObject_GetAttrString (pModule, "partition"); Py_DECREF (pModule); if (pArity == NULL) { PYGILSTATE_RELEASE (gstate); error ("Couldn't find the arity function in the CSA library"); return false; } pCSAClasses = PyTuple_Pack (2, pMask, pConnectionSet); PYGILSTATE_RELEASE (gstate); return true; }
PyObject * atom(PyObject *obj) { PyObject *moddict = NULL; PyObject *mod = NULL; PyObject *fun = NULL; PyObject *res = NULL; moddict = PyImport_GetModuleDict(); if (!moddict) goto error; mod = PyMapping_GetItemString(moddict, "toy"); if (!mod) goto error; fprintf(stderr, "Found toy module\n"); fun = PyObject_GetAttrString(mod, "atom"); if (!fun) goto error; fprintf(stderr, "Found atom there\n"); res = PyObject_CallFunctionObjArgs(fun, obj, NULL); if (!res) goto error; fprintf(stderr, "Called atom\n"); success: Py_XDECREF(fun); Py_XDECREF(mod); fprintf(stderr, "Returning from atom\n"); return res; error: Py_XDECREF(fun); Py_XDECREF(mod); Py_XDECREF(res); return NULL; }
std::vector<mitk::PythonVariable> mitk::PythonService::GetVariableStack() const { std::vector<mitk::PythonVariable> list; PyObject* dict = PyImport_GetModuleDict(); PyObject* object = PyDict_GetItemString(dict, "__main__"); PyObject* dirMain = PyObject_Dir(object); PyObject* tempObject = 0; //PyObject* strTempObject = 0; if(dirMain) { std::string name, attrValue, attrType; for(int i = 0; i<PyList_Size(dirMain); i++) { tempObject = PyList_GetItem(dirMain, i); name = PyString_AsString(tempObject); tempObject = PyObject_GetAttrString( object, name.c_str() ); attrType = tempObject->ob_type->tp_name; //disabled due to strange errors, see T24085 //strTempObject = PyObject_Repr(tempObject); //if(strTempObject && ( PyUnicode_Check(strTempObject) || PyString_Check(strTempObject) ) ) // attrValue = PyString_AsString(strTempObject); //else // attrValue = ""; mitk::PythonVariable var; var.m_Name = name; //var.m_Value = attrValue; var.m_Type = attrType; list.push_back(var); } } return list; }
void PyC_FileAndNum(const char **filename, int *lineno) { PyFrameObject *frame; if (filename) *filename= NULL; if (lineno) *lineno = -1; if (!(frame= PyThreadState_GET()->frame)) { return; } /* when executing a script */ if (filename) { *filename = _PyUnicode_AsString(frame->f_code->co_filename); } /* when executing a module */ if(filename && *filename == NULL) { /* try an alternative method to get the filename - module based * references below are all borrowed (double checked) */ PyObject *mod_name= PyDict_GetItemString(PyEval_GetGlobals(), "__name__"); if(mod_name) { PyObject *mod= PyDict_GetItem(PyImport_GetModuleDict(), mod_name); if(mod) { *filename= PyModule_GetFilename(mod); } /* unlikely, fallback */ if(*filename == NULL) { *filename= _PyUnicode_AsString(mod_name); } } } if (lineno) { *lineno = PyFrame_GetLineNumber(frame); } }