Skip to content

Commit

Permalink
Enable python external package loading by default.
Browse files Browse the repository at this point in the history
  • Loading branch information
dom96 committed Jan 16, 2025
1 parent e8b4f58 commit 5507666
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 66 deletions.
36 changes: 0 additions & 36 deletions src/pyodide/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,48 +20,12 @@ capnp_embed(
deps = ["pyodide_packages_archive"],
)

copy_file(
name = "python_entrypoint_file",
src = "python-entrypoint.js",
out = "generated/python-entrypoint.js",
)

capnp_embed(
name = "python_entrypoint_file_embed",
src = "generated/python-entrypoint.js",
deps = ["python_entrypoint_file"],
)

copy_file(
name = "pyodide_extra_capnp_file",
src = "pyodide_extra.capnp",
out = "generated/pyodide_extra.capnp",
)

capnp_embed(
name = "pyodide_extra_file_embed",
src = "generated/pyodide_extra.capnp",
deps = ["pyodide_extra_capnp_file"],
)

capnp_embed(
name = "pyodide_lock_file_embed",
src = "generated/pyodide-lock.json",
deps = ["pyodide-lock.js@rule"],
)

cc_capnp_library(
name = "pyodide_extra_capnp",
srcs = ["generated/pyodide_extra.capnp"],
visibility = ["//visibility:public"],
deps = [
":pyodide_extra_file_embed",
":pyodide_lock_file_embed",
":pyodide_packages_archive_embed",
":python_entrypoint_file_embed",
],
)

copy_file(
name = "pyodide.asm.wasm@rule",
src = "@pyodide//:pyodide/pyodide.asm.wasm",
Expand Down
5 changes: 0 additions & 5 deletions src/pyodide/pyodide_extra.capnp

This file was deleted.

9 changes: 0 additions & 9 deletions src/pyodide/python-entrypoint.js

This file was deleted.

3 changes: 0 additions & 3 deletions src/workerd/api/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ wd_cc_library(
hdrs = [
"modules.h",
"rtti.h",
"//src/pyodide:generated/pyodide_extra.capnp.h",
"//src/workerd/server:workerd.capnp.h",
],
visibility = ["//visibility:public"],
Expand All @@ -96,7 +95,6 @@ wd_cc_library(
":pyodide",
":r2",
"//src/pyodide",
"//src/pyodide:pyodide_extra_capnp",
"//src/workerd/api/node",
"//src/workerd/io",
"//src/workerd/jsg:rtti",
Expand Down Expand Up @@ -140,7 +138,6 @@ wd_cc_library(
implementation_deps = ["//src/workerd/util:string-buffer"],
visibility = ["//visibility:public"],
deps = [
"//src/pyodide:pyodide_extra_capnp",
"//src/workerd/io",
"//src/workerd/jsg",
"//src/workerd/server:workerd_capnp",
Expand Down
14 changes: 14 additions & 0 deletions src/workerd/api/pyodide/pyodide.c++
Original file line number Diff line number Diff line change
Expand Up @@ -521,4 +521,18 @@ bool hasPythonModules(capnp::List<server::config::Worker::Module>::Reader module
return false;
}

kj::StringPtr getPythonEntrypoint() {
return R"JS_SCRIPT(
// The entrypoint to a worker has to be an ES6 module (well, ignoring service
// workers). For Python workers, we use this file as the ES6 entrypoint.
//
// This file is treated as part of the user bundle and cannot import internal
// modules. Any imports from this file must be user-visible. To deal with this,
// we delegate the implementation to `python-entrypoint-helper` which is a
// BUILTIN module that can see our INTERNAL modules.
export { default } from 'pyodide:python-entrypoint-helper';
)JS_SCRIPT"_kj;
}

} // namespace workerd::api::pyodide
2 changes: 2 additions & 0 deletions src/workerd/api/pyodide/pyodide.h
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,8 @@ jsg::Ref<PyodideMetadataReader> makePyodideMetadataReader(Worker::Reader conf,

bool hasPythonModules(capnp::List<server::config::Worker::Module>::Reader modules);

kj::StringPtr getPythonEntrypoint();

#define EW_PYODIDE_ISOLATE_TYPES \
api::pyodide::ReadOnlyBuffer, api::pyodide::PyodideMetadataReader, \
api::pyodide::ArtifactBundler, api::pyodide::DiskCache, \
Expand Down
1 change: 0 additions & 1 deletion src/workerd/server/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ wd_cc_binary(
":v8-platform-impl",
":workerd-meta_capnp",
":workerd_capnp",
"//src/pyodide:pyodide_extra_capnp",
"//src/rust/cxx-integration",
"//src/workerd/util:autogate",
"//src/workerd/util:perfetto",
Expand Down
16 changes: 4 additions & 12 deletions src/workerd/server/workerd-api.c++
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@
#include <workerd/util/thread-scopes.h>
#include <workerd/util/use-perfetto-categories.h>

#include <pyodide/generated/pyodide_extra.capnp.h>

#include <kj/compat/http.h>
#include <kj/compat/tls.h>
#include <kj/compat/url.h>
Expand Down Expand Up @@ -553,7 +551,7 @@ void WorkerdApi::compileModules(jsg::Lock& lockParam,
auto mainModule = confModules.begin();
capnp::MallocMessageBuilder message;
auto module = message.getRoot<config::Worker::Module>();
module.setEsModule(PYTHON_ENTRYPOINT);
module.setEsModule(getPythonEntrypoint());
auto info = tryCompileModule(lockParam, module, modules->getObserver(), featureFlags);
auto path = kj::Path::parse(mainModule->getName());
modules->add(path, kj::mv(KJ_REQUIRE_NONNULL(info)));
Expand All @@ -565,14 +563,8 @@ void WorkerdApi::compileModules(jsg::Lock& lockParam,
jsg::ModuleRegistry::Type::INTERNAL);

// Inject packages tar file
if (featureFlags.getPythonExternalPackages()) {
modules->addBuiltinModule("pyodide-internal:packages_tar_reader", "export default { }"_kj,
workerd::jsg::ModuleRegistry::Type::INTERNAL, {});
} else {
modules->addBuiltinModule("pyodide-internal:packages_tar_reader",
jsg::alloc<ReadOnlyBuffer>(PYODIDE_PACKAGES_TAR.get()),
workerd::jsg::ModuleRegistry::Type::INTERNAL);
}
modules->addBuiltinModule("pyodide-internal:packages_tar_reader", "export default { }"_kj,
workerd::jsg::ModuleRegistry::Type::INTERNAL, {});

// Inject artifact bundler.
modules->addBuiltinModule("pyodide-internal:artifacts",
Expand Down Expand Up @@ -967,7 +959,7 @@ kj::Own<jsg::modules::ModuleRegistry> WorkerdApi::initializeBundleModuleRegistry
KJ_REQUIRE(featureFlags.getPythonWorkers(),
"The python_workers compatibility flag is required to use Python.");
hasPythonModules = true;
bundleBuilder.addEsmModule(def.getName(), kj::str(PYTHON_ENTRYPOINT).releaseArray());
bundleBuilder.addEsmModule(def.getName(), kj::str(getPythonEntrypoint()).releaseArray());
break;
}
case config::Worker::Module::PYTHON_REQUIREMENT: {
Expand Down

0 comments on commit 5507666

Please sign in to comment.