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

Function object_is_convertible_to_std_map

include/pybind11/stl.h:110–133  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

108}
109
110inline bool object_is_convertible_to_std_map(const handle &src, bool convert) {
111 // Allow dict.
112 if (PyDict_Check(src.ptr())) {
113 return true;
114 }
115 // Allow types conforming to Mapping Protocol.
116 // According to https://docs.python.org/3/c-api/mapping.html, `PyMappingCheck()` checks for
117 // `__getitem__()` without checking the type of keys. In order to restrict the allowed types
118 // closer to actual Mapping-like types, we also check for the `items()` method.
119 if (PyMapping_Check(src.ptr()) != 0) {
120 PyObject *items = PyObject_GetAttrString(src.ptr(), "items");
121 if (items != nullptr) {
122 bool is_convertible = (PyCallable_Check(items) != 0);
123 Py_DECREF(items);
124 if (is_convertible) {
125 return true;
126 }
127 } else {
128 PyErr_Clear();
129 }
130 }
131 // In convert mode: Allow types derived from collections.abc.Mapping
132 return convert && isinstance(src, module_::import("collections.abc").attr("Mapping"));
133}
134
135//
136// End: Equivalent of clif/python/runtime.cc

Callers 1

loadMethod · 0.85

Calls 4

isinstanceFunction · 0.85
importFunction · 0.85
ptrMethod · 0.80
attrMethod · 0.80

Tested by

no test coverage detected