Patchwork [2,of,8] util: explicitly tests for None

login
register
mail settings
Submitter Pierre-Yves David
Date March 16, 2017, 11:28 a.m.
Message ID <6d73a06abb85612fca72.1489663686@nodosa.octopoid.net>
Download mbox | patch
Permalink /patch/19386/
State Accepted
Headers show

Comments

Pierre-Yves David - March 16, 2017, 11:28 a.m.
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david@ens-lyon.org>
# Date 1489615634 25200
#      Wed Mar 15 15:07:14 2017 -0700
# Node ID 6d73a06abb85612fca7231fb71f245f82f7d6863
# Parent  6c6d8c0cc67de1ecd744a30889419510aa2064d2
# EXP-Topic mutable-default
# Available At https://www.mercurial-scm.org/repo/users/marmoute/mercurial/
#              hg pull https://www.mercurial-scm.org/repo/users/marmoute/mercurial/ -r 6d73a06abb85
util: explicitly tests for None

Changeset 8b6927eb7efd removed the mutable default value, but did not explicitly
tested for None. Such implicit checking can introduce semantic and performance
issue. We move to an explicit check for None as recommended by PEP8:

https://www.python.org/dev/peps/pep-0008/#programming-recommendations
Augie Fackler - March 16, 2017, 5 p.m.
On Thu, Mar 16, 2017 at 04:28:06AM -0700, Pierre-Yves David wrote:
> # HG changeset patch
> # User Pierre-Yves David <pierre-yves.david@ens-lyon.org>
> # Date 1489615634 25200
> #      Wed Mar 15 15:07:14 2017 -0700
> # Node ID 6d73a06abb85612fca7231fb71f245f82f7d6863
> # Parent  6c6d8c0cc67de1ecd744a30889419510aa2064d2
> # EXP-Topic mutable-default
> # Available At https://www.mercurial-scm.org/repo/users/marmoute/mercurial/
> #              hg pull https://www.mercurial-scm.org/repo/users/marmoute/mercurial/ -r 6d73a06abb85
> util: explicitly tests for None

FYI, this one failed to apply, but I didn't investigate due to time
constraints on my end.

>
> Changeset 8b6927eb7efd removed the mutable default value, but did not explicitly
> tested for None. Such implicit checking can introduce semantic and performance
> issue. We move to an explicit check for None as recommended by PEP8:
>
> https://www.python.org/dev/peps/pep-0008/#programming-recommendations
>
> diff --git a/mercurial/util.py b/mercurial/util.py
> --- a/mercurial/util.py
> +++ b/mercurial/util.py
> @@ -1830,7 +1830,8 @@ def parsetimezone(s):
>  def strdate(string, format, defaults=None):
>      """parse a localized time string and return a (unixtime, offset) tuple.
>      if the string cannot be parsed, ValueError is raised."""
> -    defaults = defaults or []
> +    if defaults is None:
> +        defaults = []
>
>      # NOTE: unixtime = localunixtime + offset
>      offset, date = parsetimezone(string)
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel

Patch

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -1830,7 +1830,8 @@  def parsetimezone(s):
 def strdate(string, format, defaults=None):
     """parse a localized time string and return a (unixtime, offset) tuple.
     if the string cannot be parsed, ValueError is raised."""
-    defaults = defaults or []
+    if defaults is None:
+        defaults = []
 
     # NOTE: unixtime = localunixtime + offset
     offset, date = parsetimezone(string)