Change in pysim[master]: pySim/filesystem: fix mutable default list/dict arguments

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

laforge gerrit-no-reply at lists.osmocom.org
Sat Mar 27 18:28:43 UTC 2021


laforge has submitted this change. ( https://gerrit.osmocom.org/c/pysim/+/23513 )

Change subject: pySim/filesystem: fix mutable default list/dict arguments
......................................................................

pySim/filesystem: fix mutable default list/dict arguments

Having lists and dictionaries as default argument values is a bad
idea, because the same instance of list/dict will be used by all
objects instantiated using such constructor:

  def appendItem(itemName, itemList=[]):
      itemList.append(itemName)
      return itemList

  print(appendItem('notebook'))
  print(appendItem('pencil'))
  print(appendItem('eraser'))

Output:

  ['notebook']
  ['notebook', 'pencil']
  ['notebook', 'pencil', 'eraser']

Change-Id: I83d718ff9c3ff6aef47930f38d7f50424f9b880f
---
M pySim/filesystem.py
1 file changed, 8 insertions(+), 8 deletions(-)

Approvals:
  laforge: Looks good to me, approved
  Jenkins Builder: Verified



diff --git a/pySim/filesystem.py b/pySim/filesystem.py
index a1bb127..04fa250 100644
--- a/pySim/filesystem.py
+++ b/pySim/filesystem.py
@@ -706,10 +706,10 @@
 class CardApplication(object):
     """A card application is represented by an ADF (with contained hierarchy) and optionally
        some SW definitions."""
-    def __init__(self, name, adf=None, sw={}):
+    def __init__(self, name, adf=None, sw=None):
         self.name = name
         self.adf = adf
-        self.sw = sw
+        self.sw = sw or dict()
 
     def __str__(self):
         return "APP(%s)" % (self.name)
@@ -723,13 +723,13 @@
     """A Card Profile describes a card, it's filessystem hierarchy, an [initial] list of
        applications as well as profile-specific SW and shell commands.  Every card has
        one card profile, but there may be multiple applications within that profile."""
-    def __init__(self, name, desc=None, files_in_mf=[], sw=[], applications=[], shell_cmdsets=[]):
+    def __init__(self, name, **kw):
         self.name = name
-        self.desc = desc
-        self.files_in_mf = files_in_mf
-        self.sw = sw
-        self.applications = applications
-        self.shell_cmdsets = shell_cmdsets
+        self.desc = kw.get("desc", None)
+        self.files_in_mf = kw.get("files_in_mf", [])
+        self.sw = kw.get("sw", [])
+        self.applications = kw.get("applications", [])
+        self.shell_cmdsets = kw.get("shell_cmdsets", [])
 
     def __str__(self):
         return self.name

-- 
To view, visit https://gerrit.osmocom.org/c/pysim/+/23513
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: I83d718ff9c3ff6aef47930f38d7f50424f9b880f
Gerrit-Change-Number: 23513
Gerrit-PatchSet: 4
Gerrit-Owner: fixeria <vyanitskiy at sysmocom.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: dexter <pmaier at sysmocom.de>
Gerrit-Reviewer: laforge <laforge at osmocom.org>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20210327/105c161e/attachment.htm>


More information about the gerrit-log mailing list