Skip to content

Commit 6f80ac0

Browse files
authored
Align SQL comment parsing with CPython (RustPython#5996)
1 parent d7a9b69 commit 6f80ac0

File tree

2 files changed

+33
-23
lines changed

2 files changed

+33
-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: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3081,36 +3081,52 @@ mod _sqlite {
30813081
}
30823082

30833083
fn lstrip_sql(sql: &[u8]) -> Option<&[u8]> {
3084-
let mut pos = sql;
3085-
loop {
3086-
match pos.first()? {
3084+
let mut pos = 0;
3085+
3086+
// This loop is borrowed from the SQLite source code.
3087+
while let Some(t_char) = sql.get(pos) {
3088+
match t_char {
30873089
b' ' | b'\t' | b'\x0c' | b'\n' | b'\r' => {
3088-
pos = &pos[1..];
3090+
// Skip whitespace.
3091+
pos += 1;
30893092
}
30903093
b'-' => {
3091-
if *pos.get(1)? == b'-' {
3092-
// line comments
3093-
pos = &pos[2..];
3094-
while *pos.first()? != b'\n' {
3095-
pos = &pos[1..];
3094+
// Skip line comments.
3095+
if sql.get(pos + 1) == Some(&b'-') {
3096+
pos += 2;
3097+
while let Some(&ch) = sql.get(pos) {
3098+
if ch == b'\n' {
3099+
break;
3100+
}
3101+
pos += 1;
30963102
}
3103+
let _ = sql.get(pos)?;
30973104
} else {
3098-
return Some(pos);
3105+
return Some(&sql[pos..]);
30993106
}
31003107
}
31013108
b'/' => {
3102-
if *pos.get(1)? == b'*' {
3103-
// c style comments
3104-
pos = &pos[2..];
3105-
while *pos.first()? != b'*' || *pos.get(1)? != b'/' {
3106-
pos = &pos[1..];
3109+
// Skip C style comments.
3110+
if sql.get(pos + 1) == Some(&b'*') {
3111+
pos += 2;
3112+
while let Some(&ch) = sql.get(pos) {
3113+
if ch == b'*' && sql.get(pos + 1) == Some(&b'/') {
3114+
break;
3115+
}
3116+
pos += 1;
31073117
}
3118+
let _ = sql.get(pos)?;
3119+
pos += 2;
31083120
} else {
3109-
return Some(pos);
3121+
return Some(&sql[pos..]);
31103122
}
31113123
}
3112-
_ => return Some(pos),
3124+
_ => {
3125+
return Some(&sql[pos..]);
3126+
}
31133127
}
31143128
}
3129+
3130+
None
31153131
}
31163132
}

0 commit comments

Comments
 (0)