Skip to content

Commit

Permalink
Make PyodideMetadataReader::getWorkerFiles not fail when it actuall…
Browse files Browse the repository at this point in the history
…y filters (#3413)

ArrayBuilder assumes the array we are constructing has the exact size given.
For constructing variable size arrays, we need a `kj::Vector`
  • Loading branch information
hoodmane authored Feb 10, 2025
1 parent f847730 commit 065d723
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/workerd/api/pyodide/pyodide.c++
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ kj::Array<jsg::JsRef<jsg::JsString>> PyodideMetadataReader::getNames(jsg::Lock&

kj::Array<jsg::JsRef<jsg::JsString>> PyodideMetadataReader::getWorkerFiles(
jsg::Lock& js, kj::String ext) {
auto builder = kj::heapArrayBuilder<jsg::JsRef<jsg::JsString>>(this->names.size());
for (auto i: kj::zeroTo(builder.capacity())) {
auto builder = kj::Vector<jsg::JsRef<jsg::JsString>>(this->names.size());
for (auto i: kj::zeroTo(this->names.size())) {
if (this->names[i].endsWith(ext)) {
builder.add(js, js.str(this->contents[i]));
}
}
return builder.finish();
return builder.releaseAsArray();
}

kj::Array<jsg::JsRef<jsg::JsString>> PyodideMetadataReader::getRequirements(jsg::Lock& js) {
Expand Down
2 changes: 2 additions & 0 deletions src/workerd/server/tests/python/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,5 @@ py_wd_test(
)

py_wd_test("dont-snapshot-pyodide")

py_wd_test("filter-non-py-files")
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Workerd = import "/workerd/workerd.capnp";

# This is a really slow way to test that PyodideMetadataReader::getWorkerFiles works.
# TODO: replace with a unit test?
const unitTests :Workerd.Config = (
services = [
( name = "dont-snapshot-pyodide",
worker = (
modules = [
(name = "worker.py", pythonModule = embed "worker.py"),
# a file with no `.py` extension to get filtered out
(name = "fake_shared_library.so", data = "This isn't really a shared library..."),
# We need a package dependency to trigger the package snapshot logic which we're trying to
# test.
(name = "numpy", pythonRequirement = "")
],
compatibilityDate = "2024-01-15",
compatibilityFlags = [%PYTHON_FEATURE_FLAGS],
)
),
],
);
2 changes: 2 additions & 0 deletions src/workerd/server/tests/python/filter-non-py-files/worker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def test():
pass

0 comments on commit 065d723

Please sign in to comment.