Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[core ] Fix std::filesystem::path convert to string in OV error messages (#33150)

Pawel Raasz358f1fe9971e
diff --git a/src/core/include/openvino/core/except.hpp b/src/core/include/openvino/core/except.hpp
index a3043c93fc7..e549cd71cf2 100644
--- a/src/core/include/openvino/core/except.hpp
+++ b/src/core/include/openvino/core/except.hpp
@@ -4,6 +4,7 @@
#pragma once
+#include <filesystem>
#include <iostream>
#include <sstream>
#include <stdexcept>
@@ -13,6 +14,40 @@
namespace ov {
+template <class T>
+auto stringify(T&& arg) -> std::conditional_t<std::is_same_v<std::decay_t<T>, std::string>, T&, std::string> {
+ if constexpr (std::is_same_v<std::decay_t<T>, std::string>) {
+ return arg;
+ } else {
+ std::stringstream stream;
+ stream << arg;
+ return stream.str();
+ }
+}
+
+#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32)
+OPENVINO_API std::string stringify(const std::filesystem::path& arg);
+#endif
+
+template <typename... TS>
+std::ostream& write_all_to_stream(std::ostream& str, TS&&... args) {
+ if constexpr (std::is_same_v<typename std::filesystem::path::string_type, std::wstring>) {
+ constexpr auto fwd_or_str =
+ [](auto&& arg) -> std::conditional_t<std::is_same_v<std::filesystem::path, std::decay_t<decltype(arg)>>,
+ decltype(stringify(arg)),
+ decltype(arg)&> {
+ if constexpr (std::is_same_v<std::filesystem::path, std::decay_t<decltype(arg)>>) {
+ return stringify(arg);
+ } else {
+ return arg;
+ }
+ };
+ return (str << ... << (fwd_or_str(std::forward<TS>(args))));
+ } else {
+ return (str << ... << args);
+ }
+}
+
/// Base error for ov runtime errors.
class OPENVINO_API Exception : public std::runtime_error {
public:
@@ -31,29 +66,6 @@ protected:
const std::string& explanation);
};
-static inline std::ostream& write_all_to_stream(std::ostream& str) {
- return str;
-}
-
-template <typename T, typename... TS>
-std::ostream& write_all_to_stream(std::ostream& str, T&& arg, TS&&... args) {
- return write_all_to_stream(str << arg, std::forward<TS>(args)...);
-}
-
-template <class T,
- typename std::enable_if<!std::is_same<typename std::decay<T>::type, std::string>::value>::type* = nullptr>
-std::string stringify(T&& arg) {
- std::stringstream stream;
- stream << arg;
- return stream.str();
-}
-
-template <class T,
- typename std::enable_if<std::is_same<typename std::decay<T>::type, std::string>::value>::type* = nullptr>
-T& stringify(T&& arg) {
- return arg;
-}
-
/// Base class for check failure exceptions.
class OPENVINO_API AssertFailure : public Exception {
public:
diff --git a/src/core/src/except.cpp b/src/core/src/except.cpp
index d8c47d26799..9c9a2799b85 100644
--- a/src/core/src/except.cpp
+++ b/src/core/src/except.cpp
@@ -54,3 +54,11 @@ void ov::NotImplemented::create(const char* file, int line, const std::string& e
ov::NotImplemented::~NotImplemented() = default;
const std::string ov::NotImplemented::default_msg{"Not Implemented"};
+
+namespace ov {
+#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32)
+std::string stringify(const std::filesystem::path& arg) {
+ return std::string("\"") + ov::util::path_to_string(arg) + '"';
+}
+#endif
+} // namespace ov
diff --git a/src/core/tests/check.cpp b/src/core/tests/check.cpp
index f903d95c224..ec9527f6442 100644
--- a/src/core/tests/check.cpp
+++ b/src/core/tests/check.cpp
@@ -4,6 +4,7 @@
#include <gtest/gtest.h>
+#include "common_test_utils/file_utils.hpp"
#include "common_test_utils/test_assertions.hpp"
#include "openvino/core/except.hpp"
#include "openvino/util/file_util.hpp"
@@ -61,3 +62,18 @@ TEST(check, ov_throw_exception_check_relative_path_to_source) {
ov::Exception,
AnyOf(StartsWith(exp_native_slash), StartsWith(exp_fwd_slash)));
}
+
+#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT)
+TEST(check, error_message_with_fs_path_and_unicode) {
+ const auto path = ov::test::utils::to_fs_path("这是.folder") / ov::test::utils::to_fs_path(L"这.txt");
+ auto description = std::string("Error detail");
+ const auto exp_error_str = std::string("Test read file: \"这是.folder") +
+ ov::util::FileTraits<char>::file_separator +
+ std::string("这.txt\", because: Error detail");
+
+ std::stringstream error;
+ ov::write_all_to_stream(error, "Test read file: ", path, ", because: ", description);
+
+ EXPECT_EQ(error.str(), exp_error_str);
+}
+#endif
diff --git a/src/inference/src/dev/core_impl.cpp b/src/inference/src/dev/core_impl.cpp
index 2aa0759c882..e1ff566525f 100644
--- a/src/inference/src/dev/core_impl.cpp
+++ b/src/inference/src/dev/core_impl.cpp
@@ -712,7 +712,7 @@ ov::Plugin ov::CoreImpl::get_plugin(const std::string& plugin_name) const {
// Check that device plugin name is the same as requested for HW plugins
if (!plugin_name.empty() && !ov::is_virtual_device(plugin_name)) {
OPENVINO_ASSERT(device_name.find(plugin_name) != std::string::npos,
- ov::util::path_to_string(desc.m_lib_location),
+ desc.m_lib_location,
" is used for ",
device_name,
" , while it contains implementation for ",
@@ -820,7 +820,7 @@ ov::Plugin ov::CoreImpl::get_plugin(const std::string& plugin_name) const {
return m_plugins.emplace(device_name, plugin).first->second;
} catch (const ov::Exception& ex) {
OPENVINO_THROW("Failed to create plugin ",
- ov::util::path_to_string(desc.m_lib_location),
+ desc.m_lib_location,
" for device ",
device_name,
"\n",