Skip to content

refactor(tests): replace exec() with importlib in example runner#1628

Open
OfficialSerge wants to merge 4 commits intoNVIDIA:mainfrom
OfficialSerge:feat/refactor-example-tests
Open

refactor(tests): replace exec() with importlib in example runner#1628
OfficialSerge wants to merge 4 commits intoNVIDIA:mainfrom
OfficialSerge:feat/refactor-example-tests

Conversation

@OfficialSerge
Copy link

Description

This PR is in response to the following TODO item in cuda_core/tests/example_tests/utils.py.

# TODO: Refactor the examples to give them a common callable `main()` to avoid needing to use exec here?
exec(script, env if env else {})

Key Changes

  • refactor run_example in utils.py using importlib, adding support for both main() and exec() scripts, tracebacks should now provide real line number feedback, thus paving the way for us to progressively enhance the remaining non-module tests and examples.
  • updated test_basic_example.py to use pathlib.Path instead of os.path.join().
  • removed redundant manual Device(0) and set_current() calls in test class, instead relying on exiting deinit_cuda() fixture for consistent teardowns (see conftest.py)

Next Steps

  • go through the remaining examples still without main() and refactor them into Python modules, some examples are currently refactored while others are still plain executables so it's a mix.

@copy-pr-bot
Copy link
Contributor

copy-pr-bot bot commented Feb 14, 2026

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

# Collect metadata for file 'module_name' located at 'fullpath'.
# CASE: file does not exist -> spec is none.
# CASE: file is not .py -> spec is none.
# CASE: file does not have proper loader (module.spec.__loader__) -> spec.loader is none.
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we necessarily need to duplicate information from the Python stdlib docs here, but don't feel strongly either way.

Copy link
Author

Choose a reason for hiding this comment

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

I see, my ruff linter was flagging it as a potential point of runtime failure, the if-none check fixed these issues though I figured I'd leave an explanation to help people understand the need for the check, in case there are any future revisions.

@rparolin rparolin added this to the cuda.core v0.7.0 milestone Feb 18, 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.

3 participants

Comments