Skip to content

Commit 60729e9

Browse files
committed
Fixed support for UTF8.
1 parent 14a1487 commit 60729e9

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

Diff for: addons/source-python/packages/source-python/memory/manager.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ def fset(ptr, value):
479479

480480
# Handle string array type
481481
elif type_name == Type.STRING_ARRAY:
482-
if length and len(value) >= length:
482+
if length and len(value.encode()) >= length:
483483
raise ValueError(
484484
'The string length exceeds'
485485
'the limit "{0}".'.format(length-1))
@@ -558,7 +558,7 @@ def fset(ptr, value):
558558

559559
# Handle string array type
560560
elif type_name == Type.STRING_ARRAY:
561-
if length and len(value) >= length:
561+
if length and len(value.encode()) >= length:
562562
raise ValueError(
563563
'The string length exceeds'
564564
'the limit "{0}".'.format(length-1))

Diff for: src/core/modules/memory/memory_pointer.cpp

+15-5
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,24 @@ const char * CPointer::GetStringPointer(int iOffset /* = 0 */)
105105
CPointer * CPointer::SetStringPointer(str oString, int iOffset /* = 0 */)
106106
{
107107
Validate();
108-
unsigned long length = len(oString) + 1;
109-
CPointer * pPtr = new CPointer((unsigned long) UTIL_Alloc(length), false);
110-
char * value = (char *) pPtr->m_ulAddr;
111-
memcpy(value, extract<const char *>(oString), length);
108+
109+
// Encode Unicode object and extract Python bytes object.
110+
PyObject * pObj = PyUnicode_AsUTF8String(oString.ptr());
111+
if (!pObj)
112+
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Invalid UTF-8 string.");
113+
114+
// Get string and length of bytes.
115+
const char * szString = PyBytes_AS_STRING(pObj);
116+
unsigned long length = PyBytes_GET_SIZE(pObj) + 1;
117+
118+
char * value = (char *) UTIL_Alloc(length);
119+
memcpy(value, szString, length);
120+
112121
TRY_SEGV()
113122
*(const char **) (m_ulAddr + iOffset) = value;
114123
EXCEPT_SEGV()
115-
return pPtr;
124+
125+
return new CPointer((unsigned long) value, false);
116126
}
117127

118128
const char * CPointer::GetStringArray(int iOffset /* = 0 */)

0 commit comments

Comments
 (0)