Skip to content

Commit fd4658b

Browse files
authored
fix: pass byteLength of string when creating Python String (#40)
1 parent 9a66b87 commit fd4658b

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

src/python.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -495,10 +495,12 @@ export class PyObject {
495495
case "symbol":
496496
case "string": {
497497
const str = String(v);
498+
const encoder = new TextEncoder();
499+
const u8 = encoder.encode(str);
498500
return new PyObject(
499501
py.PyUnicode_DecodeUTF8(
500-
cstr(str),
501-
str.length,
502+
u8,
503+
u8.byteLength,
502504
null,
503505
),
504506
);

test/test.ts

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ Deno.test("types", async (t) => {
3535
await t.step("str", () => {
3636
const value = python.str("hello");
3737
assertEquals(value.valueOf(), "hello");
38+
39+
const unicode = python.str("'中文'");
40+
assertEquals(unicode.valueOf(), "'中文'");
3841
});
3942

4043
await t.step("list", () => {

0 commit comments

Comments
 (0)