Skip to content

Commit 467fcbb

Browse files
committed
Implement small missing pyexpat backend features
1 parent 554c62c commit 467fcbb

3 files changed

Lines changed: 40 additions & 28 deletions

File tree

‎graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/pyexpat/XMLParserBuiltins.java‎

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,7 @@ final class Handler extends DefaultHandler2 {
983983
int eventOrdinal;
984984
Locator locator;
985985
boolean keepCurrentPositionForNextCall;
986+
boolean notStandaloneDtd;
986987
final Map<String, ExternalEntityInfo> externalEntities = new HashMap<>();
987988
final Set<String> resolvedExternalEntities = new HashSet<>();
988989

@@ -1021,16 +1022,18 @@ public void startDocument() {
10211022

10221023
@Override
10231024
public void startDTD(String name, String publicId, String systemId) {
1024-
if (xmlDeclInfo != null && xmlDeclInfo.standalone == 0) {
1025-
call(parser.getNotStandaloneHandler());
1025+
notStandaloneDtd = (xmlDeclInfo != null && xmlDeclInfo.standalone == 0) ||
1026+
((systemId != null || publicId != null) && (xmlDeclInfo == null || xmlDeclInfo.standalone != 1));
1027+
if (notStandaloneDtd) {
1028+
callInt(parser.getNotStandaloneHandler());
10261029
}
10271030
call(parser.getStartDoctypeDeclHandler(), toTs(name), toTs(systemId), toOptionalTs(publicId), doctypeHasInternalSubset ? 1 : 0);
10281031
}
10291032

10301033
@Override
10311034
public void endDTD() {
1032-
if (xmlDeclInfo != null && xmlDeclInfo.standalone == 0) {
1033-
call(parser.getNotStandaloneHandler());
1035+
if (notStandaloneDtd) {
1036+
callInt(parser.getNotStandaloneHandler());
10341037
}
10351038
call(parser.getEndDoctypeDeclHandler());
10361039
}
@@ -1200,10 +1203,10 @@ private String attributeName(Attributes attrs, int i) {
12001203
return qName == null || qName.isEmpty() ? localName : qName;
12011204
}
12021205

1203-
private void call(Object handler, Object... args) {
1206+
private Object call(Object handler, Object... args) {
12041207
boolean shouldDeliver = eventOrdinal++ >= parser.getDeliveredEventCount();
12051208
if (!shouldDeliver) {
1206-
return;
1209+
return PNone.NO_VALUE;
12071210
}
12081211
if (!keepCurrentPositionForNextCall && locator != null) {
12091212
line = Math.max(1, locator.getLineNumber());
@@ -1215,7 +1218,15 @@ private void call(Object handler, Object... args) {
12151218
}
12161219
keepCurrentPositionForNextCall = false;
12171220
if (handler != PNone.NONE) {
1218-
CallNode.executeUncached(handler, args);
1221+
return CallNode.executeUncached(handler, args);
1222+
}
1223+
return PNone.NO_VALUE;
1224+
}
1225+
1226+
private void callInt(Object handler, Object... args) {
1227+
Object result = call(handler, args);
1228+
if (result != PNone.NO_VALUE) {
1229+
PyLongAsLongNode.executeUncached(result);
12191230
}
12201231
}
12211232

@@ -1510,20 +1521,27 @@ private static boolean detectDoctypeHasInternalSubset(byte[] data) {
15101521

15111522
@TruffleBoundary
15121523
private static XmlDeclInfo detectXmlDecl(byte[] data) {
1513-
if (data.length == 0 || data[0] != '<') {
1524+
if (data.length == 0) {
15141525
return null;
15151526
}
1516-
int end = -1;
1517-
for (int i = 1; i < data.length; i++) {
1518-
if (data[i - 1] == '?' && data[i] == '>') {
1519-
end = i + 1;
1520-
break;
1521-
}
1527+
String text;
1528+
if ((data.length >= 2 && data[0] == (byte) 0xfe && data[1] == (byte) 0xff) ||
1529+
(data.length >= 4 && data[0] == 0 && data[1] == '<' && data[2] == 0 && data[3] == '?')) {
1530+
text = new String(data, java.nio.charset.StandardCharsets.UTF_16BE);
1531+
} else if ((data.length >= 2 && data[0] == (byte) 0xff && data[1] == (byte) 0xfe) ||
1532+
(data.length >= 4 && data[0] == '<' && data[1] == 0 && data[2] == '?' && data[3] == 0)) {
1533+
text = new String(data, java.nio.charset.StandardCharsets.UTF_16LE);
1534+
} else {
1535+
text = new String(data, java.nio.charset.StandardCharsets.ISO_8859_1);
1536+
}
1537+
if (!text.isEmpty() && text.charAt(0) == '\ufeff') {
1538+
text = text.substring(1);
15221539
}
1523-
if (end == -1) {
1540+
int end = text.indexOf("?>");
1541+
if (end < 0) {
15241542
return null;
15251543
}
1526-
String decl = new String(data, 0, end, java.nio.charset.StandardCharsets.ISO_8859_1);
1544+
String decl = text.substring(0, end + 2);
15271545
if (!decl.startsWith("<?xml")) {
15281546
return null;
15291547
}
@@ -1583,6 +1601,9 @@ private static String formatErrorMessage(SAXParseException e) {
15831601
return "undefined entity &" + entity + ";: line " + e.getLineNumber() + ", column " + Math.max(0, e.getColumnNumber() - 1);
15841602
}
15851603
}
1604+
if (message.contains("Content is not allowed in trailing section")) {
1605+
return "junk after document element: line " + e.getLineNumber() + ", column " + Math.max(0, e.getColumnNumber() - 1);
1606+
}
15861607
return message;
15871608
}
15881609

‎graalpython/lib-python/3/test/test_pyexpat.py‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,10 @@ def test_supported_encodings(self, encoding):
320320
'UTF-8', 'utf-8', 'utf-16', 'utf-16le', 'utf-16be',
321321
'koi8-u', 'cp1125', 'cp1251', 'iso8859-5', 'mac-cyrillic',
322322
])
323-
@_skip_if_java_pyexpat_backend(
324-
"Java pyexpat backend currently does not support all encodings accepted through CPython's "
325-
"unknown-encoding handler and cannot report UTF-16 XML declarations consistently."
326-
)
327323
def test_supported_encodings2(self, encoding):
324+
if (_is_graalpy_java_pyexpat_backend() and
325+
encoding in ('cp1125', 'mac-cyrillic')):
326+
self.skipTest("Java pyexpat backend does not yet use Python's unknown-encoding handler")
328327
out = self.Outputter()
329328
parser = expat.ParserCreate()
330329
self._hookup_callbacks(parser, out)
@@ -864,10 +863,6 @@ def test_change_size_2(self):
864863
self.assertEqual(self.n, 4)
865864

866865
class ElementDeclHandlerTest(unittest.TestCase):
867-
@_skip_if_java_pyexpat_backend(
868-
"Java pyexpat backend currently does not enforce Expat's integer return-value contract for "
869-
"NotStandaloneHandler."
870-
)
871866
def test_trigger_leak(self):
872867
# Unfixed, this test would leak the memory of the so-called
873868
# "content model" in function ``my_ElementDeclHandler`` of pyexpat.

‎graalpython/lib-python/3/test/test_xml_etree.py‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,10 +1500,6 @@ def test_resource_warnings_not_exhausted(self):
15001500
del it, elem
15011501
gc_collect()
15021502

1503-
@_skip_if_java_pyexpat_backend(
1504-
"Java pyexpat backend currently cannot match Expat's error wording for iterparse "
1505-
"trailing-content failures ('junk after document element')."
1506-
)
15071503
def test_resource_warnings_failed_iteration(self):
15081504
self.addCleanup(os_helper.unlink, TESTFN)
15091505
with open(TESTFN, "wb") as f:

0 commit comments

Comments
 (0)