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:

Description

FORMAT_QMARK_REGEX is not correct (in django/db/backends/sqlite3/base.py).

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).

Example:

In the regex '(?!..)b' applied on 'abc' the '..' will match 'bc' so:
 - re.search(r'(?!a)b','abc') => match 'b'
 - re.search(r'(?!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 Aymeric Augustin 5 years ago.

Download all attachments as: .zip

Change History (3)

comment:1 Changed 5 years ago by Aymeric Augustin

Easy pickings: set
Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Triage Stage: UnreviewedAccepted

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 Aymeric Augustin

Attachment: 15927.patch added

comment:2 Changed 5 years ago by Aymeric Augustin

Resolution: duplicate
Status: newclosed

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

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