Ticket #21740: client_mutable_argument_fix_01.patch

File client_mutable_argument_fix_01.patch, 3.6 KB (added by sleepydragon, 8 years ago)
  • django/test/client.py

    From f433a63c9958124ba4e955e42cb1b8c1eeb8fae6 Mon Sep 17 00:00:00 2001
    From: Denver Coneybeare <denver@sleepydragon.org>
    Date: Tue, 7 Jan 2014 00:17:10 -0500
    Subject: [PATCH] django/test/client.py: replace mutable default arguments with
     immutable objects (ie. the None object)
    
    ---
     django/test/client.py | 30 ++++++++++++++++++++++++------
     1 file changed, 24 insertions(+), 6 deletions(-)
    
    diff --git a/django/test/client.py b/django/test/client.py
    index 9197a5a..11b9b23 100644
    a b class RequestFactory(object): 
    277277            path = path.encode('utf-8').decode('iso-8859-1')
    278278        return path
    279279
    280     def get(self, path, data={}, secure=False, **extra):
     280    def get(self, path, data=None, secure=False, **extra):
    281281        "Construct a GET request."
    282282
     283        if data is None:
     284            data = {}
     285
    283286        r = {
    284287            'QUERY_STRING': urlencode(data, doseq=True),
    285288        }
    286289        r.update(extra)
    287290        return self.generic('GET', path, secure=secure, **r)
    288291
    289     def post(self, path, data={}, content_type=MULTIPART_CONTENT,
     292    def post(self, path, data=None, content_type=MULTIPART_CONTENT,
    290293             secure=False, **extra):
    291294        "Construct a POST request."
    292295
     296        if data is None:
     297            data = {}
     298
    293299        post_data = self._encode_data(data, content_type)
    294300
    295301        return self.generic('POST', path, post_data, content_type,
    296302                            secure=secure, **extra)
    297303
    298     def head(self, path, data={}, secure=False, **extra):
     304    def head(self, path, data=None, secure=False, **extra):
    299305        "Construct a HEAD request."
    300306
     307        if data is None:
     308            data = {}
     309
    301310        r = {
    302311            'QUERY_STRING': urlencode(data, doseq=True),
    303312        }
    class Client(RequestFactory): 
    461470            signals.template_rendered.disconnect(dispatch_uid=signal_uid)
    462471            got_request_exception.disconnect(dispatch_uid="request-exception")
    463472
    464     def get(self, path, data={}, follow=False, secure=False, **extra):
     473    def get(self, path, data=None, follow=False, secure=False, **extra):
    465474        """
    466475        Requests a response from the server using GET.
    467476        """
     477        if data is None:
     478            data = {}
     479
    468480        response = super(Client, self).get(path, data=data, secure=secure,
    469481                                           **extra)
    470482        if follow:
    471483            response = self._handle_redirects(response, **extra)
    472484        return response
    473485
    474     def post(self, path, data={}, content_type=MULTIPART_CONTENT,
     486    def post(self, path, data=None, content_type=MULTIPART_CONTENT,
    475487             follow=False, secure=False, **extra):
    476488        """
    477489        Requests a response from the server using POST.
    478490        """
     491        if data is None:
     492            data = {}
     493
    479494        response = super(Client, self).post(path, data=data,
    480495                                            content_type=content_type,
    481496                                            secure=secure, **extra)
    class Client(RequestFactory): 
    483498            response = self._handle_redirects(response, **extra)
    484499        return response
    485500
    486     def head(self, path, data={}, follow=False, secure=False, **extra):
     501    def head(self, path, data=None, follow=False, secure=False, **extra):
    487502        """
    488503        Request a response from the server using HEAD.
    489504        """
     505        if data is None:
     506            data = {}
     507
    490508        response = super(Client, self).head(path, data=data, secure=secure,
    491509                                            **extra)
    492510        if follow:
Back to Top