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

Method keyword_index

include/pybind11/pybind11.h:1385–1405  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1383 }
1384
1385 static ssize_t keyword_index(PyObject *haystack, char const *needle) {
1386 /* kwargs is usually very small (<= 5 entries). The arg strings are typically interned.
1387 * CPython itself implements the search this way, first comparing all pointers ... which is
1388 * cheap and will work if the strings are interned. If it fails, then it falls back to a
1389 * second lexicographic check. This is wildly expensive for huge argument lists, but those
1390 * are incredibly rare so we optimize for the vastly common case of just a couple of args.
1391 */
1392 auto n = PyTuple_GET_SIZE(haystack);
1393 auto s = reinterpret_steal<pybind11::str>(PyUnicode_InternFromString(needle));
1394 for (ssize_t i = 0; i < n; ++i) {
1395 if (PyTuple_GET_ITEM(haystack, i) == s.ptr()) {
1396 return i;
1397 }
1398 }
1399 for (ssize_t i = 0; i < n; ++i) {
1400 if (PyUnicode_Compare(PyTuple_GET_ITEM(haystack, i), s.ptr()) == 0) {
1401 return i;
1402 }
1403 }
1404 return -1;
1405 }
1406};
1407
1408PYBIND11_NAMESPACE_BEGIN(detail)

Callers

nothing calls this directly

Calls 1

ptrMethod · 0.80

Tested by

no test coverage detected