diff --git a/Doc/library/xml.sax.handler.rst b/Doc/library/xml.sax.handler.rst
index ae0877ca90d..3746a58c9b9 100644
--- a/Doc/library/xml.sax.handler.rst
+++ b/Doc/library/xml.sax.handler.rst
@@ -11,12 +11,12 @@
--------------
-The SAX API defines four kinds of handlers: content handlers, DTD handlers,
-error handlers, and entity resolvers. Applications normally only need to
-implement those interfaces whose events they are interested in; they can
-implement the interfaces in a single object or in multiple objects. Handler
-implementations should inherit from the base classes provided in the module
-:mod:`xml.sax.handler`, so that all methods get default implementations.
+The SAX API defines five kinds of handlers: content handlers, DTD handlers,
+error handlers, entity resolvers and lexical handlers. Applications normally
+only need to implement those interfaces whose events they are interested in;
+they can implement the interfaces in a single object or in multiple objects.
+Handler implementations should inherit from the base classes provided in the
+module :mod:`xml.sax.handler`, so that all methods get default implementations.
.. class:: ContentHandler
@@ -47,6 +47,12 @@ implementations should inherit from the base classes provided in the module
application. The methods of this object control whether errors are immediately
converted to exceptions or are handled in some other way.
+
+.. class:: LexicalHandler
+
+ Interface used by the parser to represent low freqency events which may not
+ be of interest to many applications.
+
In addition to these classes, :mod:`xml.sax.handler` provides symbolic constants
for the feature and property names.
@@ -114,7 +120,7 @@ for the feature and property names.
.. data:: property_lexical_handler
| value: ``"http://xml.org/sax/properties/lexical-handler"``
- | data type: xml.sax.sax2lib.LexicalHandler (not supported in Python 2)
+ | data type: xml.sax.handler.LexicalHandler (not supported in Python 2)
| description: An optional extension handler for lexical events like
comments.
| access: read/write
@@ -413,3 +419,45 @@ the passed-in exception object.
information will continue to be passed to the application. Raising an exception
in this method will cause parsing to end.
+
+.. _lexical-handler-objects:
+
+LexicalHandler Objects
+----------------------
+Optional SAX2 handler for lexical events.
+
+This handler is used to obtain lexical information about an XML
+document. Lexical information includes information describing the
+document encoding used and XML comments embedded in the document, as
+well as section boundaries for the DTD and for any CDATA sections.
+The lexical handlers are used in the same manner as content handlers.
+
+Set the LexicalHandler of an XMLReader by using the setProperty method
+with the property identifier
+``'http://xml.org/sax/properties/lexical-handler'``.
+
+
+.. method:: LexicalHandler.comment(content)
+
+ Reports a comment anywhere in the document (including the DTD and
+ outside the document element).
+
+.. method:: LexicalHandler.startDTD(name, public_id, system_id)
+
+ Reports the start of the DTD declarations if the document has an
+ associated DTD.
+
+.. method:: LexicalHandler.endDTD()
+
+ Reports the end of DTD declaration.
+
+.. method:: LexicalHandler.startCDATA()
+
+ Reports the start of a CDATA marked section.
+
+ The contents of the CDATA marked section will be reported through
+ the characters handler.
+
+.. method:: LexicalHandler.endCDATA()
+
+ Reports the end of a CDATA marked section.
diff --git a/Doc/whatsnew/3.10.rst b/Doc/whatsnew/3.10.rst
index 62bb1438416..2af0ea3f4dd 100644
--- a/Doc/whatsnew/3.10.rst
+++ b/Doc/whatsnew/3.10.rst
@@ -139,6 +139,13 @@ Add :data:`sys.orig_argv` attribute: the list of the original command line
arguments passed to the Python executable.
(Contributed by Victor Stinner in :issue:`23427`.)
+xml
+---
+
+Add a :class:`~xml.sax.handler.LexicalHandler` class to the
+:mod:`xml.sax.handler` module.
+(Contributed by Jonathan Gossage and Zackery Spytz in :issue:`35018`.)
+
Optimizations
=============
diff --git a/Lib/test/test_sax.py b/Lib/test/test_sax.py
index cfc674be5d1..801143f9b5f 100644
--- a/Lib/test/test_sax.py
+++ b/Lib/test/test_sax.py
@@ -13,7 +13,8 @@ except SAXReaderNotAvailable:
from xml.sax.saxutils import XMLGenerator, escape, unescape, quoteattr, \
XMLFilterBase, prepare_input_source
from xml.sax.expatreader import create_parser
-from xml.sax.handler import feature_namespaces, feature_external_ges
+from xml.sax.handler import (feature_namespaces, feature_external_ges,
+ LexicalHandler)
from xml.sax.xmlreader import InputSource, AttributesImpl, AttributesNSImpl
from io import BytesIO, StringIO
import codecs
@@ -1356,6 +1357,155 @@ class XmlReaderTest(XmlTestBase):
self.assertEqual(attrs.getQNameByName((ns_uri, "attr")), "ns:attr")
+class LexicalHandlerTest(unittest.TestCase):
+ def setUp(self):
+ self.parser = None
+
+ self.specified_version = '1.0'
+ self.specified_encoding = 'UTF-8'
+ self.specified_doctype = 'wish'
+ self.specified_entity_names = ('nbsp', 'source', 'target')
+ self.specified_comment = ('Comment in a DTD',
+ 'Really! You think so?')
+ self.test_data = StringIO()
+ self.test_data.write('\n'.
+ format(self.specified_version,
+ self.specified_encoding))
+ self.test_data.write('\n'.
+ format(self.specified_comment[0]))
+ self.test_data.write('\n'.
+ format(self.specified_doctype))
+ self.test_data.write('\n')
+ self.test_data.write('\n')
+ self.test_data.write('\n')
+ self.test_data.write('\n')
+ self.test_data.write('\n')
+ self.test_data.write('\n'.
+ format(self.specified_entity_names[0]))
+ self.test_data.write('\n'.
+ format(self.specified_entity_names[1]))
+ self.test_data.write('\n'.
+ format(self.specified_entity_names[2]))
+ self.test_data.write(']>\n')
+ self.test_data.write('<{}>'.format(self.specified_doctype))
+ self.test_data.write('