Content-Length: 403637 | pFad | http://github.com/RustPython/RustPython/pull/5996

E1 Align SQL comment parsing with CPython by ever0de · Pull Request #5996 · RustPython/RustPython · GitHub
Skip to content

Align SQL comment parsing with CPython #5996

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ever0de
Copy link
Contributor

@ever0de ever0de commented Jul 17, 2025

Reference

Summary by CodeRabbit

  • Refactor
    • Improved the internal handling of whitespace and comment skipping for SQL input, resulting in more robust and maintainable processing. No changes to user-facing features or interfaces.

Copy link
Contributor

coderabbitai bot commented Jul 17, 2025

Walkthrough

The internal implementation of the lstrip_sql function in stdlib/src/sqlite.rs was refactored. The function now uses an index-based approach to traverse and skip whitespace and comments in a byte slice, replacing the previous pointer-based slicing method. No public interfaces or exported declarations were changed.

Changes

File(s) Change Summary
stdlib/src/sqlite.rs Refactored lstrip_sql to use index-based iteration instead of slice pointers. Logic for skipping whitespace and comments remains unchanged.

Suggested reviewers

  • youknowone

Poem

In the garden of code where comments grow,
A rabbit hopped by, trimming whitespace in tow.
With careful hops and index leaps,
Through SQL bytes, the bunny peeps.
Now cleaner paths the queries show,
Thanks to a bunny and a single pos! 🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@ever0de ever0de force-pushed the fix/sqlite-lstrip-sql branch from bdfa3f3 to 3403b9b Compare July 17, 2025 08:47
@ever0de ever0de marked this pull request as ready for review July 17, 2025 08:49
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
stdlib/src/sqlite.rs (1)

3070-3118: Good refactoring with improved bounds safety.

The refactored implementation has several positive aspects:

  1. Explicit bounds checking: Using sql.get(pos) provides clear, safe bounds checking compared to the previous slicing approach
  2. Clearer control flow: The index-based iteration makes the logic more explicit and easier to follow
  3. Memory safety: Eliminates potential issues with pointer arithmetic and slicing

Minor performance consideration: The multiple sql.get(pos) calls might have slight overhead compared to slicing, but the improved safety and clarity are worth the trade-off for this parsing function.

📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 799f38b and 3403b9b.

⛔ Files ignored due to path filters (1)
  • Lib/test/test_sqlite3/test_dbapi.py is excluded by !Lib/**
📒 Files selected for processing (1)
  • stdlib/src/sqlite.rs (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs

Instructions used from:

Sources:
📄 CodeRabbit Inference Engine

  • .github/copilot-instructions.md
🧠 Learnings (1)
📓 Common learnings
Learnt from: CR
PR: RustPython/RustPython#0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-06-30T10:08:48.858Z
Learning: Applies to Lib/test/**/*.py : Add a `# TODO: RUSTPYTHON` comment when modifications are made to tests in `Lib/test`
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (9)
  • GitHub Check: Check Rust code with rustfmt and clippy
  • GitHub Check: Check the WASM package and demo
  • GitHub Check: Run snippets and cpython tests (macos-latest)
  • GitHub Check: Run snippets and cpython tests (windows-latest)
  • GitHub Check: Run snippets and cpython tests (ubuntu-latest)
  • GitHub Check: Run rust tests (windows-latest)
  • GitHub Check: Run rust tests (ubuntu-latest)
  • GitHub Check: Run rust tests (macos-latest)
  • GitHub Check: Ensure compilation on various targets

Comment on lines 3070 to 3118
fn lstrip_sql(sql: &[u8]) -> Option<&[u8]> {
let mut pos = sql;
loop {
match pos.first()? {
let mut pos = 0;

// This loop is borrowed from the SQLite source code.
while let Some(t_char) = sql.get(pos) {
match t_char {
b' ' | b'\t' | b'\x0c' | b'\n' | b'\r' => {
pos = &pos[1..];
// Skip whitespace.
pos += 1;
}
b'-' => {
if *pos.get(1)? == b'-' {
// line comments
pos = &pos[2..];
while *pos.first()? != b'\n' {
pos = &pos[1..];
// Skip line comments.
if sql.get(pos + 1) == Some(&b'-') {
pos += 2;
while let Some(&ch) = sql.get(pos) {
if ch == b'\n' {
break;
}
pos += 1;
}
let _ = sql.get(pos)?;
} else {
return Some(pos);
return Some(&sql[pos..]);
}
}
b'/' => {
if *pos.get(1)? == b'*' {
// c style comments
pos = &pos[2..];
while *pos.first()? != b'*' || *pos.get(1)? != b'/' {
pos = &pos[1..];
// Skip C style comments.
if sql.get(pos + 1) == Some(&b'*') {
pos += 2;
while let Some(&ch) = sql.get(pos) {
if ch == b'*' && sql.get(pos + 1) == Some(&b'/') {
break;
}
pos += 1;
}
let _ = sql.get(pos)?;
pos += 2;
} else {
return Some(pos);
return Some(&sql[pos..]);
}
}
_ => return Some(pos),
_ => {
return Some(&sql[pos..]);
}
}
}

None
}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Review the bounds checking logic in comment handling.

The refactored index-based approach looks good overall, but there are potential logical issues in the comment handling sections:

  1. Line 3090: let _ = sql.get(pos)?; - This line appears to be checking if pos is still valid after processing a line comment, but if it fails, the function returns None. This might not be the intended behavior. If we've reached the end of input after a line comment, we should return None (indicating everything was skipped), not fail.

  2. Line 3105: Similar issue with C-style comment handling.

The logic should be:

  • If we reach the end of input while processing comments, return None (everything was skipped)
  • Only return Some(&sql[pos..]) when we find non-skippable content

Consider this fix:

                b'-' => {
                    // Skip line comments.
                    if sql.get(pos + 1) == Some(&b'-') {
                        pos += 2;
                        while let Some(&ch) = sql.get(pos) {
                            if ch == b'\n' {
                                break;
                            }
                            pos += 1;
                        }
-                        let _ = sql.get(pos)?;
                    } else {
                        return Some(&sql[pos..]);
                    }
                }
                b'/' => {
                    // Skip C style comments.
                    if sql.get(pos + 1) == Some(&b'*') {
                        pos += 2;
                        while let Some(&ch) = sql.get(pos) {
                            if ch == b'*' && sql.get(pos + 1) == Some(&b'/') {
                                break;
                            }
                            pos += 1;
                        }
-                        let _ = sql.get(pos)?;
-                        pos += 2;
+                        if sql.get(pos).is_some() {
+                            pos += 2;
+                        }
                    } else {
                        return Some(&sql[pos..]);
                    }
                }
📝 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
fn lstrip_sql(sql: &[u8]) -> Option<&[u8]> {
let mut pos = sql;
loop {
match pos.first()? {
let mut pos = 0;
// This loop is borrowed from the SQLite source code.
while let Some(t_char) = sql.get(pos) {
match t_char {
b' ' | b'\t' | b'\x0c' | b'\n' | b'\r' => {
pos = &pos[1..];
// Skip whitespace.
pos += 1;
}
b'-' => {
if *pos.get(1)? == b'-' {
// line comments
pos = &pos[2..];
while *pos.first()? != b'\n' {
pos = &pos[1..];
// Skip line comments.
if sql.get(pos + 1) == Some(&b'-') {
pos += 2;
while let Some(&ch) = sql.get(pos) {
if ch == b'\n' {
break;
}
pos += 1;
}
let _ = sql.get(pos)?;
} else {
return Some(pos);
return Some(&sql[pos..]);
}
}
b'/' => {
if *pos.get(1)? == b'*' {
// c style comments
pos = &pos[2..];
while *pos.first()? != b'*' || *pos.get(1)? != b'/' {
pos = &pos[1..];
// Skip C style comments.
if sql.get(pos + 1) == Some(&b'*') {
pos += 2;
while let Some(&ch) = sql.get(pos) {
if ch == b'*' && sql.get(pos + 1) == Some(&b'/') {
break;
}
pos += 1;
}
let _ = sql.get(pos)?;
pos += 2;
} else {
return Some(pos);
return Some(&sql[pos..]);
}
}
_ => return Some(pos),
_ => {
return Some(&sql[pos..]);
}
}
}
None
}
fn lstrip_sql(sql: &[u8]) -> Option<&[u8]> {
let mut pos = 0;
// This loop is borrowed from the SQLite source code.
while let Some(t_char) = sql.get(pos) {
match t_char {
b' ' | b'\t' | b'\x0c' | b'\n' | b'\r' => {
// Skip whitespace.
pos += 1;
}
b'-' => {
// Skip line comments.
if sql.get(pos + 1) == Some(&b'-') {
pos += 2;
while let Some(&ch) = sql.get(pos) {
if ch == b'\n' {
break;
}
pos += 1;
}
} else {
return Some(&sql[pos..]);
}
}
b'/' => {
// Skip C style comments.
if sql.get(pos + 1) == Some(&b'*') {
pos += 2;
while let Some(&ch) = sql.get(pos) {
if ch == b'*' && sql.get(pos + 1) == Some(&b'/') {
break;
}
pos += 1;
}
if sql.get(pos).is_some() {
pos += 2;
}
} else {
return Some(&sql[pos..]);
}
}
_ => {
return Some(&sql[pos..]);
}
}
}
None
}
🤖 Prompt for AI Agents
In stdlib/src/sqlite.rs between lines 3070 and 3118, the bounds checking after
processing line and C-style comments uses sql.get(pos)? which returns None if
pos is out of bounds, causing the function to return None prematurely. To fix
this, replace these checks with explicit bounds checks: if pos is at or beyond
the input length after skipping comments, return None to indicate all input was
skipped; otherwise, continue processing. This ensures the function correctly
returns None only when all content is skipped and returns Some with the
remaining slice when non-skippable content is found.

💡 Verification agent

🧩 Analysis chain

Verify alignment with CPython's SQL comment parsing behavior.

Since the PR objective is to "align SQL comment parsing with CPython," please verify that this implementation correctly matches CPython's behavior, particularly:

  1. Edge cases: How CPython handles malformed comments (e.g., /* without closing */, -- at end of input)
  2. Comment nesting: Whether CPython supports nested C-style comments
  3. Whitespace handling: Exact whitespace characters that CPython skips

Run this script to compare behavior with CPython:


🏁 Script executed:

#!/bin/bash
# Description: Test SQL comment parsing edge cases against CPython behavior
# Expected: Verify that RustPython matches CPython's sqlite3 behavior

# Test cases for edge cases
python3 -c "
import sqlite3
test_cases = [
    '/* incomplete comment',
    '-- comment at end',
    '/* nested /* comment */ */',
    '  \t\n  SELECT 1',
    '-- comment\nSELECT 1',
    '/* comment */SELECT 1'
]

for case in test_cases:
    try:
        result = sqlite3.complete_statement(case)
        print(f'{case!r}: {result}')
    except Exception as e:
        print(f'{case!r}: ERROR - {e}')
"

Length of output: 633


🏁 Script executed:

#!/bin/bash
# Check how CPython's sqlite3.complete_statement handles various ASCII whitespace characters
python3 - <<'PYCODE'
import sqlite3

cases = [
    ('space', ' SELECT 1;'),
    ('tab', '\tSELECT 1;'),
    ('newline', '\nSELECT 1;'),
    ('carriage_return', '\rSELECT 1;'),
    ('form_feed', '\fSELECT 1;'),
    ('vertical_tab', '\vSELECT 1;'),
]

print("Whitespace Skip Tests (should be True if whitespace is skipped):")
for name, stmt in cases:
    result = sqlite3.complete_statement(stmt)
    print(f"{name:15s}: {result}")
PYCODE

Length of output: 208


Include ASCII vertical tab in whitespace skip and document comment nesting

CPython’s sqlite3.complete_statement skips all standard ASCII whitespace—including vertical tab (\v)—and does not support nested C-style comments. To fully align RustPython’s parsing:

  • Update the whitespace match in lstrip_sql to include b'\x0b' (vertical tab).
  • Add a note in the code comment that nested /*…*/ comments aren’t supported, matching CPython behavior.
  • Add tests covering nested comments (with and without trailing semicolon) to lock in this behavior.

Suggested diff for stdlib/src/sqlite.rs around line 3073:

-    while let Some(t_char) = sql.get(pos) {
-        match t_char {
-            b' ' | b'\t' | b'\x0c' | b'\n' | b'\r' => {
+    while let Some(t_char) = sql.get(pos) {
+        match t_char {
+            // Skip all ASCII whitespace (space, tab, form-feed, newline, carriage-return, vertical-tab)
+            b' ' | b'\t' | b'\x0c' | b'\n' | b'\r' | b'\x0b' => {
                 pos += 1;
             }

And above the C-style comment logic, add:

// Note: Nested C-style comments (e.g., /* /* … */ */) are not supported,
// mirroring CPython’s sqlite3.complete_statement behavior.
🤖 Prompt for AI Agents
In stdlib/src/sqlite.rs around lines 3070 to 3118, update the whitespace
matching in the lstrip_sql function to include the ASCII vertical tab character
(b'\x0b') so it skips this whitespace as well. Add a comment above the C-style
comment handling code to document that nested C-style comments are not
supported, matching CPython's sqlite3.complete_statement behavior. Additionally,
create tests that cover nested C-style comments with and without trailing
semicolons to ensure this behavior is maintained.

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.

1 participant








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/RustPython/RustPython/pull/5996

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy