From 9a15cbb9f5f0b482af3bbe04462fd2c01825a031 Mon Sep 17 00:00:00 2001 From: GalaxyPatrick <126764491+GalaxyPatrick@users.noreply.github.com> Date: Tue, 21 Nov 2023 23:01:11 +0800 Subject: [PATCH] matplotlibcpp.h Fix matplotlib Initialized problem in Python 3.11(or higher version) --- matplotlibcpp.h | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/matplotlibcpp.h b/matplotlibcpp.h index d95d46a..2ef43f8 100644 --- a/matplotlibcpp.h +++ b/matplotlibcpp.h @@ -171,17 +171,36 @@ struct _interpreter { #else char name[] = "plotting"; #endif +#if PY_MINOR_VERSION < 11 Py_SetProgramName(name); Py_Initialize(); wchar_t const *dummy_args[] = {L"Python", NULL}; // const is needed because literals must not be modified wchar_t const **argv = dummy_args; int argc = sizeof(dummy_args)/sizeof(dummy_args[0])-1; - -#if PY_MAJOR_VERSION >= 3 PySys_SetArgv(argc, const_cast(argv)); -#else - PySys_SetArgv(argc, (char **)(argv)); + #else + PyStatus status; + PyConfig config; + PyConfig_InitPythonConfig(&config); + wchar_t const* dummy_args[] = { L"Python", NULL }; // const is needed because literals must not be modified + wchar_t* const* argv = const_cast(dummy_args); + int argc = sizeof(dummy_args) / sizeof(dummy_args[0]) - 1; + if (argc && argv) { + status = PyConfig_SetString(&config, &config.program_name, name); + if (PyStatus_Exception(status)) { + PyConfig_Clear(&config); + } + status = PyConfig_SetArgv(&config, argc, argv); + if (PyStatus_Exception(status)) { + PyConfig_Clear(&config); + } + } + status = Py_InitializeFromConfig(&config); + if (PyStatus_Exception(status)) { + PyConfig_Clear(&config); + } + PyConfig_Clear(&config); #endif #ifndef WITHOUT_NUMPY @@ -351,9 +370,9 @@ template <> struct select_npy_type { const static NPY_TYPES type = NPY // Sanity checks; comment them out or change the numpy type below if you're compiling on // a platform where they don't apply static_assert(sizeof(long long) == 8); -template <> struct select_npy_type { const static NPY_TYPES type = NPY_INT64; }; +//template <> struct select_npy_type { const static NPY_TYPES type = NPY_INT64; }; static_assert(sizeof(unsigned long long) == 8); -template <> struct select_npy_type { const static NPY_TYPES type = NPY_UINT64; }; +//template <> struct select_npy_type { const static NPY_TYPES type = NPY_UINT64; }; template PyObject* get_array(const std::vector& v)