1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2025-10-21 14:53:44 +03:00

python: Add a test to reproduce bug #889

This commit is contained in:
Maks Verver
2025-04-05 13:15:03 +02:00
committed by Nick Wellnhofer
parent 8d415b8911
commit 4d24aa22ac
3 changed files with 64 additions and 1 deletions

View File

@@ -95,6 +95,7 @@ if py.found() == true
'tstURI.py',
'tstmem.py',
'tstxpath.py',
'unicode.py',
'validDTD.py',
'validRNG.py',
'validSchemas.py',

View File

@@ -45,7 +45,8 @@ PYTESTS= \
validRNG.py \
compareNodes.py \
xpathns.py \
xpathleak.py
xpathleak.py \
unicode.py
XMLS= \
tst.xml \

61
python/tests/unicode.py Executable file
View File

@@ -0,0 +1,61 @@
from io import BytesIO, StringIO
import libxml2
import sys
from xml.sax.handler import ContentHandler
from xml.sax.xmlreader import InputSource
import xml.sax
# Test data: an XML file with a 100,000 Unicode smileys, which expand
# into 400,000 bytes after UTF-8 encoding.
SMILEY = '\U0001f600'
TEXT = 100_000 * SMILEY
XML_STRING = '<?xml version="1.0" encoding="UTF-8" ?>\n<root>' + TEXT + '</root>'
XML_BYTES = XML_STRING.encode('utf-8')
def RunTest(test_name, source):
expected = TEXT
received = ''
class TestHandler(ContentHandler):
def characters(self, content):
nonlocal received
received += content
reader = xml.sax.make_parser(['drv_libxml2'])
reader.setContentHandler(TestHandler())
reader.parse(source)
if expected != received:
print(test_name, 'failed!')
print('Expected length:', len(expected))
print('Received length:', len(received))
print('Expected text: (prefix only)', expected[:100])
print('Received text: (prefix only)', received[:100])
sys.exit(1)
def TestBytesInput():
source = InputSource()
source.setByteStream(BytesIO(XML_BYTES))
RunTest('TestBytesInput', source)
def TestStringInput():
source = InputSource()
source.setCharacterStream(StringIO(XML_STRING))
RunTest('TestStringInput', source)
# Memory debug specific
libxml2.debugMemory(1)
TestBytesInput()
TestStringInput()
# Memory debug specific
libxml2.cleanupParser()
if libxml2.debugMemory(1) == 0:
print("OK")
else:
print("Memory leak %d bytes" % (libxml2.debugMemory(1)))