8000 Add `raise_if_stop!` macro for `protocol/iter.rs` by ShaharNaveh · Pull Request #5885 · RustPython/RustPython · GitHub
[go: up one dir, main page]

Skip to content

Add raise_if_stop! macro for protocol/iter.rs #5885

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jul 2, 2025
Merged
17 changes: 17 additions & 0 deletions vm/src/protocol/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,20 @@ where
(self.length_hint.unwrap_or(0), self.length_hint)
}
}

/// Macro to handle `PyIterReturn` values in iterator implementations.
///
/// Extracts the object from `PyIterReturn::Return(obj)` or performs early return
/// for `PyIterReturn::StopIteration(v)`. This macro should only be used within
/// functions that return `PyResult<PyIterReturn>`.
#[macro_export]
macro_rules! raise_stop {
($input:expr) => {
match $input {
$crate::protocol::PyIterReturn::Return(obj) => obj,
$crate::protocol::PyIterReturn::StopIteration(v) => {
return Ok($crate::protocol::PyIterReturn::StopIteration(v))
}
}
};
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add documentation for the macro's usage constraints.

The macro assumes it's used within a function that returns PyResult<PyIterReturn>, but this constraint isn't documented. Consider adding documentation to clarify the expected usage context.

Add documentation above the macro:

+/// Macro to handle `PyIterReturn` values in iterator implementations.
+/// 
+/// Extracts the object from `PyIterReturn::Return(obj)` or performs early return
+/// for `PyIterReturn::StopIteration(v)`. This macro should only be used within
+/// functions that return `PyResult<PyIterReturn>`.
+/// 
+/// # Example
+/// ```rust
+/// fn iterator_next(&self, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
+///     let value = raise_stop!(some_pyiter_return_value);
+///     // Process the extracted value...
+/// }
+/// ```
 #[macro_export]
 macro_rules! raise_stop {
🤖 Prompt for AI Agents
In vm/src/protocol/iter.rs around lines 280 to 290, the raise_stop! macro lacks
documentation specifying that it must be used within a function returning
PyResult<PyIterReturn>. Add a doc comment above the macro definition explaining
this usage constraint and provide a brief example showing how to use the macro
inside such a function to clarify its expected context.

⚠️ Potential issue

Naming inconsistency with PR objectives.

The PR title mentions handle_pyiter_return! macro, but the implemented macro is named raise_stop!. Please ensure the naming is consistent with the intended functionality and PR objectives.

Consider renaming the macro to match the PR title:

-macro_rules! raise_stop {
+macro_rules! handle_pyiter_return {

Or update the PR title if raise_stop! is the preferred name.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
#[macro_export]
macro_rules! raise_stop {
($input:expr) => {
match $input {
$crate::protocol::PyIterReturn::Return(obj) => obj,
$crate::protocol::PyIterReturn::StopIteration(v) => {
return Ok($crate::protocol::PyIterReturn::StopIteration(v))
}
}
};
}
#[macro_export]
macro_rules! handle_pyiter_return {
($input:expr) => {
match $input {
$crate::protocol::PyIterReturn::Return(obj) => obj,
$crate::protocol::PyIterReturn::StopIteration(v) => {
return Ok($crate::protocol::PyIterReturn::StopIteration(v))
}
}
};
}
🤖 Prompt for AI Agents
In vm/src/protocol/iter.rs around lines 280 to 290, the macro is named
raise_stop! but the PR title refers to handle_pyiter_return!. Rename the macro
from raise_stop! to handle_pyiter_return! to maintain consistency with the PR
objectives, or alternatively update the PR title to reflect the current macro
name if that is preferred.

Loading
Loading
0