Opened 8 years ago

Closed 7 years ago

#5367 closed defect (fixed)

python gotcha: mutable type in __init__()

Reported by: aagbsn Owned by: aagbsn
Priority: Medium Milestone:
Component: Circumvention/BridgeDB Version:
Severity: Keywords:
Cc: Actual Points:
Parent ID: Points:
Reviewer: Sponsor:

Description

In Server.py:53

http://www.ferg.org/projects/python_gotchas.html

init arguments are parsed only once, so the same list will be shared with _all_ instance objects.

Child Tickets

Change History (2)

comment:1 Changed 8 years ago by aagbsn

Status: newneeds_review

Simple fix:

diff --git a/lib/bridgedb/Server.py b/lib/bridgedb/Server.py
index a1b837a..b67edf2 100644
--- a/lib/bridgedb/Server.py
+++ b/lib/bridgedb/Server.py
@@ -50,7 +50,7 @@ class WebResource(twisted.web.resource.Resource):
     def __init__(self, distributor, schedule, N=1, useForwardedHeader=False,
                  includeFingerprints=True,
                  useRecaptcha=False,recaptchaPrivKey='', recaptchaPubKey='',
-                 domains=[]): 
+                 domains=None): 
         """Create a new WebResource.
              distributor -- an IPBasedDistributor object
              schedule -- an IntervalSchedule object
@@ -63,6 +63,9 @@ class WebResource(twisted.web.resource.Resource):
         self.nBridgesToGive = N
         self.useForwardedHeader = useForwardedHeader
         self.includeFingerprints = includeFingerprints
+
+        # do not use mutable types as __init__ defaults!
+        if not domains: domains = []
         self.domains = domains
 
         # recaptcha options

https://gitweb.torproject.org/user/aagbsn/bridgedb.git/commit/aba290f8e3a1eaa38bfd580d350e0721911b49da

comment:2 Changed 7 years ago by aagbsn

Resolution: fixed
Status: needs_reviewclosed
Note: See TracTickets for help on using tickets.