PyMODINIT_FUNC SIP_MODULE_ENTRY() #endif { static PyMethodDef sip_methods[] = { {0, 0, 0, 0} }; #if PY_MAJOR_VERSION >= 3 static PyModuleDef sip_module_def = { PyModuleDef_HEAD_INIT, "libqt_gui_cpp_sip", NULL, -1, sip_methods, NULL, NULL, NULL, NULL }; #endif PyObject *sipModule, *sipModuleDict; PyObject *sip_sipmod, *sip_capiobj; /* Initialise the module and get it's dictionary. */ #if PY_MAJOR_VERSION >= 3 sipModule = PyModule_Create(&sip_module_def); #elif PY_VERSION_HEX >= 0x02050000 sipModule = Py_InitModule(sipName_libqt_gui_cpp_sip, sip_methods); #else sipModule = Py_InitModule(const_cast<char *>(sipName_libqt_gui_cpp_sip), sip_methods); #endif if (sipModule == NULL) SIP_MODULE_RETURN(NULL); sipModuleDict = PyModule_GetDict(sipModule); /* Get the SIP module's API. */ #if PY_VERSION_HEX >= 0x02050000 sip_sipmod = PyImport_ImportModule(SIP_MODULE_NAME); #else sip_sipmod = PyImport_ImportModule(const_cast<char *>(SIP_MODULE_NAME)); #endif if (sip_sipmod == NULL) { SIP_MODULE_DISCARD(sipModule); SIP_MODULE_RETURN(NULL); } sip_capiobj = PyDict_GetItemString(PyModule_GetDict(sip_sipmod), "_C_API"); Py_DECREF(sip_sipmod); #if defined(SIP_USE_PYCAPSULE) if (sip_capiobj == NULL || !PyCapsule_CheckExact(sip_capiobj)) #else if (sip_capiobj == NULL || !PyCObject_Check(sip_capiobj)) #endif { SIP_MODULE_DISCARD(sipModule); SIP_MODULE_RETURN(NULL); } #if defined(SIP_USE_PYCAPSULE) sipAPI_libqt_gui_cpp_sip = reinterpret_cast<const sipAPIDef *>(PyCapsule_GetPointer(sip_capiobj, SIP_MODULE_NAME "._C_API")); #else sipAPI_libqt_gui_cpp_sip = reinterpret_cast<const sipAPIDef *>(PyCObject_AsVoidPtr(sip_capiobj)); #endif #if defined(SIP_USE_PYCAPSULE) if (sipAPI_libqt_gui_cpp_sip == NULL) { SIP_MODULE_DISCARD(sipModule); SIP_MODULE_RETURN(NULL); } #endif /* Export the module and publish it's API. */ if (sipExportModule(&sipModuleAPI_libqt_gui_cpp_sip,SIP_API_MAJOR_NR,SIP_API_MINOR_NR,0) < 0) { SIP_MODULE_DISCARD(sipModule); SIP_MODULE_RETURN(0); } sip_libqt_gui_cpp_sip_qt_metaobject = (sip_qt_metaobject_func)sipImportSymbol("qtcore_qt_metaobject"); sip_libqt_gui_cpp_sip_qt_metacall = (sip_qt_metacall_func)sipImportSymbol("qtcore_qt_metacall"); sip_libqt_gui_cpp_sip_qt_metacast = (sip_qt_metacast_func)sipImportSymbol("qtcore_qt_metacast"); if (!sip_libqt_gui_cpp_sip_qt_metacast) Py_FatalError("Unable to import qtcore_qt_metacast"); /* Initialise the module now all its dependencies have been set up. */ if (sipInitModule(&sipModuleAPI_libqt_gui_cpp_sip,sipModuleDict) < 0) { SIP_MODULE_DISCARD(sipModule); SIP_MODULE_RETURN(0); } /* Get the APIs of the modules that this one is dependent on. */ sipModuleAPI_libqt_gui_cpp_sip_QtCore = sipModuleAPI_libqt_gui_cpp_sip.em_imports[0].im_module; sipModuleAPI_libqt_gui_cpp_sip_QtGui = sipModuleAPI_libqt_gui_cpp_sip.em_imports[1].im_module; SIP_MODULE_RETURN(sipModule); }
void destruct(PyObject *obj) { bool *destructor_called = (bool *)PyCapsule_GetPointer(obj, "test"); *destructor_called = true; }
static Point *to_point(PyObject *obj){ return (Point *) PyCapsule_GetPointer(obj, "Point"); }
static PyObject *bpy_lib_exit(BPy_Library *self, PyObject *UNUSED(args)) { Main *bmain = CTX_data_main(BPy_GetContext()); Main *mainl = NULL; int err = 0; const bool do_append = ((self->flag & FILE_LINK) == 0); BKE_main_id_tag_all(bmain, LIB_TAG_PRE_EXISTING, true); /* here appending/linking starts */ mainl = BLO_library_link_begin(bmain, &(self->blo_handle), self->relpath); { int idcode_step = 0, idcode; while ((idcode = BKE_idcode_iter_step(&idcode_step))) { if (BKE_idcode_is_linkable(idcode) && (idcode != ID_WS || do_append)) { const char *name_plural = BKE_idcode_to_name_plural(idcode); PyObject *ls = PyDict_GetItemString(self->dict, name_plural); // printf("lib: %s\n", name_plural); if (ls && PyList_Check(ls)) { /* loop */ Py_ssize_t size = PyList_GET_SIZE(ls); Py_ssize_t i; for (i = 0; i < size; i++) { PyObject *item_src = PyList_GET_ITEM(ls, i); PyObject *item_dst; /* must be set below */ const char *item_idname = _PyUnicode_AsString(item_src); // printf(" %s\n", item_idname); if (item_idname) { ID *id = BLO_library_link_named_part( mainl, &(self->blo_handle), idcode, item_idname); if (id) { #ifdef USE_RNA_DATABLOCKS /* swap name for pointer to the id */ item_dst = PyCapsule_New((void *)id, NULL, NULL); #else /* leave as is */ continue; #endif } else { bpy_lib_exit_warn_idname(self, name_plural, item_idname); /* just warn for now */ /* err = -1; */ item_dst = Py_INCREF_RET(Py_None); } /* ID or None */ } else { /* XXX, could complain about this */ bpy_lib_exit_warn_type(self, item_src); PyErr_Clear(); item_dst = Py_INCREF_RET(Py_None); } /* item_dst must be new or already incref'd */ Py_DECREF(item_src); PyList_SET_ITEM(ls, i, item_dst); } } } } } if (err == -1) { /* exception raised above, XXX, this leaks some memory */ BLO_blendhandle_close(self->blo_handle); self->blo_handle = NULL; BKE_main_id_tag_all(bmain, LIB_TAG_PRE_EXISTING, false); return NULL; } else { Library *lib = mainl->curlib; /* newly added lib, assign before append end */ BLO_library_link_end(mainl, &(self->blo_handle), self->flag, NULL, NULL, NULL, NULL); BLO_blendhandle_close(self->blo_handle); self->blo_handle = NULL; GHash *old_to_new_ids = BLI_ghash_ptr_new(__func__); /* copied from wm_operator.c */ { /* mark all library linked objects to be updated */ BKE_main_lib_objects_recalc_all(bmain); /* append, rather than linking */ if (do_append) { BKE_library_make_local(bmain, lib, old_to_new_ids, true, false); } } BKE_main_id_tag_all(bmain, LIB_TAG_PRE_EXISTING, false); /* finally swap the capsules for real bpy objects * important since BLO_library_append_end initializes NodeTree types used by srna->refine */ #ifdef USE_RNA_DATABLOCKS { int idcode_step = 0, idcode; while ((idcode = BKE_idcode_iter_step(&idcode_step))) { if (BKE_idcode_is_linkable(idcode) && (idcode != ID_WS || do_append)) { const char *name_plural = BKE_idcode_to_name_plural(idcode); PyObject *ls = PyDict_GetItemString(self->dict, name_plural); if (ls && PyList_Check(ls)) { Py_ssize_t size = PyList_GET_SIZE(ls); Py_ssize_t i; PyObject *item; for (i = 0; i < size; i++) { item = PyList_GET_ITEM(ls, i); if (PyCapsule_CheckExact(item)) { PointerRNA id_ptr; ID *id; id = PyCapsule_GetPointer(item, NULL); id = BLI_ghash_lookup_default(old_to_new_ids, id, id); Py_DECREF(item); RNA_id_pointer_create(id, &id_ptr); item = pyrna_struct_CreatePyObject(&id_ptr); PyList_SET_ITEM(ls, i, item); } } } } } } #endif /* USE_RNA_DATABLOCKS */ BLI_ghash_free(old_to_new_ids, NULL, NULL); Py_RETURN_NONE; } }
// destructor used later in Capsule creation inline void _table_destructor(PyObject *capsule) { auto *p = static_cast<pytypeobject_table_t *>(PyCapsule_GetPointer(capsule, "__main__.__cpp2py_table")); delete p; }
/* Destruktor dla punktów */ static void del_Point(PyObject *obj) { free(PyCapsule_GetPointer(obj,"Point")); }