Skip to content

Commit 3403b9b

Browse files
committed
Align SQL comment parsing with CPython
1 parent 799f38b commit 3403b9b

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
@@ -3068,36 +3068,52 @@ 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+
3073+
// This loop is borrowed from the SQLite source code.
3074+
while let Some(t_char) = sql.get(pos) {
3075+
match t_char {
30743076
b' ' | b'\t' | b'\x0c' | b'\n' | b'\r' => {
3075-
pos = &pos[1..];
3077+
// Skip whitespace.
3078+
pos += 1;
30763079
}
30773080
b'-' => {
3078-
if *pos.get(1)? == b'-' {
3079-
// line comments
3080-
pos = &pos[2..];
3081-
while *pos.first()? != b'\n' {
3082-
pos = &pos[1..];
3081+
// Skip line comments.
3082+
if sql.get(pos + 1) == Some(&b'-') {
3083+
pos += 2;
3084+
while let Some(&ch) = sql.get(pos) {
3085+
if ch == b'\n' {
3086+
break;
3087+
}
3088+
pos += 1;
30833089
}
3090+
let _ = sql.get(pos)?;
30843091
} else {
3085-
return Some(pos);
3092+
return Some(&sql[pos..]);
30863093
}
30873094
}
30883095
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..];
3096+
// Skip C style comments.
3097+
if sql.get(pos + 1) == Some(&b'*') {
3098+
pos += 2;
3099+
while let Some(&ch) = sql.get(pos) {
3100+
if ch == b'*' && sql.get(pos + 1) == Some(&b'/') {
3101+
break;
3102+
}
3103+
pos += 1;
30943104
}
3105+
let _ = sql.get(pos)?;
3106+
pos += 2;
30953107
} else {
3096-
return Some(pos);
3108+
return Some(&sql[pos..]);
30973109
}
30983110
}
3099-
_ => return Some(pos),
3111+
_ => {
3112+
return Some(&sql[pos..]);
3113+
}
31003114
}
31013115
}
3116+
3117+
None
31023118
}
31033119
}

0 commit comments

Comments
 (0)