Skip to content

Commit 115ff1b

Browse files
authored
Rename PythonRuntimeError to PythonInternalError (#5351)
There is something called a RuntimeError in Python and if raised it will create a Python RuntimeError but this will be a user error. So the name is confusing. PythonInternalError is more accurate.
1 parent 430bd0a commit 115ff1b

File tree

10 files changed

+50
-37
lines changed

10 files changed

+50
-37
lines changed

src/pyodide/internal/loadPackage.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ import {
2121
import { parseTarInfo } from 'pyodide-internal:tar';
2222
import { createTarFS } from 'pyodide-internal:tarfs';
2323
import { default as ArtifactBundler } from 'pyodide-internal:artifacts';
24-
import { PythonUserError, PythonRuntimeError } from 'pyodide-internal:util';
24+
import {
25+
PythonUserError,
26+
PythonWorkersInternalError,
27+
} from 'pyodide-internal:util';
2528

2629
function getPackageMetadata(requirement: string): PackageDeclaration {
2730
const obj = LOCKFILE['packages'][requirement];
@@ -39,7 +42,7 @@ function loadBundleFromArtifactBundler(requirement: string): Reader {
3942
const fullPath = `python-package-bucket/${PACKAGES_VERSION}/${filename}`;
4043
const reader = ArtifactBundler.getPackage(fullPath);
4144
if (!reader) {
42-
throw new PythonRuntimeError(
45+
throw new PythonWorkersInternalError(
4346
'Failed to get package ' + fullPath + ' from ArtifactBundler'
4447
);
4548
}

src/pyodide/internal/metadatafs.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { default as MetadataReader } from 'pyodide-internal:runtime-generated/metadata';
22
import { createReadonlyFS } from 'pyodide-internal:readOnlyFS';
3-
import { PythonRuntimeError } from 'pyodide-internal:util';
3+
import { PythonWorkersInternalError } from 'pyodide-internal:util';
44

55
function createTree(paths: string[]): MetadataDirInfo {
66
const tree: MetadataFSInfo = new Map();
@@ -10,7 +10,9 @@ function createTree(paths: string[]): MetadataDirInfo {
1010
const name = parts.pop()!;
1111
for (const part of parts) {
1212
if (typeof subTree === 'number') {
13-
throw new PythonRuntimeError('expected subtree to not be a number');
13+
throw new PythonWorkersInternalError(
14+
'expected subtree to not be a number'
15+
);
1416
}
1517
let next: MetadataFSInfo | undefined = subTree.get(part);
1618
if (!next) {
@@ -20,7 +22,9 @@ function createTree(paths: string[]): MetadataDirInfo {
2022
subTree = next;
2123
}
2224
if (typeof subTree === 'number') {
23-
throw new PythonRuntimeError('expected subtree to not be a number');
25+
throw new PythonWorkersInternalError(
26+
'expected subtree to not be a number'
27+
);
2428
}
2529
subTree.set(name, idx);
2630
});
@@ -55,15 +59,15 @@ export function createMetadataFS(Module: Module): object {
5559
},
5660
readdir(node) {
5761
if (node.tree == undefined) {
58-
throw new PythonRuntimeError(
62+
throw new PythonWorkersInternalError(
5963
'cannot read directory, tree is undefined'
6064
);
6165
}
6266
return Array.from(node.tree.keys());
6367
},
6468
lookup(parent, name) {
6569
if (parent.tree == undefined) {
66-
throw new PythonRuntimeError(
70+
throw new PythonWorkersInternalError(
6771
'cannot lookup directory, tree is undefined'
6872
);
6973
}

src/pyodide/internal/pool/builtin_wrappers.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { getRandomValues as getRandomValuesType } from 'pyodide-internal:topLevelEntropy/lib';
22
import type { default as UnsafeEvalType } from 'internal:unsafe-eval';
3-
import { PythonRuntimeError } from 'pyodide-internal:util';
3+
import { PythonWorkersInternalError } from 'pyodide-internal:util';
44

55
if (typeof FinalizationRegistry === 'undefined') {
66
// @ts-expect-error cannot assign to globalThis
@@ -196,7 +196,9 @@ function checkCallee(): void {
196196
}
197197
if (!isOkay) {
198198
console.warn('Invalid call to `WebAssembly.Module`', funcName);
199-
throw new PythonRuntimeError('Invalid call to `WebAssembly.Module`');
199+
throw new PythonWorkersInternalError(
200+
'Invalid call to `WebAssembly.Module`'
201+
);
200202
}
201203
}
202204

src/pyodide/internal/python.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import type { PyodideEntrypointHelper } from 'pyodide:python-entrypoint-helper';
2727
import { default as SetupEmscripten } from 'internal:setup-emscripten';
2828

2929
import { default as UnsafeEval } from 'internal:unsafe-eval';
30-
import { PythonRuntimeError, reportError } from 'pyodide-internal:util';
30+
import { PythonWorkersInternalError, reportError } from 'pyodide-internal:util';
3131
import { loadPackages } from 'pyodide-internal:loadPackage';
3232
import { default as MetadataReader } from 'pyodide-internal:runtime-generated/metadata';
3333
import { TRANSITIVE_REQUIREMENTS } from 'pyodide-internal:metadata';
@@ -109,7 +109,7 @@ function validatePyodideVersion(pyodide: Pyodide): void {
109109
return;
110110
}
111111
if (pyodide.version !== expectedPyodideVersion) {
112-
throw new PythonRuntimeError(
112+
throw new PythonWorkersInternalError(
113113
`Pyodide version mismatch, expected '${expectedPyodideVersion}'`
114114
);
115115
}

src/pyodide/internal/setupPackages.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { createMetadataFS } from 'pyodide-internal:metadatafs';
33
import { LOCKFILE } from 'pyodide-internal:metadata';
44
import {
55
invalidateCaches,
6-
PythonRuntimeError,
6+
PythonWorkersInternalError,
77
PythonUserError,
88
simpleRunPython,
99
} from 'pyodide-internal:util';
@@ -80,7 +80,7 @@ class VirtualizedDir {
8080
const dest = dir == 'dynlib' ? this.dynlibTarFs : this.rootInfo;
8181
overlayInfo.children!.forEach((val, key) => {
8282
if (dest.children!.has(key)) {
83-
throw new PythonRuntimeError(
83+
throw new PythonWorkersInternalError(
8484
`File/folder ${key} being written by multiple packages`
8585
);
8686
}
@@ -213,7 +213,7 @@ export function patchLoadPackage(pyodide: Pyodide): void {
213213
}
214214

215215
function disabledLoadPackage(): never {
216-
throw new PythonRuntimeError(
216+
throw new PythonWorkersInternalError(
217217
'pyodide.loadPackage is disabled because packages are encoded in the binary'
218218
);
219219
}

src/pyodide/internal/snapshot.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
} from 'pyodide-internal:metadata';
1919
import {
2020
invalidateCaches,
21-
PythonRuntimeError,
21+
PythonWorkersInternalError,
2222
PythonUserError,
2323
simpleRunPython,
2424
} from 'pyodide-internal:util';
@@ -237,7 +237,7 @@ function getMemoryPatched(
237237
if (expectedTableBase && tableBase !== expectedTableBase) {
238238
// If this happens, we will segfault if we ever try to use this dynamic library.
239239
// Save ourselves some debugging pain by crashing early.
240-
throw new PythonRuntimeError(
240+
throw new PythonWorkersInternalError(
241241
`Error loading ${libName}: Expected table base ${expectedTableBase} but got table base ${tableBase}`
242242
);
243243
}
@@ -302,7 +302,7 @@ function loadDynlibFromVendor(
302302
const path = soFile.slice(3).join('/');
303303
const index = userBundleNames.indexOf(path);
304304
if (index == -1) {
305-
throw new PythonRuntimeError(
305+
throw new PythonWorkersInternalError(
306306
`Could not find ${path} in user bundle, which is required by the snapshot.`
307307
);
308308
}
@@ -618,14 +618,14 @@ function decodeSnapshot(
618618
return undefined;
619619
}
620620
if (reader.getMemorySnapshotSize() === 0) {
621-
throw new PythonRuntimeError(
621+
throw new PythonWorkersInternalError(
622622
`SnapshotReader returned memory snapshot size of 0`
623623
);
624624
}
625625
const header = new Uint32Array(4);
626626
reader.readMemorySnapshot(0, header);
627627
if (header[0] !== SNAPSHOT_MAGIC) {
628-
throw new PythonRuntimeError(
628+
throw new PythonWorkersInternalError(
629629
`Invalid magic number ${header[0]}, expected ${SNAPSHOT_MAGIC}`
630630
);
631631
}
@@ -686,7 +686,7 @@ function checkSnapshotType(snapshotType: string): void {
686686
snapshotType === 'dedicated' &&
687687
!IS_DEDICATED_SNAPSHOT_ENABLED
688688
) {
689-
throw new PythonRuntimeError(
689+
throw new PythonWorkersInternalError(
690690
'Received dedicated snapshot but compat flag for dedicated snapshots is not enabled'
691691
);
692692
}
@@ -696,7 +696,7 @@ function checkSnapshotType(snapshotType: string): void {
696696
snapshotType !== 'dedicated' &&
697697
IS_DEDICATED_SNAPSHOT_ENABLED
698698
) {
699-
throw new PythonRuntimeError(
699+
throw new PythonWorkersInternalError(
700700
'Received non-dedicated snapshot but compat flag for dedicated snapshots is enabled'
701701
);
702702
}
@@ -705,7 +705,7 @@ function checkSnapshotType(snapshotType: string): void {
705705
// should verify that the snapshot in the bundle is a dedicated snapshot. If it is not
706706
// we should fail with an error.
707707
if (snapshotType !== 'dedicated' && IS_SECOND_VALIDATION_PHASE) {
708-
throw new PythonRuntimeError(
708+
throw new PythonWorkersInternalError(
709709
'The second validation phase should receive a dedicated snapshot, got ' +
710710
snapshotType
711711
);
@@ -766,7 +766,7 @@ function collectSnapshot(
766766
);
767767
DiskCache.put('snapshot.bin', snapshot);
768768
} else {
769-
throw new PythonRuntimeError(
769+
throw new PythonWorkersInternalError(
770770
"Attempted to collect snapshot outside of context where it's supported."
771771
);
772772
}
@@ -791,13 +791,13 @@ export function maybeCollectDedicatedSnapshot(
791791
if (Module.API.version == '0.26.0a2') {
792792
// 0.26.0a2 does not support serialisation of the hiwire state, so it cannot support dedicated
793793
// snapshots.
794-
throw new PythonRuntimeError(
794+
throw new PythonWorkersInternalError(
795795
'Dedicated snapshot is not supported for Python runtime version 0.26.0a2'
796796
);
797797
}
798798

799799
if (!pyodide_entrypoint_helper) {
800-
throw new PythonRuntimeError(
800+
throw new PythonWorkersInternalError(
801801
'pyodide_entrypoint_helper is required for dedicated snapshot'
802802
);
803803
}
@@ -840,7 +840,7 @@ export function finalizeBootstrap(
840840
if ('pyodide_entrypoint_helper' in obj) {
841841
return pyodide_entrypoint_helper;
842842
}
843-
throw new PythonRuntimeError(`Can't deserialize ${obj}`);
843+
throw new PythonWorkersInternalError(`Can't deserialize ${obj}`);
844844
};
845845

846846
Module.API.config._makeSnapshot =

src/pyodide/internal/tar.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// And some trial and error with real tar files.
33
// https://en.wikipedia.org/wiki/Tar_(computing)#File_format
44

5-
import { PythonRuntimeError } from 'pyodide-internal:util';
5+
import { PythonWorkersInternalError } from 'pyodide-internal:util';
66

77
const decoder = new TextDecoder();
88
function decodeString(buf: Uint8Array): string {
@@ -134,7 +134,7 @@ export function parseTarInfo(reader: Reader): [TarFSInfo, string[]] {
134134
directory.children!.set(info.name, info);
135135
} else {
136136
// fail if we encounter other values of type (e.g., symlink, LongName, etc)
137-
throw new PythonRuntimeError(
137+
throw new PythonWorkersInternalError(
138138
`Python TarFS error: Unexpected type ${info.type}`
139139
);
140140
}

src/pyodide/internal/tarfs.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createReadonlyFS } from 'pyodide-internal:readOnlyFS';
2-
import { PythonRuntimeError } from 'pyodide-internal:util';
2+
import { PythonWorkersInternalError } from 'pyodide-internal:util';
33

44
const FSOps: FSOps<TarFSInfo> = {
55
getNodeMode(_parent, _name, info) {
@@ -19,22 +19,26 @@ const FSOps: FSOps<TarFSInfo> = {
1919
},
2020
readdir(node) {
2121
if (!node.info.children) {
22-
throw new PythonRuntimeError('Trying to readdir from a non-dir node');
22+
throw new PythonWorkersInternalError(
23+
'Trying to readdir from a non-dir node'
24+
);
2325
}
2426
return Array.from(node.info.children.keys());
2527
},
2628
lookup(parent, name) {
2729
if (!parent.info.children) {
28-
throw new PythonRuntimeError('Trying to lookup from a non-dir node');
30+
throw new PythonWorkersInternalError(
31+
'Trying to lookup from a non-dir node'
32+
);
2933
}
3034
return parent.info.children.get(name)!;
3135
},
3236
read(stream, position, buffer) {
3337
if (stream.node.contentsOffset == undefined) {
34-
throw new PythonRuntimeError('contentsOffset is undefined');
38+
throw new PythonWorkersInternalError('contentsOffset is undefined');
3539
}
3640
if (!stream.node.info.reader) {
37-
throw new PythonRuntimeError('reader is undefined');
41+
throw new PythonWorkersInternalError('reader is undefined');
3842
}
3943
return stream.node.info.reader.read(
4044
stream.node.contentsOffset + position,

src/pyodide/internal/util.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* that is **not** a result of the user doing something wrong, i.e. it's an internal error that is
44
* a result of a bug in our runtime.
55
*/
6-
export class PythonRuntimeError extends Error {
6+
export class PythonWorkersInternalError extends Error {
77
override get name(): string {
88
return this.constructor.name;
99
}
@@ -65,7 +65,7 @@ export function simpleRunPython(
6565
// PyRun_SimpleString will have written a Python traceback to stderr.
6666
console.warn('Command failed:', code);
6767
console.warn(cause);
68-
throw new PythonRuntimeError('Failed to run Python code');
68+
throw new PythonWorkersInternalError('Failed to run Python code');
6969
}
7070
return cause;
7171
}

src/pyodide/python-entrypoint-helper.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
} from 'pyodide-internal:metadata';
1919
import { default as Limiter } from 'pyodide-internal:limiter';
2020
import {
21-
PythonRuntimeError,
21+
PythonWorkersInternalError,
2222
PythonUserError,
2323
reportError,
2424
} from 'pyodide-internal:util';
@@ -73,7 +73,7 @@ let _pyodide_entrypoint_helper: PyodideEntrypointHelper | null = null;
7373

7474
function get_pyodide_entrypoint_helper(): PyodideEntrypointHelper {
7575
if (!_pyodide_entrypoint_helper) {
76-
throw new PythonRuntimeError(
76+
throw new PythonWorkersInternalError(
7777
'pyodide_entrypoint_helper is not initialized'
7878
);
7979
}

0 commit comments

Comments
 (0)