Patchwork util: explicitly tests for None

login
register
mail settings
Submitter Pierre-Yves David
Date March 16, 2017, 6:16 p.m.
Message ID <4f99a4fb0482a44178f9.1489688161@nodosa.octopoid.net>
Download mbox | patch
Permalink /patch/19396/
State Accepted
Headers show

Comments

Pierre-Yves David - March 16, 2017, 6:16 p.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 4f99a4fb0482a44178f96c29678ef4630056ff4e
# Parent  a5bad127128d8f60060be53d161acfa7a32a17d5
# 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 4f99a4fb0482
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
Pierre-Yves David - March 16, 2017, 6:41 p.m.
That one is a V2, but I failed to invoque `hg email` correctly :-(

On 03/16/2017 11:16 AM, 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 4f99a4fb0482a44178f96c29678ef4630056ff4e
> # Parent  a5bad127128d8f60060be53d161acfa7a32a17d5
> # 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 4f99a4fb0482
> 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
>
> diff --git a/mercurial/util.py b/mercurial/util.py
> --- a/mercurial/util.py
> +++ b/mercurial/util.py
> @@ -1831,7 +1831,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
>
Yuya Nishihara - March 17, 2017, 12:38 p.m.
On Thu, 16 Mar 2017 11:16:01 -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 4f99a4fb0482a44178f96c29678ef4630056ff4e
> # Parent  a5bad127128d8f60060be53d161acfa7a32a17d5
> # 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 4f99a4fb0482
> util: explicitly tests for None

Queued, thanks.

Patch

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -1831,7 +1831,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)