File tree Expand file tree Collapse file tree 2 files changed +17
-7
lines changed
addons/source-python/packages/source-python/memory Expand file tree Collapse file tree 2 files changed +17
-7
lines changed Original file line number Diff line number Diff 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 ))
Original file line number Diff line number Diff line change @@ -105,14 +105,24 @@ const char * CPointer::GetStringPointer(int iOffset /* = 0 */)
105105CPointer * 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
118128const char * CPointer::GetStringArray (int iOffset /* = 0 */ )
You can’t perform that action at this time.
0 commit comments