|
41 | 41 | # include <variant> |
42 | 42 | # define PYBIND11_HAS_VARIANT 1 |
43 | 43 | # endif |
| 44 | +// std::filesystem::path |
| 45 | +# if defined(PYBIND11_CPP17) && __has_include(<filesystem>) && \ |
| 46 | + PY_VERSION_HEX >= 0x03060000 |
| 47 | +# include <filesystem> |
| 48 | +# define PYBIND11_HAS_FILESYSTEM 1 |
| 49 | +# endif |
44 | 50 | #elif defined(_MSC_VER) && defined(PYBIND11_CPP17) |
45 | 51 | # include <optional> |
46 | 52 | # include <variant> |
47 | 53 | # define PYBIND11_HAS_OPTIONAL 1 |
48 | 54 | # define PYBIND11_HAS_VARIANT 1 |
| 55 | +# if PY_VERSION_HEX >= 0x03060000 |
| 56 | +# include <filesystem> |
| 57 | +# define PYBIND11_HAS_FILESYSTEM 1 |
| 58 | +# endif |
49 | 59 | #endif |
50 | 60 |
|
51 | 61 | PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) |
@@ -377,6 +387,77 @@ template <typename... Ts> |
377 | 387 | struct type_caster<std::variant<Ts...>> : variant_caster<std::variant<Ts...>> { }; |
378 | 388 | #endif |
379 | 389 |
|
| 390 | +#if defined(PYBIND11_HAS_FILESYSTEM) |
| 391 | +template<typename T> struct path_caster { |
| 392 | + |
| 393 | +private: |
| 394 | + static PyObject* unicode_from_fs_native(const std::string& w) { |
| 395 | +#if !defined(PYPY_VERSION) |
| 396 | + return PyUnicode_DecodeFSDefaultAndSize(w.c_str(), ssize_t(w.size())); |
| 397 | +#else |
| 398 | + // PyPy mistakenly declares the first parameter as non-const. |
| 399 | + return PyUnicode_DecodeFSDefaultAndSize( |
| 400 | + const_cast<char*>(w.c_str()), ssize_t(w.size())); |
| 401 | +#endif |
| 402 | + } |
| 403 | + |
| 404 | + static PyObject* unicode_from_fs_native(const std::wstring& w) { |
| 405 | + return PyUnicode_FromWideChar(w.c_str(), ssize_t(w.size())); |
| 406 | + } |
| 407 | + |
| 408 | +public: |
| 409 | + static handle cast(const T& path, return_value_policy, handle) { |
| 410 | + if (auto py_str = unicode_from_fs_native(path.native())) { |
| 411 | + return module::import("pathlib").attr("Path")(reinterpret_steal<object>(py_str)) |
| 412 | + .release(); |
| 413 | + } |
| 414 | + return nullptr; |
| 415 | + } |
| 416 | + |
| 417 | + bool load(handle handle, bool) { |
| 418 | + // PyUnicode_FSConverter and PyUnicode_FSDecoder normally take care of |
| 419 | + // calling PyOS_FSPath themselves, but that's broken on PyPy (PyPy |
| 420 | + // issue #3168) so we do it ourselves instead. |
| 421 | + PyObject* buf = PyOS_FSPath(handle.ptr()); |
| 422 | + if (!buf) { |
| 423 | + PyErr_Clear(); |
| 424 | + return false; |
| 425 | + } |
| 426 | + PyObject* native = nullptr; |
| 427 | + if constexpr (std::is_same_v<typename T::value_type, char>) { |
| 428 | + if (PyUnicode_FSConverter(buf, &native)) { |
| 429 | + if (auto c_str = PyBytes_AsString(native)) { |
| 430 | + // AsString returns a pointer to the internal buffer, which |
| 431 | + // must not be free'd. |
| 432 | + value = c_str; |
| 433 | + } |
| 434 | + } |
| 435 | + } else if constexpr (std::is_same_v<typename T::value_type, wchar_t>) { |
| 436 | + if (PyUnicode_FSDecoder(buf, &native)) { |
| 437 | + if (auto c_str = PyUnicode_AsWideCharString(native, nullptr)) { |
| 438 | + // AsWideCharString returns a new string that must be free'd. |
| 439 | + value = c_str; // Copies the string. |
| 440 | + PyMem_Free(c_str); |
| 441 | + } |
| 442 | + } |
| 443 | + } |
| 444 | + Py_XDECREF(native); |
| 445 | + Py_DECREF(buf); |
| 446 | + if (PyErr_Occurred()) { |
| 447 | + PyErr_Clear(); |
| 448 | + return false; |
| 449 | + } else { |
| 450 | + return true; |
| 451 | + } |
| 452 | + } |
| 453 | + |
| 454 | + PYBIND11_TYPE_CASTER(T, _("os.PathLike")); |
| 455 | +}; |
| 456 | + |
| 457 | +template<> struct type_caster<std::filesystem::path> |
| 458 | + : public path_caster<std::filesystem::path> {}; |
| 459 | +#endif |
| 460 | + |
380 | 461 | PYBIND11_NAMESPACE_END(detail) |
381 | 462 |
|
382 | 463 | inline std::ostream &operator<<(std::ostream &os, const handle &obj) { |
|
0 commit comments