Content-Length: 452286 | pFad | http://github.com/RustPython/RustPython/commit/5cd5b4dfdda34d80a7a88f338517cb3ed955a0b6

50 Align SQL comment parsing with CPython · RustPython/RustPython@5cd5b4d · GitHub
Skip to content

Commit 5cd5b4d

Browse files
committed
Align SQL comment parsing with CPython
1 parent 799f38b commit 5cd5b4d

File tree

2 files changed

+39
-23
lines changed

2 files changed

+39
-23
lines changed

Lib/test/test_sqlite3/test_dbapi.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -769,8 +769,6 @@ def test_execute_illegal_sql(self):
769769
with self.assertRaises(sqlite.OperationalError):
770770
self.cu.execute("select asdf")
771771

772-
# TODO: RUSTPYTHON
773-
@unittest.expectedFailure
774772
def test_execute_multiple_statements(self):
775773
msg = "You can only execute one statement at a time"
776774
dataset = (
@@ -793,8 +791,6 @@ def test_execute_multiple_statements(self):
793791
with self.assertRaisesRegex(sqlite.ProgrammingError, msg):
794792
self.cu.execute(query)
795793

796-
# TODO: RUSTPYTHON
797-
@unittest.expectedFailure
798794
def test_execute_with_appended_comments(self):
799795
dataset = (
800796
"select 1; -- foo bar",
@@ -963,8 +959,6 @@ def test_rowcount_update_returning(self):
963959
self.assertEqual(self.cu.fetchone()[0], 1)
964960
self.assertEqual(self.cu.rowcount, 1)
965961

966-
# TODO: RUSTPYTHON
967-
@unittest.expectedFailure
968962
def test_rowcount_prefixed_with_comment(self):
969963
# gh-79579: rowcount is updated even if query is prefixed with comments
970964
self.cu.execute("""

stdlib/src/sqlite.rs

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3068,36 +3068,58 @@ mod _sqlite {
30683068
}
30693069

30703070
fn lstrip_sql(sql: &[u8]) -> Option<&[u8]> {
3071-
let mut pos = sql;
3072-
loop {
3073-
match pos.first()? {
3071+
let mut pos = 0;
3072+
while let Some(current_char) = sql.get(pos) {
3073+
match current_char {
30743074
b' ' | b'\t' | b'\x0c' | b'\n' | b'\r' => {
3075-
pos = &pos[1..];
3075+
// Skip whitespace.
3076+
pos += 1;
30763077
}
30773078
b'-' => {
3078-
if *pos.get(1)? == b'-' {
3079-
// line comments
3080-
pos = &pos[2..];
3081-
while *pos.first()? != b'\n' {
3082-
pos = &pos[1..];
3079+
// Skip line comments.
3080+
if sql.get(pos + 1) == Some(&b'-') {
3081+
pos += 2;
3082+
while let Some(ch) = sql.get(pos) {
3083+
if *ch == b'\n' {
3084+
break;
3085+
}
3086+
pos += 1;
3087+
}
3088+
// CPython: if (pos[0] == '\0') return NULL;
3089+
if sql.get(pos).is_none() {
3090+
return None;
30833091
}
3092+
// CPython: continue; (The outer for loop handles pos++)
3093+
// Rust: The outer while loop will continue naturally
30843094
} else {
3085-
return Some(pos);
3095+
return Some(&sql[pos..]);
30863096
}
30873097
}
30883098
b'/' => {
3089-
if *pos.get(1)? == b'*' {
3090-
// c style comments
3091-
pos = &pos[2..];
3092-
while *pos.first()? != b'*' || *pos.get(1)? != b'/' {
3093-
pos = &pos[1..];
3099+
// Skip C style comments.
3100+
if sql.get(pos + 1) == Some(&b'*') {
3101+
pos += 2;
3102+
while let Some(ch) = sql.get(pos) {
3103+
if *ch == b'*' && sql.get(pos + 1) == Some(&b'/') {
3104+
break;
3105+
}
3106+
pos += 1;
30943107
}
3108+
// CPython: if (pos[0] == '\0') return NULL;
3109+
if sql.get(pos).is_none() {
3110+
return None; // Unclosed comment
3111+
}
3112+
// CPython: pos++; continue; (Handles '*' then '/' in the next loop)
3113+
pos += 2; // Skip "*/"
30953114
} else {
3096-
return Some(pos);
3115+
return Some(&sql[pos..]);
30973116
}
30983117
}
3099-
_ => return Some(pos),
3118+
_ => {
3119+
return Some(&sql[pos..]);
3120+
}
31003121
}
31013122
}
3123+
None
31023124
}
31033125
}

0 commit comments

Comments
 (0)








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/commit/5cd5b4dfdda34d80a7a88f338517cb3ed955a0b6

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy