From 173f328c53d39dd42ecdb9de9e04f9d2c266683f Mon Sep 17 00:00:00 2001 From: Mikhail Nosov Date: Mon, 28 Feb 2022 17:04:59 +0300 Subject: [PATCH] Checking compatibility between 'pyopenvino' and 'libopenvino' (#10668) * Checking compatibility between 'pyopenvino' and 'libopenvino' on 'import phase' This fix is to prevent undefined behavior when user loads OpenVINO from python, but pyopenvino loads different version of 'libopenvino' This may happen if user has several releases installed and played around PATH/PYTHONPATH environment variables. In such case, user may have undefined behavior - application may crash in the middle of the usage or use incorrect release. Fix checks build versions for pyopenvino and ov::get_openvino_version. If mismatch occurs, exception is thrown. This logic is disabled if user has built OpenVINO locally, experienced developers probably know what they're doing, so if version has 'custom_' prefix - this logic is disabled * Removed custom logic for CI_BUILD_NUMBER, it is reused from already included version.cmake * Use addVersionDefines macro --- src/bindings/python/src/pyopenvino/CMakeLists.txt | 2 ++ src/bindings/python/src/pyopenvino/pyopenvino.cpp | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/bindings/python/src/pyopenvino/CMakeLists.txt b/src/bindings/python/src/pyopenvino/CMakeLists.txt index 7e645628278..c4c14077828 100644 --- a/src/bindings/python/src/pyopenvino/CMakeLists.txt +++ b/src/bindings/python/src/pyopenvino/CMakeLists.txt @@ -78,6 +78,8 @@ endif() target_include_directories(${PROJECT_NAME} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/..") target_link_libraries(${PROJECT_NAME} PRIVATE openvino::runtime ${OFFLINE_TRANSFORMATIONS_LIB}) +addVersionDefines(pyopenvino.cpp CI_BUILD_NUMBER) + # perform copy if(OpenVINO_SOURCE_DIR) add_custom_command(TARGET ${PROJECT_NAME} diff --git a/src/bindings/python/src/pyopenvino/pyopenvino.cpp b/src/bindings/python/src/pyopenvino/pyopenvino.cpp index 012a5bf6fc5..53ac0003301 100644 --- a/src/bindings/python/src/pyopenvino/pyopenvino.cpp +++ b/src/bindings/python/src/pyopenvino/pyopenvino.cpp @@ -69,6 +69,21 @@ std::string get_version() { PYBIND11_MODULE(pyopenvino, m) { m.doc() = "Package openvino.pyopenvino which wraps openvino C++ APIs"; + std::string pyopenvino_version = CI_BUILD_NUMBER; + std::string runtime_version = get_version(); + bool is_custom_pyopenvino_version = pyopenvino_version.empty() || pyopenvino_version.find("custom_") == 0; + bool is_custom_runtime_version = runtime_version.empty() || runtime_version.find("custom_") == 0; + auto versions_compatible = + is_custom_pyopenvino_version || is_custom_runtime_version || pyopenvino_version == runtime_version; + OPENVINO_ASSERT(versions_compatible, + "OpenVINO Python version (", + pyopenvino_version, + ") mismatches with OpenVINO Runtime library version (", + runtime_version, + "). It can happen if you have 2 or more different versions of OpenVINO installed in system. " + "Please ensure that environment variables (e.g. PATH, PYTHONPATH) are set correctly so that " + "OpenVINO Runtime and Python libraries point to same release."); + m.def("get_version", &get_version); m.def("get_batch", &ov::get_batch); m.def("set_batch", &ov::set_batch);