Skip to content

Commit b3ebd11

Browse files
LlcoolsouderLonnie Souder IIpre-commit-ci[bot]
authored
feature: support compilers that use std::experimental::filesystem (#3840)
* feature: support compilers that use std::experimental::filesystem such as gcc7 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * only use this feature if cpp17 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * more specific namespace alias + style * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * move to pybind11 namespace * no namespace alias * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: Lonnie Souder II <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 65ec5de commit b3ebd11

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

include/pybind11/stl/filesystem.h

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,28 @@
1313
#include <string>
1414

1515
#ifdef __has_include
16-
# if defined(PYBIND11_CPP17) && __has_include(<filesystem>)
17-
# include <filesystem>
18-
# define PYBIND11_HAS_FILESYSTEM 1
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
1925
# endif
2026
#endif
2127

22-
#if !defined(PYBIND11_HAS_FILESYSTEM) && !defined(PYBIND11_HAS_FILESYSTEM_IS_OPTIONAL)
28+
#if !defined(PYBIND11_HAS_FILESYSTEM) && !defined(PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM) \
29+
&& !defined(PYBIND11_HAS_FILESYSTEM_IS_OPTIONAL)
2330
# error \
24-
"#include <filesystem> is not available. (Use -DPYBIND11_HAS_FILESYSTEM_IS_OPTIONAL to ignore.)"
31+
"Neither #include <filesystem> nor #include <experimental/filesystem is available. (Use -DPYBIND11_HAS_FILESYSTEM_IS_OPTIONAL to ignore.)"
2532
#endif
2633

2734
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
2835
PYBIND11_NAMESPACE_BEGIN(detail)
2936

30-
#if defined(PYBIND11_HAS_FILESYSTEM)
37+
#if defined(PYBIND11_HAS_FILESYSTEM) || defined(PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM)
3138
template <typename T>
3239
struct path_caster {
3340

@@ -94,9 +101,16 @@ struct path_caster {
94101
PYBIND11_TYPE_CASTER(T, const_name("os.PathLike"));
95102
};
96103

104+
#endif // PYBIND11_HAS_FILESYSTEM || defined(PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM)
105+
106+
#if defined(PYBIND11_HAS_FILESYSTEM)
97107
template <>
98108
struct type_caster<std::filesystem::path> : public path_caster<std::filesystem::path> {};
99-
#endif // PYBIND11_HAS_FILESYSTEM
109+
#elif defined(PYBIND11_HAS_EXPERIMENTAL_FILESYSTEM)
110+
template <>
111+
struct type_caster<std::experimental::filesystem::path>
112+
: public path_caster<std::experimental::filesystem::path> {};
113+
#endif
100114

101115
PYBIND11_NAMESPACE_END(detail)
102116
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)

0 commit comments

Comments
 (0)