static void atexit_func_call(const char *func_name, PyObject *atexit_func_arg) { /* note - no error checking, if any of these fail we'll get a crash * this is intended, but if its problematic it could be changed * - campbell */ PyObject *atexit_mod= PyImport_ImportModuleLevel((char *)"atexit", NULL, NULL, NULL, 0); PyObject *atexit_func= PyObject_GetAttrString(atexit_mod, func_name); PyObject *args= PyTuple_New(1); PyObject *ret; PyTuple_SET_ITEM(args, 0, atexit_func_arg); Py_INCREF(atexit_func_arg); /* only incref so we dont dec'ref along with 'args' */ ret= PyObject_CallObject(atexit_func, args); Py_DECREF(atexit_mod); Py_DECREF(atexit_func); Py_DECREF(args); if (ret) { Py_DECREF(ret); } else { /* should never happen */ PyErr_Print(); } }
/* For faster execution we keep a special dictionary for pydrivers, with * the needed modules and aliases. */ int bpy_pydriver_create_dict(void) { PyObject *d, *mod; /* validate namespace for driver evaluation */ if (bpy_pydriver_Dict) return -1; d = PyDict_New(); if (d == NULL) return -1; else bpy_pydriver_Dict = d; /* import some modules: builtins, bpy, math, (Blender.noise )*/ PyDict_SetItemString(d, "__builtins__", PyEval_GetBuiltins()); mod = PyImport_ImportModule("math"); if (mod) { PyDict_Merge(d, PyModule_GetDict(mod), 0); /* 0 - dont overwrite existing values */ Py_DECREF(mod); } /* add bpy to global namespace */ mod= PyImport_ImportModuleLevel((char *)"bpy", NULL, NULL, NULL, 0); if (mod) { PyDict_SetItemString(bpy_pydriver_Dict, "bpy", mod); Py_DECREF(mod); } return 0; }
void BPY_modules_update(bContext *C) { #if 0 // slow, this runs all the time poll, draw etc 100's of time a sec. PyObject *mod = PyImport_ImportModuleLevel("bpy", NULL, NULL, NULL, 0); PyModule_AddObject(mod, "data", BPY_rna_module()); PyModule_AddObject(mod, "types", BPY_rna_types()); // atm this does not need updating #endif /* refreshes the main struct */ BPY_update_rna_module(); bpy_context_module->ptr.data = (void *)C; }
static PyObject *bpy_import_test(const char *modname) { PyObject *mod= PyImport_ImportModuleLevel((char *)modname, NULL, NULL, NULL, 0); if (mod) { Py_DECREF(mod); } else { PyErr_Print(); PyErr_Clear(); } return mod; }
static PyObject *blender_import(PyObject *UNUSED(self), PyObject *args, PyObject *kw) { PyObject *exception, *err, *tb; char *name; int found = 0; PyObject *globals = NULL, *locals = NULL, *fromlist = NULL; int level = 0; /* relative imports */ PyObject *newmodule; //PyObject_Print(args, stderr, 0); static const char *kwlist[] = {"name", "globals", "locals", "fromlist", "level", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kw, "s|OOOi:bpy_import_meth", (char **)kwlist, &name, &globals, &locals, &fromlist, &level)) { return NULL; } /* import existing builtin modules or modules that have been imported already */ newmodule = PyImport_ImportModuleLevel(name, globals, locals, fromlist, level); if (newmodule) return newmodule; PyErr_Fetch(&exception, &err, &tb); /* get the python error in case we cant import as blender text either */ /* importing from existing modules failed, see if we have this module as blender text */ newmodule = bpy_text_import_name(name, &found); if (newmodule) { /* found module as blender text, ignore above exception */ PyErr_Clear(); Py_XDECREF(exception); Py_XDECREF(err); Py_XDECREF(tb); /* printf("imported from text buffer...\n"); */ } else if (found == 1) { /* blender text module failed to execute but was found, use its error message */ Py_XDECREF(exception); Py_XDECREF(err); Py_XDECREF(tb); return NULL; } else { /* no blender text was found that could import the module * reuse the original error from PyImport_ImportModuleEx */ PyErr_Restore(exception, err, tb); } return newmodule; }
/* 'builtins' is most likely PyEval_GetBuiltins() */ void bpy_import_init(PyObject *builtins) { PyObject *item; PyObject *mod; PyDict_SetItemString(builtins, "__import__", item = PyCFunction_New(&bpy_import_meth, NULL)); Py_DECREF(item); /* move reload here * XXX, use import hooks */ mod = PyImport_ImportModuleLevel((char *)"imp", NULL, NULL, NULL, 0); if (mod) { PyDict_SetItemString(PyModule_GetDict(mod), "reload", item = PyCFunction_New(&bpy_reload_meth, NULL)); Py_DECREF(item); Py_DECREF(mod); } else { BLI_assert(!"unable to load 'imp' module."); } }
/** * \note to the discerning developer, yes - this is nasty * monkey-patching our own import into Python's builtin 'imp' module. * * However Python's alternative is to use import hooks, * which are implemented in a way that we can't use our own importer as a * fall-back (instead we must try and fail - raise an exception every time). * Since importing from blenders text-blocks is not the common case * I prefer to use Pythons import by default and fall-back to * Blenders - which we can only do by intercepting import calls I'm afraid. * - Campbell */ void bpy_import_init(PyObject *builtins) { PyObject *item; PyObject *mod; PyDict_SetItemString(builtins, "__import__", item = PyCFunction_New(&bpy_import_meth, NULL)); Py_DECREF(item); /* move reload here * XXX, use import hooks */ mod = PyImport_ImportModuleLevel("importlib", NULL, NULL, NULL, 0); if (mod) { PyObject *mod_dict = PyModule_GetDict(mod); /* blender owns the function */ imp_reload_orig = PyDict_GetItemString(mod_dict, "reload"); Py_INCREF(imp_reload_orig); PyDict_SetItemString(mod_dict, "reload", item = PyCFunction_New(&bpy_reload_meth, NULL)); Py_DECREF(item); Py_DECREF(mod); } else { BLI_assert(!"unable to load 'importlib' module."); } }
PyMODINIT_FUNC PyInit_optimized(void) { PyObject *mod; PyObject *msgtypes; PyObject *fromlist, *fromstr; long l; /* make some constants */ if (serialize_strob == NULL) { serialize_strob = PyUnicode_FromString("serialize"); if (serialize_strob == NULL) return(NULL); } if (msgtype_strob == NULL) { msgtype_strob = PyUnicode_FromString("type"); if (msgtype_strob == NULL) return(NULL); } mod = PyModule_Create(&optimized_module); if (mod == NULL) return(NULL); /* cpp abuse; ready types */ #define mTYPE(name) \ if (PyType_Ready(&name##_Type) < 0) \ goto cleanup; \ if (PyModule_AddObject(mod, #name, \ (PyObject *) &name##_Type) < 0) \ goto cleanup; /* buffer.c */ include_buffer_types #undef mTYPE l = 1; if (((char *) &l)[0] == 1) { /* little */ local_ntohl = swap_int4; local_ntohs = swap_short; } else { /* big */ local_ntohl = return_int4; local_ntohs = return_short; } /* * Get the message_types tuple to type "instantiation". */ fromlist = PyList_New(1); fromstr = PyUnicode_FromString("message_types"); PyList_SetItem(fromlist, 0, fromstr); msgtypes = PyImport_ImportModuleLevel( "message_types", PyModule_GetDict(mod), PyModule_GetDict(mod), fromlist, 1 ); Py_DECREF(fromlist); if (msgtypes == NULL) goto cleanup; message_types = PyObject_GetAttrString(msgtypes, "message_types"); Py_DECREF(msgtypes); if (!PyObject_IsInstance(message_types, (PyObject *) (&PyTuple_Type))) { PyErr_SetString(PyExc_RuntimeError, "local protocol.message_types.message_types is not a tuple object"); goto cleanup; } return(mod); cleanup: Py_DECREF(mod); return(NULL); }