@node-ctypes/python 0.3.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +58 -196
- package/dist/bootstrap.js +102 -21
- package/dist/buffers.js +93 -13
- package/dist/conversions.js +239 -68
- package/dist/executor.js +12 -5
- package/dist/native.d.ts +6 -12
- package/dist/native.js +7 -7
- package/dist/objects.d.ts +2 -1
- package/dist/objects.js +9 -37
- package/dist/platform.js +3 -0
- package/dist/runtime.d.ts +2 -2
- package/dist/runtime.js +465 -233
- package/dist/scheduler.js +452 -153
- package/package.json +6 -2
package/dist/conversions.js
CHANGED
|
@@ -1,71 +1,198 @@
|
|
|
1
|
+
var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
|
|
2
|
+
if (value !== null && value !== void 0) {
|
|
3
|
+
if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
|
|
4
|
+
var dispose, inner;
|
|
5
|
+
if (async) {
|
|
6
|
+
if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
|
|
7
|
+
dispose = value[Symbol.asyncDispose];
|
|
8
|
+
}
|
|
9
|
+
if (dispose === void 0) {
|
|
10
|
+
if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
|
|
11
|
+
dispose = value[Symbol.dispose];
|
|
12
|
+
if (async) inner = dispose;
|
|
13
|
+
}
|
|
14
|
+
if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
|
|
15
|
+
if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
|
|
16
|
+
env.stack.push({ value: value, dispose: dispose, async: async });
|
|
17
|
+
}
|
|
18
|
+
else if (async) {
|
|
19
|
+
env.stack.push({ async: true });
|
|
20
|
+
}
|
|
21
|
+
return value;
|
|
22
|
+
};
|
|
23
|
+
var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
|
|
24
|
+
return function (env) {
|
|
25
|
+
function fail(e) {
|
|
26
|
+
env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
|
|
27
|
+
env.hasError = true;
|
|
28
|
+
}
|
|
29
|
+
var r, s = 0;
|
|
30
|
+
function next() {
|
|
31
|
+
while (r = env.stack.pop()) {
|
|
32
|
+
try {
|
|
33
|
+
if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
|
|
34
|
+
if (r.dispose) {
|
|
35
|
+
var result = r.dispose.call(r.value);
|
|
36
|
+
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
|
|
37
|
+
}
|
|
38
|
+
else s |= 1;
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
41
|
+
fail(e);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
|
|
45
|
+
if (env.hasError) throw env.error;
|
|
46
|
+
}
|
|
47
|
+
return next();
|
|
48
|
+
};
|
|
49
|
+
})(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
50
|
+
var e = new Error(message);
|
|
51
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
52
|
+
});
|
|
1
53
|
// Copyright (c) 2026 Damiano Mazzella. MIT License (see LICENSE).
|
|
2
54
|
import { PythonError, kwargs } from "./values.js";
|
|
3
55
|
/** Standard-library operations invoked through the runtime's C API proxies. */
|
|
4
56
|
export class PythonConversions {
|
|
5
|
-
#
|
|
57
|
+
#isinstance;
|
|
58
|
+
#hasattr;
|
|
59
|
+
#getattr;
|
|
60
|
+
#setattr;
|
|
61
|
+
#delattr;
|
|
62
|
+
#len;
|
|
63
|
+
#iter;
|
|
64
|
+
#str;
|
|
65
|
+
#int;
|
|
66
|
+
#float;
|
|
67
|
+
#typeFn;
|
|
6
68
|
#mapping;
|
|
7
69
|
#mutableMapping;
|
|
8
|
-
#
|
|
9
|
-
#
|
|
10
|
-
#
|
|
11
|
-
#
|
|
12
|
-
#
|
|
70
|
+
#numberType;
|
|
71
|
+
#integralType;
|
|
72
|
+
#dtDatetime;
|
|
73
|
+
#dtDate;
|
|
74
|
+
#dtTime;
|
|
75
|
+
#dtTimedelta;
|
|
76
|
+
#dtTimezone;
|
|
77
|
+
#zoneInfo;
|
|
78
|
+
#contains;
|
|
79
|
+
#getitem;
|
|
80
|
+
#setitem;
|
|
81
|
+
#delitem;
|
|
82
|
+
#add;
|
|
83
|
+
#sub;
|
|
84
|
+
#mul;
|
|
85
|
+
#floordiv;
|
|
86
|
+
#truediv;
|
|
87
|
+
#formatException;
|
|
13
88
|
#epoch;
|
|
14
89
|
#microsecond;
|
|
15
90
|
constructor(builtins, importModule) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
91
|
+
const env_1 = { stack: [], error: void 0, hasError: false };
|
|
92
|
+
try {
|
|
93
|
+
this.#isinstance = builtins.isinstance;
|
|
94
|
+
this.#hasattr = builtins.hasattr;
|
|
95
|
+
this.#getattr = builtins.getattr;
|
|
96
|
+
this.#setattr = builtins.setattr;
|
|
97
|
+
this.#delattr = builtins.delattr;
|
|
98
|
+
this.#len = builtins.len;
|
|
99
|
+
this.#iter = builtins.iter;
|
|
100
|
+
this.#str = builtins.str;
|
|
101
|
+
this.#int = builtins.int;
|
|
102
|
+
this.#float = builtins.float;
|
|
103
|
+
this.#typeFn = builtins.type;
|
|
104
|
+
const abc = __addDisposableResource(env_1, importModule("collections.abc"), false);
|
|
105
|
+
this.#mapping = abc.Mapping;
|
|
106
|
+
this.#mutableMapping = abc.MutableMapping;
|
|
107
|
+
const numbers = __addDisposableResource(env_1, importModule("numbers"), false);
|
|
108
|
+
this.#numberType = numbers.Number;
|
|
109
|
+
this.#integralType = numbers.Integral;
|
|
110
|
+
const datetime = __addDisposableResource(env_1, importModule("datetime"), false);
|
|
111
|
+
this.#dtDatetime = datetime.datetime;
|
|
112
|
+
this.#dtDate = datetime.date;
|
|
113
|
+
this.#dtTime = datetime.time;
|
|
114
|
+
this.#dtTimedelta = datetime.timedelta;
|
|
115
|
+
this.#dtTimezone = datetime.timezone;
|
|
116
|
+
const zoneinfo = __addDisposableResource(env_1, importModule("zoneinfo"), false);
|
|
117
|
+
this.#zoneInfo = zoneinfo.ZoneInfo;
|
|
118
|
+
const operator = __addDisposableResource(env_1, importModule("operator"), false);
|
|
119
|
+
this.#contains = operator.contains;
|
|
120
|
+
this.#getitem = operator.getitem;
|
|
121
|
+
this.#setitem = operator.setitem;
|
|
122
|
+
this.#delitem = operator.delitem;
|
|
123
|
+
this.#add = operator.add;
|
|
124
|
+
this.#sub = operator.sub;
|
|
125
|
+
this.#mul = operator.mul;
|
|
126
|
+
this.#floordiv = operator.floordiv;
|
|
127
|
+
this.#truediv = operator.truediv;
|
|
128
|
+
const traceback = __addDisposableResource(env_1, importModule("traceback"), false);
|
|
129
|
+
this.#formatException = traceback.format_exception;
|
|
130
|
+
const utc = __addDisposableResource(env_1, this.#dtTimezone.utc, false);
|
|
131
|
+
this.#epoch = this.#dtDatetime(1970, 1, 1, kwargs({ tzinfo: utc }));
|
|
132
|
+
this.#microsecond = this.#dtTimedelta(kwargs({ microseconds: 1 }));
|
|
133
|
+
}
|
|
134
|
+
catch (e_1) {
|
|
135
|
+
env_1.error = e_1;
|
|
136
|
+
env_1.hasError = true;
|
|
137
|
+
}
|
|
138
|
+
finally {
|
|
139
|
+
__disposeResources(env_1);
|
|
140
|
+
}
|
|
27
141
|
}
|
|
28
142
|
getFallback(object, name) {
|
|
29
|
-
if (this.#
|
|
30
|
-
return this.#
|
|
31
|
-
if (name === "length"
|
|
32
|
-
|
|
33
|
-
|
|
143
|
+
if (this.#isinstance(object, this.#mapping) && (this.#contains(object, name) || name !== "length"))
|
|
144
|
+
return this.#getitem(object, name);
|
|
145
|
+
if (name === "length") {
|
|
146
|
+
const env_2 = { stack: [], error: void 0, hasError: false };
|
|
147
|
+
try {
|
|
148
|
+
const type = __addDisposableResource(env_2, this.#typeFn(object), false);
|
|
149
|
+
if (this.#hasattr(type, "__len__"))
|
|
150
|
+
return this.#len(object);
|
|
151
|
+
}
|
|
152
|
+
catch (e_2) {
|
|
153
|
+
env_2.error = e_2;
|
|
154
|
+
env_2.hasError = true;
|
|
155
|
+
}
|
|
156
|
+
finally {
|
|
157
|
+
__disposeResources(env_2);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return this.#getattr(object, name);
|
|
34
161
|
}
|
|
35
162
|
setFallback(object, name, value) {
|
|
36
|
-
if (this.#
|
|
37
|
-
this.#
|
|
163
|
+
if (this.#isinstance(object, this.#mutableMapping))
|
|
164
|
+
this.#setitem(object, name, value);
|
|
38
165
|
else
|
|
39
|
-
this.#
|
|
166
|
+
this.#setattr(object, name, value);
|
|
40
167
|
}
|
|
41
168
|
deleteFallback(object, name) {
|
|
42
|
-
if (this.#
|
|
43
|
-
this.#
|
|
169
|
+
if (this.#isinstance(object, this.#mutableMapping))
|
|
170
|
+
this.#delitem(object, name);
|
|
44
171
|
else
|
|
45
|
-
this.#
|
|
172
|
+
this.#delattr(object, name);
|
|
46
173
|
}
|
|
47
174
|
isKey(object, name) {
|
|
48
|
-
if (this.#
|
|
49
|
-
return this.#
|
|
50
|
-
const attributes = this.#
|
|
175
|
+
if (this.#isinstance(object, this.#mapping))
|
|
176
|
+
return this.#contains(object, name);
|
|
177
|
+
const attributes = this.#getattr(object, "__dict__", null);
|
|
51
178
|
if (attributes === null)
|
|
52
179
|
return false;
|
|
53
180
|
try {
|
|
54
|
-
return this.#
|
|
181
|
+
return this.#contains(attributes, name);
|
|
55
182
|
}
|
|
56
183
|
finally {
|
|
57
184
|
attributes[Symbol.dispose]();
|
|
58
185
|
}
|
|
59
186
|
}
|
|
60
187
|
item(object, key) {
|
|
61
|
-
return this.#
|
|
188
|
+
return this.#isinstance(object, this.#mapping) ? this.#getitem(object, key) : this.#getattr(object, key);
|
|
62
189
|
}
|
|
63
190
|
keys(object) {
|
|
64
|
-
const mapping = this.#
|
|
65
|
-
const attributes = mapping ? null : this.#
|
|
191
|
+
const mapping = this.#isinstance(object, this.#mapping);
|
|
192
|
+
const attributes = mapping ? null : this.#getattr(object, "__dict__", null);
|
|
66
193
|
if (!mapping && attributes === null)
|
|
67
194
|
return [];
|
|
68
|
-
const iterator = this.#
|
|
195
|
+
const iterator = this.#iter(mapping ? object : attributes);
|
|
69
196
|
try {
|
|
70
197
|
return [...iterator].filter((key) => typeof key === "string");
|
|
71
198
|
}
|
|
@@ -75,11 +202,11 @@ export class PythonConversions {
|
|
|
75
202
|
}
|
|
76
203
|
}
|
|
77
204
|
toPrimitive(object, hint) {
|
|
78
|
-
if (hint !== "string" && this.#
|
|
205
|
+
if (hint !== "string" && this.#isinstance(object, this.#dtDatetime))
|
|
79
206
|
return this.timestamp(object);
|
|
80
|
-
if (hint !== "string" && this.#
|
|
207
|
+
if (hint !== "string" && this.#isinstance(object, this.#numberType)) {
|
|
81
208
|
try {
|
|
82
|
-
return this.#
|
|
209
|
+
return this.#isinstance(object, this.#integralType) ? this.#int(object) : this.#float(object);
|
|
83
210
|
}
|
|
84
211
|
catch (error) {
|
|
85
212
|
if (!(error instanceof PythonError) || error.type !== "TypeError")
|
|
@@ -87,21 +214,44 @@ export class PythonConversions {
|
|
|
87
214
|
error.exception[Symbol.dispose]();
|
|
88
215
|
}
|
|
89
216
|
}
|
|
90
|
-
return this.#
|
|
217
|
+
return this.#str(object);
|
|
91
218
|
}
|
|
92
219
|
timestamp(object) {
|
|
93
|
-
if (!this.#
|
|
220
|
+
if (!this.#isinstance(object, this.#dtDatetime))
|
|
94
221
|
return null;
|
|
95
222
|
let local;
|
|
96
223
|
try {
|
|
97
|
-
|
|
98
|
-
local = object.astimezone();
|
|
99
|
-
const delta = this.#operator.sub(local ?? object, this.#epoch);
|
|
224
|
+
const env_3 = { stack: [], error: void 0, hasError: false };
|
|
100
225
|
try {
|
|
101
|
-
|
|
226
|
+
const tzinfo = __addDisposableResource(env_3, object.tzinfo, false);
|
|
227
|
+
if (tzinfo === null) {
|
|
228
|
+
const env_4 = { stack: [], error: void 0, hasError: false };
|
|
229
|
+
try {
|
|
230
|
+
const astimezone = __addDisposableResource(env_4, object.astimezone, false);
|
|
231
|
+
local = astimezone();
|
|
232
|
+
}
|
|
233
|
+
catch (e_3) {
|
|
234
|
+
env_4.error = e_3;
|
|
235
|
+
env_4.hasError = true;
|
|
236
|
+
}
|
|
237
|
+
finally {
|
|
238
|
+
__disposeResources(env_4);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
const delta = this.#sub(local ?? object, this.#epoch);
|
|
242
|
+
try {
|
|
243
|
+
return this.#truediv(this.#floordiv(delta, this.#microsecond), 1000);
|
|
244
|
+
}
|
|
245
|
+
finally {
|
|
246
|
+
delta[Symbol.dispose]();
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
catch (e_4) {
|
|
250
|
+
env_3.error = e_4;
|
|
251
|
+
env_3.hasError = true;
|
|
102
252
|
}
|
|
103
253
|
finally {
|
|
104
|
-
|
|
254
|
+
__disposeResources(env_3);
|
|
105
255
|
}
|
|
106
256
|
}
|
|
107
257
|
catch (error) {
|
|
@@ -115,29 +265,40 @@ export class PythonConversions {
|
|
|
115
265
|
}
|
|
116
266
|
}
|
|
117
267
|
fromTimestamp(ms) {
|
|
118
|
-
const delta = this.#
|
|
268
|
+
const delta = this.#dtTimedelta(kwargs({ milliseconds: ms }));
|
|
119
269
|
try {
|
|
120
|
-
return this.#
|
|
270
|
+
return this.#add(this.#epoch, delta);
|
|
121
271
|
}
|
|
122
272
|
finally {
|
|
123
273
|
delta[Symbol.dispose]();
|
|
124
274
|
}
|
|
125
275
|
}
|
|
126
276
|
toJSValue(object, temporal) {
|
|
127
|
-
|
|
128
|
-
const dt = this.#datetime;
|
|
129
|
-
if (b.isinstance(object, dt.datetime)) {
|
|
277
|
+
if (this.#isinstance(object, this.#dtDatetime)) {
|
|
130
278
|
const ms = this.timestamp(object);
|
|
131
279
|
return ms === null ? null : ["Date", ms];
|
|
132
280
|
}
|
|
133
281
|
if (!temporal)
|
|
134
282
|
return null;
|
|
135
|
-
if (
|
|
283
|
+
if (this.#isinstance(object, this.#dtDate))
|
|
136
284
|
return ["PlainDate", object.year, object.month, object.day];
|
|
137
|
-
if (
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
285
|
+
if (this.#isinstance(object, this.#dtTime)) {
|
|
286
|
+
const env_5 = { stack: [], error: void 0, hasError: false };
|
|
287
|
+
try {
|
|
288
|
+
const tzinfo = __addDisposableResource(env_5, object.tzinfo, false);
|
|
289
|
+
if (tzinfo === null)
|
|
290
|
+
return ["PlainTime", object.hour, object.minute, object.second, object.microsecond];
|
|
291
|
+
}
|
|
292
|
+
catch (e_5) {
|
|
293
|
+
env_5.error = e_5;
|
|
294
|
+
env_5.hasError = true;
|
|
295
|
+
}
|
|
296
|
+
finally {
|
|
297
|
+
__disposeResources(env_5);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
if (this.#isinstance(object, this.#dtTimedelta)) {
|
|
301
|
+
let total = BigInt(this.#floordiv(object, this.#microsecond));
|
|
141
302
|
const sign = total < 0n ? -1n : 1n;
|
|
142
303
|
if (total < 0n)
|
|
143
304
|
total = -total;
|
|
@@ -152,21 +313,20 @@ export class PythonConversions {
|
|
|
152
313
|
}
|
|
153
314
|
fromTemporal(fields) {
|
|
154
315
|
const [kind, ...values] = fields;
|
|
155
|
-
const dt = this.#datetime;
|
|
156
316
|
switch (kind) {
|
|
157
317
|
case "date":
|
|
158
|
-
return
|
|
318
|
+
return this.#dtDate(...values);
|
|
159
319
|
case "time":
|
|
160
|
-
return
|
|
320
|
+
return this.#dtTime(...values);
|
|
161
321
|
case "datetime":
|
|
162
|
-
return
|
|
322
|
+
return this.#dtDatetime(...values);
|
|
163
323
|
case "instant":
|
|
164
324
|
case "duration": {
|
|
165
|
-
const delta = this.#
|
|
325
|
+
const delta = this.#mul(values[0], this.#microsecond);
|
|
166
326
|
if (kind === "duration")
|
|
167
327
|
return delta;
|
|
168
328
|
try {
|
|
169
|
-
return this.#
|
|
329
|
+
return this.#add(this.#epoch, delta);
|
|
170
330
|
}
|
|
171
331
|
finally {
|
|
172
332
|
delta[Symbol.dispose]();
|
|
@@ -175,15 +335,15 @@ export class PythonConversions {
|
|
|
175
335
|
case "zoned": {
|
|
176
336
|
let timezone;
|
|
177
337
|
try {
|
|
178
|
-
timezone = this.#
|
|
338
|
+
timezone = this.#zoneInfo(values[1]);
|
|
179
339
|
}
|
|
180
340
|
catch (error) {
|
|
181
341
|
if (!(error instanceof PythonError) || !["ZoneInfoNotFoundError", "ValueError"].includes(error.type))
|
|
182
342
|
throw error;
|
|
183
343
|
error.exception[Symbol.dispose]();
|
|
184
|
-
const offset = this.#
|
|
344
|
+
const offset = this.#mul(values[2], this.#microsecond);
|
|
185
345
|
try {
|
|
186
|
-
timezone =
|
|
346
|
+
timezone = this.#dtTimezone(offset);
|
|
187
347
|
}
|
|
188
348
|
finally {
|
|
189
349
|
offset[Symbol.dispose]();
|
|
@@ -191,7 +351,18 @@ export class PythonConversions {
|
|
|
191
351
|
}
|
|
192
352
|
const instant = this.fromTemporal(["instant", values[0]]);
|
|
193
353
|
try {
|
|
194
|
-
|
|
354
|
+
const env_6 = { stack: [], error: void 0, hasError: false };
|
|
355
|
+
try {
|
|
356
|
+
const astimezone = __addDisposableResource(env_6, instant.astimezone, false);
|
|
357
|
+
return astimezone(timezone);
|
|
358
|
+
}
|
|
359
|
+
catch (e_6) {
|
|
360
|
+
env_6.error = e_6;
|
|
361
|
+
env_6.hasError = true;
|
|
362
|
+
}
|
|
363
|
+
finally {
|
|
364
|
+
__disposeResources(env_6);
|
|
365
|
+
}
|
|
195
366
|
}
|
|
196
367
|
finally {
|
|
197
368
|
instant[Symbol.dispose]();
|
|
@@ -203,10 +374,10 @@ export class PythonConversions {
|
|
|
203
374
|
}
|
|
204
375
|
}
|
|
205
376
|
formatException(exception) {
|
|
206
|
-
const type = this.#
|
|
377
|
+
const type = this.#typeFn(exception);
|
|
207
378
|
const traceback = exception.__traceback__;
|
|
208
379
|
try {
|
|
209
|
-
const lines = this.#
|
|
380
|
+
const lines = this.#formatException(type, exception, traceback);
|
|
210
381
|
try {
|
|
211
382
|
return [...lines].join("");
|
|
212
383
|
}
|
package/dist/executor.js
CHANGED
|
@@ -166,7 +166,7 @@ export class PythonExecutor {
|
|
|
166
166
|
api.Py_IncRef(helper);
|
|
167
167
|
this.#helpers.set(name, helper);
|
|
168
168
|
}
|
|
169
|
-
entry =
|
|
169
|
+
entry = check(api.PyLong_AsVoidPtr(check(api.PyDict_GetItemString(this.#namespace, "_entry_address"))));
|
|
170
170
|
}
|
|
171
171
|
catch (error) {
|
|
172
172
|
this.#releasePython();
|
|
@@ -220,11 +220,18 @@ export class PythonExecutor {
|
|
|
220
220
|
};
|
|
221
221
|
const settled = task.then((value) => {
|
|
222
222
|
try {
|
|
223
|
-
if (
|
|
224
|
-
|
|
225
|
-
|
|
223
|
+
if (value !== null) {
|
|
224
|
+
if (cancelled)
|
|
225
|
+
this.#tokenHelper("_discard", token);
|
|
226
226
|
return { value };
|
|
227
|
-
|
|
227
|
+
}
|
|
228
|
+
return {
|
|
229
|
+
error: this.#withGIL(() => this.#withToken(token, (id) => {
|
|
230
|
+
if (cancelled)
|
|
231
|
+
this.#api.Py_DecRef(this.#call("_discard", [id]));
|
|
232
|
+
return this.#call("_take_failure", [id]);
|
|
233
|
+
})),
|
|
234
|
+
};
|
|
228
235
|
}
|
|
229
236
|
finally {
|
|
230
237
|
done();
|
package/dist/native.d.ts
CHANGED
|
@@ -9,16 +9,16 @@ export declare const METH_KEYWORDS = 2;
|
|
|
9
9
|
export declare const METH_NOARGS = 4;
|
|
10
10
|
export declare const PyCFunctionWithKeywords: import("node-ctypes").FuncPtrFactory<import("node-ctypes").SimpleCDataConstructor<"pointer">, readonly [import("node-ctypes").SimpleCDataConstructor<"pointer">, import("node-ctypes").SimpleCDataConstructor<"pointer">, import("node-ctypes").SimpleCDataConstructor<"pointer">]>;
|
|
11
11
|
export declare class PyMethodDef extends Structure {
|
|
12
|
-
static _fields_: ((string | import("node-ctypes").SimpleCDataConstructor<"string">)[] | (string | import("node-ctypes").
|
|
12
|
+
static _fields_: ((string | import("node-ctypes").SimpleCDataConstructor<"string">)[] | (string | import("node-ctypes").SimpleCDataConstructor<"pointer">)[] | (string | import("node-ctypes").SimpleCDataConstructor<"int32">)[])[];
|
|
13
13
|
}
|
|
14
14
|
export declare class PyCFunctionMethodDef extends Structure {
|
|
15
|
-
static _fields_: ((string | import("node-ctypes").SimpleCDataConstructor<"string">)[] | (string | import("node-ctypes").SimpleCDataConstructor<"
|
|
15
|
+
static _fields_: ((string | import("node-ctypes").SimpleCDataConstructor<"string">)[] | (string | import("node-ctypes").SimpleCDataConstructor<"pointer">)[] | (string | import("node-ctypes").SimpleCDataConstructor<"int32">)[])[];
|
|
16
16
|
}
|
|
17
17
|
export declare class PyTypeSlot extends Structure {
|
|
18
|
-
static _fields_: ((string | import("node-ctypes").SimpleCDataConstructor<"
|
|
18
|
+
static _fields_: ((string | import("node-ctypes").SimpleCDataConstructor<"pointer">)[] | (string | import("node-ctypes").SimpleCDataConstructor<"int32">)[])[];
|
|
19
19
|
}
|
|
20
20
|
export declare class PyTypeSpec extends Structure {
|
|
21
|
-
static _fields_: ((string | import("node-ctypes").SimpleCDataConstructor<"string">)[] | (string | import("node-ctypes").SimpleCDataConstructor<"
|
|
21
|
+
static _fields_: ((string | import("node-ctypes").SimpleCDataConstructor<"string">)[] | (string | import("node-ctypes").SimpleCDataConstructor<"pointer">)[] | (string | import("node-ctypes").SimpleCDataConstructor<"int32">)[] | (string | import("node-ctypes").SimpleCDataConstructor<"uint32">)[])[];
|
|
22
22
|
}
|
|
23
23
|
export declare const API: {
|
|
24
24
|
readonly Py_GetVersion: readonly [import("node-ctypes").SimpleCDataConstructor<"string">, readonly []];
|
|
@@ -38,7 +38,6 @@ export declare const API: {
|
|
|
38
38
|
readonly PyObject_GetAttrString: readonly [import("node-ctypes").SimpleCDataConstructor<"pointer">, readonly [import("node-ctypes").SimpleCDataConstructor<"pointer">, import("node-ctypes").SimpleCDataConstructor<"string">]];
|
|
39
39
|
readonly PyObject_GenericGetAttr: readonly [import("node-ctypes").SimpleCDataConstructor<"pointer">, readonly [import("node-ctypes").SimpleCDataConstructor<"pointer">, import("node-ctypes").SimpleCDataConstructor<"pointer">]];
|
|
40
40
|
readonly PyObject_GenericSetAttr: readonly [import("node-ctypes").SimpleCDataConstructor<"int32">, readonly [import("node-ctypes").SimpleCDataConstructor<"pointer">, import("node-ctypes").SimpleCDataConstructor<"pointer">, import("node-ctypes").SimpleCDataConstructor<"pointer">]];
|
|
41
|
-
readonly PyObject_GetTypeData: readonly [import("node-ctypes").SimpleCDataConstructor<"pointer">, readonly [import("node-ctypes").SimpleCDataConstructor<"pointer">, import("node-ctypes").SimpleCDataConstructor<"pointer">]];
|
|
42
41
|
readonly PyObject_SetAttrString: readonly [import("node-ctypes").SimpleCDataConstructor<"int32">, readonly [import("node-ctypes").SimpleCDataConstructor<"pointer">, import("node-ctypes").SimpleCDataConstructor<"string">, import("node-ctypes").SimpleCDataConstructor<"pointer">]];
|
|
43
42
|
readonly PyObject_HasAttrString: readonly [import("node-ctypes").SimpleCDataConstructor<"int32">, readonly [import("node-ctypes").SimpleCDataConstructor<"pointer">, import("node-ctypes").SimpleCDataConstructor<"string">]];
|
|
44
43
|
readonly PyObject_GetItem: readonly [import("node-ctypes").SimpleCDataConstructor<"pointer">, readonly [import("node-ctypes").SimpleCDataConstructor<"pointer">, import("node-ctypes").SimpleCDataConstructor<"pointer">]];
|
|
@@ -58,13 +57,10 @@ export declare const API: {
|
|
|
58
57
|
readonly PyErr_SetRaisedException: readonly [null, readonly [import("node-ctypes").SimpleCDataConstructor<"pointer">]];
|
|
59
58
|
readonly PyErr_SetString: readonly [null, readonly [import("node-ctypes").SimpleCDataConstructor<"pointer">, import("node-ctypes").SimpleCDataConstructor<"string">]];
|
|
60
59
|
readonly PyType_GetName: readonly [import("node-ctypes").SimpleCDataConstructor<"pointer">, readonly [import("node-ctypes").SimpleCDataConstructor<"pointer">]];
|
|
61
|
-
readonly PyType_FromSpec: readonly [import("node-ctypes").SimpleCDataConstructor<"pointer">, readonly [import("node-ctypes").SimpleCDataConstructor<"pointer">]];
|
|
62
60
|
readonly PyType_FromSpecWithBases: readonly [import("node-ctypes").SimpleCDataConstructor<"pointer">, readonly [import("node-ctypes").SimpleCDataConstructor<"pointer">, import("node-ctypes").SimpleCDataConstructor<"pointer">]];
|
|
63
61
|
readonly PyType_GenericAlloc: readonly [import("node-ctypes").SimpleCDataConstructor<"pointer">, readonly [import("node-ctypes").SimpleCDataConstructor<"pointer">, import("node-ctypes").SimpleCDataConstructor<"ssize_t">]];
|
|
64
|
-
readonly PyType_GetSlot: readonly [import("node-ctypes").SimpleCDataConstructor<"pointer">, readonly [import("node-ctypes").SimpleCDataConstructor<"pointer">, import("node-ctypes").SimpleCDataConstructor<"int32">]];
|
|
65
62
|
readonly PyErr_Clear: readonly [null, readonly []];
|
|
66
63
|
readonly PyErr_ExceptionMatches: readonly [import("node-ctypes").SimpleCDataConstructor<"int32">, readonly [import("node-ctypes").SimpleCDataConstructor<"pointer">]];
|
|
67
|
-
readonly PyThreadState_SetAsyncExc: readonly [import("node-ctypes").SimpleCDataConstructor<"int32">, readonly [import("node-ctypes").SimpleCDataConstructor<"ulong">, import("node-ctypes").SimpleCDataConstructor<"pointer">]];
|
|
68
64
|
readonly Py_IncRef: readonly [null, readonly [import("node-ctypes").SimpleCDataConstructor<"pointer">]];
|
|
69
65
|
readonly Py_DecRef: readonly [null, readonly [import("node-ctypes").SimpleCDataConstructor<"pointer">]];
|
|
70
66
|
readonly PyBool_FromLong: readonly [import("node-ctypes").SimpleCDataConstructor<"pointer">, readonly [import("node-ctypes").SimpleCDataConstructor<"long">]];
|
|
@@ -73,6 +69,7 @@ export declare const API: {
|
|
|
73
69
|
readonly PyLong_FromString: readonly [import("node-ctypes").SimpleCDataConstructor<"pointer">, readonly [import("node-ctypes").SimpleCDataConstructor<"string">, import("node-ctypes").SimpleCDataConstructor<"pointer">, import("node-ctypes").SimpleCDataConstructor<"int32">]];
|
|
74
70
|
readonly PyLong_AsLongLong: readonly [import("node-ctypes").SimpleCDataConstructor<"int64">, readonly [import("node-ctypes").SimpleCDataConstructor<"pointer">]];
|
|
75
71
|
readonly PyLong_AsLongLongAndOverflow: readonly [import("node-ctypes").SimpleCDataConstructor<"int64">, readonly [import("node-ctypes").SimpleCDataConstructor<"pointer">, import("node-ctypes").SimpleCDataConstructor<"pointer">]];
|
|
72
|
+
readonly PyLong_AsVoidPtr: readonly [import("node-ctypes").SimpleCDataConstructor<"pointer">, readonly [import("node-ctypes").SimpleCDataConstructor<"pointer">]];
|
|
76
73
|
readonly PyNumber_AsSsize_t: readonly [import("node-ctypes").SimpleCDataConstructor<"ssize_t">, readonly [import("node-ctypes").SimpleCDataConstructor<"pointer">, import("node-ctypes").SimpleCDataConstructor<"pointer">]];
|
|
77
74
|
readonly PyFloat_FromDouble: readonly [import("node-ctypes").SimpleCDataConstructor<"pointer">, readonly [import("node-ctypes").SimpleCDataConstructor<"double">]];
|
|
78
75
|
readonly PyFloat_AsDouble: readonly [import("node-ctypes").SimpleCDataConstructor<"double">, readonly [import("node-ctypes").SimpleCDataConstructor<"pointer">]];
|
|
@@ -124,7 +121,6 @@ export declare class PythonLibrary extends CDLL implements NativeAPI {
|
|
|
124
121
|
PyObject_GetAttrString: NativeAPI["PyObject_GetAttrString"];
|
|
125
122
|
PyObject_GenericGetAttr: NativeAPI["PyObject_GenericGetAttr"];
|
|
126
123
|
PyObject_GenericSetAttr: NativeAPI["PyObject_GenericSetAttr"];
|
|
127
|
-
PyObject_GetTypeData: NativeAPI["PyObject_GetTypeData"];
|
|
128
124
|
PyObject_SetAttrString: NativeAPI["PyObject_SetAttrString"];
|
|
129
125
|
PyObject_HasAttrString: NativeAPI["PyObject_HasAttrString"];
|
|
130
126
|
PyObject_GetItem: NativeAPI["PyObject_GetItem"];
|
|
@@ -144,13 +140,10 @@ export declare class PythonLibrary extends CDLL implements NativeAPI {
|
|
|
144
140
|
PyErr_SetRaisedException: NativeAPI["PyErr_SetRaisedException"];
|
|
145
141
|
PyErr_SetString: NativeAPI["PyErr_SetString"];
|
|
146
142
|
PyType_GetName: NativeAPI["PyType_GetName"];
|
|
147
|
-
PyType_FromSpec: NativeAPI["PyType_FromSpec"];
|
|
148
143
|
PyType_FromSpecWithBases: NativeAPI["PyType_FromSpecWithBases"];
|
|
149
144
|
PyType_GenericAlloc: NativeAPI["PyType_GenericAlloc"];
|
|
150
|
-
PyType_GetSlot: NativeAPI["PyType_GetSlot"];
|
|
151
145
|
PyErr_Clear: NativeAPI["PyErr_Clear"];
|
|
152
146
|
PyErr_ExceptionMatches: NativeAPI["PyErr_ExceptionMatches"];
|
|
153
|
-
PyThreadState_SetAsyncExc: NativeAPI["PyThreadState_SetAsyncExc"];
|
|
154
147
|
Py_IncRef: NativeAPI["Py_IncRef"];
|
|
155
148
|
Py_DecRef: NativeAPI["Py_DecRef"];
|
|
156
149
|
PyBool_FromLong: NativeAPI["PyBool_FromLong"];
|
|
@@ -159,6 +152,7 @@ export declare class PythonLibrary extends CDLL implements NativeAPI {
|
|
|
159
152
|
PyLong_FromString: NativeAPI["PyLong_FromString"];
|
|
160
153
|
PyLong_AsLongLong: NativeAPI["PyLong_AsLongLong"];
|
|
161
154
|
PyLong_AsLongLongAndOverflow: NativeAPI["PyLong_AsLongLongAndOverflow"];
|
|
155
|
+
PyLong_AsVoidPtr: NativeAPI["PyLong_AsVoidPtr"];
|
|
162
156
|
PyNumber_AsSsize_t: NativeAPI["PyNumber_AsSsize_t"];
|
|
163
157
|
PyFloat_FromDouble: NativeAPI["PyFloat_FromDouble"];
|
|
164
158
|
PyFloat_AsDouble: NativeAPI["PyFloat_AsDouble"];
|
package/dist/native.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// Copyright (c) 2026 Damiano Mazzella. MIT License (see LICENSE).
|
|
2
|
-
import { CDLL, RTLD_GLOBAL, Structure, CFUNCTYPE, c_char_p, c_void_p, c_int, c_uint, c_long,
|
|
2
|
+
import { CDLL, RTLD_GLOBAL, Structure, CFUNCTYPE, c_char_p, c_void_p, c_int, c_uint, c_long, c_int64, c_double, c_ssize_t } from "node-ctypes";
|
|
3
3
|
export const MIN_PYTHON = [3, 12];
|
|
4
4
|
export const Py_file_input = 257;
|
|
5
5
|
export const Py_eval_input = 258;
|
|
@@ -9,17 +9,20 @@ export const METH_NOARGS = 0x0004;
|
|
|
9
9
|
// PyObject *f(PyObject *self, PyObject *args, PyObject *kwargs)
|
|
10
10
|
export const PyCFunctionWithKeywords = CFUNCTYPE(c_void_p, c_void_p, c_void_p, c_void_p);
|
|
11
11
|
export class PyMethodDef extends Structure {
|
|
12
|
+
// A PyCFunctionWithKeywords address (a callback's .pointer): as in CPython,
|
|
13
|
+
// a CFUNCTYPE field takes only function pointer instances, c_void_p an address.
|
|
12
14
|
static _fields_ = [
|
|
13
15
|
["ml_name", c_char_p],
|
|
14
|
-
["ml_meth",
|
|
16
|
+
["ml_meth", c_void_p],
|
|
15
17
|
["ml_flags", c_int],
|
|
16
18
|
["ml_doc", c_char_p],
|
|
17
19
|
];
|
|
18
20
|
}
|
|
19
21
|
export class PyCFunctionMethodDef extends Structure {
|
|
22
|
+
// A PyCFunction (self, args) address; see PyMethodDef.
|
|
20
23
|
static _fields_ = [
|
|
21
24
|
["ml_name", c_char_p],
|
|
22
|
-
["ml_meth",
|
|
25
|
+
["ml_meth", c_void_p],
|
|
23
26
|
["ml_flags", c_int],
|
|
24
27
|
["ml_doc", c_char_p],
|
|
25
28
|
];
|
|
@@ -58,7 +61,6 @@ export const API = {
|
|
|
58
61
|
PyObject_GetAttrString: [c_void_p, [c_void_p, c_char_p]],
|
|
59
62
|
PyObject_GenericGetAttr: [c_void_p, [c_void_p, c_void_p]],
|
|
60
63
|
PyObject_GenericSetAttr: [c_int, [c_void_p, c_void_p, c_void_p]],
|
|
61
|
-
PyObject_GetTypeData: [c_void_p, [c_void_p, c_void_p]],
|
|
62
64
|
PyObject_SetAttrString: [c_int, [c_void_p, c_char_p, c_void_p]],
|
|
63
65
|
PyObject_HasAttrString: [c_int, [c_void_p, c_char_p]],
|
|
64
66
|
PyObject_GetItem: [c_void_p, [c_void_p, c_void_p]],
|
|
@@ -78,13 +80,10 @@ export const API = {
|
|
|
78
80
|
PyErr_SetRaisedException: [null, [c_void_p]],
|
|
79
81
|
PyErr_SetString: [null, [c_void_p, c_char_p]],
|
|
80
82
|
PyType_GetName: [c_void_p, [c_void_p]],
|
|
81
|
-
PyType_FromSpec: [c_void_p, [c_void_p]],
|
|
82
83
|
PyType_FromSpecWithBases: [c_void_p, [c_void_p, c_void_p]],
|
|
83
84
|
PyType_GenericAlloc: [c_void_p, [c_void_p, c_ssize_t]],
|
|
84
|
-
PyType_GetSlot: [c_void_p, [c_void_p, c_int]],
|
|
85
85
|
PyErr_Clear: [null, []],
|
|
86
86
|
PyErr_ExceptionMatches: [c_int, [c_void_p]],
|
|
87
|
-
PyThreadState_SetAsyncExc: [c_int, [c_ulong, c_void_p]],
|
|
88
87
|
Py_IncRef: [null, [c_void_p]],
|
|
89
88
|
Py_DecRef: [null, [c_void_p]],
|
|
90
89
|
PyBool_FromLong: [c_void_p, [c_long]],
|
|
@@ -93,6 +92,7 @@ export const API = {
|
|
|
93
92
|
PyLong_FromString: [c_void_p, [c_char_p, c_void_p, c_int]],
|
|
94
93
|
PyLong_AsLongLong: [c_int64, [c_void_p]],
|
|
95
94
|
PyLong_AsLongLongAndOverflow: [c_int64, [c_void_p, c_void_p]],
|
|
95
|
+
PyLong_AsVoidPtr: [c_void_p, [c_void_p]],
|
|
96
96
|
PyNumber_AsSsize_t: [c_ssize_t, [c_void_p, c_void_p]],
|
|
97
97
|
PyFloat_FromDouble: [c_void_p, [c_double]],
|
|
98
98
|
PyFloat_AsDouble: [c_double, [c_void_p]],
|
package/dist/objects.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { c_int, c_ssize_t, c_void_p } from "node-ctypes";
|
|
2
2
|
import type { PythonLibrary } from "./native.ts";
|
|
3
3
|
type Pointer = bigint | null;
|
|
4
|
+
type Result = bigint | number | null;
|
|
4
5
|
export interface NativeSlot {
|
|
5
6
|
readonly pointer: bigint;
|
|
6
7
|
release(): void;
|
|
@@ -8,7 +9,7 @@ export interface NativeSlot {
|
|
|
8
9
|
setNativeError(error: bigint): void;
|
|
9
10
|
drain(): number;
|
|
10
11
|
}
|
|
11
|
-
export declare function nativeSlot(api: PythonLibrary, handler: (...args: unknown[]) =>
|
|
12
|
+
export declare function nativeSlot(api: PythonLibrary, handler: (...args: unknown[]) => Result | void, restype: typeof c_void_p | typeof c_ssize_t | typeof c_int | null, argtypes: (typeof c_void_p | typeof c_int)[], notify?: bigint): NativeSlot;
|
|
12
13
|
interface ObjectRuntime {
|
|
13
14
|
withGIL<T>(fn: () => T): T;
|
|
14
15
|
check(value: Pointer): bigint;
|