Skip to content

Commit

Permalink
get pyodide version from compat flag annotations, set pyodide-load-ex…
Browse files Browse the repository at this point in the history
…ternal autogate on all python tests
  • Loading branch information
garrettgu10 committed Aug 7, 2024
1 parent c312b50 commit 928869a
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 4 deletions.
60 changes: 60 additions & 0 deletions src/workerd/io/compatibility-date.c++
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,64 @@ kj::Maybe<kj::String> normalizeCompatDate(kj::StringPtr date) {
return CompatDate::parse(date).map([](auto v) { return v.toString(); });
}

struct PythonSnapshotParsedField {
PythonSnapshotRelease::Reader pythonSnapshotRelease;
capnp::StructSchema::Field field;
};

kj::Array<const PythonSnapshotParsedField> makePythonSnapshotFieldTable(
capnp::StructSchema::FieldList fields) {
kj::Vector<PythonSnapshotParsedField> table(fields.size());

for (auto field: fields) {
kj::Maybe<PythonSnapshotRelease::Reader> maybePythonSnapshotRelease;

for (auto annotation: field.getProto().getAnnotations()) {
if (annotation.getId() == PYTHON_SNAPSHOT_RELEASE_ANNOTATION_ID) {
maybePythonSnapshotRelease =
annotation.getValue().getStruct().getAs<workerd::PythonSnapshotRelease>();
}
}

KJ_IF_SOME(pythonSnapshotRelease, maybePythonSnapshotRelease) {
table.add(PythonSnapshotParsedField{
.pythonSnapshotRelease = pythonSnapshotRelease,
.field = field,
});
}
}

return table.releaseAsArray();
}

kj::Maybe<PythonSnapshotRelease::Reader> getPythonSnapshotRelease(
CompatibilityFlags::Reader featureFlags) {
uint latestFieldOrdinal = 0;
kj::Maybe<PythonSnapshotRelease::Reader> result;

static auto fieldTable =
makePythonSnapshotFieldTable(capnp::Schema::from<CompatibilityFlags>().getFields());

for (auto field: fieldTable) {
bool isEnabled = capnp::toDynamic(featureFlags).get(field.field).as<bool>();
if (!isEnabled) {
continue;
}

// We pick the flag with the highest ordinal value that is enabled and has a
// pythonSnapshotRelease annotation.
//
// The fieldTable is probably ordered by the ordinal anyway, but doesn't hurt to be explicit
// here.
//
// TODO(later): make sure this is well tested once we have more than one compat flag.
if (latestFieldOrdinal < field.field.getIndex()) {
latestFieldOrdinal = field.field.getIndex();
result = field.pythonSnapshotRelease;
}
}

return result;
}

} // namespace workerd
3 changes: 3 additions & 0 deletions src/workerd/io/compatibility-date.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ kj::Maybe<kj::String> normalizeCompatDate(kj::StringPtr date);
// Returns the current date as a string formatted by CompatDate.
kj::String currentDateStr();

kj::Maybe<PythonSnapshotRelease::Reader> getPythonSnapshotRelease(
CompatibilityFlags::Reader featureFlags);

// These values come from src/workerd/io/compatibility-date.capnp
static constexpr uint64_t COMPAT_ENABLE_FLAG_ANNOTATION_ID = 0xb6dabbc87cd1b03eull;
static constexpr uint64_t COMPAT_DISABLE_FLAG_ANNOTATION_ID = 0xd145cf1adc42577cull;
Expand Down
4 changes: 4 additions & 0 deletions src/workerd/server/tests/python/env-param/env.wd-test
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ const unitTests :Workerd.Config = (
)
),
],

autogates = [
"workerd-autogate-pyodide-load-external",
]
);
4 changes: 4 additions & 0 deletions src/workerd/server/tests/python/hello/hello.wd-test
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ const unitTests :Workerd.Config = (
)
),
],

autogates = [
"workerd-autogate-pyodide-load-external",
]
);
4 changes: 4 additions & 0 deletions src/workerd/server/tests/python/import_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ const unitTests :Workerd.Config = (
)
),
],
autogates = [
"workerd-autogate-pyodide-load-external",
]
);"""

def generate_wd_test_file(requirement):
Expand Down
4 changes: 4 additions & 0 deletions src/workerd/server/tests/python/langchain/langchain.wd-test
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ const unitTests :Workerd.Config = (
)
),
],

autogates = [
"workerd-autogate-pyodide-load-external",
]
);
4 changes: 4 additions & 0 deletions src/workerd/server/tests/python/random/random.wd-test
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ const unitTests :Workerd.Config = (
)
),
],

autogates = [
"workerd-autogate-pyodide-load-external",
]
);
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@ const unitTests :Workerd.Config = (
)
),
],

autogates = [
"workerd-autogate-pyodide-load-external",
]
);

9 changes: 5 additions & 4 deletions src/workerd/server/workerd-api.c++
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ bool fetchPyodideBundle(const api::pyodide::PythonConfig& pyConfig, kj::StringPt

kj::HttpHeaders headers(table);

kj::String url = kj::str("https://pyodide.runtime-playground.workers.dev/python-runtime-capnp-bin/pyodide-", version, ".capnp.bin");
kj::String url = kj::str("https://pyodide.runtime-playground.workers.dev/pyodide-capnp-bin/pyodide-", version, ".capnp.bin");

auto req = client->request(kj::HttpMethod::GET, kj::StringPtr(url), headers);

Expand Down Expand Up @@ -538,9 +538,10 @@ void WorkerdApi::compileModules(
if (fetchPyodideBundle(impl->pythonConfig, "dev"_kj)) {
modules->addBuiltinBundle(getPyodideBundle("dev"_kj), kj::none);
} else {
// TODO: hardcoded version number
KJ_REQUIRE(fetchPyodideBundle(impl->pythonConfig, "2"_kj), "Failed to get both dev and hardcoded Pyodide version");
modules->addBuiltinBundle(getPyodideBundle("2"_kj), kj::none);
auto version = KJ_ASSERT_NONNULL(getPythonSnapshotRelease(featureFlags)).getPyodide();

KJ_REQUIRE(fetchPyodideBundle(impl->pythonConfig, version), "Failed to get Pyodide bundle");
modules->addBuiltinBundle(getPyodideBundle(version), kj::none);
}
}
// Inject pyodide bootstrap module (TODO: load this from the capnproto bundle?)
Expand Down

0 comments on commit 928869a

Please sign in to comment.