-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-128302: fix apparent bug in xml.dom.xmlbuilder.DOMBuilder.parse()
#128284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bcf9cc1
fix apparent bug in xml.dom.xmlbuilder.DOMBuilder.parse()
tungol 4e2c24c
add NEWS entry
tungol 6ce5ebd
update DOMEntityResolver._guess_media_encoding for python 3
tungol 9a0d009
update NEWS
tungol ad5275c
split the NEWS entry into two, and simplify _guess_media_encoding
tungol b0cfb6a
use Message.get_param
tungol 737f856
add a few tests for xml.dom.xmlbuilder
tungol File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import io | ||
import unittest | ||
from http import client | ||
from test.test_httplib import FakeSocket | ||
from unittest import mock | ||
from xml.dom import getDOMImplementation, minidom, xmlbuilder | ||
|
||
SMALL_SAMPLE = b"""<?xml version="1.0"?> | ||
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:xdc="http://www.xml.com/books"> | ||
<!-- A comment --> | ||
<title>Introduction to XSL</title> | ||
<hr/> | ||
<p><xdc:author xdc:attrib="prefixed attribute" attrib="other attrib">A. Namespace</xdc:author></p> | ||
</html>""" | ||
|
||
|
||
class XMLBuilderTest(unittest.TestCase): | ||
def test_entity_resolver(self): | ||
body = ( | ||
b"HTTP/1.1 200 OK\r\nContent-Type: text/xml; charset=utf-8\r\n\r\n" | ||
+ SMALL_SAMPLE | ||
) | ||
|
||
sock = FakeSocket(body) | ||
response = client.HTTPResponse(sock) | ||
response.begin() | ||
attrs = {"open.return_value": response} | ||
opener = mock.Mock(**attrs) | ||
|
||
resolver = xmlbuilder.DOMEntityResolver() | ||
|
||
with mock.patch("urllib.request.build_opener") as mock_build: | ||
mock_build.return_value = opener | ||
source = resolver.resolveEntity(None, "http://example.com/2000/svg") | ||
|
||
self.assertIsInstance(source, xmlbuilder.DOMInputSource) | ||
self.assertIsNone(source.publicId) | ||
self.assertEqual(source.systemId, "http://example.com/2000/svg") | ||
self.assertEqual(source.baseURI, "http://example.com/2000/") | ||
self.assertEqual(source.encoding, "utf-8") | ||
self.assertIs(source.byteStream, response) | ||
|
||
self.assertIsNone(source.characterStream) | ||
self.assertIsNone(source.stringData) | ||
|
||
def test_builder(self): | ||
imp = getDOMImplementation() | ||
self.assertIsInstance(imp, xmlbuilder.DOMImplementationLS) | ||
|
||
builder = imp.createDOMBuilder(imp.MODE_SYNCHRONOUS, None) | ||
self.assertIsInstance(builder, xmlbuilder.DOMBuilder) | ||
|
||
def test_parse_uri(self): | ||
body = ( | ||
b"HTTP/1.1 200 OK\r\nContent-Type: text/xml; charset=utf-8\r\n\r\n" | ||
+ SMALL_SAMPLE | ||
) | ||
|
||
sock = FakeSocket(body) | ||
response = client.HTTPResponse(sock) | ||
response.begin() | ||
attrs = {"open.return_value": response} | ||
opener = mock.Mock(**attrs) | ||
|
||
with mock.patch("urllib.request.build_opener") as mock_build: | ||
mock_build.return_value = opener | ||
|
||
imp = getDOMImplementation() | ||
builder = imp.createDOMBuilder(imp.MODE_SYNCHRONOUS, None) | ||
document = builder.parseURI("http://example.com/2000/svg") | ||
|
||
self.assertIsInstance(document, minidom.Document) | ||
self.assertEqual(len(document.childNodes), 1) | ||
|
||
def test_parse_with_systemId(self): | ||
response = io.BytesIO(SMALL_SAMPLE) | ||
|
||
with mock.patch("urllib.request.urlopen") as mock_open: | ||
mock_open.return_value = response | ||
|
||
imp = getDOMImplementation() | ||
source = imp.createDOMInputSource() | ||
builder = imp.createDOMBuilder(imp.MODE_SYNCHRONOUS, None) | ||
source.systemId = "http://example.com/2000/svg" | ||
document = builder.parse(source) | ||
|
||
self.assertIsInstance(document, minidom.Document) | ||
self.assertEqual(len(document.childNodes), 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/Library/2024-12-27-16-28-57.gh-issue-128302.2GMvyl.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Allow :meth:`!xml.dom.xmlbuilder.DOMParser.parse` to correctly handle | ||
:class:`!xml.dom.xmlbuilder.DOMInputSource` instances that only have a | ||
:attr:`!systemId` attribute set. |
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Library/2024-12-29-13-49-46.gh-issue-128302.psRpPN.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Fix :meth:`!xml.dom.xmlbuilder.DOMEntityResolver.resolveEntity`, which was | ||
broken by the Python 3.0 transition. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.