#21047: set the default value for the *convert_charrefs* argument of … · python/cpython@6fc16d8

GitHub

Original file line numberDiff line numberDiff line change@@ -16,15 +16,13 @@

1616This module defines a class :class:`HTMLParser` which serves as the basis for

1717parsing text files formatted in HTML (HyperText Mark-up Language) and XHTML.

181819-.. class:: HTMLParser(*, convert_charrefs=False)

19+.. class:: HTMLParser(*, convert_charrefs=True)

20202121 Create a parser instance able to parse invalid markup.

222223- If *convert_charrefs* is ``True`` (default: ``False``), all character

23+ If *convert_charrefs* is ``True`` (the default), all character

2424 references (except the ones in ``script``/``style`` elements) are

2525 automatically converted to the corresponding Unicode characters.

26- The use of ``convert_charrefs=True`` is encouraged and will become

27- the default in Python 3.5.

28262927 An :class:`.HTMLParser` instance is fed HTML data and calls handler methods

3028 when start tags, end tags, text, comments, and other markup elements are

@@ -37,6 +35,9 @@ parsing text files formatted in HTML (HyperText Mark-up Language) and XHTML.

3735 .. versionchanged:: 3.4

3836 *convert_charrefs* keyword argument added.

393738+ .. versionchanged:: 3.5

39+ The default value for argument *convert_charrefs* is now ``True``.

40+40414142Example HTML Parser Application

4243-------------------------------

Original file line numberDiff line numberDiff line change@@ -59,7 +59,6 @@

5959endtagfind=re.compile('</\s*([a-zA-Z][-.a-zA-Z0-9:_]*)\s*>')

6060616162-_default_sentinel=object()

63626463classHTMLParser(_markupbase.ParserBase):

6564"""Find tags and other markup and call handler functions.

@@ -85,17 +84,12 @@ class HTMLParser(_markupbase.ParserBase):

85848685CDATA_CONTENT_ELEMENTS= ("script", "style")

878688-def__init__(self, *, convert_charrefs=_default_sentinel):

87+def__init__(self, *, convert_charrefs=True):

8988"""Initialize and reset this instance.

908991- If convert_charrefs is True (default: False), all character references

90+ If convert_charrefs is True (the default), all character references

9291 are automatically converted to the corresponding Unicode characters.

9392 """

94-ifconvert_charrefsis_default_sentinel:

95-convert_charrefs=False# default

96-warnings.warn("The value of convert_charrefs will become True in "

97-"3.5. You are encouraged to set the value explicitly.",

98-DeprecationWarning, stacklevel=2)

9993self.convert_charrefs=convert_charrefs

10094self.reset()

10195Original file line numberDiff line numberDiff line change@@ -346,7 +346,8 @@ def test_condcoms(self):

346346self._run_check(html, expected)

347347348348deftest_convert_charrefs(self):

349-collector=lambda: EventCollectorCharrefs(convert_charrefs=True)

349+# default value for convert_charrefs is now True

350+collector=lambda: EventCollectorCharrefs()

350351self.assertTrue(collector().convert_charrefs)

351352charrefs= ['&quot;', '&#34;', '&#x22;', '&quot', '&#34', '&#x22']

352353# check charrefs in the middle of the text/attributes

@@ -383,10 +384,6 @@ def test_convert_charrefs(self):

383384self._run_check('no charrefs here', [('data', 'no charrefs here')],

384385collector=collector())

385386386-deftest_deprecation_warnings(self):

387-withself.assertWarns(DeprecationWarning):

388-EventCollector() # convert_charrefs not passed explicitly

389-390387# the remaining tests were for the "tolerant" parser (which is now

391388# the default), and check various kind of broken markup

392389deftest_tolerant_parsing(self):

Original file line numberDiff line numberDiff line change@@ -121,6 +121,9 @@ Core and Builtins

121121Library

122122-------

123123124+- Issue #21047: set the default value for the *convert_charrefs* argument

125+ of HTMLParser to True. Patch by Berker Peksag.

126+124127- Add an __all__ to html.entities.

125128126129- Issue #15114: the strict mode and argument of HTMLParser, HTMLParser.error,