Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
clippy
  • Loading branch information
ShaharNaveh committed Mar 6, 2026
commit 85e92654f280d814fa463af9c5bdc5501bee4fa5
2 changes: 1 addition & 1 deletion crates/stdlib/src/_tokenize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
vm.state
.codec_registry
.decode_text(bytes.into(), encoding, None, vm)
.map(|s| s.to_str().unwrap_or_else(|| "").into())?
.map(|s| s.to_str().unwrap_or("").into())?
}
None => raw_line
.downcast::<PyStr>()
Expand Down Expand Up @@ -120,8 +120,8 @@
break tok;
}

let nline = zelf.readline(vm)?;

Check warning on line 123 in crates/stdlib/src/_tokenize.rs

View workflow job for this annotation

GitHub Actions / Lint Rust & Python code

Unknown word (nline)
if nline.is_empty() {

Check warning on line 124 in crates/stdlib/src/_tokenize.rs

View workflow job for this annotation

GitHub Actions / Lint Rust & Python code

Unknown word (nline)
state.eof = true;
*zelf.state.write() = state.clone();

Expand All @@ -142,7 +142,7 @@
.into();
return Ok(PyIterReturn::Return(out));
}
state.push_line(&nline);

Check warning on line 145 in crates/stdlib/src/_tokenize.rs

View workflow job for this annotation

GitHub Actions / Lint Rust & Python code

Unknown word (nline)
};
Comment on lines +124 to +146
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

End-of-file tuple needs to come from the real EOF token.

Synthesizing the tuple here hardcodes (line, -1) and reuses the previous line text, so consumers see the wrong coordinates and line content. Grab the parser’s EndOfFile token, set it as prev_token, and let the existing emission path produce the tuple.

-                if nline.is_empty() {
-                    state.eof = true;
-                    *zelf.state.write() = state.clone();
-
-                    let line_num = &state.start().0;
-                    let out = vm
-                        .ctx
-                        .new_tuple(vec![
-                            token_kind_value(TokenKind::EndOfFile).to_pyobject(vm),
-                            vm.ctx.new_str("").into(),
-                            vm.ctx
-                                .new_tuple(vec![line_num.to_pyobject(vm), (-1).to_pyobject(vm)])
-                                .into(),
-                            vm.ctx
-                                .new_tuple(vec![line_num.to_pyobject(vm), (-1).to_pyobject(vm)])
-                                .into(),
-                            vm.ctx.new_str(state.current_line()).into(),
-                        ])
-                        .into();
-                    return Ok(PyIterReturn::Return(out));
-                }
+                if nline.is_empty() {
+                    state.eof = true;
+                    if let Some(eof) = state
+                        .tokens
+                        .iter()
+                        .rev()
+                        .find(|token| token.kind() == TokenKind::EndOfFile)
+                    {
+                        state.prev_token = Some(*eof);
+                        break eof.clone();
+                    }
+                    return Ok(PyIterReturn::StopIteration(None));
+                }

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In stdlib/src/tokenize.rs around lines 116 to 138, don't synthesize the EOF
tuple here (which hardcodes (line, -1) and reuses the previous line text);
instead obtain the parser's EndOfFile token, set it as the state's prev_token
(or otherwise set prev_token to that EOF token), set state.eof = true and
persist the state as before, then allow the existing token-emission path to
produce and return the EOF tuple so the coordinates and line content come from
the real EOF token rather than being hardcoded.


*zelf.state.write() = state.clone();
Expand Down
Loading