Test (original) (raw)
changeset: 86842:0a56709eb798 user: Ezio Melotti ezio.melotti@gmail.com date: Sat Nov 02 17:08:24 2013 +0200 files: Doc/library/html.parser.rst Lib/html/parser.py Lib/test/test_htmlparser.py Misc/NEWS description: #15114: The html.parser module now raises a DeprecationWarning when the strict argument of HTMLParser or the HTMLParser.error method are used. diff -r cee56ef59a6a -r 0a56709eb798 Doc/library/html.parser.rst --- a/Doc/library/html.parser.rst Sat Nov 02 16:41:23 2013 +0200 +++ b/Doc/library/html.parser.rst Sat Nov 02 17:08:24 2013 +0200 @@ -74,7 +74,7 @@ def handle_data(self, data): print("Encountered some data :", data) - parser = MyHTMLParser(strict=False) + parser = MyHTMLParser() parser.feed('Test' '
Parse me!
') @@ -272,7 +272,7 @@ def handle_decl(self, data): print("Decl :", data) - parser = MyHTMLParser(strict=False) + parser = MyHTMLParser() Parsing a doctype:: diff -r cee56ef59a6a -r 0a56709eb798 Lib/html/parser.py --- a/Lib/html/parser.py Sat Nov 02 16:41:23 2013 +0200 +++ b/Lib/html/parser.py Sat Nov 02 17:08:24 2013 +0200 @@ -94,6 +94,8 @@ return result +_strict_sentinel = object() + class HTMLParser(_markupbase.ParserBase): """Find tags and other markup and call handler functions. @@ -116,16 +118,18 @@ CDATA_CONTENT_ELEMENTS = ("script", "style") - def __init__(self, strict=False): + def __init__(self, strict=_strict_sentinel): """Initialize and reset this instance. If strict is set to False (the default) the parser will parse invalid markup, otherwise it will raise an error. Note that the strict mode - is deprecated. + and argument are deprecated. """ - if strict: - warnings.warn("The strict mode is deprecated.", + if strict is not _strict_sentinel: + warnings.warn("The strict argument and mode are deprecated.", DeprecationWarning, stacklevel=2) + else: + strict = False # default self.strict = strict self.reset() @@ -151,6 +155,8 @@ self.goahead(1) def error(self, message): + warnings.warn("The 'error' method is deprecated.", + DeprecationWarning, stacklevel=2) raise HTMLParseError(message, self.getpos()) __starttag_text = None diff -r cee56ef59a6a -r 0a56709eb798 Lib/test/test_htmlparser.py --- a/Lib/test/test_htmlparser.py Sat Nov 02 16:41:23 2013 +0200 +++ b/Lib/test/test_htmlparser.py Sat Nov 02 17:08:24 2013 +0200 @@ -96,7 +96,9 @@ parser = self.get_collector() parser.feed(source) parser.close() - self.assertRaises(html.parser.HTMLParseError, parse) + with self.assertRaises(html.parser.HTMLParseError): + with self.assertWarns(DeprecationWarning): + parse() class HTMLParserStrictTestCase(TestCaseBase): @@ -360,7 +362,16 @@ class HTMLParserTolerantTestCase(HTMLParserStrictTestCase): def get_collector(self): - return EventCollector(strict=False) + return EventCollector() + + def test_deprecation_warnings(self): + with self.assertWarns(DeprecationWarning): + EventCollector(strict=True) + with self.assertWarns(DeprecationWarning): + EventCollector(strict=False) + with self.assertRaises(html.parser.HTMLParseError): + with self.assertWarns(DeprecationWarning): + EventCollector().error('test') def test_tolerant_parsing(self): self._run_check('te>>xt&a<<bc< a="">\n' @@ -676,7 +687,7 @@ class AttributesTolerantTestCase(AttributesStrictTestCase): def get_collector(self): - return EventCollector(strict=False) + return EventCollector() def test_attr_funky_names2(self): self._run_check( diff -r cee56ef59a6a -r 0a56709eb798 Misc/NEWS --- a/Misc/NEWS Sat Nov 02 16:41:23 2013 +0200 +++ b/Misc/NEWS Sat Nov 02 17:08:24 2013 +0200 @@ -31,6 +31,9 @@ Library ------- +- Issue #15114: The html.parser module now raises a DeprecationWarning when the + strict argument of HTMLParser or the HTMLParser.error method are used. + - Issue #19410: Undo the special-casing removal of '' for importlib.machinery.FileFinder. </bc<>/ezio.melotti@gmail.com