Skip to content

Commit fb3de9f

Browse files
bstaleticdrmoose
authored andcommitted
Define wide_char_arg_deleter outside set_interpreter_argv.
1 parent 27ad85e commit fb3de9f

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

include/pybind11/embed.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,17 @@ struct embedded_module {
8989
}
9090
};
9191

92+
struct wide_char_arg_deleter {
93+
void operator()(void* ptr) const {
94+
#if PY_VERSION_HEX >= 0x030500f0
95+
// API docs: https://docs.python.org/3/c-api/sys.html#c.Py_DecodeLocale
96+
PyMem_RawFree(ptr);
97+
#else
98+
delete ptr;
99+
#endif
100+
}
101+
};
102+
92103
/// Python 2.x/3.x-compatible version of `PySys_SetArgv`
93104
inline void set_interpreter_argv(int argc, char** argv, bool add_current_dir_to_path) {
94105
// Before it was special-cased in python 3.8, passing an empty or null argv
@@ -106,18 +117,7 @@ inline void set_interpreter_argv(int argc, char** argv, bool add_current_dir_to_
106117
size_t argv_size = static_cast<size_t>(argc);
107118
// SetArgv* on python 3 takes wchar_t, so we have to convert.
108119
std::unique_ptr<wchar_t*[]> widened_argv(new wchar_t*[argv_size]);
109-
# if PY_MINOR_VERSION >= 5
110-
// Use of PyMem_RawFree here instead of PyMem_Free is as recommended by the python
111-
// API docs: https://docs.python.org/3/c-api/sys.html#c.Py_DecodeLocale
112-
struct pymem_rawfree_deleter {
113-
void operator()(void* ptr) const {
114-
PyMem_RawFree(ptr);
115-
}
116-
};
117-
std::vector< std::unique_ptr<wchar_t[], pymem_rawfree_deleter> > widened_argv_entries;
118-
# else
119-
std::vector< std::unique_ptr<wchar_t[]> > widened_argv_entries;
120-
# endif
120+
std::vector< std::unique_ptr<wchar_t[], wide_char_arg_deleter> > widened_argv_entries;
121121
for (size_t ii = 0; ii < argv_size; ++ii) {
122122
# if PY_MINOR_VERSION >= 5
123123
// From Python 3.5 onwards, we're supposed to use Py_DecodeLocale to

0 commit comments

Comments
 (0)