Skip to content

Commit 0224bcc

Browse files
committed
Eliminate mutable default parameters from methods
Fixed ----- * Eliminate mutable default parameters from methods. Because the parameters are passed to observers, it is possible for an observer to accidentally -- and permanently -- modify a default argument. ```python observers = [lambda x: x.append("bad")] def demo(groups=[]): print(groups) for observer in observers: observer(groups) demo() # [] demo() # ["bad"] demo() # ["bad", "bad"] ```
1 parent 0186f21 commit 0224bcc

File tree

6 files changed

+22
-9
lines changed

6 files changed

+22
-9
lines changed

src/smartcard/CardConnection.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def doTransmit(self, command, protocol):
187187
transmission."""
188188
return [], 0, 0
189189

190-
def control(self, controlCode, command=[]):
190+
def control(self, controlCode, command=None):
191191
"""Send a control command and buffer. Internally calls
192192
L{doControl()} class method and notify observers upon
193193
command/response events. Subclasses must override the
@@ -197,6 +197,8 @@ def control(self, controlCode, command=[]):
197197
198198
@param command: list of bytes to transmit
199199
"""
200+
if command is None:
201+
command = []
200202
Observable.setChanged(self)
201203
Observable.notifyObservers(
202204
self, CardConnectionEvent("command", [controlCode, command])

src/smartcard/CardConnectionDecorator.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,10 @@ def transmit(self, command, protocol=None):
8585
"""call inner component transmit"""
8686
return self.component.transmit(command, protocol)
8787

88-
def control(self, controlCode, command=[]):
88+
def control(self, controlCode, command=None):
8989
"""call inner component control"""
90+
if command is None:
91+
command = []
9092
return self.component.control(controlCode, command)
9193

9294
def getAttrib(self, attribId):

src/smartcard/System.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import smartcard.reader.ReaderFactory
2929

3030

31-
def readers(groups=[]):
31+
def readers(groups=None):
3232
"""Returns the list of smartcard readers in groups as
3333
L{smartcard.reader.Reader}.
3434
@@ -39,6 +39,8 @@ def readers(groups=[]):
3939
>>> r=smartcard.readers(['SCard$DefaultReaders', 'MyReaderGroup'])
4040
"""
4141

42+
if groups is None:
43+
groups = []
4244
return smartcard.reader.ReaderFactory.ReaderFactory.readers(groups)
4345

4446

src/smartcard/pcsc/PCSCCardConnection.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ def doTransmit(self, command, protocol=None):
285285
data = [(x + 256) % 256 for x in response[:-2]]
286286
return list(data), sw1, sw2
287287

288-
def doControl(self, controlCode, command=[]):
288+
def doControl(self, controlCode, command=None):
289289
"""Transmit a control command to the reader and return response.
290290
291291
@param controlCode: control command
@@ -294,6 +294,8 @@ def doControl(self, controlCode, command=[]):
294294
295295
@return: response are the response bytes (if any)
296296
"""
297+
if command is None:
298+
command = []
297299
CardConnection.doControl(self, controlCode, command)
298300
hresult, response = SCardControl(self.hcard, controlCode, command)
299301
if hresult != SCARD_S_SUCCESS:

src/smartcard/pcsc/PCSCReader.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,15 @@
3131
from smartcard.scard import *
3232

3333

34-
def __PCSCreaders__(hcontext, groups=[]):
34+
def __PCSCreaders__(hcontext, groups=None):
3535
"""Returns the list of PCSC smartcard readers in PCSC group.
3636
3737
If group is not specified, returns the list of all PCSC smartcard readers.
3838
"""
3939

40-
# in case we have a string instead of a list
41-
if isinstance(groups, str):
40+
if groups is None:
41+
groups = []
42+
elif isinstance(groups, str):
4243
groups = [groups]
4344
hresult, readers = SCardListReaders(hcontext, groups)
4445
if hresult != SCARD_S_SUCCESS:
@@ -104,7 +105,9 @@ def create(readername):
104105
return PCSCReader(readername)
105106

106107
@staticmethod
107-
def readers(groups=[]):
108+
def readers(groups=None):
109+
if groups is None:
110+
groups = []
108111
creaders = []
109112
hcontext = PCSCContext().getContext()
110113

src/smartcard/reader/ReaderFactory.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ def createReader(clazz, readername):
5353
return ReaderFactory.factories[clazz].create(readername)
5454

5555
@staticmethod
56-
def readers(groups=[]):
56+
def readers(groups=None):
57+
if groups is None:
58+
groups = []
5759
zreaders = []
5860
for fm in ReaderFactory.factorymethods:
5961
zreaders += fm(groups)

0 commit comments

Comments
 (0)