2
2
import { py } from "./ffi.ts" ;
3
3
import { cstr } from "./util.ts" ;
4
4
5
+ /**
6
+ * Symbol used on proxied Python objects to point to the original PyObject object.
7
+ * Can be used to implement PythonProxy and create your own proxies.
8
+ *
9
+ * See `PyObject#proxy` for more info on proxies.
10
+ */
11
+ export const ProxiedPyObject = Symbol ( "ProxiedPyObject" ) ;
12
+
13
+ /**
14
+ * Proxied Python object.
15
+ *
16
+ * When an object implements this interface, the object will not be converted
17
+ * to a Python Object and the original PyObject will be used.
18
+ */
19
+ export interface PythonProxy {
20
+ [ ProxiedPyObject ] : PyObject ;
21
+ }
22
+
5
23
/**
6
24
* JS types that can be converted to Python Objects.
7
25
*
@@ -30,6 +48,8 @@ import { cstr } from "./util.ts";
30
48
* - `Set` becomes `set` in Python.
31
49
*
32
50
* If you pass a PyObject, it is used as-is.
51
+ *
52
+ * If you pass a PythonProxy, its original PyObject will be used.
33
53
*/
34
54
export type PythonConvertible =
35
55
| number
@@ -41,18 +61,12 @@ export type PythonConvertible =
41
61
| string
42
62
// deno-lint-ignore ban-types
43
63
| Symbol
64
+ | PythonProxy
44
65
| PythonConvertible [ ]
45
66
| { [ key : string ] : PythonConvertible }
46
67
| Map < PythonConvertible , PythonConvertible >
47
68
| Set < PythonConvertible > ;
48
69
49
- /**
50
- * Symbol used on proxied Python objects to point to the original PyObject object.
51
- *
52
- * See `PyObject#proxy` for more info on proxies.
53
- */
54
- export const ProxiedPyObject = Symbol ( "ProxiedPyObject" ) ;
55
-
56
70
/**
57
71
* An argument that can be passed to PyObject calls to indicate that the
58
72
* argument should be passed as a named one.
@@ -310,6 +324,9 @@ export class PyObject {
310
324
case "object" : {
311
325
if ( v === null ) {
312
326
return python . builtins . None [ ProxiedPyObject ] ;
327
+ } else if ( ProxiedPyObject in v ) {
328
+ const proxy = v as PythonProxy ;
329
+ return proxy [ ProxiedPyObject ] ;
313
330
} else if ( Array . isArray ( v ) ) {
314
331
const list = py . PyList_New ( v . length ) as Deno . UnsafePointer ;
315
332
for ( let i = 0 ; i < v . length ; i ++ ) {
0 commit comments