[elbe-devel] [PATCH 09/13] pylint: fix dangerous arguments

Manuel Traut manut at linutronix.de
Wed Aug 29 21:07:30 CEST 2018


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>
---
 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 0c6b4902..b05ac547 100644
--- a/elbepack/xmlpreprocess.py
+++ b/elbepack/xmlpreprocess.py
@@ -21,12 +21,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.18.0




More information about the elbe-devel mailing list