forked from denosaurs/deno_python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathffi.ts
81 lines (69 loc) · 2.29 KB
/
ffi.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { SYMBOLS } from "./symbols.ts";
import { postSetup } from "./util.ts";
const searchPath: string[] = [];
const SUPPORTED_VERSIONS = [[3, 13], [3, 12], [3, 11], [3, 10], [3, 9], [3, 8]];
const DENO_PYTHON_PATH = Deno.env.get("DENO_PYTHON_PATH");
if (DENO_PYTHON_PATH) {
searchPath.push(DENO_PYTHON_PATH);
} else {
if (
Deno.build.os === "windows" || Deno.build.os === "linux" ||
// @ts-ignore: users reported that `windows_nt` exists at runtime
Deno.build.os === "windows_nt"
) {
searchPath.push(
...SUPPORTED_VERSIONS.map(([major, minor]) =>
`${Deno.build.os === "linux" ? "lib" : ""}python${major}${
Deno.build.os === "linux" ? "." : ""
}${minor}.${Deno.build.os === "linux" ? "so" : "dll"}`
),
);
} else if (Deno.build.os === "darwin") {
for (
const framework of [
"/Library/Frameworks/Python.framework/Versions",
"/opt/homebrew/Frameworks/Python.framework/Versions",
"/usr/local/Frameworks/Python.framework/Versions",
]
) {
for (const [major, minor] of SUPPORTED_VERSIONS) {
searchPath.push(`${framework}/${major}.${minor}/Python`);
}
}
} else {
throw new Error(`Unsupported OS: ${Deno.build.os}`);
}
}
let py!: Deno.DynamicLibrary<typeof SYMBOLS>["symbols"];
for (const path of searchPath) {
try {
py = Deno.dlopen(path, SYMBOLS).symbols;
postSetup(path);
break;
} catch (err) {
if (err instanceof TypeError && !("Bun" in globalThis)) {
throw new Error(
"Cannot load dynamic library because --unstable-ffi flag was not set",
{ cause: err },
);
}
continue;
}
}
const LIBRARY_NOT_FOUND = new Error(`
Could not find Python library!
Tried searching for these versions:
${searchPath.map((e) => " " + e).join("\n")}
Make sure you have a supported version of Python
installed on your system, which should be one of
these: ${SUPPORTED_VERSIONS.map((e) => `${e[0]}.${e[1]}`).join(", ")}
If the module still somehow fails to find it,
you can open an issue: https://github.com/denosaurs/deno_python/issues
However, if your Python distribution is not in search
path, you can set DENO_PYTHON_PATH env variable pointing
to dll/dylib/so file for Python library.
`);
if (typeof py !== "object") {
throw LIBRARY_NOT_FOUND;
}
export { py };