From fd4658bbf41ac6bfe12728a1ec0e35559d48b09a Mon Sep 17 00:00:00 2001 From: Dj <43033058+DjDeveloperr@users.noreply.github.com> Date: Fri, 21 Jul 2023 12:45:16 +0530 Subject: [PATCH] fix: pass byteLength of string when creating Python String (#40) --- src/python.ts | 6 ++++-- test/test.ts | 3 +++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/python.ts b/src/python.ts index 4f75089..77c67f2 100644 --- a/src/python.ts +++ b/src/python.ts @@ -495,10 +495,12 @@ export class PyObject { case "symbol": case "string": { const str = String(v); + const encoder = new TextEncoder(); + const u8 = encoder.encode(str); return new PyObject( py.PyUnicode_DecodeUTF8( - cstr(str), - str.length, + u8, + u8.byteLength, null, ), ); diff --git a/test/test.ts b/test/test.ts index 99ce34a..af063a5 100644 --- a/test/test.ts +++ b/test/test.ts @@ -35,6 +35,9 @@ Deno.test("types", async (t) => { await t.step("str", () => { const value = python.str("hello"); assertEquals(value.valueOf(), "hello"); + + const unicode = python.str("'中文'"); + assertEquals(unicode.valueOf(), "'中文'"); }); await t.step("list", () => {