Skip to content

Fix opaque OSError when @cudaq.kernel is used in the REPL (#2593)#4365

Merged
mitchdz merged 3 commits intoNVIDIA:mainfrom
mitchdz:issue-2593
Apr 23, 2026
Merged

Fix opaque OSError when @cudaq.kernel is used in the REPL (#2593)#4365
mitchdz merged 3 commits intoNVIDIA:mainfrom
mitchdz:issue-2593

Conversation

@mitchdz
Copy link
Copy Markdown
Collaborator

@mitchdz mitchdz commented Apr 22, 2026

This adds better error messages for #2593.

  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>
@schweitzpgi schweitzpgi requested a review from anpaz April 22, 2026 18:05
github-actions Bot pushed a commit that referenced this pull request Apr 22, 2026
@github-actions
Copy link
Copy Markdown

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

Copy link
Copy Markdown
Collaborator

@schweitzpgi schweitzpgi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks, Mitch!

Comment thread python/cudaq/kernel/utils.py Outdated
Copy link
Copy Markdown
Collaborator

@anpaz anpaz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Copy link
Copy Markdown
Collaborator

@1tnguyen 1tnguyen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍 Thanks, @mitchdz!

github-actions Bot pushed a commit that referenced this pull request Apr 22, 2026
@github-actions
Copy link
Copy Markdown

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

@mitchdz mitchdz added this pull request to the merge queue Apr 23, 2026
Merged via the queue into NVIDIA:main with commit 8db3d83 Apr 23, 2026
209 checks passed
@mitchdz mitchdz deleted the issue-2593 branch April 23, 2026 06:18
github-actions Bot pushed a commit that referenced this pull request Apr 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants