[elbe-devel] [PATCH 16/40] pylint: fix dangerous arguments

Torben Hohn torben.hohn at linutronix.de
Thu Sep 20 11:50:00 CEST 2018


On Fri, Sep 14, 2018 at 01:56:28PM +0200, Manuel Traut wrote:
> To a Python novice, this may seem like a reasonable way to default to an
> empty array for the extras param. What really happens is that this
> "default" array gets created as a persistent object, and every
> invocation of my_method that doesn't specify an extras param will be
> using that same list object—any changes to it will persist and be
> carried to every other invocation!
> 
> Signed-off-by: Manuel Traut <manut at linutronix.de>

Reviewed-by: Torben Hohn <torben.hohn at linutronix.de>

> ---
>  elbepack/filesystem.py    | 15 ++++++++++++---
>  elbepack/virtapt.py       |  9 +++++----
>  elbepack/xmlpreprocess.py |  4 +++-
>  3 files changed, 20 insertions(+), 8 deletions(-)
> 
> diff --git a/elbepack/filesystem.py b/elbepack/filesystem.py
> index 5fd97c60..e7ce208d 100644
> --- a/elbepack/filesystem.py
> +++ b/elbepack/filesystem.py
> @@ -120,13 +120,17 @@ class Filesystem(object):
>      def rmtree(self, path):
>          shutil.rmtree(self.fname(path))
>  
> -    def listdir(self, path='', ignore=[], skiplinks=False):
> +    def listdir(self, path='', ignore=None, skiplinks=False):
> +        if not ignore:
> +            ignore = []
> +
>          retval = [
>              os.path.join(
>                  self.path,
>                  path,
>                  x) for x in os.listdir(
>                  self.fname(path)) if x not in ignore]
> +
>          if skiplinks:
>              retval = [
>                  x for x in retval if (
> @@ -178,7 +182,10 @@ class Filesystem(object):
>              fp = self.open(fname, "w")
>              fp.close()
>  
> -    def walk_files(self, directory='', exclude_dirs=[]):
> +    def walk_files(self, directory='', exclude_dirs=None):
> +        if not exclude_dirs:
> +            exclude_dirs = []
> +
>          dirname = self.fname(directory)
>          if dirname == "/":
>              striplen = 0
> @@ -203,7 +210,9 @@ class Filesystem(object):
>                  realpath = os.path.join(dirpath, f)
>                  yield "/" + fpath, realpath
>  
> -    def mtime_snap(self, dirname='', exclude_dirs=[]):
> +    def mtime_snap(self, dirname='', exclude_dirs=None):
> +        if not exclude_dirs:
> +            exclude_dirs = []
>          mtime_index = {}
>  
>          for fpath, realpath in self.walk_files(dirname, exclude_dirs):
> diff --git a/elbepack/virtapt.py b/elbepack/virtapt.py
> index 62090115..085d23ef 100644
> --- a/elbepack/virtapt.py
> +++ b/elbepack/virtapt.py
> @@ -74,7 +74,7 @@ def lookup_uri(v, d, target_pkg):
>  
>  
>  class VirtApt(object):
> -    def __init__(self, arch, suite, sources, prefs, keylist=[], noauth=False):
> +    def __init__(self, arch, suite, sources, prefs, keylist=None, noauth=False):
>  
>          # pylint: disable=too-many-arguments
>  
> @@ -84,8 +84,9 @@ class VirtApt(object):
>          self.create_apt_sources_list(sources)
>          self.create_apt_prefs(prefs)
>          self.setup_gpg()
> -        for k in keylist:
> -            self.add_pubkey_url(k)
> +        if keylist:
> +            for k in keylist:
> +                self.add_pubkey_url(k)
>  
>          apt_pkg.config.set("APT::Architecture", arch)
>          apt_pkg.config.set("APT::Architectures", arch)
> @@ -273,7 +274,7 @@ class MyMan(BaseManager):
>  
>  MyMan.register("VirtRPCAPTCache", VirtApt)
>  
> -def get_virtaptcache(arch, suite, sources, prefs, keylist=[]):
> +def get_virtaptcache(arch, suite, sources, prefs, keylist=None):
>      mm = MyMan()
>      mm.start()
>  
> diff --git a/elbepack/xmlpreprocess.py b/elbepack/xmlpreprocess.py
> index fdbaee3f..39185ceb 100644
> --- a/elbepack/xmlpreprocess.py
> +++ b/elbepack/xmlpreprocess.py
> @@ -20,12 +20,14 @@ class XMLPreprocessError(Exception):
>      pass
>  
>  
> -def xmlpreprocess(fname, output, variants=[]):
> +def xmlpreprocess(fname, output, variants=None):
>  
>      # pylint: disable=too-many-locals
>      # pylint: disable=too-many-branches
>  
>      # first convert variants to a set
> +    if not variants:
> +        variants = []
>      variants = set(variants)
>  
>      schema_file = "https://www.linutronix.de/projects/Elbe/dbsfed.xsd"
> -- 
> 2.19.0.rc2
> 

-- 
Torben Hohn
Linutronix GmbH | Bahnhofstrasse 3 | D-88690 Uhldingen-Mühlhofen
Phone: +49 7556 25 999 18; Fax.: +49 7556 25 999 99

Hinweise zum Datenschutz finden Sie hier (Informations on data privacy 
can be found here): https://linutronix.de/kontakt/Datenschutz.php

Linutronix GmbH | Firmensitz (Registered Office): Uhldingen-Mühlhofen | 
Registergericht (Registration Court): Amtsgericht Freiburg i.Br., HRB700 
806 | Geschäftsführer (Managing Directors): Heinz Egger, Thomas Gleixner
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: not available
URL: <http://lists.linutronix.de/pipermail/elbe-devel/attachments/20180920/2b3fbdb5/attachment.sig>


More information about the elbe-devel mailing list