Fix #382: Proxies should return the same object (#394)

This commit is contained in:
Michael Droettboom 2019-04-22 14:10:09 -04:00 committed by GitHub
parent 67ae415f09
commit dfbe045895
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 1 deletions

View File

@ -121,16 +121,34 @@ _pyproxy_destroy(int ptrobj)
{
PyObject* pyobj = (PyObject*)ptrobj;
Py_DECREF(ptrobj);
EM_ASM(delete Module.PyProxies[ptrobj];);
}
EM_JS(int, pyproxy_new, (int ptrobj), {
// Proxies we've already created are just returned again, so that the
// same object on the Python side is always the same object on the
// Javascript side.
// Technically, this leaks memory, since we're holding on to a reference
// to the proxy forever. But we have that problem anyway since we don't
// have a destructor in Javascript to free the Python object.
// _pyproxy_destroy, which is a way for users to manually delete the proxy,
// also deletes the proxy from this set.
if (Module.PyProxies.hasOwnProperty(ptrobj)) {
return Module.hiwire_new_value(Module.PyProxies[ptrobj]);
}
var target = function(){};
target['$$'] = { ptr : ptrobj, type : 'PyProxy' };
return Module.hiwire_new_value(new Proxy(target, Module.PyProxy));
var proxy = new Proxy(target, Module.PyProxy);
Module.PyProxies[ptrobj] = proxy;
return Module.hiwire_new_value(proxy);
});
EM_JS(int, pyproxy_init, (), {
// clang-format off
Module.PyProxies = {};
Module.PyProxy = {
getPtr: function(jsobj) {
var ptr = jsobj['$$']['ptr'];

View File

@ -317,6 +317,14 @@ def test_pyimport_multiple(selenium):
selenium.run_js("pyodide.pyimport('v')")
def test_pyimport_same(selenium):
"""See #382"""
selenium.run("def func(): return 42")
assert selenium.run_js(
"return pyodide.pyimport('func') == pyodide.pyimport('func')"
)
def test_pyproxy(selenium):
selenium.run(
"""