Ticket #1201: 1201.diff

File 1201.diff, 6.6 KB (added by arien <regexbot@…>, 17 years ago)
  • django/core/cache/__init__.py

     
    1515See docs/cache.txt for information on the public API.
    1616"""
    1717
    18 from cgi import parse_qsl
    1918from django.conf import settings
    2019from django.core.cache.backends.base import InvalidCacheBackendError
     20# For bugfixes not in Python < 2.3.5
     21from django.utils._cgi import parse_qsl
    2122
    2223BACKENDS = {
    2324    # name for use in settings file --> name of module in "backends" directory
     
    4142    host = rest[2:]
    4243    qpos = rest.find('?')
    4344    if qpos != -1:
    44         params = dict(parse_qsl(rest[qpos+1:]))
     45        params = dict(parse_qsl(rest[qpos+1:], True)) # keep_blank_values=True
    4546        host = rest[2:qpos]
    4647    else:
    4748        params = {}
  • django/http/__init__.py

     
    1313    # The mod_python version is more efficient, so try importing it first.
    1414    from mod_python.util import parse_qsl
    1515except ImportError:
    16     from cgi import parse_qsl
     16    # For bugfixes not in Python < 2.3.5
     17    from django.utils._cgi import parse_qsl
    1718
    1819class Http404(Exception):
    1920    pass
     
    100101def parse_file_upload(header_dict, post_data):
    101102    "Returns a tuple of (POST QueryDict, FILES MultiValueDict)"
    102103    import email, email.Message
    103     from cgi import parse_header
     104    # For bugfixes not in Python < 2.3.5
     105    from django.utils._cgi import parse_header
    104106    raw_message = '\r\n'.join(['%s:%s' % pair for pair in header_dict.items()])
    105107    raw_message += '\r\n\r\n' + post_data
    106108    msg = email.message_from_string(raw_message)
  • django/utils/_cgi.py

     
     1"""
     2Provides updated (to 2.5.1) versions of the standard cgi.parse_qs,
     3cgi.parse_qsl and cgi.parse_header functions for Python < 2.3.5.
     4The last two have had bugfixes since then.
     5"""
     6
     7# Relevant revisions from Python svn:
     8#
     9# 34965 = Python 2.3.3
     10# 35532   Change parse_qsl() to accept control-name's with no equal sign
     11#         (e.g., "name") when keep_blank_values is true.
     12# 35974 = Python 2.3.4
     13# 36582   Don't return spurious empty fields if 'keep_empty_values' is True.
     14# 36995   Let cgi.parse_header() properly unquote headers.
     15# 37906 = Python 2.4
     16# 38447 = Python 2.3.5
     17
     18import urllib
     19
     20def parse_qs(qs, keep_blank_values=0, strict_parsing=0):
     21    """Parse a query given as a string argument.
     22
     23        Arguments:
     24
     25        qs: URL-encoded query string to be parsed
     26
     27        keep_blank_values: flag indicating whether blank values in
     28            URL encoded queries should be treated as blank strings.
     29            A true value indicates that blanks should be retained as
     30            blank strings.  The default false value indicates that
     31            blank values are to be ignored and treated as if they were
     32            not included.
     33
     34        strict_parsing: flag indicating what to do with parsing errors.
     35            If false (the default), errors are silently ignored.
     36            If true, errors raise a ValueError exception.
     37    """
     38    dict = {}
     39    for name, value in parse_qsl(qs, keep_blank_values, strict_parsing):
     40        if name in dict:
     41            dict[name].append(value)
     42        else:
     43            dict[name] = [value]
     44    return dict
     45
     46def parse_qsl(qs, keep_blank_values=0, strict_parsing=0):
     47    """Parse a query given as a string argument.
     48
     49    Arguments:
     50
     51    qs: URL-encoded query string to be parsed
     52
     53    keep_blank_values: flag indicating whether blank values in
     54        URL encoded queries should be treated as blank strings.  A
     55        true value indicates that blanks should be retained as blank
     56        strings.  The default false value indicates that blank values
     57        are to be ignored and treated as if they were  not included.
     58
     59    strict_parsing: flag indicating what to do with parsing errors. If
     60        false (the default), errors are silently ignored. If true,
     61        errors raise a ValueError exception.
     62
     63    Returns a list, as G-d intended.
     64    """
     65    pairs = [s2 for s1 in qs.split('&') for s2 in s1.split(';')]
     66    r = []
     67    for name_value in pairs:
     68        if not name_value and not strict_parsing:
     69            continue
     70        nv = name_value.split('=', 1)
     71        if len(nv) != 2:
     72            if strict_parsing:
     73                raise ValueError, "bad query field: %r" % (name_value,)
     74            # Handle case of a control-name with no equal sign
     75            if keep_blank_values:
     76                nv.append('')
     77            else:
     78                continue
     79        if len(nv[1]) or keep_blank_values:
     80            name = urllib.unquote(nv[0].replace('+', ' '))
     81            value = urllib.unquote(nv[1].replace('+', ' '))
     82            r.append((name, value))
     83
     84    return r
     85
     86def parse_header(line):
     87    """Parse a Content-type like header.
     88
     89    Return the main content-type and a dictionary of options.
     90
     91    """
     92    plist = [x.strip() for x in line.split(';')]
     93    key = plist.pop(0).lower()
     94    pdict = {}
     95    for p in plist:
     96        i = p.find('=')
     97        if i >= 0:
     98            name = p[:i].strip().lower()
     99            value = p[i+1:].strip()
     100            if len(value) >= 2 and value[0] == value[-1] == '"':
     101                value = value[1:-1]
     102                value = value.replace('\\\\', '\\').replace('\\"', '"')
     103            pdict[name] = value
     104    return key, pdict
  • django/utils/simplejson/jsonfilter.py

     
    11from django.utils import simplejson
    2 import cgi
     2# For bugfixes not in Python < 2.3.5
     3from django.utils._cgi import parse_qs
    34
    45class JSONFilter(object):
    56    def __init__(self, app, mime_type='text/x-json'):
     
    1920                data = environ['wsgi.input'].read(*map(int, args))
    2021                environ['jsonfilter.json'] = simplejson.loads(data)
    2122        res = simplejson.dumps(self.app(environ, json_start_response))
    22         jsonp = cgi.parse_qs(environ.get('QUERY_STRING', '')).get('jsonp')
     23        # In the line below: keep_blank_values=True
     24        jsonp = parse_qs(environ.get('QUERY_STRING', ''), True).get('jsonp')
    2325        if jsonp:
    2426            content_type = 'text/javascript'
    2527            res = ''.join(jsonp + ['(', res, ')'])
Back to Top