in language-extensions/python/src/PythonParam.cpp [301:376]
void PythonStringParam<CharType>::RetrieveValueAndStrLenInd(bp::object mainNamespace)
{
bp::dict dictNamespace = bp::extract<bp::dict>(mainNamespace);
if (dictNamespace.has_key(m_name))
{
bp::object tempObj = mainNamespace[m_name];
m_strLenOrInd = SQL_NULL_DATA;
if (!tempObj.is_none())
{
if constexpr (is_same_v<CharType,char>)
{
// Check to make sure the extracted data exists and is of the correct type
//
bp::extract<string> extractedObj(tempObj);
if (extractedObj.check())
{
// Extract and copy the string characters into the vector.
//
string value = extractedObj;
if (!value.empty())
{
m_value = vector<CharType>(value.begin(), value.end());
}
else
{
m_value.push_back('\0');
m_strLenOrInd = 0;
}
}
}
else
{
// Get length of the unicode object in pyObj
//
int size = PyUnicode_GET_LENGTH(tempObj.ptr());
if (size > 0)
{
// Get a byte representation of the string as UTF16.
// PyUnicode_AsUTF16String adds a BOM to the front of every string.
//
char *utf16str = PyBytes_AsString(PyUnicode_AsUTF16String(tempObj.ptr()));
// Reinterpret the bytes as wchar_t *, which we will return.
//
CharType *wData = reinterpret_cast<CharType *>(utf16str);
// Ignore 2 byte BOM at front of wData that was added by PyUnicode_AsUTF16String
//
m_value = vector<CharType>(wData + 1, wData + 1 + size);
}
else
{
m_value.push_back(L'\0');
m_strLenOrInd = 0;
}
}
// Truncate the return data to only be the size specified when creating
//
if (m_value.size() > m_size)
{
m_value.resize(m_size);
m_value.shrink_to_fit();
}
if (m_strLenOrInd == SQL_NULL_DATA)
{
m_strLenOrInd = m_value.size() * sizeof(CharType);
}
}
}
}