Skip to content

Commit

Permalink
Merge pull request #1624 from cloudflare/dominik/python-disallow-file…
Browse files Browse the repository at this point in the history
…xtless

Disallow file extension-less Python modules.
  • Loading branch information
dom96 authored Feb 7, 2024
2 parents 061a15a + 1f7e57c commit 9e1cf65
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/pyodide/python-entrypoint-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ async function setupPackages(pyodide) {
let mainModuleName = MetadataReader.getMainModule();
if (mainModuleName.endsWith(".py")) {
mainModuleName = mainModuleName.slice(0, -3);
} else {
throw new Error("Main module needs to end with a .py file extension");
}
return pyodide.pyimport(mainModuleName);
}
Expand Down
10 changes: 2 additions & 8 deletions src/workerd/api/pyodide/pyodide.c++
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ jsg::Ref<PyodideMetadataReader> makePyodideMetadataReader(Worker::Reader conf) {
auto contents = kj::heapArrayBuilder<kj::Array<kj::byte>>(numFiles);
auto requirements = kj::heapArrayBuilder<kj::String>(numRequirements);
for (auto module : modules) {
bool pymodule = false;
switch (module.which()) {
case Worker::Module::TEXT:
contents.add(kj::heapArray(module.getText().asBytes()));
Expand All @@ -93,7 +92,7 @@ jsg::Ref<PyodideMetadataReader> makePyodideMetadataReader(Worker::Reader conf) {
contents.add(kj::heapArray(module.getJson().asBytes()));
break;
case Worker::Module::PYTHON_MODULE:
pymodule = true;
KJ_REQUIRE(module.getName().endsWith(".py"));
contents.add(kj::heapArray(module.getPythonModule().asBytes()));
break;
case Worker::Module::PYTHON_REQUIREMENT:
Expand All @@ -102,12 +101,7 @@ jsg::Ref<PyodideMetadataReader> makePyodideMetadataReader(Worker::Reader conf) {
default:
continue;
}
auto name = module.getName();
if (pymodule && !name.endsWith(".py")) {
names.add(kj::str(name, ".py"));
} else {
names.add(kj::str(name));
}
names.add(kj::str(module.getName()));
}
return jsg::alloc<PyodideMetadataReader>(kj::mv(mainModule), names.finish(), contents.finish(),
requirements.finish(), true);
Expand Down

0 comments on commit 9e1cf65

Please sign in to comment.