μHAL (v2.8.17)
Part of the IPbus software repository
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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) && __has_include(<filesystem>) && \
17 PY_VERSION_HEX >= 0x03060000
18# include <filesystem>
19# define PYBIND11_HAS_FILESYSTEM 1
20# endif
21#endif
22
23#if !defined(PYBIND11_HAS_FILESYSTEM) && !defined(PYBIND11_HAS_FILESYSTEM_IS_OPTIONAL)
24# error \
25 "#include <filesystem> is not available. (Use -DPYBIND11_HAS_FILESYSTEM_IS_OPTIONAL to ignore.)"
26#endif
27
30
31#if defined(PYBIND11_HAS_FILESYSTEM)
32template <typename T>
33struct path_caster {
34
35private:
36 static PyObject *unicode_from_fs_native(const std::string &w) {
37# if !defined(PYPY_VERSION)
38 return PyUnicode_DecodeFSDefaultAndSize(w.c_str(), ssize_t(w.size()));
39# else
40 // PyPy mistakenly declares the first parameter as non-const.
41 return PyUnicode_DecodeFSDefaultAndSize(const_cast<char *>(w.c_str()), ssize_t(w.size()));
42# endif
43 }
44
45 static PyObject *unicode_from_fs_native(const std::wstring &w) {
46 return PyUnicode_FromWideChar(w.c_str(), ssize_t(w.size()));
47 }
48
49public:
50 static handle cast(const T &path, return_value_policy, handle) {
51 if (auto py_str = unicode_from_fs_native(path.native())) {
52 return module_::import("pathlib")
53 .attr("Path")(reinterpret_steal<object>(py_str))
54 .release();
55 }
56 return nullptr;
57 }
58
59 bool load(handle handle, bool) {
60 // PyUnicode_FSConverter and PyUnicode_FSDecoder normally take care of
61 // calling PyOS_FSPath themselves, but that's broken on PyPy (PyPy
62 // issue #3168) so we do it ourselves instead.
63 PyObject *buf = PyOS_FSPath(handle.ptr());
64 if (!buf) {
65 PyErr_Clear();
66 return false;
67 }
68 PyObject *native = nullptr;
69 if constexpr (std::is_same_v<typename T::value_type, char>) {
70 if (PyUnicode_FSConverter(buf, &native) != 0) {
71 if (auto *c_str = PyBytes_AsString(native)) {
72 // AsString returns a pointer to the internal buffer, which
73 // must not be free'd.
74 value = c_str;
75 }
76 }
77 } else if constexpr (std::is_same_v<typename T::value_type, wchar_t>) {
78 if (PyUnicode_FSDecoder(buf, &native) != 0) {
79 if (auto *c_str = PyUnicode_AsWideCharString(native, nullptr)) {
80 // AsWideCharString returns a new string that must be free'd.
81 value = c_str; // Copies the string.
82 PyMem_Free(c_str);
83 }
84 }
85 }
86 Py_XDECREF(native);
87 Py_DECREF(buf);
88 if (PyErr_Occurred()) {
89 PyErr_Clear();
90 return false;
91 }
92 return true;
93 }
94
95 PYBIND11_TYPE_CASTER(T, const_name("os.PathLike"));
96};
97
98template <>
99struct type_caster<std::filesystem::path> : public path_caster<std::filesystem::path> {};
100#endif // PYBIND11_HAS_FILESYSTEM
101
\rst Holds a reference to a Python object (no reference counting)
Definition: pytypes.h:194
PyObject * ptr() const
Return the underlying PyObject * pointer.
Definition: pytypes.h:203
static module_ import(const char *name)
Import and return a module or throws error_already_set.
Definition: pybind11.h:1200
#define PYBIND11_TYPE_CASTER(type, py_name)
Definition: cast.h:82
T cast(const handle &handle)
Definition: cast.h:1050
#define PYBIND11_NAMESPACE_END(name)
Definition: common.h:21
#define PYBIND11_NAMESPACE_BEGIN(name)
Definition: common.h:20
Py_ssize_t ssize_t
Definition: common.h:460
return_value_policy
Approach used to cast a previously unknown C++ instance into a Python object.
Definition: common.h:470
constexpr descr< N - 1 > const_name(char const (&text)[N])
Definition: descr.h:60
const char * c_str(Args &&...args)
Constructs a std::string with the given arguments, stores it in internals, and returns its c_str().
Definition: internals.h:534