Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #20462 -- null/non-string regex lookups are now consistent

Thanks to noirbizarre for the report and initial patch.
  • Loading branch information...
commit 273dc550a4eccdad958541f456332312b3369504 1 parent c6862d5
amclark authored June 26, 2013 timgraham committed June 26, 2013
2  AUTHORS
@@ -152,6 +152,7 @@ answer newbie questions, and generally made Django that much better:
152 152
     Antonis Christofides <anthony@itia.ntua.gr>
153 153
     Michal Chruszcz <troll@pld-linux.org>
154 154
     Can Burak Çilingir <canburak@cs.bilgi.edu.tr>
  155
+    Andrew Clark <amclark7@gmail.com>
155 156
     Ian Clelland <clelland@gmail.com>
156 157
     Travis Cline <travis.cline@gmail.com>
157 158
     Russell Cloran <russell@rucus.net>
@@ -273,6 +274,7 @@ answer newbie questions, and generally made Django that much better:
273 274
     Brian Harring <ferringb@gmail.com>
274 275
     Brant Harris
275 276
     Ronny Haryanto <http://ronny.haryan.to/>
  277
+    Axel Haustant <noirbizarre@gmail.com>
276 278
     Hawkeye
277 279
     Kent Hauser <kent@khauser.net>
278 280
     Joe Heck <http://www.rhonabwy.com/wp/>
2  django/db/backends/postgresql_psycopg2/operations.py
@@ -69,7 +69,7 @@ def lookup_cast(self, lookup_type):
69 69
 
70 70
         # Cast text lookups to text to allow things like filter(x__contains=4)
71 71
         if lookup_type in ('iexact', 'contains', 'icontains', 'startswith',
72  
-                           'istartswith', 'endswith', 'iendswith'):
  72
+                           'istartswith', 'endswith', 'iendswith', 'regex', 'iregex'):
73 73
             lookup = "%s::text"
74 74
 
75 75
         # Use UPPER(x) for case-insensitive lookups; it's faster.
2  django/db/backends/sqlite3/base.py
@@ -522,4 +522,4 @@ def _sqlite_format_dtdelta(dt, conn, days, secs, usecs):
522 522
     return str(dt)
523 523
 
524 524
 def _sqlite_regexp(re_pattern, re_string):
525  
-    return bool(re.search(re_pattern, re_string))
  525
+    return bool(re.search(re_pattern, str(re_string))) if re_string is not None else False
15  tests/lookup/tests.py
@@ -610,6 +610,21 @@ def test_regex_backreferencing(self):
610 610
         self.assertQuerysetEqual(Article.objects.filter(headline__regex=r'b(.).*b\1'),
611 611
             ['<Article: barfoobaz>', '<Article: bazbaRFOO>', '<Article: foobarbaz>'])
612 612
 
  613
+    def test_regex_null(self):
  614
+        """
  615
+        Ensure that a regex lookup does not fail on null/None values
  616
+        """
  617
+        Season.objects.create(year=2012, gt=None)
  618
+        self.assertQuerysetEqual(Season.objects.filter(gt__regex=r'^$'), [])
  619
+
  620
+    def test_regex_non_string(self):
  621
+        """
  622
+        Ensure that a regex lookup does not fail on non-string fields
  623
+        """
  624
+        Season.objects.create(year=2013, gt=444)
  625
+        self.assertQuerysetEqual(Season.objects.filter(gt__regex=r'^444$'),
  626
+            ['<Season: 2013>'])
  627
+
613 628
     def test_nonfield_lookups(self):
614 629
         """
615 630
         Ensure that a lookup query containing non-fields raises the proper

0 notes on commit 273dc55

Please sign in to comment.
Something went wrong with that request. Please try again.