Fix #291: Allow loading of arbitrary packages

This commit is contained in:
Michael Droettboom 2019-01-10 14:42:45 -05:00
parent b66c160dc8
commit 4142ac0be0
2 changed files with 45 additions and 2 deletions

View File

@ -120,7 +120,6 @@ var languagePluginLoader = new Promise((resolve, reject) => {
});
} else {
console.error(`Unknown package '${package}'`);
return;
}
}
}
@ -176,7 +175,19 @@ var languagePluginLoader = new Promise((resolve, reject) => {
} else {
script.src = `${package_uri}`;
}
script.onerror = (e) => { reject(e); };
script.onerror = (e) => {
// If the package_uri fails to load, call monitorRunDependencies twice
// (so packageCounter will still hit 0 and finish loading), and remove
// the package from toLoad so we don't mark it as loaded.
console.log(`Couldn't load package from URL ${script.src}`)
let index = toLoad.indexOf(package);
if (index !== -1) {
toLoad.splice(index, 1);
}
for (let i = 0; i < 2; i++) {
window.pyodide._module.monitorRunDependencies();
}
};
document.body.appendChild(script);
}

View File

@ -1,5 +1,8 @@
import pytest
from pathlib import Path
import shutil
@pytest.mark.parametrize('active_server', ['main', 'secondary'])
def test_load_from_url(selenium_standalone, web_server_secondary,
@ -114,4 +117,33 @@ def test_load_handle_failure(selenium_standalone):
assert 'Loading pytz' in selenium.logs
assert 'Loading pytz2' in selenium.logs
assert "Unknown package 'pytz2'" in selenium.logs
assert "Couldn't load package from URL" in selenium.logs
assert 'Loading pyparsing' in selenium.logs # <- this fails
def test_load_package_unknown(selenium_standalone):
url = selenium_standalone.server_hostname
port = selenium_standalone.server_port
build_dir = Path(__file__).parent.parent / 'build'
shutil.copyfile(
build_dir / 'pyparsing.js',
build_dir / 'pyparsing-custom.js'
)
shutil.copyfile(
build_dir / 'pyparsing.data',
build_dir / 'pyparsing-custom.data'
)
try:
selenium_standalone.load_package(
f'http://{url}:{port}/pyparsing-custom.js'
)
finally:
(build_dir / 'pyparsing-custom.js').unlink()
(build_dir / 'pyparsing-custom.data').unlink()
assert selenium_standalone.run_js(
"return window.pyodide.loadedPackages."
"hasOwnProperty('pyparsing-custom')"
)