Skip to content

Instantly share code, notes, and snippets.

@elenzil
Created October 9, 2018 18:10
Show Gist options
  • Save elenzil/d3ca288078574c0c16550c84b3dd53b8 to your computer and use it in GitHub Desktop.
Save elenzil/d3ca288078574c0c16550c84b3dd53b8 to your computer and use it in GitHub Desktop.
brief demonstration of what seems like a bug in brython's handling of javascript dictionary objects.
<html>
<head>
<script type="text/javascript" src="/src/brython.js"></script>
<script type="text/python">
from browser import window
import json
def call_me():
s = json.dumps(window.my_dict_1.to_dict())
print("dict 1 serialized by python: %s" % (s))
# the following line fails.
s = json.dumps(window.my_dict_2.to_dict())
print("dict 2 serialized by python: %s" % (s))
window.call_me = call_me
</script>
<script type="text/javascript">
function onLoad() {
brython(1);
window.my_dict_1 = {"s":"x"};
window.my_dict_2 = {"foo":{"s":"x"}};
console.log("dict 1 serialized by javascript: " + JSON.stringify(window.my_dict_1));
console.log("dict 2 serialized by javascript: " + JSON.stringify(window.my_dict_2));
window.call_me();
}
</script>
</head>
<body onload="onLoad()">
<pre>
this is an example of some squirrelyness with converting dictionary objects from javascript to python (and back).
it seems like there's a bug with the keys of sub-dictionaries.
to try the script out, host it somewhere with /src/brython.js available,
for example as a child of <a href='https://github.com/brython-dev/brython/tree/master/www/tests'>the 'tests' folder</a>.
</pre>
</body>
</html>
@elenzil
Copy link
Author

elenzil commented Oct 10, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment