Patchwork [05,of,10] py3: convert date and format arguments str before passing in time.strptime

login
register
mail settings
Submitter Pulkit Goyal
Date May 6, 2017, 3:01 a.m.
Message ID <1a38d712f305b2dd5f2b.1494039677@pulkit-goyal>
Download mbox | patch
Permalink /patch/20473/
State Accepted
Headers show

Comments

Pulkit Goyal - May 6, 2017, 3:01 a.m.
# HG changeset patch
# User Pulkit Goyal <7895pulkit@gmail.com>
# Date 1494026485 -19800
#      Sat May 06 04:51:25 2017 +0530
# Node ID 1a38d712f305b2dd5f2b2833cd78815ec3b383fd
# Parent  cfc021a731d59f713ffa1c54eb855d680125168b
py3: convert date and format arguments str before passing in time.strptime

time.strptime() raises ValueError if the arguments are not str.
Source Code: https://hg.python.org/cpython/file/3.5/Lib/_strptime.py#l307
Yuya Nishihara - May 7, 2017, 2:14 a.m.
On Sat, 06 May 2017 08:31:17 +0530, Pulkit Goyal wrote:
> # HG changeset patch
> # User Pulkit Goyal <7895pulkit@gmail.com>
> # Date 1494026485 -19800
> #      Sat May 06 04:51:25 2017 +0530
> # Node ID 1a38d712f305b2dd5f2b2833cd78815ec3b383fd
> # Parent  cfc021a731d59f713ffa1c54eb855d680125168b
> py3: convert date and format arguments str before passing in time.strptime
> 
> time.strptime() raises ValueError if the arguments are not str.
> Source Code: https://hg.python.org/cpython/file/3.5/Lib/_strptime.py#l307
> 
> diff --git a/mercurial/util.py b/mercurial/util.py
> --- a/mercurial/util.py
> +++ b/mercurial/util.py
> @@ -1923,7 +1923,7 @@
>              # elements are relative to today
>              usenow = True
>  
> -    timetuple = time.strptime(date, format)
> +    timetuple = time.strptime(pycompat.sysstr(date), pycompat.sysstr(format))

Perhaps date string is locale dependent and may contain a valid 8-bit character.
encoding.strfromlocal() can be used instead.

Patch

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -1923,7 +1923,7 @@ 
             # elements are relative to today
             usenow = True
 
-    timetuple = time.strptime(date, format)
+    timetuple = time.strptime(pycompat.sysstr(date), pycompat.sysstr(format))
     localunixtime = int(calendar.timegm(timetuple))
     if offset is None:
         # local timezone