| 1 |
from pypy.module.pyexpat.interp_pyexpat import global_storage |
| 2 |
from pytest import skip |
| 3 |
|
| 4 |
class AppTestPyexpat: |
| 5 |
spaceconfig = dict(usemodules=['pyexpat', '_multibytecodec']) |
| 6 |
|
| 7 |
def teardown_class(cls): |
| 8 |
global_storage.clear() |
| 9 |
|
| 10 |
def test_simple(self): |
| 11 |
import pyexpat |
| 12 |
p = pyexpat.ParserCreate() |
| 13 |
res = p.Parse("<xml></xml>") |
| 14 |
assert res == 1 |
| 15 |
|
| 16 |
exc = raises(pyexpat.ExpatError, p.Parse, "3") |
| 17 |
assert exc.value.lineno == 1 |
| 18 |
assert exc.value.offset == 11 |
| 19 |
assert exc.value.code == 9 |
| 20 |
|
| 21 |
pyexpat.ExpatError("error") |
| 22 |
|
| 23 |
def test_attributes(self): |
| 24 |
import pyexpat |
| 25 |
p = pyexpat.ParserCreate() |
| 26 |
def test_setget(p, attr, default=False): |
| 27 |
assert getattr(p, attr) is default |
| 28 |
for x in 0, 1, 2, 0: |
| 29 |
setattr(p, attr, x) |
| 30 |
assert getattr(p, attr) is bool(x), attr |
| 31 |
for attr in ('buffer_text', 'namespace_prefixes', 'ordered_attributes', |
| 32 |
'specified_attributes'): |
| 33 |
test_setget(p, attr) |
| 34 |
|
| 35 |
def test_version(self): |
| 36 |
import pyexpat |
| 37 |
assert pyexpat.EXPAT_VERSION.startswith('expat_') |
| 38 |
assert isinstance(pyexpat.version_info, tuple) |
| 39 |
assert isinstance(pyexpat.version_info[0], int) |
| 40 |
|
| 41 |
def test_malformed_xml(self): |
| 42 |
import sys |
| 43 |
if sys.platform == "darwin": |
| 44 |
skip("Fails with the version of expat on Mac OS 10.6.6") |
| 45 |
import pyexpat |
| 46 |
xml = "\0\r\n" |
| 47 |
parser = pyexpat.ParserCreate() |
| 48 |
exc = raises(pyexpat.ExpatError, "parser.Parse(xml, True)") |
| 49 |
assert 'unclosed token: line 2, column 0' in exc.value.args[0] |
| 50 |
|
| 51 |
def test_encoding_argument(self): |
| 52 |
import pyexpat |
| 53 |
for encoding_arg in (None, 'utf-8', 'iso-8859-1'): |
| 54 |
for namespace_arg in (None, '{'): |
| 55 |
print(encoding_arg, namespace_arg) |
| 56 |
p = pyexpat.ParserCreate(encoding_arg, namespace_arg) |
| 57 |
data = [] |
| 58 |
p.CharacterDataHandler = lambda s: data.append(s) |
| 59 |
encoding = encoding_arg is None and 'utf-8' or encoding_arg |
| 60 |
|
| 61 |
res = p.Parse(u"<xml>\u00f6</xml>".encode(encoding), True) |
| 62 |
assert res == 1 |
| 63 |
assert data == [u"\u00f6"] |
| 64 |
|
| 65 |
def test_get_handler(self): |
| 66 |
import pyexpat |
| 67 |
p = pyexpat.ParserCreate() |
| 68 |
assert p.StartElementHandler is None |
| 69 |
assert p.EndElementHandler is None |
| 70 |
def f(*args): pass |
| 71 |
p.StartElementHandler = f |
| 72 |
assert p.StartElementHandler is f |
| 73 |
def g(*args): pass |
| 74 |
p.EndElementHandler = g |
| 75 |
assert p.StartElementHandler is f |
| 76 |
assert p.EndElementHandler is g |
| 77 |
|
| 78 |
def test_intern(self): |
| 79 |
import pyexpat |
| 80 |
p = pyexpat.ParserCreate() |
| 81 |
def f(*args): pass |
| 82 |
p.StartElementHandler = f |
| 83 |
p.EndElementHandler = f |
| 84 |
p.Parse("<xml></xml>") |
| 85 |
assert len(p.intern) == 1 |
| 86 |
|
| 87 |
def test_set_buffersize(self): |
| 88 |
import pyexpat, sys |
| 89 |
p = pyexpat.ParserCreate() |
| 90 |
p.buffer_size = 150 |
| 91 |
assert p.buffer_size == 150 |
| 92 |
raises(OverflowError, setattr, p, 'buffer_size', sys.maxsize + 1) |
| 93 |
|
| 94 |
def test_encoding_xml(self): |
| 95 |
|
| 96 |
xml = "<?xml version='1.0' encoding='iso-8859-1'?><s>caf\xe9</s>" |
| 97 |
import pyexpat |
| 98 |
p = pyexpat.ParserCreate() |
| 99 |
def gotText(text): |
| 100 |
assert text == u"caf\xe9" |
| 101 |
p.CharacterDataHandler = gotText |
| 102 |
p.Parse(xml) |
| 103 |
|
| 104 |
def test_explicit_encoding(self): |
| 105 |
xml = "<?xml version='1.0'?><s>caf\xe9</s>" |
| 106 |
import pyexpat |
| 107 |
p = pyexpat.ParserCreate(encoding='iso-8859-1') |
| 108 |
def gotText(text): |
| 109 |
assert text == u"caf\xe9" |
| 110 |
p.CharacterDataHandler = gotText |
| 111 |
p.Parse(xml) |
| 112 |
|
| 113 |
def test_python_encoding(self): |
| 114 |
|
| 115 |
xml = "<?xml version='1.0' encoding='latin1'?><s>caf\xe9</s>" |
| 116 |
import pyexpat |
| 117 |
p = pyexpat.ParserCreate() |
| 118 |
def gotText(text): |
| 119 |
assert text == u"caf\xe9" |
| 120 |
p.CharacterDataHandler = gotText |
| 121 |
p.Parse(xml) |
| 122 |
|
| 123 |
def test_external_entity(self): |
| 124 |
xml = ('<!DOCTYPE doc [\n' |
| 125 |
' <!ENTITY test SYSTEM "whatever">\n' |
| 126 |
']>\n' |
| 127 |
'<doc>&test;</doc>') |
| 128 |
import pyexpat |
| 129 |
p = pyexpat.ParserCreate() |
| 130 |
def handler(*args): |
| 131 |
|
| 132 |
assert args == ('test', None, 'whatever', None) |
| 133 |
return True |
| 134 |
p.ExternalEntityRefHandler = handler |
| 135 |
p.Parse(xml) |
| 136 |
|
| 137 |
def test_errors(self): |
| 138 |
import types |
| 139 |
import pyexpat |
| 140 |
assert isinstance(pyexpat.errors, types.ModuleType) |
| 141 |
|
| 142 |
assert pyexpat.errors.XML_ERROR_SYNTAX == 'syntax error' |
| 143 |
assert (pyexpat.errors.XML_ERROR_INCORRECT_ENCODING == |
| 144 |
'encoding specified in XML declaration is incorrect') |
| 145 |
assert (pyexpat.errors.XML_ERROR_XML_DECL == |
| 146 |
'XML declaration not well-formed') |
| 147 |
|
| 148 |
def test_model(self): |
| 149 |
import pyexpat |
| 150 |
assert isinstance(pyexpat.model.XML_CTYPE_EMPTY, int) |
| 151 |
|
| 152 |
def test_read_chunks(self): |
| 153 |
import pyexpat |
| 154 |
from _io import BytesIO |
| 155 |
|
| 156 |
xml = b'<xml>' + (b' ' * 4096) + b'</xml>' |
| 157 |
sio = BytesIO(xml) |
| 158 |
try: |
| 159 |
class FakeReader(): |
| 160 |
def __init__(self): |
| 161 |
self.read_count = 0 |
| 162 |
|
| 163 |
def read(self, size): |
| 164 |
self.read_count += 1 |
| 165 |
assert size > 0 |
| 166 |
return sio.read(size) |
| 167 |
|
| 168 |
fake_reader = FakeReader() |
| 169 |
p = pyexpat.ParserCreate() |
| 170 |
p.ParseFile(fake_reader) |
| 171 |
assert fake_reader.read_count == 4 |
| 172 |
finally: |
| 173 |
sio.close() |
| 174 |
|
| 175 |
def test_entities(self): |
| 176 |
import pyexpat |
| 177 |
parser = pyexpat.ParserCreate(None, "") |
| 178 |
|
| 179 |
def startElement(tag, attrs): |
| 180 |
assert tag == 'http://www.w3.org/1999/02/22-rdf-syntax-ns#RDF' |
| 181 |
assert attrs == { |
| 182 |
'http://www.w3.org/XML/1998/namespacebase': |
| 183 |
'http://www.semanticweb.org/jiba/ontologies/2017/0/test'} |
| 184 |
parser.StartElementHandler = startElement |
| 185 |
parser.Parse("""<?xml version="1.0"?> |
| 186 |
|
| 187 |
<!DOCTYPE rdf:RDF [ |
| 188 |
<!ENTITY owl "http://www.w3.org/2002/07/owl#" > |
| 189 |
<!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" > |
| 190 |
<!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" > |
| 191 |
<!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" > |
| 192 |
]> |
| 193 |
|
| 194 |
<rdf:RDF xmlns="http://www.semanticweb.org/jiba/ontologies/2017/0/test#" |
| 195 |
xml:base="http://www.semanticweb.org/jiba/ontologies/2017/0/test" |
| 196 |
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" |
| 197 |
xmlns:owl="http://www.w3.org/2002/07/owl#" |
| 198 |
xmlns:xsd="http://www.w3.org/2001/XMLSchema#" |
| 199 |
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> |
| 200 |
</rdf:RDF> |
| 201 |
""", True) |
| 202 |
|
| 203 |
def test_ErrorString_crash(self): |
| 204 |
import pyexpat |
| 205 |
assert pyexpat.ErrorString(-1) is None |