diff mercurial/util.py @ 32496:bb18728ea617

util: raise ParseError when parsing dates (BC) a7dce526c462 refactored util.parsedate in order to raise ValueError instead of Abort for using with ui.configwith. It causes several problems, putting arbitrary bytes in ValueError can cause issues with Python 3. Moreover, we added a function to convert ValueError exceptions back to Abort. A better approach would be to make parsedate raises ParseError, removing the convert function and update configwith to also catch ParseError. The side-effect is that error message when giving an invalid date in CLI change from: abort: invalid date: 'foo bar' to: hg: parse error: invalid date: 'foo bar' I'm not sure if it's an acceptable change, I found personally the error message more clear but more verbose too.
author Boris Feld <boris.feld@octobus.net>
date Wed, 24 May 2017 17:50:17 +0200
parents a7dce526c462
children 47ce079b1afa
line wrap: on
line diff
--- a/mercurial/util.py	Tue May 16 14:31:21 2017 -0700
+++ b/mercurial/util.py	Wed May 24 17:50:17 2017 +0200
@@ -1924,9 +1924,6 @@
     The date may be a "unixtime offset" string or in one of the specified
     formats. If the date already is a (unixtime, offset) tuple, it is returned.
 
-    This function calls rawparsedate and convert ValueError to Abort for
-    functions that needs higher level exception.
-
     >>> parsedate(' today ') == parsedate(\
                                   datetime.date.today().strftime('%b %d'))
     True
@@ -1941,20 +1938,6 @@
     >>> tz == strtz
     True
     """
-    try:
-        return rawparsedate(date, formats=formats, bias=bias)
-    except ValueError as exception:
-        raise Abort(str(exception))
-
-def rawparsedate(date, formats=None, bias=None):
-    """parse a localized date/time and return a (unixtime, offset) tuple.
-
-    The date may be a "unixtime offset" string or in one of the specified
-    formats. If the date already is a (unixtime, offset) tuple, it is returned.
-
-    See docstring of parsedate for example.
-    Raise ValueError for invalid date value.
-    """
     if bias is None:
         bias = {}
     if not date:
@@ -2001,15 +1984,15 @@
             else:
                 break
         else:
-            raise ValueError(_('invalid date: %r') % date)
+            raise error.ParseError(_('invalid date: %r') % date)
     # validate explicit (probably user-specified) date and
     # time zone offset. values must fit in signed 32 bits for
     # current 32-bit linux runtimes. timezones go from UTC-12
     # to UTC+14
     if when < -0x80000000 or when > 0x7fffffff:
-        raise ValueError(_('date exceeds 32 bits: %d') % when)
+        raise error.ParseError(_('date exceeds 32 bits: %d') % when)
     if offset < -50400 or offset > 43200:
-        raise ValueError(_('impossible time zone offset: %d') % offset)
+        raise error.ParseError(_('impossible time zone offset: %d') % offset)
     return when, offset
 
 def matchdate(date):