PyObject* PythonInterface::getClassInstance() { if (pInstance == NULL) { Py_Initialize(); if (modulePath != NULL) { PyRun_SimpleString("import sys"); PyRun_SimpleString(("sys.path.append(\"" + std::string(modulePath) + "\")").c_str()); } std::string modName = getModuleName(); PyObject *pName = PyString_FromString(modName.c_str()); PyObject *pModule = PyImport_Import(pName); Py_DECREF(pName); char* fileName = PyModule_GetFilename(pModule); char* argv[1] = { fileName }; PySys_SetArgvEx(1, argv, 0); PyObject *pDict = PyModule_GetDict(pModule); Py_DECREF(pModule); PyObject *pClass = PyDict_GetItemString(pDict, getClassName().c_str()); Py_DECREF(pDict); pInstance = PyObject_CallObject(pClass, NULL); Py_DECREF(pClass); } return pInstance; }
PyObject *bpy_text_reimport(PyObject *module, int *found) { Text *text; const char *name; char *filepath; char *buf = NULL; //XXX Main *maggie = bpy_import_main ? bpy_import_main:G.main; Main *maggie = bpy_import_main; if (!maggie) { printf("ERROR: bpy_import_main_set() was not called before running python. this is a bug.\n"); return NULL; } *found = 0; /* get name, filename from the module itself */ if ((name = PyModule_GetName(module)) == NULL) return NULL; if ((filepath = (char *)PyModule_GetFilename(module)) == NULL) return NULL; /* look up the text object */ text = BLI_findstring(&maggie->text, BLI_path_basename(filepath), offsetof(ID, name) + 2); /* uh-oh.... didn't find it */ if (!text) return NULL; else *found = 1; /* if previously compiled, free the object */ /* (can't see how could be NULL, but check just in case) */ if (text->compiled) { Py_DECREF((PyObject *)text->compiled); } /* compile the buffer */ buf = txt_to_buf(text); text->compiled = Py_CompileString(buf, text->id.name + 2, Py_file_input); MEM_freeN(buf); /* if compile failed.... return this error */ if (PyErr_Occurred()) { PyErr_Print(); PyErr_Clear(); PySys_SetObject("last_traceback", NULL); free_compiled_text(text); return NULL; } /* make into a module */ return PyImport_ExecCodeModule((char *)name, text->compiled); }
static PyObject * module_repr(PyModuleObject *m) { char *name; char *filename; name = PyModule_GetName((PyObject *)m); if (name == NULL) { PyErr_Clear(); name = "?"; } filename = PyModule_GetFilename((PyObject *)m); if (filename == NULL) { PyErr_Clear(); return PyString_FromFormat("<module '%s' (built-in)>", name); } return PyString_FromFormat("<module '%s' from '%s'>", name, filename); }
static int ensure_cmap_loaded() { char cwd[PATH_MAX+1]; // load the cmap module so fract funcs we compile later // can call its methods if(NULL != cmap_module_handle) { return 1; // already loaded } char *filename = PyModule_GetFilename(pymod); //printf("base name: %s\n",filename); char *path_end = strrchr(filename,'/'); if(path_end == NULL) { filename = getcwd(cwd,sizeof(cwd)); path_end = filename + strlen(filename); } int path_len = strlen(filename) - strlen(path_end); int len = path_len + strlen(CMAP_NAME); char *new_filename = (char *)malloc(len+1); strncpy(new_filename, filename, path_len); new_filename[path_len] = '\0'; strcat(new_filename, CMAP_NAME); //printf("Filename: %s\n", new_filename); cmap_module_handle = dlopen(new_filename, RTLD_GLOBAL | RTLD_NOW); if(NULL == cmap_module_handle) { /* an error */ PyErr_SetString(PyExc_ValueError, dlerror()); return 0; } return 1; }
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); } }
void pybase::OpenEditor() { if(!module) return; const char *mname = PyModule_GetFilename(module); if(!mname) { PyErr_Clear(); return; } char fname[1024]; strcpy(fname,mname); // replacing .pyc or .pyo for source file name char *dt = strrchr(fname,'.'); if(dt && !strncmp(dt,".py",2)) strcpy(dt,".py"); // this should open the editor.... #if FLEXT_OS == FLEXT_OS_WIN int err = (int)ShellExecute(NULL,"edit",fname,NULL,NULL,SW_SHOW); if(err == SE_ERR_NOASSOC) { // no association found - try notepad err = (int)ShellExecute(NULL,NULL,"notepad.exe",fname,NULL,SW_SHOW); } else if(err == ERROR_FILE_NOT_FOUND || err == SE_ERR_FNF) post("py/pyext - File not %s found",fname); else if(err <= 32) post("py/pyext - Unknown error opening %s",fname); #elif FLEXT_OS == FLEXT_OS_MAC FSRef ref; OSStatus err = FSPathMakeRef((unsigned char *)fname,&ref,NULL); if(err) post("py/pyext - Error interpreting path %s",fname); else { FSRef editor; err = LSGetApplicationForItem(&ref,kLSRolesEditor,&editor,NULL); if(err) { // Can't find associated application... try Textedit err = FSPathMakeRef((unsigned char *)"/Applications/TextEdit.app",&editor,NULL); if(err) post("py/pyext - Can't find Textedit application"); } if(!err) { LSLaunchFSRefSpec lspec; lspec.appRef = &editor; lspec.numDocs = 1; lspec.itemRefs = &ref; lspec.passThruParams = NULL; lspec.launchFlags = kLSLaunchDefaults; lspec.asyncRefCon = NULL; err = LSOpenFromRefSpec(&lspec,NULL); if(err) post("py/pyext - Couldn't launch editor"); } } #else // thanks to Tim Blechmann char *editor = getenv("EDITOR"); if(!editor) { // || !strcmp(editor, "/usr/bin/nano") || !strcmp(editor, "/usr/bin/pico") || !strcmp(editor, "/usr/bin/vi")) { // no environment variable or console text editor found ... use idle instead (should have come with Python) editor = "idle"; } pid_t child = fork(); if(!child) { char cmd[80]; strcpy(cmd,editor); strcat(cmd," "); strcat(cmd,fname); execl("/bin/sh", "sh", "-c", cmd, (char *) NULL); } #endif }