Esempio n. 1
0
def urlencode(query, doseq=False):
    """
    A version of Python's urllib.parse.urlencode() function that can operate on
    MultiValueDict and non-string values.
    """
    if isinstance(query, MultiValueDict):
        query = query.lists()
    elif hasattr(query, 'items'):
        query = query.items()
    query_params = []
    for key, value in query:
        if isinstance(value, (str, bytes)):
            query_val = value
        else:
            try:
                iter(value)
            except TypeError:
                query_val = value
            else:
                # Consume generators and iterators, even when doseq=True, to
                # work around https://bugs.python.org/issue31706.
                query_val = [
                    item if isinstance(item, bytes) else str(item)
                    for item in value
                ]
        query_params.append((key, query_val))
    return original_urlencode(query_params, doseq)
Esempio n. 2
0
def urlencode(query, doseq=False):
    """
    A version of Python's urllib.parse.urlencode() function that can operate on
    MultiValueDict and non-string values.
    """
    if isinstance(query, MultiValueDict):
        query = query.lists()
    elif hasattr(query, 'items'):
        query = query.items()
    query_params = []
    for key, value in query:
        if isinstance(value, (str, bytes)):
            query_val = value
        else:
            try:
                iter(value)
            except TypeError:
                query_val = value
            else:
                # Consume generators and iterators, even when doseq=True, to
                # work around https://bugs.python.org/issue31706.
                query_val = [
                    item if isinstance(item, bytes) else str(item)
                    for item in value
                ]
        query_params.append((key, query_val))
    return original_urlencode(query_params, doseq)
Esempio n. 3
0
def urlencode(query, doseq=False):
    """
    A version of Python's urllib.parse.urlencode() function that can operate on
    MultiValueDict and non-string values.
    """
    if isinstance(query, MultiValueDict):
        query = query.lists()
    elif hasattr(query, 'items'):
        query = query.items()
    return original_urlencode(
        [(k, [str(i) for i in v] if isinstance(v, (list, tuple)) else str(v))
         for k, v in query], doseq)
Esempio n. 4
0
 def test_alternate_charset_POST(self):
     """
     Test a POST with non-utf-8 payload encoding.
     """
     payload = FakePayload(original_urlencode({'key': 'España'.encode('latin-1')}))
     request = WSGIRequest({
         'REQUEST_METHOD': 'POST',
         'CONTENT_LENGTH': len(payload),
         'CONTENT_TYPE': 'application/x-www-form-urlencoded; charset=iso-8859-1',
         'wsgi.input': payload,
     })
     self.assertEqual(request.POST, {'key': ['España']})
Esempio n. 5
0
File: http.py Progetto: mnach/django
def urlencode(query, doseq=False):
    """
    A version of Python's urllib.parse.urlencode() function that can operate on
    MultiValueDict and non-string values.
    """
    if isinstance(query, MultiValueDict):
        query = query.lists()
    elif hasattr(query, 'items'):
        query = query.items()
    return original_urlencode(
        [(k, [str(i) for i in v] if isinstance(v, (list, tuple)) else str(v))
         for k, v in query],
        doseq
    )
Esempio n. 6
0
def urlencode(query, doseq=0):
    """
    A version of Python's urllib.urlencode() function that can operate on
    unicode strings. The parameters are first cast to UTF-8 encoded strings and
    then encoded as per normal.
    """
    if isinstance(query, MultiValueDict):
        query = query.lists()
    elif hasattr(query, 'items'):
        query = query.items()
    return original_urlencode(
        [(force_str(k),
         [force_str(i) for i in v] if isinstance(v, (list, tuple)) else force_str(v))
            for k, v in query],
        doseq)
Esempio n. 7
0
def urlencode(query, doseq=False):
    """
    A version of Python's urllib.parse.urlencode() function that can operate on
    MultiValueDict and non-string values.
    """
    if isinstance(query, MultiValueDict):
        query = query.lists()
    elif hasattr(query, "items"):
        query = query.items()
    query_params = []
    for key, value in query:
        if value is None:
            raise TypeError(
                "Cannot encode None for key '%s' in a query string. Did you "
                "mean to pass an empty string or omit the value?" % key
            )
        elif not doseq or isinstance(value, (str, bytes)):
            query_val = value
        else:
            try:
                itr = iter(value)
            except TypeError:
                query_val = value
            else:
                # Consume generators and iterators, when doseq=True, to
                # work around https://bugs.python.org/issue31706.
                query_val = []
                for item in itr:
                    if item is None:
                        raise TypeError(
                            "Cannot encode None for key '%s' in a query "
                            "string. Did you mean to pass an empty string or "
                            "omit the value?" % key
                        )
                    elif not isinstance(item, bytes):
                        item = str(item)
                    query_val.append(item)
        query_params.append((key, query_val))
    return original_urlencode(query_params, doseq)
Esempio n. 8
0
def urlencode(query, doseq=False):
    """
    A version of Python's urllib.parse.urlencode() function that can operate on
    MultiValueDict and non-string values.
    """
    if isinstance(query, MultiValueDict):
        query = query.lists()
    elif hasattr(query, 'items'):
        query = query.items()
    query_params = []
    for key, value in query:
        if value is None:
            raise TypeError(
                'Cannot encode None in a query string. Did you mean to pass '
                'an empty string or omit the value?'
            )
        elif isinstance(value, (str, bytes)):
            query_val = value
        else:
            try:
                itr = iter(value)
            except TypeError:
                query_val = value
            else:
                # Consume generators and iterators, even when doseq=True, to
                # work around https://bugs.python.org/issue31706.
                query_val = []
                for item in itr:
                    if item is None:
                        raise TypeError(
                            'Cannot encode None in a query string. Did you '
                            'mean to pass an empty string or omit the value?'
                        )
                    elif not isinstance(item, bytes):
                        item = str(item)
                    query_val.append(item)
        query_params.append((key, query_val))
    return original_urlencode(query_params, doseq)