MCPcopy Create free account
hub / github.com/pybind/pybind11 / initialize

Method initialize

include/pybind11/pybind11.h:1776–1850  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1774 PYBIND11_OBJECT_DEFAULT(generic_type, object, PyType_Check)
1775protected:
1776 void initialize(const type_record &rec) {
1777 if (rec.scope && hasattr(rec.scope, "__dict__")
1778 && rec.scope.attr("__dict__").contains(rec.name)) {
1779 pybind11_fail("generic_type: cannot initialize type \"" + std::string(rec.name)
1780 + "\": an object with that name is already defined");
1781 }
1782
1783 if ((rec.module_local ? get_local_type_info(*rec.type) : get_global_type_info(*rec.type))
1784 != nullptr) {
1785 pybind11_fail("generic_type: type \"" + std::string(rec.name)
1786 + "\" is already registered!");
1787 }
1788
1789 m_ptr = make_new_python_type(rec);
1790
1791 /* Register supplemental type information in C++ dict */
1792 auto *tinfo = new detail::type_info();
1793 tinfo->type = reinterpret_cast<PyTypeObject *>(m_ptr);
1794 tinfo->cpptype = rec.type;
1795 tinfo->type_size = rec.type_size;
1796 tinfo->type_align = rec.type_align;
1797 tinfo->operator_new = rec.operator_new;
1798 tinfo->holder_size_in_ptrs = size_in_ptrs(rec.holder_size);
1799 tinfo->init_instance = rec.init_instance;
1800 tinfo->dealloc = rec.dealloc;
1801 tinfo->get_trampoline_self_life_support = rec.get_trampoline_self_life_support;
1802 tinfo->simple_type = true;
1803 tinfo->simple_ancestors = true;
1804 tinfo->module_local = rec.module_local;
1805 tinfo->holder_enum_v = rec.holder_enum_v;
1806
1807 with_internals([&](internals &internals) {
1808 auto tindex = std::type_index(*rec.type);
1809 tinfo->direct_conversions = &internals.direct_conversions[tindex];
1810 auto &local_internals = get_local_internals();
1811 if (rec.module_local) {
1812 local_internals.registered_types_cpp[rec.type] = tinfo;
1813 } else {
1814 internals.registered_types_cpp[tindex] = tinfo;
1815#if PYBIND11_INTERNALS_VERSION >= 12
1816 internals.registered_types_cpp_fast[rec.type] = tinfo;
1817#endif
1818 }
1819
1820 PYBIND11_WARNING_PUSH
1821#if defined(__GNUC__) && __GNUC__ == 12
1822 // When using GCC 12 these warnings are disabled as they trigger
1823 // false positive warnings. Discussed here:
1824 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115824.
1825 PYBIND11_WARNING_DISABLE_GCC("-Warray-bounds")
1826 PYBIND11_WARNING_DISABLE_GCC("-Wstringop-overread")
1827#endif
1828 internals.registered_types_py[reinterpret_cast<PyTypeObject *>(m_ptr)] = {tinfo};
1829 PYBIND11_WARNING_POP
1830 });
1831
1832 if (rec.bases.size() > 1 || rec.multiple_inheritance) {
1833 mark_parents_nonsimple(tinfo->type);

Callers

nothing calls this directly

Calls 12

hasattrFunction · 0.85
get_local_type_infoFunction · 0.85
get_global_type_infoFunction · 0.85
make_new_python_typeFunction · 0.85
size_in_ptrsFunction · 0.85
with_internalsFunction · 0.85
setattrFunction · 0.85
capsuleFunction · 0.85
attrMethod · 0.80
ptrMethod · 0.80
containsMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected