Fix opaque OSError when @cudaq.kernel is used in the REPL (#2593)#4365
Merged
mitchdz merged 3 commits intoNVIDIA:mainfrom Apr 23, 2026
Merged
Fix opaque OSError when @cudaq.kernel is used in the REPL (#2593)#4365mitchdz merged 3 commits intoNVIDIA:mainfrom
mitchdz merged 3 commits intoNVIDIA:mainfrom
Conversation
Fixes NVIDIA#2593. `@cudaq.kernel` called `inspect.getsource()` on the decorated function. For functions defined in the standard CPython REPL this raises `OSError: could not get source code`, which surfaced as an uncaught traceback with no hint at the cause. This wraps the three `inspect.*` calls (in `kernel_decorator.py` and `analysis.py`) in a single helper that catches the error and raises a `RuntimeError` explaining the situation and pointing at the workarounds: run in Jupyter/IPython, or move the kernel into a `.py` file. The original `OSError` is chained as `__cause__`. Three failure buckets produce different messages: - `<stdin>` / `<python-input-...>` — the REPL case. - Other synthetic filenames (`eval`, `exec`) — non-file context. - Real paths that fail to read — missing/frozen/compiled. Also fixes a latent `None` deref in `FetchDepFuncsSourceCode.fetch` when a dependency function can't be resolved. Does not try to make REPL-defined kernels actually compile — that would need `readline`/PyREPL interception and isn't worth the complexity. ## Test - New `python/tests/kernel/test_kernel_repl_source.py`: 4 cases (REPL filename, other synthetic filename, dep-fetch path, non-regression on normal path). - Manual reproducer from the issue now prints the new message. - Regression: 201 passed across `test_kernel_features`, `test_kernel_signature`, `test_kernel_call_return`, `test_kernel_return`, `test_kernel_parameters`, `test_kernel_complex`, `test_kernel_exp_pauli`, `test_kernel_float`. Signed-off-by: mdzurick <mitch_dz@hotmail.com>
|
CUDA Quantum Docs Bot: A preview of the documentation can be found here. |
schweitzpgi
reviewed
Apr 22, 2026
Collaborator
schweitzpgi
left a comment
There was a problem hiding this comment.
LGTM. Thanks, Mitch!
anpaz
reviewed
Apr 22, 2026
anpaz
approved these changes
Apr 22, 2026
Collaborator
anpaz
left a comment
There was a problem hiding this comment.
There is a minor comment about the error might be miss leading as we're reporting the same message for Type exceptions.
Signed-off-by: mdzurick <mitch_dz@hotmail.com>
Signed-off-by: mdzurick <mitch_dz@hotmail.com>
|
CUDA Quantum Docs Bot: A preview of the documentation can be found here. |
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.
This adds better error messages for #2593.