8000 fix: ffi breaking changes (#31) · denosaurs/deno_python@c3ae19b · GitHub
[go: up one dir, main page]

Skip to content

Commit c3ae19b

Browse files
authored
fix: ffi breaking changes (#31)
* fix: ffi breaking changes * fix: pyobject type assertion
1 parent 293a73c commit c3ae19b

File tree

6 files changed

+61
-42
lines changed

6 files changed

+61
-42
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
MIT License
22

33
Copyright (c) 2021 DjDeveloperr
4-
Copyright (c) 2022 the denosaurs team
4+
Copyright (c) 2023 the denosaurs team
55

66
Permission is hereby granted, free of charge, to any person obtaining a copy
77
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# deno_python
22

33
[![Tags](https://img.shields.io/github/release/denosaurs/deno_python)](https://github.com/denosaurs/deno_python/releases)
4-
[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/python@0.2.3/mod.ts)
4+
[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/python/mod.ts)
55
[![checks](https://github.com/denosaurs/deno_python/actions/workflows/checks.yml/badge.svg)](https://github.com/denosaurs/deno_python/actions/workflows/checks.yml)
66
[![License](https://img.shields.io/github/license/denosaurs/deno_python)](https://github.com/denosaurs/deno_python/blob/master/LICENSE)
77

@@ -12,7 +12,7 @@ Python interpreter bindings for Deno.
1212
Import any locally installed Python package, for example, `matplotlib`:
1313

1414
```ts
15-
import { python } from "https://deno.land/x/python@0.2.3/mod.ts";
15+
import { python } from "https://deno.land/x/python/mod.ts";
1616

1717
const np = python.import("numpy");
1818
const plt = python.import("matplotlib.pyplot");
@@ -35,7 +35,7 @@ deno run -A --unstable <file>
3535
## Documentation
3636

3737
Check out the docs
38-
[here](https://doc.deno.land/https://deno.land/x/python@0.2.3/mod.ts).
38+
[here](https://doc.deno.land/https://deno.land/x/python/mod.ts).
3939

4040
## Python Installation
4141

@@ -70,4 +70,4 @@ Pull request, issues and feedback are very welcome. Code style is formatted with
7070

7171
Copyright 2021, DjDeveloperr.
7272

73-
Copyright 2022, the Denosaurs team. All rights reserved. MIT license.
73+
Copyright 2023, the Denosaurs team. All rights reserved. MIT license.

deno.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
{
22
"tasks": {
3-
"test": "deno test --unstable -A test/test.ts"
3+
"test": "deno test --unstable -A test/test.ts",
4+
"example_hello_python": "deno run -A --unstable examples/hello_python.ts",
5+
"example_matplotlib": "deno run -A --unstable examples/matplotlib.ts",
6+
"example_run_code": "deno run -A --unstable examples/run_code.ts",
7+
"example_tensorflow": "deno run -A --unstable examples/tensorflow.ts"
48
}
59
}

src/python.ts

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,11 @@ export class Callback {
149149
kwargs: Deno.PointerValue,
150150
) => {
151151
return PyObject.from(callback(
152-
kwargs === 0 ? {} : Object.fromEntries(
152+
kwargs === null ? {} : Object.fromEntries(
153153
new PyObject(kwargs).asDict()
154154
.entries(),
155155
),
156-
...(args === 0 ? [] : new PyObject(args).valueOf()),
156+
...(args === null ? [] : new PyObject(args).valueOf()),
157157
)).handle;
158158
},
159159
);
@@ -193,8 +193,7 @@ export class PyObject {
193193
* Check if the object is NULL (pointer) or None type in Python.
194194
*/
195195
get isNone() {
196-
return this.handle === 0 ||
197-
this.handle === 0n ||
196+
return this.handle === null ||
198197
this.handle === python.None[ProxiedPyObject].handle;
199198
}
200199

@@ -290,7 +289,7 @@ export class PyObject {
290289
this.handle,
291290
parseInt(name),
292291
);
293-
if (item !== 0) {
292+
if (item !== null) {
294293
return new PyObject(item).proxy;
295294
}
296295
}
@@ -302,7 +301,7 @@ export class PyObject {
302301
this.handle,
303302
slice.handle,
304303
);
305-
if (item !== 0) {
304+
if (item !== null) {
306305
return new PyObject(item).proxy;
307306
}
308307
}
@@ -319,7 +318,7 @@ export class PyObject {
319318
this.handle,
320319
cstr(name),
321320
);
322-
if (value !== 0) {
321+
if (value !== null) {
323322
return new PyObject(value).proxy;
324323
}
325324
}
@@ -443,20 +442,24 @@ export class PyObject {
443442
);
444443
view.setBigUint64(
445444
0,
446-
BigInt(Deno.UnsafePointer.of(nameBuf)),
445+
BigInt(Deno.UnsafePointer.value(Deno.UnsafePointer.of(nameBuf)!)),
446+
LE,
447+
);
448+
view.setBigUint64(
449+
8,
450+
BigInt(Deno.UnsafePointer.value(v.unsafe.pointer)),
447451
LE,
448452
);
449-
view.setBigUint64(8, BigInt(v.unsafe.pointer), LE);
450453
view.setInt32(16, 0x1 | 0x2, LE);
451454
view.setBigUint64(
452455
20,
453-
BigInt(Deno.UnsafePointer.of(nameBuf)),
456+
BigInt(Deno.UnsafePointer.value(Deno.UnsafePointer.of(nameBuf)!)),
454457
LE,
455458
);
456459
const fn = py.PyCFunction_NewEx(
457460
struct,
458461
PyObject.from(null).handle,
459-
0n,
462+
null,
460463
);
461464
return new PyObject(fn);
462465
} else if (v instanceof PyObject) {
@@ -526,7 +529,7 @@ export class PyObject {
526529
const obj = new PyObject(
527530
py.PyObject_GetAttrString(this.handle, cstr(name)),
528531
);
529-
if (obj.handle === 0) {
532+
if (obj.handle === null) {
530533
py.PyErr_Clear();
531534
return undefined;
532535
} else {
@@ -592,11 +595,7 @@ export class PyObject {
592595
*/
593596
asString() {
594597
const str = py.PyUnicode_AsUTF8(this.handle);
595-
if (str === 0) {
596-
return null;
597-
} else {
598-
return Deno.UnsafePointerView.getCString(str as bigint);
599-
}
598+
return str !== null ? Deno.UnsafePointerView.getCString(str) : null;
600599
}
601600

602601
/**
@@ -635,7 +634,7 @@ export class PyObject {
635634
*[Symbol.iterator]() {
636635
const iter = py.PyObject_GetIter(this.handle);
637636
let item = py.PyIter_Next(iter);
638-
while (item !== 0) {
637+
while (item !== null) {
639638
yield new PyObject(item);
640639
item = py.PyIter_Next(iter);
641640
}
@@ -677,23 +676,39 @@ export class PyObject {
677676
valueOf() {
678677
const type = py.PyObject_Type(this.handle);
679678

680-
if (type === python.None[ProxiedPyObject].handle) {
679+
if (Deno.UnsafePointer.equals(type, python.None[ProxiedPyObject].handle)) {
681680
return null;
682-
} else if (type === python.bool[ProxiedPyObject].handle) {
681+
} else if (
682+
Deno.UnsafePointer.equals(type, python.bool[ProxiedPyObject].handle)
683+
) {
683684
return this.asBoolean();
684-
} else if (type === python.int[ProxiedPyObject].handle) {
685+
} else if (
686+
Deno.UnsafePointer.equals(type, python.int[ProxiedPyObject].handle)
687+
) {
685688
return this.asLong();
686-
} else if (type === python.float[ProxiedPyObject].handle) {
689+
} else if (
690+
Deno.UnsafePointer.equals(type, python.float[ProxiedPyObject].handle)
691+
) {
687692
return this.asDouble();
688-
} else if (type === python.str[ProxiedPyObject].handle) {
693+
} else if (
694+
Deno.UnsafePointer.equals(type, python.str[ProxiedPyObject].handle)
695+
) {
689696
return this.asString();
690-
} else if (type === python.list[ProxiedPyObject].handle) {
697+
} else if (
698+
Deno.UnsafePointer.equals(type, python.list[ProxiedPyObject].handle)
699+
) {
691700
return this.asArray();
692-
} else if (type === python.dict[ProxiedPyObject].handle) {
701+
} else if (
702+
Deno.UnsafePointer.equals(type, python.dict[ProxiedPyObject].handle)
703+
) {
693704
return this.asDict();
694-
} else if (type === python.set[ProxiedPyObject].handle) {
705+
} else if (
706+
Deno.UnsafePointer.equals(type, python.set[ProxiedPyObject].handle)
707+
) {
695708
return this.asSet();
696-
} else if (type === python.tuple[ProxiedPyObject].handle) {
709+
} else if (
710+
Deno.UnsafePointer.equals(type, python.tuple[ProxiedPyObject].handle)
711+
) {
697712
return this.asTuple();
698713
} else {
699714
return this.proxy;
@@ -777,7 +792,7 @@ export class PythonError extends Error {
777792
*/
778793
export function maybeThrowError() {
779794
const error = py.PyErr_Occurred();
780-
if (error === 0) {
795+
if (error === null) {
781796
return;
782797
}
783798

@@ -788,9 +803,9 @@ export function maybeThrowError() {
788803
pointers.subarray(2, 3),
789804
);
790805

791-
const type = new PyObject(pointers[0]),
792-
value = new PyObject(pointers[1]),
793-
traceback = new PyObject(pointers[2]);
806+
const type = new PyObject(Deno.UnsafePointer.create(pointers[0])),
807+
value = new PyObject(Deno.UnsafePointer.create(pointers[1])),
808+
traceback = new PyObject(Deno.UnsafePointer.create(pointers[2]));
794809

795810
let errorMessage = (value ?? type).toString() ?? "Unknown error";
796811
if (!traceback.isNone) {
@@ -880,7 +895,7 @@ export class Python {
880895
)
881896
.handle,
882897
);
883-
if (module === 0) {
898+
if (module === null) {
884899
throw new PythonError("Failed to run module");
885900
}
886901
return new PyObject(module)?.proxy;
@@ -891,7 +906,7 @@ export class Python {
891906
*/
892907
importObject(name: string) {
893908
const mod = py.PyImport_ImportModule(cstr(name));
894-
if (mod === 0) {
909+
if (mod === null) {
895910
maybeThrowError();
896911
throw new PythonError(`Failed to import module ${name}`);
897912
}
@@ -949,7 +964,7 @@ function toSlice(sliceList: string): PyObject {
949964
const pyTuple_Pack = new Deno.UnsafeFnPointer(
950965
// TODO: this isn't really a `bigint`, but Deno's type definitions
951966
// haven't been updated to support `number` yet
952-
py.PyTuple_Pack as bigint,
967+
py.PyTuple_Pack!,
953968
{
954969
parameters: ["i32", ...pySlicesHandle.map(() => "pointer" as const)],
955970
result: "pointer",

src/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function postSetup(lib: string) {
2222
gnu_get_libc_version: { parameters: [], result: "pointer" },
2323
});
2424
const ptrView = new Deno.UnsafePointerView(
25-
BigInt(libc.symbols.gnu_get_libc_version()),
25+
libc.symbols.gnu_get_libc_version()!,
2626
);
2727
const glibcVersion = parseFloat(ptrView.getCString());
2828

test/deps.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from "https://deno.land/std@0.139.0/testing/asserts.ts";
1+
export * from "https://deno.land/std@0.178.0/testing/asserts.ts";

0 commit comments

Comments
 (0)
0