ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
pybind_filesystem.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 // Adapted from <pybind11/stl/filesystem.h> to support C++14.
9 // Original attribution:
10 // Copyright (c) 2021 The Pybind Development Team.
11 // All rights reserved. Use of this source code is governed by a
12 // BSD-style license.
13 
14 #pragma once
15 
16 #include <pybind11/cast.h>
17 #include <pybind11/detail/common.h>
18 #include <pybind11/detail/descr.h>
19 #include <pybind11/pybind11.h>
20 #include <pybind11/pytypes.h>
21 
22 #include <string>
23 
24 #ifdef _WIN32
25 #define _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING
26 #endif
27 #ifdef __APPLE__
28 #include <filesystem>
29 namespace fs = std::__fs::filesystem;
30 #else
31 #include <experimental/filesystem>
32 namespace fs = std::experimental::filesystem;
33 #endif
34 
35 namespace pybind11 {
36 namespace detail {
37 
38 template <typename T>
39 struct path_caster {
40 private:
41  static PyObject *unicode_from_fs_native(const std::string &w) {
42 #if !defined(PYPY_VERSION)
43  return PyUnicode_DecodeFSDefaultAndSize(w.c_str(), ssize_t(w.size()));
44 #else
45  // PyPy mistakenly declares the first parameter as non-const.
46  return PyUnicode_DecodeFSDefaultAndSize(const_cast<char *>(w.c_str()),
47  ssize_t(w.size()));
48 #endif
49  }
50 
51  static PyObject *unicode_from_fs_native(const std::wstring &w) {
52  return PyUnicode_FromWideChar(w.c_str(), ssize_t(w.size()));
53  }
54 
55 public:
56  static handle cast(const T &path, return_value_policy, handle) {
57  if (auto py_str = unicode_from_fs_native(path.native())) {
58  return module_::import("pathlib")
59  .attr("Path")(reinterpret_steal<object>(py_str))
60  .release();
61  }
62  return nullptr;
63  }
64 
65  bool load(handle handle, bool) {
66  // PyUnicode_FSConverter and PyUnicode_FSDecoder normally take care of
67  // calling PyOS_FSPath themselves, but that's broken on PyPy (PyPy
68  // issue #3168) so we do it ourselves instead.
69  PyObject *buf = PyOS_FSPath(handle.ptr());
70  if (!buf) {
71  PyErr_Clear();
72  return false;
73  }
74  PyObject *native = nullptr;
75  if (std::is_same<typename T::value_type, char>::value) {
76  if (PyUnicode_FSConverter(buf, &native) != 0) {
77  if (auto *c_str = PyBytes_AsString(native)) {
78  // AsString returns a pointer to the internal buffer, which
79  // must not be free'd.
80  value = c_str;
81  }
82  }
83  } else if (std::is_same<typename T::value_type, wchar_t>::value) {
84  if (PyUnicode_FSDecoder(buf, &native) != 0) {
85  if (auto *c_str = PyUnicode_AsWideCharString(native, nullptr)) {
86  // AsWideCharString returns a new string that must be
87  // free'd.
88  value = c_str; // Copies the string.
89  PyMem_Free(c_str);
90  }
91  }
92  }
93  Py_XDECREF(native);
94  Py_DECREF(buf);
95  if (PyErr_Occurred()) {
96  PyErr_Clear();
97  return false;
98  }
99  return true;
100  }
101 
103  io_name("Union[os.PathLike, str, bytes]",
104  "pathlib.Path"));
105 };
106 
107 template <>
108 struct type_caster<fs::path> : public path_caster<fs::path> {};
109 
110 } // namespace detail
111 } // namespace pybind11
static const std::string path
Definition: PointCloud.cpp:59
PYBIND11_TYPE_CASTER(T, io_name("Union[os.PathLike, str, bytes]", "pathlib.Path"))
bool load(handle handle, bool)
static handle cast(const T &path, return_value_policy, handle)