Skip to content

Commit 88f708a

Browse files
committed
Fixed support for UTF8.
Fixed missing support for based_attribute. Modified the description of SetStringPointer.
1 parent 3a5124d commit 88f708a

File tree

4 files changed

+21
-10
lines changed

4 files changed

+21
-10
lines changed

Diff for: addons/source-python/packages/source-python/entities/classes.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,8 @@ def _get_server_class(self, class_name, datamap):
359359
attribute = method(
360360
Key.as_attribute_type(self, data['type']),
361361
offset,
362-
data.get('doc')
362+
data.get('doc'),
363+
Key.as_int(self, data.get('length', 0)),
363364
)
364365

365366
# Assign the attribute to the instance

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

+3-3
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))
@@ -533,7 +533,7 @@ def fset(ptr, value):
533533
if not instance_ptr:
534534
# Allocate space for the value
535535
if type_name == Type.STRING_ARRAY:
536-
size = length if length else len(value) + 1
536+
size = length if length else len(value.encode()) + 1
537537
else:
538538
size = TYPE_SIZES[type_name.upper()]
539539

@@ -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 */)

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ void export_pointer(scope _memory)
229229

230230
.def("set_string_pointer",
231231
&CPointer::SetStringPointer,
232-
"Sets the value at the given memory location. This string object must be deallocated by the user. Returns the string object.",
232+
"Sets the value at the given memory location. Returns the string object. This string object must be deallocated by the user.",
233233
("value", arg( "offset")=0),
234234
manage_new_object_policy()
235235
)

0 commit comments

Comments
 (0)