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", () => {