Work around a writable-strings warning with Python 2's PyMapping_Keys macro, and while we're at it give them consistent behavior by making sure the returned value is a list. As with PyMapping_Keys, returns a new reference. On failure, returns nullptr.
| 858 | // |
| 859 | // On failure, returns nullptr. |
| 860 | PyObject* MappingKeys(PyObject* o) { |
| 861 | #if PY_MAJOR_VERSION >= 3 |
| 862 | return PyMapping_Keys(o); |
| 863 | #else |
| 864 | static char key_method_name[] = "keys"; |
| 865 | Safe_PyObjectPtr raw_result(PyObject_CallMethod(o, key_method_name, nullptr)); |
| 866 | if (PyErr_Occurred() || raw_result.get() == nullptr) { |
| 867 | return nullptr; |
| 868 | } |
| 869 | return PySequence_Fast( |
| 870 | raw_result.get(), |
| 871 | "The '.keys()' method of a custom mapping returned a non-sequence."); |
| 872 | #endif |
| 873 | } |
| 874 | |
| 875 | PyObject* Flatten(PyObject* nested, bool expand_composites) { |
| 876 | PyObject* list = PyList_New(0); |
no test coverage detected