Rev 4192: (mbp) better error on bad regexps in file:///home/pqm/archives/thelove/bzr/%2Btrunk/

Canonical.com Patch Queue Manager pqm at pqm.ubuntu.com
Tue Mar 24 04:25:01 GMT 2009


At file:///home/pqm/archives/thelove/bzr/%2Btrunk/

------------------------------------------------------------
revno: 4192
revision-id: pqm at pqm.ubuntu.com-20090324042457-53xouxxcxehxoi5v
parent: pqm at pqm.ubuntu.com-20090324034116-embqt47gflj9vv54
parent: mbp at sourcefrog.net-20090324014741-deqmumc0lq5w5v9a
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Tue 2009-03-24 04:24:57 +0000
message:
  (mbp) better error on bad regexps
modified:
  NEWS                           NEWS-20050323055033-4e00b5db738777ff
  bzrlib/log.py                  log.py-20050505065812-c40ce11702fe5fb1
  bzrlib/osutils.py              osutils.py-20050309040759-eeaff12fbf77ac86
  bzrlib/tests/__init__.py       selftest.py-20050531073622-8d0e3c8845c97a64
  bzrlib/tests/blackbox/test_log.py test_log.py-20060112090212-78f6ea560c868e24
  bzrlib/tests/test_osutils.py   test_osutils.py-20051201224856-e48ee24c12182989
    ------------------------------------------------------------
    revno: 4183.6.5
    revision-id: mbp at sourcefrog.net-20090324014741-deqmumc0lq5w5v9a
    parent: mbp at sourcefrog.net-20090324014350-z4qg62nv8ly1sox1
    committer: Martin Pool <mbp at sourcefrog.net>
    branch nick: 251352-re-compile
    timestamp: Tue 2009-03-24 12:47:41 +1100
    message:
      selftest should use re_compile_checked too
    modified:
      bzrlib/tests/__init__.py       selftest.py-20050531073622-8d0e3c8845c97a64
    ------------------------------------------------------------
    revno: 4183.6.4
    revision-id: mbp at sourcefrog.net-20090324014350-z4qg62nv8ly1sox1
    parent: mbp at sourcefrog.net-20090324004319-1ppkkdgsztr3mdph
    committer: Martin Pool <mbp at sourcefrog.net>
    branch nick: 251352-re-compile
    timestamp: Tue 2009-03-24 12:43:50 +1100
    message:
      Separate out re_compile_checked
    modified:
      bzrlib/log.py                  log.py-20050505065812-c40ce11702fe5fb1
      bzrlib/osutils.py              osutils.py-20050309040759-eeaff12fbf77ac86
      bzrlib/tests/blackbox/test_log.py test_log.py-20060112090212-78f6ea560c868e24
      bzrlib/tests/test_osutils.py   test_osutils.py-20051201224856-e48ee24c12182989
    ------------------------------------------------------------
    revno: 4183.6.3
    revision-id: mbp at sourcefrog.net-20090324004319-1ppkkdgsztr3mdph
    parent: mbp at sourcefrog.net-20090324004225-k4rjgjq3ek05x0cc
    committer: Martin Pool <mbp at sourcefrog.net>
    branch nick: 251352-re-compile
    timestamp: Tue 2009-03-24 11:43:19 +1100
    message:
      NEWS for 251352
    modified:
      NEWS                           NEWS-20050323055033-4e00b5db738777ff
    ------------------------------------------------------------
    revno: 4183.6.2
    revision-id: mbp at sourcefrog.net-20090324004225-k4rjgjq3ek05x0cc
    parent: mbp at sourcefrog.net-20090324004028-boqgqdpc58ku3eui
    committer: Martin Pool <mbp at sourcefrog.net>
    branch nick: 251352-re-compile
    timestamp: Tue 2009-03-24 11:42:25 +1100
    message:
      Add better message and test for 251352
    modified:
      bzrlib/log.py                  log.py-20050505065812-c40ce11702fe5fb1
      bzrlib/tests/blackbox/test_log.py test_log.py-20060112090212-78f6ea560c868e24
    ------------------------------------------------------------
    revno: 4183.6.1
    revision-id: mbp at sourcefrog.net-20090324004028-boqgqdpc58ku3eui
    parent: pqm at pqm.ubuntu.com-20090323043327-txb4jri5i3gssuj5
    committer: Martin Pool <mbp at sourcefrog.net>
    branch nick: 251352-re-compile
    timestamp: Tue 2009-03-24 11:40:28 +1100
    message:
      Better message if log -m option is not a valid RE
    modified:
      bzrlib/log.py                  log.py-20050505065812-c40ce11702fe5fb1
=== modified file 'NEWS'
--- a/NEWS	2009-03-24 02:56:17 +0000
+++ b/NEWS	2009-03-24 04:24:57 +0000
@@ -79,6 +79,9 @@
   authentication section name, username and encoded password were 
   provided. (Jean-Francois Roy)
 
+* bzr gives a better message if an invalid regexp is passed to ``bzr log
+  -m``.  (Anne Mohsen, Martin Pool)
+
 * Fix "is not a stackable format" error when pushing a
   stackable-format branch with an unstackable-format repository to a
   destination with a default stacking policy.  (Andrew Bennetts)

=== modified file 'bzrlib/log.py'
--- a/bzrlib/log.py	2009-03-23 10:06:00 +0000
+++ b/bzrlib/log.py	2009-03-24 04:24:57 +0000
@@ -82,6 +82,7 @@
 from bzrlib.osutils import (
     format_date,
     get_terminal_encoding,
+    re_compile_checked,
     terminal_width,
     )
 
@@ -587,8 +588,8 @@
     """
     if search is None:
         return log_rev_iterator
-    # Compile the search now to get early errors.
-    searchRE = re.compile(search, re.IGNORECASE)
+    searchRE = re_compile_checked(search, re.IGNORECASE,
+            'log message filter')
     return _filter_message_re(searchRE, log_rev_iterator)
 
 

=== modified file 'bzrlib/osutils.py'
--- a/bzrlib/osutils.py	2009-03-18 23:43:51 +0000
+++ b/bzrlib/osutils.py	2009-03-24 01:43:50 +0000
@@ -1,4 +1,4 @@
-# Copyright (C) 2005, 2006, 2007 Canonical Ltd
+# Copyright (C) 2005, 2006, 2007, 2009 Canonical Ltd
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -1740,6 +1740,28 @@
                 continue
             raise
 
+def re_compile_checked(re_string, flags=0, where=""):
+    """Return a compiled re, or raise a sensible error.
+    
+    This should only be used when compiling user-supplied REs.
+
+    :param re_string: Text form of regular expression.
+    :param flags: eg re.IGNORECASE
+    :param where: Message explaining to the user the context where 
+        it occurred, eg 'log search filter'.
+    """
+    # from https://bugs.launchpad.net/bzr/+bug/251352
+    try:
+        re_obj = re.compile(re_string, flags)
+        re_obj.search("")
+        return re_obj
+    except re.error, e:
+        if where:
+            where = ' in ' + where
+        # despite the name 'error' is a type
+        raise errors.BzrCommandError('Invalid regular expression%s: %r: %s'
+            % (where, re_string, e))
+
 
 if sys.platform == "win32":
     import msvcrt

=== modified file 'bzrlib/tests/__init__.py'
--- a/bzrlib/tests/__init__.py	2009-03-23 10:14:10 +0000
+++ b/bzrlib/tests/__init__.py	2009-03-24 04:24:57 +0000
@@ -2434,7 +2434,8 @@
     :param pattern: A regular expression string.
     :return: A callable that returns True if the re matches.
     """
-    filter_re = re.compile(pattern)
+    filter_re = osutils.re_compile_checked(pattern, 0,
+        'test filter')
     def condition(test):
         test_id = test.id()
         return filter_re.search(test_id)

=== modified file 'bzrlib/tests/blackbox/test_log.py'
--- a/bzrlib/tests/blackbox/test_log.py	2009-03-11 08:58:17 +0000
+++ b/bzrlib/tests/blackbox/test_log.py	2009-03-24 01:43:50 +0000
@@ -1,5 +1,4 @@
-# Copyright (C) 2005, 2006, 2007 Canonical Ltd
-# -*- coding: utf-8 -*-
+# Copyright (C) 2005, 2006, 2007, 2009 Canonical Ltd
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -243,6 +242,18 @@
         self.assertContainsRe(log, r'revno: 2\n')
         self.assertContainsRe(log, r'revno: 3\n')
 
+    def test_log_bad_message_re(self):
+        """Bad --message argument gives a sensible message
+        
+        See https://bugs.launchpad.net/bzr/+bug/251352
+        """
+        self._prepare()
+        out, err = self.run_bzr(['log', '-m', '*'], retcode=3)
+        self.assertEqual("bzr: ERROR: Invalid regular expression"
+            " in log message filter"
+            ": '*'"
+            ": nothing to repeat\n", err)
+        self.assertEqual('', out)
 
 class TestLogVerbose(TestCaseWithTransport):
 

=== modified file 'bzrlib/tests/test_osutils.py'
--- a/bzrlib/tests/test_osutils.py	2009-03-18 23:43:51 +0000
+++ b/bzrlib/tests/test_osutils.py	2009-03-24 01:43:50 +0000
@@ -19,6 +19,7 @@
 from cStringIO import StringIO
 import errno
 import os
+import re
 import socket
 import stat
 import sys
@@ -1525,3 +1526,21 @@
             'yyy.xx')
         # test unknown resource
         self.assertRaises(IOError, osutils.resource_string, 'bzrlib', 'yyy.xx')
+
+
+class TestReCompile(TestCase):
+
+    def test_re_compile_checked(self):
+        r = osutils.re_compile_checked(r'A*', re.IGNORECASE)
+        self.assertTrue(r.match('aaaa'))
+        self.assertTrue(r.match('aAaA'))
+
+    def test_re_compile_checked_error(self):
+        # like https://bugs.launchpad.net/bzr/+bug/251352
+        err = self.assertRaises(
+            errors.BzrCommandError,
+            osutils.re_compile_checked, '*', re.IGNORECASE, 'test case')
+        self.assertEqual(
+            "Invalid regular expression in test case: '*': "
+            "nothing to repeat",
+            str(err))




More information about the bazaar-commits mailing list