const char * PyModule_GetName(PyObject *m) { PyObject *name = PyModule_GetNameObject(m); if (name == NULL) return NULL; Py_DECREF(name); /* module dict has still a reference */ return _PyUnicode_AsString(name); }
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; }
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; }
int PyModule_AddFunctions(PyObject *m, PyMethodDef *functions) { int res; PyObject *name = PyModule_GetNameObject(m); if (name == NULL) { return -1; } res = _add_methods_to_object(m, name, functions); Py_DECREF(name); return res; }
PyObject *BPyInit_idprop(void) { PyObject *mod; PyObject *submodule; PyObject *sys_modules = PyThreadState_GET()->interp->modules; mod = PyModule_Create(&IDProp_module_def); /* idprop.types */ PyModule_AddObject(mod, "types", (submodule = BPyInit_idprop_types())); PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule); Py_INCREF(submodule); return mod; }
static PyObject * normalizeUserObj(PyObject *obj) { PyCFunctionObject *fn; if (!PyCFunction_Check(obj)) { Py_INCREF(obj); return obj; } /* Replace built-in function objects with a descriptive string because of built-in methods -- keeping a reference to __self__ is probably not a good idea. */ fn = (PyCFunctionObject *)obj; if (fn->m_self == NULL) { /* built-in function: look up the module name */ PyObject *mod = fn->m_module; PyObject *modname = NULL; if (mod != NULL) { if (PyUnicode_Check(mod)) { modname = mod; Py_INCREF(modname); } else if (PyModule_Check(mod)) { modname = PyModule_GetNameObject(mod); if (modname == NULL) PyErr_Clear(); } } if (modname != NULL) { if (PyUnicode_CompareWithASCIIString(modname, "builtins") != 0) { PyObject *result; result = PyUnicode_FromFormat("<%U.%s>", modname, fn->m_ml->ml_name); Py_DECREF(modname); return result; } Py_DECREF(modname); } return PyUnicode_FromFormat("<%s>", fn->m_ml->ml_name); } else { /* built-in method: try to return repr(getattr(type(__self__), __name__)) */ PyObject *self = fn->m_self; PyObject *name = PyUnicode_FromString(fn->m_ml->ml_name); PyObject *modname = fn->m_module; if (name != NULL) { PyObject *mo = _PyType_Lookup(Py_TYPE(self), name); Py_XINCREF(mo); Py_DECREF(name); if (mo != NULL) { PyObject *res = PyObject_Repr(mo); Py_DECREF(mo); if (res != NULL) return res; } } /* Otherwise, use __module__ */ PyErr_Clear(); if (modname != NULL && PyUnicode_Check(modname)) return PyUnicode_FromFormat("<built-in method %S.%s>", modname, fn->m_ml->ml_name); else return PyUnicode_FromFormat("<built-in method %s>", fn->m_ml->ml_name); } }
PyMODINIT_FUNC PyInit_mathutils(void) { PyObject *mod; PyObject *submodule; PyObject *sys_modules = PyImport_GetModuleDict(); if (PyType_Ready(&vector_Type) < 0) { return NULL; } if (PyType_Ready(&matrix_Type) < 0) { return NULL; } if (PyType_Ready(&matrix_access_Type) < 0) { return NULL; } if (PyType_Ready(&euler_Type) < 0) { return NULL; } if (PyType_Ready(&quaternion_Type) < 0) { return NULL; } if (PyType_Ready(&color_Type) < 0) { return NULL; } mod = PyModule_Create(&M_Mathutils_module_def); /* each type has its own new() function */ PyModule_AddObject(mod, vector_Type.tp_name, (PyObject *)&vector_Type); PyModule_AddObject(mod, matrix_Type.tp_name, (PyObject *)&matrix_Type); PyModule_AddObject(mod, euler_Type.tp_name, (PyObject *)&euler_Type); PyModule_AddObject(mod, quaternion_Type.tp_name, (PyObject *)&quaternion_Type); PyModule_AddObject(mod, color_Type.tp_name, (PyObject *)&color_Type); /* submodule */ PyModule_AddObject(mod, "geometry", (submodule = PyInit_mathutils_geometry())); /* XXX, python doesn't do imports with this usefully yet * 'from mathutils.geometry import PolyFill' * ...fails without this. */ PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule); PyModule_AddObject(mod, "interpolate", (submodule = PyInit_mathutils_interpolate())); /* XXX, python doesnt do imports with this usefully yet * 'from mathutils.geometry import PolyFill' * ...fails without this. */ PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule); #ifndef MATH_STANDALONE /* Noise submodule */ PyModule_AddObject(mod, "noise", (submodule = PyInit_mathutils_noise())); PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule); /* BVHTree submodule */ PyModule_AddObject(mod, "bvhtree", (submodule = PyInit_mathutils_bvhtree())); PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule); /* KDTree_3d submodule */ PyModule_AddObject(mod, "kdtree", (submodule = PyInit_mathutils_kdtree())); PyDict_SetItem(sys_modules, PyModule_GetNameObject(submodule), submodule); #endif mathutils_matrix_row_cb_index = Mathutils_RegisterCallback(&mathutils_matrix_row_cb); mathutils_matrix_col_cb_index = Mathutils_RegisterCallback(&mathutils_matrix_col_cb); mathutils_matrix_translation_cb_index = Mathutils_RegisterCallback( &mathutils_matrix_translation_cb); return mod; }