| 108 | } |
| 109 | |
| 110 | inline 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 |
no test coverage detected