Skip to content

Commit b4d5d5f

Browse files
committed
test_package: Fix deprecation warnings
Python 3.8 introduced a new configuration API that provides a lot more control, especially for embedding. The old API has been marked as deprecated in Python 3.11 (e.g. functions like `Py_SetPythonHome()`).
1 parent 19e5f7d commit b4d5d5f

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

core/test_package/src/main.cpp

+16-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,23 @@
33
#include <filesystem>
44

55
int main(int argc, const char* argv[]) {
6+
auto config = PyConfig{};
7+
PyConfig_InitIsolatedConfig(&config);
8+
69
const auto bin = std::filesystem::path(argv[0]).parent_path();
7-
const auto python_home = (bin / "python").wstring();
8-
Py_SetPythonHome(python_home.data());
9-
Py_Initialize();
10+
const auto python_home = (bin / "python").string();
11+
if (auto status = PyConfig_SetBytesString(&config, &config.home, python_home.c_str());
12+
PyStatus_Exception(status)) {
13+
PyConfig_Clear(&config);
14+
return 1;
15+
}
16+
17+
if (auto status = Py_InitializeFromConfig(&config); PyStatus_Exception(status)) {
18+
PyConfig_Clear(&config);
19+
return 1;
20+
}
21+
PyConfig_Clear(&config);
22+
1023
std::cout << Py_GetVersion() << std::endl;
1124
Py_Finalize();
1225
}

test_package/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ if(NOT Python_EXECUTABLE MATCHES "embedded_python")
77
message(FATAL_ERROR "`cmake` failed to find the correct Python")
88
endif()
99

10-
add_executable(test_package src/main.cpp)
10+
add_executable(test_package ../core/test_package/src/main.cpp)
1111
target_link_libraries(test_package PRIVATE Python::Python)
1212
target_compile_definitions(test_package PRIVATE MS_NO_COREDLL) # avoid linking to `_d.lib` in debug mode
1313
set_target_properties(test_package PROPERTIES CXX_STANDARD 17)

test_package/src/main.cpp

-12
This file was deleted.

0 commit comments

Comments
 (0)