Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix Python -> .Net unicode string conversion
  • Loading branch information
pkese committed Jun 8, 2021
commit 1b6c6c059b5adc6b09d3673a1d4034c40902b777
7 changes: 4 additions & 3 deletions src/runtime/runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1646,11 +1646,12 @@ internal static string GetManagedString(IntPtr op)
if (type == PyUnicodeType)
{
using var p = PyUnicode_AsUTF16String(new BorrowedReference(op));
int length = (int)PyUnicode_GetSize(op);
char* codePoints = (char*)PyBytes_AsString(p.DangerousGetAddress());
var bytesPtr = p.DangerousGetAddress();
int bytesLength = (int)Runtime.PyBytes_Size(bytesPtr);
char* codePoints = (char*)PyBytes_AsString(bytesPtr);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: better add overload for PyBytes_AsString that takes BorrowedReference. This would remove unnecessary call to DangerousGetAddress above.

return new string(codePoints,
startIndex: 1, // skip BOM
length: length);
length: bytesLength/2-1); // utf16 - BOM
}

return null;
Expand Down