From 14f7b6068417ae249cd48e543a70ae807e113eef Mon Sep 17 00:00:00 2001 From: Sam Stavinoha Date: Thu, 10 Jul 2014 20:33:37 +0000 Subject: [PATCH] default kwargs are evaluated at definition time MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This stackoverflow question covers the topic pretty well: http://stackoverflow.com/q/1132941/1547030 From docs.python.org: Default parameter values are evaluated when the function definition is executed. This means that the expression is evaluated once, when the function is defined, and that the same “pre-computed” value is used for each call. This is especially important to understand when a default parameter is a mutable object, such as a list or a dictionary: if the function modifies the object (e.g. by appending an item to a list), the default value is in effect modified. This is generally not what was intended. A way around this is to use None as the default, and explicitly test for it in the body of the function --- pythonwhois/net.py | 5 +++-- pythonwhois/parse.py | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/pythonwhois/net.py b/pythonwhois/net.py index 156ddde..4bebccc 100644 --- a/pythonwhois/net.py +++ b/pythonwhois/net.py @@ -2,7 +2,9 @@ import socket, re, sys from codecs import encode, decode from . import shared -def get_whois_raw(domain, server="", previous=[], rfc3490=True, never_cut=False, with_server_list=False, server_list=[]): +def get_whois_raw(domain, server="", previous=None, rfc3490=True, never_cut=False, with_server_list=False, server_list=None): + previous = previous or [] + server_list = server_list or [] # Sometimes IANA simply won't give us the right root WHOIS server exceptions = { ".ac.uk": "whois.ja.net", @@ -20,7 +22,6 @@ def get_whois_raw(domain, server="", previous=[], rfc3490=True, never_cut=False, if len(previous) == 0 and server == "": # Root query - server_list = [] # Otherwise it retains the list on subsequent queries, for some reason. is_exception = False for exception, exc_serv in exceptions.items(): if domain.endswith(exception): diff --git a/pythonwhois/parse.py b/pythonwhois/parse.py index 6a96c06..666340b 100644 --- a/pythonwhois/parse.py +++ b/pythonwhois/parse.py @@ -426,7 +426,8 @@ else: return isinstance(data, str) -def parse_raw_whois(raw_data, normalized=[], never_query_handles=True, handle_server=""): +def parse_raw_whois(raw_data, normalized=None, never_query_handles=True, handle_server=""): + normalized = normalized or [] data = {} raw_data = [segment.replace("\r", "") for segment in raw_data] # Carriage returns are the devil @@ -995,4 +996,4 @@ def parse_nic_contact(data): for match in matches: handle_contacts.append(match.groupdict()) - return handle_contacts \ No newline at end of file + return handle_contacts