From cdd758f11aad53cbfaf4c4e823d578c94aaad8b8 Mon Sep 17 00:00:00 2001 From: Paul Tremberth Date: Wed, 23 Mar 2016 17:02:38 +0100 Subject: [PATCH] Remove deprecated encode_multipart() and related tests Was deprecated since v1.6 --- tests/test_form.py | 67 --------------------------------------------- w3lib/form.py | 68 ---------------------------------------------- 2 files changed, 135 deletions(-) delete mode 100644 tests/test_form.py delete mode 100644 w3lib/form.py diff --git a/tests/test_form.py b/tests/test_form.py deleted file mode 100644 index 280d8795..00000000 --- a/tests/test_form.py +++ /dev/null @@ -1,67 +0,0 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import -import warnings -import unittest -from collections import OrderedDict -from w3lib.form import encode_multipart - - -class EncodeMultipartTest(unittest.TestCase): - - def test_encode_multipart(self): - data = {'key': 'value'} - with warnings.catch_warnings(record=True): - body, boundary = encode_multipart(data) - expected_body = ( - '\r\n--{boundary}' - '\r\nContent-Disposition: form-data; name="key"\r\n' - '\r\nvalue' - '\r\n--{boundary}--' - '\r\n'.format(boundary=boundary).encode('utf8') - ) - self.assertEqual(body, expected_body) - - def test_encode_multipart_unicode(self): - data = OrderedDict([ - (u'ключ1', u'значение1'.encode('utf8')), - (u'ключ2', u'значение2'), - ]) - with warnings.catch_warnings(record=True): - body, boundary = encode_multipart(data) - expected_body = ( - u'\r\n--{boundary}' - u'\r\nContent-Disposition: form-data; name="ключ1"\r\n' - u'\r\nзначение1' - u'\r\n--{boundary}' - u'\r\nContent-Disposition: form-data; name="ключ2"\r\n' - u'\r\nзначение2' - u'\r\n--{boundary}--' - u'\r\n'.format(boundary=boundary).encode('utf8') - ) - self.assertEqual(body, expected_body) - - def test_encode_multipart_file(self): - # this data is not decodable using utf8 - data = {'key': ('file/name', b'\xa1\xa2\xa3\xa4\r\n\r')} - with warnings.catch_warnings(record=True): - body, boundary = encode_multipart(data) - body_lines = [ - b'\r\n--' + boundary.encode('ascii'), - b'\r\nContent-Disposition: form-data; name="key"; filename="file/name"\r\n', - b'\r\n\xa1\xa2\xa3\xa4\r\n\r', - b'\r\n--' + boundary.encode('ascii') + b'--\r\n', - ] - expected_body = b''.join(body_lines) - self.assertEqual(body, expected_body) - - #def test_encode_multipart_int(self): - # data = {'key': 123} - # body, boundary = encode_multipart2(data) - # expected_body = ( - # '\n--{boundary}' - # '\nContent-Disposition: form-data; name="key"\n' - # '\n123' - # '\n--{boundary}--' - # '\n'.format(boundary=boundary) - # ) - # self.assertEqual(body, expected_body) diff --git a/w3lib/form.py b/w3lib/form.py deleted file mode 100644 index 6a5eb403..00000000 --- a/w3lib/form.py +++ /dev/null @@ -1,68 +0,0 @@ -import warnings -import six -if six.PY2: - from cStringIO import StringIO as BytesIO -else: - from io import BytesIO -from w3lib.util import unicode_to_str - - -def encode_multipart(data): - r""" - - .. warning:: - - This function is deprecated and will be removed in future. - Please use ``urllib3.filepost.encode_multipart_formdata`` instead. - - Encode the given data to be used in a multipart HTTP POST. - - `data` is a dictionary where keys are the field name, and values are - either strings or tuples as `(filename, content)` for file uploads. - - This code is based on :class:`distutils.command.upload`. - - Returns a `(body, boundary)` tuple where `body` is binary body value, - and `boundary` is the boundary used (as native string). - - >>> import w3lib.form - >>> w3lib.form.encode_multipart({'key': 'value'}) - ('\r\n----------------GHSKFJDLGDS7543FJKLFHRE75642756743254\r\nContent-Disposition: form-data; name="key"\r\n\r\nvalue\r\n----------------GHSKFJDLGDS7543FJKLFHRE75642756743254--\r\n', '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254') - >>> w3lib.form.encode_multipart({'key1': 'value1', 'key2': 'value2'}) # doctest: +SKIP - ('\r\n----------------GHSKFJDLGDS7543FJKLFHRE75642756743254\r\nContent-Disposition: form-data; name="key2"\r\n\r\nvalue2\r\n----------------GHSKFJDLGDS7543FJKLFHRE75642756743254\r\nContent-Disposition: form-data; name="key1"\r\n\r\nvalue1\r\n----------------GHSKFJDLGDS7543FJKLFHRE75642756743254--\r\n', '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254') - >>> w3lib.form.encode_multipart({'somekey': ('path/to/filename', b'\xa1\xa2\xa3\xa4\r\n\r')}) - ('\r\n----------------GHSKFJDLGDS7543FJKLFHRE75642756743254\r\nContent-Disposition: form-data; name="somekey"; filename="path/to/filename"\r\n\r\n\xa1\xa2\xa3\xa4\r\n\r\r\n----------------GHSKFJDLGDS7543FJKLFHRE75642756743254--\r\n', '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254') - >>> - - """ - - warnings.warn( - "`w3lib.form.encode_multipart` function is deprecated and " - "will be removed in future releases. Please use " - "`urllib3.filepost.encode_multipart_formdata` instead.", - DeprecationWarning - ) - - # Build up the MIME payload for the POST data - boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254' - sep_boundary = b'\r\n--' + boundary.encode('ascii') - end_boundary = sep_boundary + b'--' - body = BytesIO() - for key, value in data.items(): - title = u'\r\nContent-Disposition: form-data; name="%s"' % key - # handle multiple entries for the same name - if type(value) != type([]): - value = [value] - for value in value: - if type(value) is tuple: - title += u'; filename="%s"' % value[0] - value = value[1] - else: - value = unicode_to_str(value) # in distutils: str(value).encode('utf-8') - body.write(sep_boundary) - body.write(title.encode('utf-8')) - body.write(b"\r\n\r\n") - body.write(value) - body.write(end_boundary) - body.write(b"\r\n") - return body.getvalue(), boundary