Opened 5 years ago

Closed 5 years ago

#15927 closed Bug (duplicate)

FORMAT_QMARK_REGEX in sqlite backend does not work as expected

Reported by: anonymous Owned by: nobody
Component: Database layer (models, ORM) Version: 1.3-rc
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX:


FORMAT_QMARK_REGEX is not correct (in django/db/backends/sqlite3/

It uses a lookahead assertion trying to not match if the char before the "%s" is not "%" but lookahead assertion can't be used to do that (it is for looking ahead not behind).


In the regex '(?!..)b' applied on 'abc' the '..' will match 'bc' so:
 -'(?!a)b','abc') => match 'b'
 -'(?!b)b','abc') => no match

I don't think this regex could be writen with lookhead assertions.

A possible solution could be:

re.compile(r'(^|[^%])%s').sub(r'\1?','%s %%s %s')

Attachments (1)

15927.patch (883 bytes) - added by aaugustin 5 years ago.

Download all attachments as: .zip

Change History (3)

comment:1 Changed 5 years ago by aaugustin

  • Easy pickings set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

Indeed, the current regex r'(?![^%])%s' is equivalent to r'%s'. The first part r'(?![^%])' will match if and only if it is not followed by r'[^%]', which is equivalent to being followed by %. This is always true since % is the next character in the regex.

This is much clearer with an example; I'm attaching a failing test case.

Changed 5 years ago by aaugustin

comment:2 Changed 5 years ago by aaugustin

  • Resolution set to duplicate
  • Status changed from new to closed

It's actually a duplicate of #15155 and #13648.

Note: See TracTickets for help on using tickets.
Back to Top