1 /*
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5  *
6  * Copyright (C) 2017, James R. Barlow (https://github.com/jbarlow83/)
7  */
8 
9 #include <cstdlib>
10 #include <system_error>
11 
12 #include "utils.h"
13 
14 /* POSIX functions on Windows have a leading underscore
15  */
16 #if defined(_WIN32)
17 #    define posix_fdopen _fdopen
18 #    define posix_close _close
19 #else
20 #    define posix_fdopen fdopen
21 #    define posix_close close
22 #endif
23 
24 /* Convert a Python object to a filesystem encoded path
25  * Use Python's os.fspath() which accepts os.PathLike (str, bytes, pathlib.Path)
26  * and returns bytes encoded in the filesystem encoding.
27  * Cast to a string without transcoding.
28  */
29 
fspath(py::object filename)30 py::object fspath(py::object filename)
31 {
32     py::handle handle = PyOS_FSPath(filename.ptr());
33     if (!handle)
34         throw py::error_already_set();
35     return py::reinterpret_steal<py::object>(handle);
36 }
37