Skip to content

Commit

Permalink
fix: pass byteLength of string when creating Python String (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
DjDeveloperr authored Jul 21, 2023
1 parent 9a66b87 commit fd4658b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
);
Expand Down
3 changes: 3 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down

0 comments on commit fd4658b

Please sign in to comment.