diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 986099db..37b9a64b 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -18,13 +18,13 @@ jobs: - python-version: 3.7 env: TOXENV: docs - - python-version: 3.8 + - python-version: 3.9 env: TOXENV: flake8 - - python-version: 3.8 + - python-version: 3.9 env: TOXENV: pylint - - python-version: 3.8 + - python-version: 3.9 env: TOXENV: security diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 9390e788..26b1c58a 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -12,10 +12,10 @@ jobs: steps: - uses: actions/checkout@v2 - - name: Set up Python 3.8 + - name: Set up Python 3.9 uses: actions/setup-python@v2 with: - python-version: 3.8 + python-version: 3.9 - name: Check Tag id: check-release-tag diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index c47af5f8..643c2655 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -14,7 +14,7 @@ jobs: runs-on: ubuntu-20.04 strategy: matrix: - python-version: [2.7, 3.5, 3.6, 3.7, 3.8, pypy3] + python-version: [3.6, 3.7, 3.8, 3.9, pypy3] steps: - uses: actions/checkout@v2 diff --git a/README.rst b/README.rst index e9cbd231..d097492b 100644 --- a/README.rst +++ b/README.rst @@ -27,7 +27,7 @@ This is a Python library of web-related functions, such as: Requirements ============ -Python 2.7 or Python 3.5+ +Python 3.6+ Install ======= diff --git a/docs/conf.py b/docs/conf.py index b786f2b9..eb57263a 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- -# # w3lib documentation build configuration file, created by # sphinx-quickstart on Sun Jan 26 22:19:38 2014. # @@ -47,8 +45,8 @@ master_doc = 'index' # General information about the project. -project = u'w3lib' -copyright = u'2014, w3lib developers' +project = 'w3lib' +copyright = '2014, w3lib developers' # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the @@ -190,8 +188,8 @@ # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, author, documentclass [howto/manual]). latex_documents = [ - ('index', 'w3lib.tex', u'w3lib Documentation', - u'w3lib developers', 'manual'), + ('index', 'w3lib.tex', 'w3lib Documentation', + 'w3lib developers', 'manual'), ] # The name of an image file (relative to this directory) to place at the top of @@ -220,8 +218,8 @@ # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). man_pages = [ - ('index', 'w3lib', u'w3lib Documentation', - [u'w3lib developers'], 1) + ('index', 'w3lib', 'w3lib Documentation', + ['w3lib developers'], 1) ] # If true, show URL addresses after external links. @@ -234,8 +232,8 @@ # (source start file, target name, title, author, # dir menu entry, description, category) texinfo_documents = [ - ('index', 'w3lib', u'w3lib Documentation', - u'w3lib developers', 'w3lib', 'One line description of project.', + ('index', 'w3lib', 'w3lib Documentation', + 'w3lib developers', 'w3lib', 'One line description of project.', 'Miscellaneous'), ] diff --git a/docs/index.rst b/docs/index.rst index fdbda607..bd14188b 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -28,7 +28,7 @@ Modules Requirements ============ -Python 2.7 or Python 3.3+ +Python 3.6+ Install ======= diff --git a/pytest.ini b/pytest.ini index 4f23e3f8..94b29688 100644 --- a/pytest.ini +++ b/pytest.ini @@ -1,6 +1,8 @@ [pytest] doctest_optionflags = ALLOW_UNICODE ALLOW_BYTES flake8-ignore = + W503 # https://www.flake8rules.com/rules/W503.html + docs/conf.py E121 E122 E265 E401 E501 tests/test_encoding.py E128 E221 E241 E302 E401 E501 E731 tests/test_form.py E265 E501 diff --git a/setup.py b/setup.py index ea0ca0a9..6f24f5eb 100644 --- a/setup.py +++ b/setup.py @@ -18,16 +18,13 @@ 'License :: OSI Approved :: BSD License', 'Operating System :: OS Independent', 'Programming Language :: Python', - 'Programming Language :: Python :: 2', - 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.5', 'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.7', 'Programming Language :: Python :: 3.8', + 'Programming Language :: Python :: 3.9', 'Programming Language :: Python :: Implementation :: CPython', 'Programming Language :: Python :: Implementation :: PyPy', 'Topic :: Internet :: WWW/HTTP', ], - install_requires=['six >= 1.4.1'], ) diff --git a/stdeb.cfg b/stdeb.cfg deleted file mode 100644 index 5a7e8a2d..00000000 --- a/stdeb.cfg +++ /dev/null @@ -1,2 +0,0 @@ -[w3lib] -Depends: python-six (>= 1.4.1) diff --git a/tests/py3-ignores.txt b/tests/py3-ignores.txt deleted file mode 100644 index 09f34ec9..00000000 --- a/tests/py3-ignores.txt +++ /dev/null @@ -1,5 +0,0 @@ -w3lib/encoding.py -w3lib/form.py -w3lib/html.py -w3lib/http.py -w3lib/url.py diff --git a/tests/test_encoding.py b/tests/test_encoding.py index 649c189a..9faced10 100644 --- a/tests/test_encoding.py +++ b/tests/test_encoding.py @@ -1,7 +1,14 @@ -import unittest, codecs -import six -from w3lib.encoding import (html_body_declared_encoding, read_bom, to_unicode, - http_content_type_encoding, resolve_encoding, html_to_unicode) +import codecs +import unittest + +from w3lib.encoding import ( + html_body_declared_encoding, + http_content_type_encoding, + html_to_unicode, + read_bom, + resolve_encoding, + to_unicode, +) class RequestEncodingTests(unittest.TestCase): utf8_fragments = [ @@ -22,7 +29,7 @@ class RequestEncodingTests(unittest.TestCase): def test_bom(self): # cjk water character in unicode - water_unicode = u'\u6C34' + water_unicode = '\u6C34' # BOM + water character encoded utf16be = b'\xfe\xff\x6c\x34' utf16le = b'\xff\xfe\x34\x6c' @@ -62,19 +69,19 @@ def test_html_body_declared_encoding(self): def test_html_body_declared_encoding_unicode(self): # html_body_declared_encoding should work when unicode body is passed - self.assertEqual(None, html_body_declared_encoding(u"something else")) + self.assertEqual(None, html_body_declared_encoding("something else")) for fragment in self.utf8_fragments: encoding = html_body_declared_encoding(fragment.decode('utf8')) self.assertEqual(encoding, 'utf-8', fragment) - self.assertEqual(None, html_body_declared_encoding(u""" + self.assertEqual(None, html_body_declared_encoding("""
this isn't searched """)) self.assertEqual(None, html_body_declared_encoding( - u"""""")) + """""")) class CodecsEncodingTestCase(unittest.TestCase): @@ -88,10 +95,10 @@ def test_resolve_encoding(self): class UnicodeDecodingTestCase(unittest.TestCase): def test_utf8(self): - self.assertEqual(to_unicode(b'\xc2\xa3', 'utf-8'), u'\xa3') + self.assertEqual(to_unicode(b'\xc2\xa3', 'utf-8'), '\xa3') def test_invalid_utf8(self): - self.assertEqual(to_unicode(b'\xc2\xc2\xa3', 'utf-8'), u'\ufffd\xa3') + self.assertEqual(to_unicode(b'\xc2\xc2\xa3', 'utf-8'), '\ufffd\xa3') def ct(charset): @@ -103,22 +110,22 @@ def norm_encoding(enc): class HtmlConversionTests(unittest.TestCase): def test_unicode_body(self): - unicode_string = u'\u043a\u0438\u0440\u0438\u043b\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0442\u0435\u043a\u0441\u0442' + unicode_string = '\u043a\u0438\u0440\u0438\u043b\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0442\u0435\u043a\u0441\u0442' original_string = unicode_string.encode('cp1251') encoding, body_unicode = html_to_unicode(ct('cp1251'), original_string) # check body_as_unicode - self.assertTrue(isinstance(body_unicode, six.text_type)) + self.assertTrue(isinstance(body_unicode, str)) self.assertEqual(body_unicode, unicode_string) def _assert_encoding(self, content_type, body, expected_encoding, expected_unicode): - assert not isinstance(body, six.text_type) + assert not isinstance(body, str) encoding, body_unicode = html_to_unicode(ct(content_type), body) - self.assertTrue(isinstance(body_unicode, six.text_type)) + self.assertTrue(isinstance(body_unicode, str)) self.assertEqual(norm_encoding(encoding), norm_encoding(expected_encoding)) - if isinstance(expected_unicode, six.string_types): + if isinstance(expected_unicode, str): self.assertEqual(body_unicode, expected_unicode) else: self.assertTrue( @@ -130,23 +137,23 @@ def test_content_type_and_conversion(self): """Test content type header is interpreted and text converted as expected """ - self._assert_encoding('utf-8', b"\xc2\xa3", 'utf-8', u"\xa3") + self._assert_encoding('utf-8', b"\xc2\xa3", 'utf-8', "\xa3") # something like this in the scrapy tests - but that's invalid? - # self._assert_encoding('', "\xa3", 'utf-8', u"\xa3") + # self._assert_encoding('', "\xa3", 'utf-8', "\xa3") # iso-8859-1 is overridden to cp1252 - self._assert_encoding('iso-8859-1', b"\xa3", 'cp1252', u"\xa3") - self._assert_encoding('', b"\xc2\xa3", 'utf-8', u"\xa3") - self._assert_encoding('none', b"\xc2\xa3", 'utf-8', u"\xa3") - self._assert_encoding('gb2312', b"\xa8D", 'gb18030', u"\u2015") - self._assert_encoding('gbk', b"\xa8D", 'gb18030', u"\u2015") - self._assert_encoding('big5', b"\xf9\xda", 'big5hkscs', u"\u6052") + self._assert_encoding('iso-8859-1', b"\xa3", 'cp1252', "\xa3") + self._assert_encoding('', b"\xc2\xa3", 'utf-8', "\xa3") + self._assert_encoding('none', b"\xc2\xa3", 'utf-8', "\xa3") + self._assert_encoding('gb2312', b"\xa8D", 'gb18030', "\u2015") + self._assert_encoding('gbk', b"\xa8D", 'gb18030', "\u2015") + self._assert_encoding('big5', b"\xf9\xda", 'big5hkscs', "\u6052") def test_invalid_utf8_encoded_body_with_valid_utf8_BOM(self): # unlike scrapy, the BOM is stripped self._assert_encoding('utf-8', b"\xef\xbb\xbfWORD\xe3\xabWORD2", - 'utf-8', u'WORD\ufffdWORD2') + 'utf-8', 'WORD\ufffdWORD2') self._assert_encoding(None, b"\xef\xbb\xbfWORD\xe3\xabWORD2", - 'utf-8', u'WORD\ufffdWORD2') + 'utf-8', 'WORD\ufffdWORD2') def test_utf8_unexpected_end_of_data_with_valid_utf8_BOM(self): # Python implementations handle unexpected end of UTF8 data @@ -156,69 +163,69 @@ def test_utf8_unexpected_end_of_data_with_valid_utf8_BOM(self): # unlike scrapy, the BOM is stripped self._assert_encoding('utf-8', b"\xef\xbb\xbfWORD\xe3\xab", - 'utf-8', [u'WORD\ufffd\ufffd', u'WORD\ufffd']) + 'utf-8', ['WORD\ufffd\ufffd', 'WORD\ufffd']) self._assert_encoding(None, b"\xef\xbb\xbfWORD\xe3\xab", - 'utf-8', [u'WORD\ufffd\ufffd', u'WORD\ufffd']) + 'utf-8', ['WORD\ufffd\ufffd', 'WORD\ufffd']) def test_replace_wrong_encoding(self): """Test invalid chars are replaced properly""" encoding, body_unicode = html_to_unicode(ct('utf-8'), b'PREFIX\xe3\xabSUFFIX') # XXX: Policy for replacing invalid chars may suffer minor variations - # but it should always contain the unicode replacement char (u'\ufffd') - assert u'\ufffd' in body_unicode, repr(body_unicode) - assert u'PREFIX' in body_unicode, repr(body_unicode) - assert u'SUFFIX' in body_unicode, repr(body_unicode) + # but it should always contain the unicode replacement char ('\ufffd') + assert '\ufffd' in body_unicode, repr(body_unicode) + assert 'PREFIX' in body_unicode, repr(body_unicode) + assert 'SUFFIX' in body_unicode, repr(body_unicode) # Do not destroy html tags due to encoding bugs encoding, body_unicode = html_to_unicode(ct('utf-8'), b'\xf0value') - assert u'value' in body_unicode, repr(body_unicode) + assert 'value' in body_unicode, repr(body_unicode) def _assert_encoding_detected(self, content_type, expected_encoding, body, **kwargs): - assert not isinstance(body, six.text_type) + assert not isinstance(body, str) encoding, body_unicode = html_to_unicode(ct(content_type), body, **kwargs) - self.assertTrue(isinstance(body_unicode, six.text_type)) + self.assertTrue(isinstance(body_unicode, str)) self.assertEqual(norm_encoding(encoding), norm_encoding(expected_encoding)) def test_BOM(self): # utf-16 cases already tested, as is the BOM detection function # http header takes precedence, irrespective of BOM - bom_be_str = codecs.BOM_UTF16_BE + u"hi".encode('utf-16-be') - expected = u'\ufffd\ufffd\x00h\x00i' + bom_be_str = codecs.BOM_UTF16_BE + "hi".encode('utf-16-be') + expected = '\ufffd\ufffd\x00h\x00i' self._assert_encoding('utf-8', bom_be_str, 'utf-8', expected) # BOM is stripped when it agrees with the encoding, or used to # determine encoding bom_utf8_str = codecs.BOM_UTF8 + b'hi' - self._assert_encoding('utf-8', bom_utf8_str, 'utf-8', u"hi") - self._assert_encoding(None, bom_utf8_str, 'utf-8', u"hi") + self._assert_encoding('utf-8', bom_utf8_str, 'utf-8', "hi") + self._assert_encoding(None, bom_utf8_str, 'utf-8', "hi") def test_utf16_32(self): # tools.ietf.org/html/rfc2781 section 4.3 # USE BOM and strip it - bom_be_str = codecs.BOM_UTF16_BE + u"hi".encode('utf-16-be') - self._assert_encoding('utf-16', bom_be_str, 'utf-16-be', u"hi") - self._assert_encoding(None, bom_be_str, 'utf-16-be', u"hi") + bom_be_str = codecs.BOM_UTF16_BE + "hi".encode('utf-16-be') + self._assert_encoding('utf-16', bom_be_str, 'utf-16-be', "hi") + self._assert_encoding(None, bom_be_str, 'utf-16-be', "hi") - bom_le_str = codecs.BOM_UTF16_LE + u"hi".encode('utf-16-le') - self._assert_encoding('utf-16', bom_le_str, 'utf-16-le', u"hi") - self._assert_encoding(None, bom_le_str, 'utf-16-le', u"hi") + bom_le_str = codecs.BOM_UTF16_LE + "hi".encode('utf-16-le') + self._assert_encoding('utf-16', bom_le_str, 'utf-16-le', "hi") + self._assert_encoding(None, bom_le_str, 'utf-16-le', "hi") - bom_be_str = codecs.BOM_UTF32_BE + u"hi".encode('utf-32-be') - self._assert_encoding('utf-32', bom_be_str, 'utf-32-be', u"hi") - self._assert_encoding(None, bom_be_str, 'utf-32-be', u"hi") + bom_be_str = codecs.BOM_UTF32_BE + "hi".encode('utf-32-be') + self._assert_encoding('utf-32', bom_be_str, 'utf-32-be', "hi") + self._assert_encoding(None, bom_be_str, 'utf-32-be', "hi") - bom_le_str = codecs.BOM_UTF32_LE + u"hi".encode('utf-32-le') - self._assert_encoding('utf-32', bom_le_str, 'utf-32-le', u"hi") - self._assert_encoding(None, bom_le_str, 'utf-32-le', u"hi") + bom_le_str = codecs.BOM_UTF32_LE + "hi".encode('utf-32-le') + self._assert_encoding('utf-32', bom_le_str, 'utf-32-le', "hi") + self._assert_encoding(None, bom_le_str, 'utf-32-le', "hi") # if there is no BOM, big endian should be chosen - self._assert_encoding('utf-16', u"hi".encode('utf-16-be'), 'utf-16-be', u"hi") - self._assert_encoding('utf-32', u"hi".encode('utf-32-be'), 'utf-32-be', u"hi") + self._assert_encoding('utf-16', "hi".encode('utf-16-be'), 'utf-16-be', "hi") + self._assert_encoding('utf-32', "hi".encode('utf-32-be'), 'utf-32-be', "hi") def test_python_crash(self): import random diff --git a/tests/test_form.py b/tests/test_form.py index 280d8795..ac0696b5 100644 --- a/tests/test_form.py +++ b/tests/test_form.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import absolute_import import warnings import unittest from collections import OrderedDict @@ -23,20 +21,20 @@ def test_encode_multipart(self): def test_encode_multipart_unicode(self): data = OrderedDict([ - (u'ключ1', u'значение1'.encode('utf8')), - (u'ключ2', u'значение2'), + ('ключ1', 'значение1'.encode('utf8')), + ('ключ2', 'значение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') + '\r\n--{boundary}' + '\r\nContent-Disposition: form-data; name="ключ1"\r\n' + '\r\nзначение1' + '\r\n--{boundary}' + '\r\nContent-Disposition: form-data; name="ключ2"\r\n' + '\r\nзначение2' + '\r\n--{boundary}--' + '\r\n'.format(boundary=boundary).encode('utf8') ) self.assertEqual(body, expected_body) diff --git a/tests/test_html.py b/tests/test_html.py index a3c31d87..d6a6c22b 100644 --- a/tests/test_html.py +++ b/tests/test_html.py @@ -1,47 +1,54 @@ -# -*- coding: utf-8 -*- import unittest -import six -from w3lib.html import (replace_entities, replace_tags, remove_comments, - remove_tags_with_content, replace_escape_chars, remove_tags, unquote_markup, - get_base_url, get_meta_refresh) + +from w3lib.html import ( + get_base_url, + get_meta_refresh, + remove_comments, + remove_tags, + remove_tags_with_content, + replace_entities, + replace_escape_chars, + replace_tags, + unquote_markup, +) class RemoveEntitiesTest(unittest.TestCase): def test_returns_unicode(self): # make sure it always return uncode - assert isinstance(replace_entities(b'no entities'), six.text_type) - assert isinstance(replace_entities(b'Price: £100!'), six.text_type) - assert isinstance(replace_entities(u'no entities'), six.text_type) - assert isinstance(replace_entities(u'Price: £100!'), six.text_type) + assert isinstance(replace_entities(b'no entities'), str) + assert isinstance(replace_entities(b'Price: £100!'), str) + assert isinstance(replace_entities('no entities'), str) + assert isinstance(replace_entities('Price: £100!'), str) def test_regular(self): # regular conversions - self.assertEqual(replace_entities(u'As low as £100!'), - u'As low as \xa3100!') + self.assertEqual(replace_entities('As low as £100!'), + 'As low as \xa3100!') self.assertEqual(replace_entities(b'As low as £100!'), - u'As low as \xa3100!') + 'As low as \xa3100!') self.assertEqual(replace_entities('redirectTo=search&searchtext=MR0221Y&aff=buyat&affsrc=d_data&cm_mmc=buyat-_-ELECTRICAL & SEASONAL-_-MR0221Y-_-9-carat gold ½oz solid crucifix pendant'), - u'redirectTo=search&searchtext=MR0221Y&aff=buyat&affsrc=d_data&cm_mmc=buyat-_-ELECTRICAL & SEASONAL-_-MR0221Y-_-9-carat gold \xbdoz solid crucifix pendant') + 'redirectTo=search&searchtext=MR0221Y&aff=buyat&affsrc=d_data&cm_mmc=buyat-_-ELECTRICAL & SEASONAL-_-MR0221Y-_-9-carat gold \xbdoz solid crucifix pendant') def test_keep_entities(self): # keep some entities self.assertEqual(replace_entities(b'Low < High & Medium £ six', keep=['lt', 'amp']), - u'Low < High & Medium \xa3 six') - self.assertEqual(replace_entities(u'Low < High & Medium £ six', keep=[u'lt', u'amp']), - u'Low < High & Medium \xa3 six') + 'Low < High & Medium \xa3 six') + self.assertEqual(replace_entities('Low < High & Medium £ six', keep=['lt', 'amp']), + 'Low < High & Medium \xa3 six') def test_illegal_entities(self): self.assertEqual(replace_entities('a < b &illegal; c six', remove_illegal=False), - u'a < b &illegal; c six') + 'a < b &illegal; c six') self.assertEqual(replace_entities('a < b &illegal; c six', remove_illegal=True), - u'a < b c six') - self.assertEqual(replace_entities('x≤y'), u'x\u2264y') - self.assertEqual(replace_entities('xy'), u'xy') - self.assertEqual(replace_entities('xy', remove_illegal=False), u'xy') + 'a < b c six') + self.assertEqual(replace_entities('x≤y'), 'x\u2264y') + self.assertEqual(replace_entities('xy'), 'xy') + self.assertEqual(replace_entities('xy', remove_illegal=False), 'xy') def test_browser_hack(self): # check browser hack for numeric character references in the 80-9F range - self.assertEqual(replace_entities('xy', encoding='cp1252'), u'x\u2122y') + self.assertEqual(replace_entities('xy', encoding='cp1252'), 'x\u2122y') self.assertEqual(replace_entities('xy', encoding='cp1252'), u'x\u2122y') def test_missing_semicolon(self): @@ -53,200 +60,200 @@ def test_missing_semicolon(self): ('Ah', 'Ah',), ('A!', 'A!',), ('Ax', 'Ax',), - ('³!', u'\u00B3!',), - ('Á!', u'\u00C1!',), - ('☃!', u'\u2603!',), - ('', u'\u2122',), - ('', u'\u2122',), + ('³!', '\u00B3!',), + ('Á!', '\u00C1!',), + ('☃!', '\u2603!',), + ('', '\u2122',), + ('', '\u2122',), ): self.assertEqual(replace_entities(entity, encoding='cp1252'), result) - self.assertEqual(replace_entities('x%sy' % entity, encoding='cp1252'), u'x%sy' % result) + self.assertEqual(replace_entities('x%sy' % entity, encoding='cp1252'), 'x%sy' % result) def test_encoding(self): self.assertEqual(replace_entities(b'x\x99™y', encoding='cp1252'), \ - u'x\u2122\u2122\u2122y') + 'x\u2122\u2122\u2122y') class ReplaceTagsTest(unittest.TestCase): def test_returns_unicode(self): # make sure it always return uncode - assert isinstance(replace_tags(b'no entities'), six.text_type) - assert isinstance(replace_tags('no entities'), six.text_type) + assert isinstance(replace_tags(b'no entities'), str) + assert isinstance(replace_tags('no entities'), str) def test_replace_tags(self): - self.assertEqual(replace_tags(u'This text contains some tag'), - u'This text contains some tag') + self.assertEqual(replace_tags('This text contains some tag'), + 'This text contains some tag') self.assertEqual(replace_tags(b'This text is very important', ' '), - u'This text is very im port ant') + 'This text is very im port ant') def test_replace_tags_multiline(self): self.assertEqual(replace_tags(b'Click here'), - u'Click here') + 'Click here') class RemoveCommentsTest(unittest.TestCase): def test_returns_unicode(self): # make sure it always return unicode - assert isinstance(remove_comments(b'without comments'), six.text_type) - assert isinstance(remove_comments(b''), six.text_type) - assert isinstance(remove_comments(u'without comments'), six.text_type) - assert isinstance(remove_comments(u''), six.text_type) + assert isinstance(remove_comments(b'without comments'), str) + assert isinstance(remove_comments(b''), str) + assert isinstance(remove_comments('without comments'), str) + assert isinstance(remove_comments(''), str) def test_no_comments(self): # text without comments - self.assertEqual(remove_comments(u'text without comments'), u'text without comments') + self.assertEqual(remove_comments('text without comments'), 'text without comments') def test_remove_comments(self): # text with comments - self.assertEqual(remove_comments(u''), u'') - self.assertEqual(remove_comments(u'Hello'), u'Hello') - self.assertEqual(remove_comments(u'Hello'), u'Hello') + self.assertEqual(remove_comments(''), '') + self.assertEqual(remove_comments('Hello'), 'Hello') + self.assertEqual(remove_comments('Hello'), 'Hello') - self.assertEqual(remove_comments(b"test whatever"), u'test whatever') - self.assertEqual(remove_comments(b"test whatever"), u'test whatever') + self.assertEqual(remove_comments(b"test whatever"), 'test whatever') + self.assertEqual(remove_comments(b"test whatever"), 'test whatever') - self.assertEqual(remove_comments(b"test |$)', re.DOTALL) +_REMOVECOMMENTS_RE = re.compile('|$)', re.DOTALL) def remove_comments(text, encoding=None): """ Remove HTML Comments. >>> import w3lib.html >>> w3lib.html.remove_comments(b"test whatever") - u'test whatever' + 'test whatever' >>> """ text = to_unicode(text, encoding) - return _REMOVECOMMENTS_RE.sub(u'', text) + return _REMOVECOMMENTS_RE.sub('', text) def remove_tags(text, which_ones=(), keep=(), encoding=None): """ Remove HTML Tags only. @@ -156,19 +157,19 @@ def remove_tags(text, which_ones=(), keep=(), encoding=None): >>> import w3lib.html >>> doc = 'This is a link: example
This is a link: example
This is a link: example
This is a link: example