Skip to content

Commit 0368be9

Browse files
committed
Increasing code coverage
1 parent 5a4f6b1 commit 0368be9

File tree

2 files changed

+240
-199
lines changed

2 files changed

+240
-199
lines changed

mssql_python/pybind/ddbc_bindings.h

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#pragma once
88

99
// pybind11.h must be the first include
10+
#include <clocale>
11+
#include <cwchar>
1012
#include <memory>
1113
#include <pybind11/chrono.h>
1214
#include <pybind11/complex.h>
@@ -458,8 +460,28 @@ inline std::wstring Utf8ToWString(const std::string& str) {
458460
return {};
459461
return result;
460462
#else
461-
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
462-
return converter.from_bytes(str);
463+
// Use mbstowcs as a replacement for deprecated wstring_convert
464+
// Set locale to UTF-8 for proper conversion
465+
const char* old_locale = setlocale(LC_CTYPE, nullptr);
466+
setlocale(LC_CTYPE, "en_US.UTF-8");
467+
468+
size_t size_needed = mbstowcs(nullptr, str.c_str(), 0);
469+
if (size_needed == static_cast<size_t>(-1)) {
470+
LOG_ERROR("mbstowcs failed for UTF8 to wide string conversion");
471+
setlocale(LC_CTYPE, old_locale);
472+
return {};
473+
}
474+
475+
std::wstring result(size_needed, 0);
476+
size_t converted = mbstowcs(&result[0], str.c_str(), size_needed);
477+
if (converted == static_cast<size_t>(-1)) {
478+
LOG_ERROR("mbstowcs failed for UTF8 to wide string conversion");
479+
setlocale(LC_CTYPE, old_locale);
480+
return {};
481+
}
482+
483+
setlocale(LC_CTYPE, old_locale);
484+
return result;
463485
#endif
464486
}
465487

0 commit comments

Comments
 (0)