filesystem.h
Go to the documentation of this file.
1 // Copyright (c) 2021 The Pybind Development Team.
2 // All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4 
5 #pragma once
6 
7 #include "../pybind11.h"
8 #include "../detail/common.h"
9 #include "../detail/descr.h"
10 #include "../cast.h"
11 #include "../pytypes.h"
12 
13 #include <string>
14 
15 #ifdef __has_include
16 # if defined(PYBIND11_CPP17)
17 # if __has_include(<filesystem>) && \
18  PY_VERSION_HEX >= 0x03060000
19 # include <filesystem>
20 # define PYBIND11_HAS_FILESYSTEM 1
21 # elif __has_include(<experimental/filesystem>)
22 # include <experimental/filesystem>
23 # define PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM 1
24 # endif
25 # endif
26 #endif
27 
28 #if !defined(PYBIND11_HAS_FILESYSTEM) && !defined(PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM) \
29  && !defined(PYBIND11_HAS_FILESYSTEM_IS_OPTIONAL)
30 # error \
31  "Neither #include <filesystem> nor #include <experimental/filesystem is available. (Use -DPYBIND11_HAS_FILESYSTEM_IS_OPTIONAL to ignore.)"
32 #endif
33 
36 
37 #if defined(PYBIND11_HAS_FILESYSTEM) || defined(PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM)
38 template <typename T>
39 struct path_caster {
40 
41 private:
42  static PyObject *unicode_from_fs_native(const std::string &w) {
43 # if !defined(PYPY_VERSION)
44  return PyUnicode_DecodeFSDefaultAndSize(w.c_str(), ssize_t(w.size()));
45 # else
46  // PyPy mistakenly declares the first parameter as non-const.
47  return PyUnicode_DecodeFSDefaultAndSize(const_cast<char *>(w.c_str()), 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 constexpr (std::is_same_v<typename T::value_type, char>) {
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 constexpr (std::is_same_v<typename T::value_type, wchar_t>) {
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 free'd.
87  value = c_str; // Copies the string.
88  PyMem_Free(c_str);
89  }
90  }
91  }
92  Py_XDECREF(native);
93  Py_DECREF(buf);
94  if (PyErr_Occurred()) {
95  PyErr_Clear();
96  return false;
97  }
98  return true;
99  }
100 
101  PYBIND11_TYPE_CASTER(T, const_name("os.PathLike"));
102 };
103 
104 #endif // PYBIND11_HAS_FILESYSTEM || defined(PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM)
105 
106 #if defined(PYBIND11_HAS_FILESYSTEM)
107 template <>
108 struct type_caster<std::filesystem::path> : public path_caster<std::filesystem::path> {};
109 #elif defined(PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM)
110 template <>
112  : public path_caster<std::experimental::filesystem::path> {};
113 #endif
114 
Definition: BFloat16.h:88
const char * c_str(Args &&...args)
Definition: internals.h:524
EIGEN_DEVICE_FUNC NewType cast(const OldType &x)
RowVector3d w
handle release()
Definition: pytypes.h:330
static module_ import(const char *name)
Import and return a module or throws error_already_set.
Definition: pybind11.h:1194
PyObject * ptr() const
Return the underlying PyObject * pointer.
Definition: pytypes.h:238
constexpr descr< N - 1 > const_name(char const (&text)[N])
Definition: descr.h:60
return_value_policy
Approach used to cast a previously unknown C++ instance into a Python object.
#define PYBIND11_TYPE_CASTER(type, py_name)
Definition: cast.h:82
#define PYBIND11_NAMESPACE_END(name)
#define PYBIND11_NAMESPACE_BEGIN(name)


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:13