diff --git a/bleach/__init__.py b/bleach/__init__.py index d619fb2c..e847de2a 100644 --- a/bleach/__init__.py +++ b/bleach/__init__.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - import packaging.version from bleach.linkifier import ( diff --git a/bleach/html5lib_shim.py b/bleach/html5lib_shim.py index 3c9c3306..d97d5c49 100644 --- a/bleach/html5lib_shim.py +++ b/bleach/html5lib_shim.py @@ -257,7 +257,7 @@ class BleachHTMLTokenizer(HTMLTokenizer): """Tokenizer that doesn't consume character entities""" def __init__(self, consume_entities=False, **kwargs): - super(BleachHTMLTokenizer, self).__init__(**kwargs) + super().__init__(**kwargs) self.consume_entities = consume_entities @@ -267,7 +267,7 @@ def __init__(self, consume_entities=False, **kwargs): def __iter__(self): last_error_token = None - for token in super(BleachHTMLTokenizer, self).__iter__(): + for token in super().__iter__(): if last_error_token is not None: if ( last_error_token["data"] == "invalid-character-in-attribute-name" @@ -342,9 +342,7 @@ def consumeEntity(self, allowedChar=None, fromAttribute=False): # If this tokenizer is set to consume entities, then we can let the # superclass do its thing. if self.consume_entities: - return super(BleachHTMLTokenizer, self).consumeEntity( - allowedChar, fromAttribute - ) + return super().consumeEntity(allowedChar, fromAttribute) # If this tokenizer is set to not consume entities, then we don't want # to consume and convert them, so this overrides the html5lib tokenizer's @@ -364,7 +362,7 @@ def tagOpenState(self): # we've collected so far and we do that by calling start_tag() on # the input stream wrapper. self.stream.start_tag() - return super(BleachHTMLTokenizer, self).tagOpenState() + return super().tagOpenState() def emitCurrentToken(self): token = self.currentToken @@ -397,7 +395,7 @@ def emitCurrentToken(self): self.state = self.dataState return - super(BleachHTMLTokenizer, self).emitCurrentToken() + super().emitCurrentToken() class BleachHTMLParser(HTMLParser): @@ -416,7 +414,7 @@ def __init__(self, tags, strip, consume_entities, **kwargs): self.tags = [tag.lower() for tag in tags] if tags is not None else None self.strip = strip self.consume_entities = consume_entities - super(BleachHTMLParser, self).__init__(**kwargs) + super().__init__(**kwargs) def _parse( self, stream, innerHTML=False, container="div", scripting=True, **kwargs @@ -642,15 +640,14 @@ def serialize(self, treewalker, encoding=None): in_tag = False after_equals = False - for stoken in super(BleachHTMLSerializer, self).serialize(treewalker, encoding): + for stoken in super().serialize(treewalker, encoding): if in_tag: if stoken == ">": in_tag = False elif after_equals: if stoken != '"': - for part in self.escape_base_amp(stoken): - yield part + yield from self.escape_base_amp(stoken) after_equals = False continue diff --git a/bleach/linkifier.py b/bleach/linkifier.py index 759882e9..9a991dd2 100644 --- a/bleach/linkifier.py +++ b/bleach/linkifier.py @@ -228,7 +228,7 @@ def __init__( :arg re email_re: email matching regex """ - super(LinkifyFilter, self).__init__(source) + super().__init__(source) self.callbacks = callbacks or [] self.skip_tags = skip_tags or [] @@ -332,8 +332,7 @@ def handle_email_addresses(self, src_iter): if end < len(text): new_tokens.append({"type": "Characters", "data": text[end:]}) - for new_token in new_tokens: - yield new_token + yield from new_tokens continue @@ -460,8 +459,7 @@ def handle_links(self, src_iter): if end < len(text): new_tokens.append({"type": "Characters", "data": text[end:]}) - for new_token in new_tokens: - yield new_token + yield from new_tokens continue @@ -499,8 +497,7 @@ def handle_a_tag(self, token_buffer): # The callbacks didn't change the text, so we yield the new "a" # token, then whatever else was there, then the end "a" token yield a_token - for mem in token_buffer[1:]: - yield mem + yield from token_buffer[1:] else: # If the callbacks changed the text, then we're going to drop @@ -516,7 +513,7 @@ def __iter__(self): token_buffer = [] - for token in super(LinkifyFilter, self).__iter__(): + for token in super().__iter__(): if in_a: # Handle the case where we're in an "a" tag--we want to buffer tokens # until we hit an end "a" tag. @@ -524,8 +521,7 @@ def __iter__(self): # Add the end tag to the token buffer and then handle them # and yield anything returned token_buffer.append(token) - for new_token in self.handle_a_tag(token_buffer): - yield new_token + yield from self.handle_a_tag(token_buffer) # Clear "a" related state and continue since we've yielded all # the tokens we're going to yield diff --git a/bleach/sanitizer.py b/bleach/sanitizer.py index 89aff1f4..0e981996 100644 --- a/bleach/sanitizer.py +++ b/bleach/sanitizer.py @@ -280,7 +280,7 @@ def __init__( category=DeprecationWarning, module="bleach._vendor.html5lib", ) - return super(BleachSanitizerFilter, self).__init__(source, **kwargs) + return super().__init__(source, **kwargs) def sanitize_stream(self, token_iterator): for token in token_iterator: @@ -290,8 +290,7 @@ def sanitize_stream(self, token_iterator): continue if isinstance(ret, list): - for subtoken in ret: - yield subtoken + yield from ret else: yield ret @@ -575,7 +574,7 @@ def disallowed_token(self, token): if ns is None or ns not in html5lib_shim.prefixes: namespaced_name = name else: - namespaced_name = "%s:%s" % (html5lib_shim.prefixes[ns], name) + namespaced_name = "{}:{}".format(html5lib_shim.prefixes[ns], name) attrs.append( ' %s="%s"' @@ -587,7 +586,7 @@ def disallowed_token(self, token): v, ) ) - token["data"] = "<%s%s>" % (token["name"], "".join(attrs)) + token["data"] = "<{}{}>".format(token["name"], "".join(attrs)) else: token["data"] = "<%s>" % token["name"] diff --git a/docs/conf.py b/docs/conf.py index 6a490050..7ec15153 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # Bleach documentation build configuration file, created by # sphinx-quickstart on Fri May 11 21:11:39 2012. @@ -40,8 +39,8 @@ master_doc = 'index' # General information about the project. -project = u'Bleach' -copyright = u'2012-2015, James Socol; 2015-2017, Mozilla Foundation' +project = 'Bleach' +copyright = '2012-2015, James Socol; 2015-2017, Mozilla Foundation' # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the @@ -196,8 +195,8 @@ # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, author, documentclass [howto/manual]). latex_documents = [ - ('index', 'Bleach.tex', u'Bleach Documentation', - u'Will Kahn-Greene', 'manual'), + ('index', 'Bleach.tex', 'Bleach Documentation', + 'Will Kahn-Greene', 'manual'), ] # The name of an image file (relative to this directory) to place at the top of @@ -226,8 +225,8 @@ # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). man_pages = [ - ('index', 'bleach', u'Bleach Documentation', - [u'Will Kahn-Greene'], 1) + ('index', 'bleach', 'Bleach Documentation', + ['Will Kahn-Greene'], 1) ] # If true, show URL addresses after external links. @@ -240,8 +239,8 @@ # (source start file, target name, title, author, # dir menu entry, description, category) texinfo_documents = [ - ('index', 'Bleach', u'Bleach Documentation', - u'Will Kahn-Greene', 'Bleach', 'One line description of project.', + ('index', 'Bleach', 'Bleach Documentation', + 'Will Kahn-Greene', 'Bleach', 'One line description of project.', 'Miscellaneous'), ] diff --git a/setup.py b/setup.py index b139d4dc..4aa92289 100755 --- a/setup.py +++ b/setup.py @@ -16,10 +16,10 @@ def get_long_desc(): - with io.open('README.rst', encoding='utf-8') as fp: + with open('README.rst', encoding='utf-8') as fp: desc = fp.read() desc += '\n\n' - with io.open('CHANGES', encoding='utf-8') as fp: + with open('CHANGES', encoding='utf-8') as fp: desc += fp.read() return desc @@ -27,7 +27,7 @@ def get_long_desc(): def get_version(): fn = os.path.join('bleach', '__init__.py') vsre = r"""^__version__ = ['"]([^'"]*)['"]""" - with io.open(fn, encoding='utf-8') as fp: + with open(fn, encoding='utf-8') as fp: version_file = fp.read() return re.search(vsre, version_file, re.M).group(1) diff --git a/tests/test_linkify.py b/tests/test_linkify.py index a215da51..2880dc75 100644 --- a/tests/test_linkify.py +++ b/tests/test_linkify.py @@ -48,7 +48,7 @@ def test_mangle_link(): def filter_url(attrs, new=False): if not attrs.get((None, "href"), "").startswith("http://bouncer"): quoted = quote_plus(attrs[(None, "href")]) - attrs[(None, "href")] = "http://bouncer/?u={0!s}".format(quoted) + attrs[(None, "href")] = "http://bouncer/?u={!s}".format(quoted) return attrs assert ( diff --git a/tests/test_unicode.py b/tests/test_unicode.py index db3545e1..8261675f 100644 --- a/tests/test_unicode.py +++ b/tests/test_unicode.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- import pytest from bleach import clean, linkify diff --git a/tests_website/data_to_json.py b/tests_website/data_to_json.py index 5f888b8c..3043bae8 100755 --- a/tests_website/data_to_json.py +++ b/tests_website/data_to_json.py @@ -39,7 +39,7 @@ def main(): for infn in ins: case_name = infn.rsplit(".test", 1)[0] - with open(infn, "r") as fin: + with open(infn) as fin: data, expected = fin.read().split("\n--\n") data = data.strip() expected = expected.strip() diff --git a/tests_website/open_test_page.py b/tests_website/open_test_page.py index 23e15277..b1bfb65f 100755 --- a/tests_website/open_test_page.py +++ b/tests_website/open_test_page.py @@ -36,4 +36,4 @@ browser = webbrowser.get(browser_name) browser.open_new_tab("http://localhost:8080") except Exception as error: - print("error getting test browser %s: %s" % (browser_name, error)) + print("error getting test browser {}: {}".format(browser_name, error)) diff --git a/tests_website/server.py b/tests_website/server.py index 2d25ea25..0f0ed740 100755 --- a/tests_website/server.py +++ b/tests_website/server.py @@ -27,7 +27,7 @@ class BleachCleanHandler(http.server.SimpleHTTPRequestHandler): def do_POST(self): content_len = int(self.headers.get("content-length", 0)) body = self.rfile.read(content_len) - print("read %s bytes: %s" % (content_len, body)) + print("read {} bytes: {}".format(content_len, body)) body = body.decode("utf-8") print("input: %r" % body)