Raise real exceptions instead of strings in the runtime Python bindings - #3524
Open
LeonardNJU wants to merge 1 commit into
Open
Raise real exceptions instead of strings in the runtime Python bindings#3524LeonardNJU wants to merge 1 commit into
LeonardNJU wants to merge 1 commit into
Conversation
`raise "some message"` is invalid in Python 3 — a str is not an exception — so
the interpreter discards the message and raises
TypeError: exceptions must derive from BaseException
instead. Paired with a bare `except:`, the original cause is lost too, and the
user is told to install something they may already have.
Concretely: funasr was installed, but importing it failed on a missing
torchaudio. The message said 'please install funasr', the traceback said
TypeError, and the actual ModuleNotFoundError was nowhere to be seen.
This converts all 30 occurrences under runtime/python to real exceptions:
- guarded imports -> except ImportError as e: raise ImportError(...) from e
- everything else -> except Exception as e: raise RuntimeError(...) from e
`from e` keeps the original traceback, so a missing transitive dependency stays
visible. Narrowing the bare `except:` also stops unrelated failures from being
reported as a missing package — the model_dir branch in particular fired for any
failure, so a mistyped path also surfaced as TypeError.
Messages are unchanged. No behaviour changes beyond the exception type.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3523.
raise "some message"is invalid in Python 3 — astris not an exception. Theinterpreter discards the intended message and raises
instead. Paired with the surrounding bare
except:, the original cause is lost as well,so the user is told to install a package they may already have while the real failure is
invisible.
What this looked like in practice
funasrwas installed. The import failed on a missing transitive dependency:What I saw:
The message pointed at the wrong package, and the traceback pointed at the wrong error.
Diagnosing it meant reading the library source and reproducing the import by hand. I hit
the same wall a second time on
onnxscript(required by newertorch.onnx), and a thirdtime when loading from a local path — the
model_dirbranch fires on any failure, so amistyped path also surfaced as
TypeError.The change
All 30 occurrences under
runtime/python(7 files, bothfunasr_onnxandfunasr_torch):except ImportError as e:/raise ImportError(...) from eexcept Exception as e:/raise RuntimeError(...) from efrom epreserves the original traceback, which is the point — a missingtorchaudionow stays visible under the friendly message. Narrowing the bare
except:also stopsunrelated failures from being reported as a missing package.
Messages are unchanged. The only behavioural difference is the exception type, which
previously could not be caught by anything anyway.
Verification
raise "no longer appears anywhere underruntime/pythonpython -m py_compileI deliberately did not run
blackover these files. It reformats unrelated code inthem (the tree is not currently black-clean), which would have buried a 140-line fix in a
280-line diff. The new code follows the project's 100-column style by hand. Happy to run
the formatter if you would rather have it.
Two occurrences outside
runtime/pythonare left alone as out of scope:funasr/models/qwen3_asr/model.pyandfunasr/download/runtime_sdk_download_tool.py.Context: found while building a local-ASR adapter for Jitsi Meet
(https://github.com/LeonardNJU/jitsi-local-asr) that runs SenseVoice and streaming
Paraformer via
funasr_onnxon an ARM box.