8000 Fix Python -> .Net unicode string conversion · pythonnet/pythonnet@1b6c6c0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1b6c6c0

Browse files
committed
Fix Python -> .Net unicode string conversion
1 parent da051a9 commit 1b6c6c0

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/runtime/runtime.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,11 +1646,12 @@ internal static string GetManagedString(IntPtr op)
16461646
if (type == PyUnicodeType)
16471647
{
16481648
using var p = PyUnicode_AsUTF16String(new BorrowedReference(op));
1649-
int length = (int)PyUnicode_GetSize(op);
1650-
char* codePoints = (char*)PyBytes_AsString(p.DangerousGetAddress());
1649+
var bytesPtr = p.DangerousGetAddress();
1650+
int bytesLength = (int)Runtime.PyBytes_Size(bytesPtr);
1651+
char* codePoints = (char*)PyBytes_AsString(bytesPtr);
16511652
return new string(codePoints,
16521653
startIndex: 1, // skip BOM
1653-
length: length);
1654+
length: bytesLength/2-1); // utf16 - BOM
16541655
}
16551656

16561657
return null;

0 commit comments

Comments
 (0)
0