Skip to content

Commit b1bcc4a

Browse files
committed
feat(JSObjectProxy): get JSObjectProxy dict() conversion working
1 parent 53fcefb commit b1bcc4a

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

python/pythonmonkey/__init__.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,18 @@
88

99
# Load the module by default to make `console` globally available
1010
require("console")
11+
12+
# Add the `.keys()` method on `Object.prototype` to get JSObjectProxy dict() conversion working
13+
# Conversion from a dict-subclass to a strict dict by `dict(subclass)` internally calls the .keys() method to read the dictionary keys,
14+
# but .keys on a JSObjectProxy can only come from the JS side
15+
pm.eval("""
16+
(makeList) => {
17+
const keysMethod = {
18+
get() {
19+
return () => makeList(...Object.keys(this))
20+
}
21+
}
22+
Object.defineProperty(Object.prototype, "keys", keysMethod)
23+
Object.defineProperty(Array.prototype, "keys", keysMethod)
24+
}
25+
""")(lambda *args: list(args))

0 commit comments

Comments
 (0)