From 4142ac0be0d508d512be5059495f850af091d375 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Thu, 10 Jan 2019 14:42:45 -0500 Subject: [PATCH] Fix #291: Allow loading of arbitrary packages --- src/pyodide.js | 15 +++++++++++++-- test/test_package_loading.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/pyodide.js b/src/pyodide.js index 1057295b6..46e38d468 100644 --- a/src/pyodide.js +++ b/src/pyodide.js @@ -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); } diff --git a/test/test_package_loading.py b/test/test_package_loading.py index a12e6e283..86088f8ad 100644 --- a/test/test_package_loading.py +++ b/test/test_package_loading.py @@ -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')" + )