Add support for os.PathLike inputs in the parsers

Resolve: #974
This commit is contained in:
Romain Thomas
2023-10-21 05:23:42 +02:00
parent dffcff5df7
commit a27bd6a644
24 changed files with 201 additions and 60 deletions
+26
View File
@@ -0,0 +1,26 @@
#include "pyutils.hpp"
namespace LIEF::py {
result<std::string> path_to_str(nb::object pathlike) {
PyObject* buf = PyOS_FSPath(pathlike.ptr());
if (!buf) {
PyErr_Clear();
return make_error_code(lief_errors::conversion_error);
}
PyObject* native = nullptr;
std::string path_str;
if (PyUnicode_FSConverter(buf, &native) != 0) {
if (char* c_str = PyBytes_AsString(native)) {
path_str = c_str;
}
}
Py_XDECREF(native);
Py_DECREF(buf);
if (PyErr_Occurred()) {
PyErr_Clear();
return make_error_code(lief_errors::conversion_error);
}
return path_str;
}
}