File tree 2 files changed +17
-7
lines changed
addons/source-python/packages/source-python/memory
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):
479
479
480
480
# Handle string array type
481
481
elif type_name == Type .STRING_ARRAY :
482
- if length and len (value ) >= length :
482
+ if length and len (value . encode () ) >= length :
483
483
raise ValueError (
484
484
'The string length exceeds'
485
485
'the limit "{0}".' .format (length - 1 ))
@@ -558,7 +558,7 @@ def fset(ptr, value):
558
558
559
559
# Handle string array type
560
560
elif type_name == Type .STRING_ARRAY :
561
- if length and len (value ) >= length :
561
+ if length and len (value . encode () ) >= length :
562
562
raise ValueError (
563
563
'The string length exceeds'
564
564
'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 */)
105
105
CPointer * CPointer::SetStringPointer (str oString, int iOffset /* = 0 */ )
106
106
{
107
107
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
+
112
121
TRY_SEGV ()
113
122
*(const char **) (m_ulAddr + iOffset) = value;
114
123
EXCEPT_SEGV ()
115
- return pPtr;
124
+
125
+ return new CPointer ((unsigned long ) value, false );
116
126
}
117
127
118
128
const char * CPointer::GetStringArray (int iOffset /* = 0 */ )
You can’t perform that action at this time.
0 commit comments