static PyObject * module_repr(PyModuleObject *m) { PyObject *name, *filename, *repr, *loader = NULL; /* See if the module has an __loader__. If it does, give the loader the * first shot at producing a repr for the module. */ if (m->md_dict != NULL) { loader = PyDict_GetItemString(m->md_dict, "__loader__"); } if (loader != NULL) { repr = PyObject_CallMethod(loader, "module_repr", "(O)", (PyObject *)m, NULL); if (repr == NULL) { PyErr_Clear(); } else { return repr; } } /* __loader__.module_repr(m) did not provide us with a repr. Next, see if * the module has an __file__. If it doesn't then use repr(__loader__) if * it exists, otherwise, just use module.__name__. */ name = PyModule_GetNameObject((PyObject *)m); if (name == NULL) { PyErr_Clear(); name = PyUnicode_FromStringAndSize("?", 1); if (name == NULL) return NULL; } filename = PyModule_GetFilenameObject((PyObject *)m); if (filename == NULL) { PyErr_Clear(); /* There's no m.__file__, so if there was an __loader__, use that in * the repr, otherwise, the only thing you can use is m.__name__ */ if (loader == NULL) { repr = PyUnicode_FromFormat("<module %R>", name); } else { repr = PyUnicode_FromFormat("<module %R (%R)>", name, loader); } } /* Finally, use m.__file__ */ else { repr = PyUnicode_FromFormat("<module %R from %R>", name, filename); Py_DECREF(filename); } Py_DECREF(name); return repr; }
const char * PyModule_GetFilename(PyObject *m) { PyObject *fileobj; char *utf8; fileobj = PyModule_GetFilenameObject(m); if (fileobj == NULL) return NULL; utf8 = _PyUnicode_AsString(fileobj); Py_DECREF(fileobj); /* module dict has still a reference */ return utf8; }
const char * PyModule_GetFilename(PyObject *m) { PyObject *fileobj; char *utf8; fileobj = PyModule_GetFilenameObject(m); if (fileobj == NULL) return NULL; utf8 = _PyUnicode_AsString(fileobj); Py_DECREF(fileobj); return utf8; }
PyObject *bpy_text_reimport(PyObject *module, int *found) { Text *text; const char *name; const char *filepath; //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; { PyObject *module_file = PyModule_GetFilenameObject(module); if (module_file == NULL) { return NULL; } filepath = _PyUnicode_AsString(module_file); Py_DECREF(module_file); if (filepath == 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 (bpy_text_compile(text) == false) { return NULL; } /* make into a module */ return PyImport_ExecCodeModule(name, text->compiled); }
static PyObject * module_repr(PyModuleObject *m) { const char *name; PyObject *filename, *repr; name = PyModule_GetName((PyObject *)m); if (name == NULL) { PyErr_Clear(); name = "?"; } filename = PyModule_GetFilenameObject((PyObject *)m); if (filename == NULL) { PyErr_Clear(); return PyUnicode_FromFormat("<module '%s' (built-in)>", name); } repr = PyUnicode_FromFormat("<module '%s' from '%U'>", name, filename); Py_DECREF(filename); return repr; }
/* call once __file__ is set */ void bpy_module_delay_init(PyObject *bpy_proxy) { const int argc = 1; const char *argv[2]; /* updating the module dict below will loose the reference to __file__ */ PyObject *filename_obj = PyModule_GetFilenameObject(bpy_proxy); const char *filename_rel = _PyUnicode_AsString(filename_obj); /* can be relative */ char filename_abs[1024]; BLI_strncpy(filename_abs, filename_rel, sizeof(filename_abs)); BLI_path_cwd(filename_abs); argv[0] = filename_abs; argv[1] = NULL; // printf("module found %s\n", argv[0]); main_python_enter(argc, argv); /* initialized in BPy_init_modules() */ PyDict_Update(PyModule_GetDict(bpy_proxy), PyModule_GetDict(bpy_package_py)); }