Skip to content

Commit

Permalink
[svn r4131] r5088@delle: sbehnel | 2009-03-02 14:06:27 +0100
Browse files Browse the repository at this point in the history
 fix crash bug when parsing an XMLSchema with imports from a filename

--HG--
branch : trunk
  • Loading branch information
scoder committed Mar 2, 2009
1 parent 2d1afec commit 9490a09
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 11 deletions.
10 changes: 10 additions & 0 deletions CHANGES.txt
Expand Up @@ -2,6 +2,16 @@
lxml changelog
==============

2.2 (?)
=======

Bugs fixed
----------

* Crash when parsing an XML Schema with external imports from a
filename.


2.2beta4 (2009-02-27)
=====================

Expand Down
10 changes: 10 additions & 0 deletions src/lxml/tests/test_import.xsd
@@ -0,0 +1,10 @@
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://codespeak.net/lxml/schema/ns1"
xmlns:a="http://codespeak.net/lxml/schema/ns"
>
<xsd:import
namespace="http://codespeak.net/lxml/schema/ns"
schemaLocation="test_inc.xsd" />

<xsd:element name="x" type="a:AType"/>
</xsd:schema>
10 changes: 10 additions & 0 deletions src/lxml/tests/test_inc.xsd
@@ -0,0 +1,10 @@
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://codespeak.net/lxml/schema/ns"
targetNamespace="http://codespeak.net/lxml/schema/ns">
<xsd:element name="a" type="AType"/>
<xsd:complexType name="AType">
<xsd:sequence>
<xsd:element name="b" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
20 changes: 16 additions & 4 deletions src/lxml/tests/test_xmlschema.py
Expand Up @@ -132,10 +132,21 @@ def test_xmlschema_file(self):
# this will only work if we access the file through path or
# file object..
f = open(fileInTestDir('test.xsd'), 'rb')
schema = etree.XMLSchema(file=f)
try:
schema = etree.XMLSchema(file=f)
finally:
f.close()
tree_valid = self.parse('<a><b></b></a>')
self.assert_(schema.validate(tree_valid))

def test_xmlschema_import_file(self):
# this will only work if we access the file through path or
# file object..
schema = etree.XMLSchema(file=fileInTestDir('test_import.xsd'))
tree_valid = self.parse(
'<a:x xmlns:a="http://codespeak.net/lxml/schema/ns1"><b></b></a:x>')
self.assert_(schema.validate(tree_valid))

def test_xmlschema_shortcut(self):
tree_valid = self.parse('<a><b></b></a>')
tree_invalid = self.parse('<a><c></c></a>')
Expand All @@ -152,10 +163,8 @@ def test_xmlschema_shortcut(self):
self.assert_(tree_valid.xmlschema(schema))
self.assert_(not tree_invalid.xmlschema(schema))

#
# schema + resolvers tests&data:
#

class ETreeXMLSchemaResolversTestCase(HelperTestCase):
resolver_schema_int = BytesIO("""\
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:etype="http://codespeak.net/lxml/test/external"
Expand Down Expand Up @@ -188,6 +197,8 @@ def resolve(self, url, id, context):
assert url == 'XXX.xsd'
return self.resolve_string(self.schema, context)

# tests:

def test_xmlschema_resolvers(self):
"""Test that resolvers work with schema."""
parser = etree.XMLParser()
Expand Down Expand Up @@ -259,6 +270,7 @@ def resolve(self, url, id, context):
def test_suite():
suite = unittest.TestSuite()
suite.addTests([unittest.makeSuite(ETreeXMLSchemaTestCase)])
suite.addTests([unittest.makeSuite(ETreeXMLSchemaResolversTestCase)])
suite.addTests(
[make_doctest('../../../doc/validation.txt')])
return suite
Expand Down
19 changes: 12 additions & 7 deletions src/lxml/xmlschema.pxi
Expand Up @@ -54,6 +54,7 @@ cdef class XMLSchema(_Validator):
parser_ctxt = xmlschema.xmlSchemaNewDocParserCtxt(fake_c_doc)
elif file is not None:
if _isString(file):
doc = None
filename = _encodeFilename(file)
self._error_log.connect()
parser_ctxt = xmlschema.xmlSchemaNewParserCtxt(_cstr(filename))
Expand All @@ -65,13 +66,17 @@ cdef class XMLSchema(_Validator):
raise XMLSchemaParseError, u"No tree or file given"

if parser_ctxt is not NULL:
# calling xmlSchemaParse on a schema with imports or
# includes will cause libxml2 to create an internal
# context for parsing, so push an implied context to route
# resolve requests to the document's parser
__GLOBAL_PARSER_CONTEXT.pushImpliedContextFromParser(doc._parser)
self._c_schema = xmlschema.xmlSchemaParse(parser_ctxt)
__GLOBAL_PARSER_CONTEXT.popImpliedContext()
if doc is None:
with nogil:
self._c_schema = xmlschema.xmlSchemaParse(parser_ctxt)
else:
# calling xmlSchemaParse on a schema with imports or
# includes will cause libxml2 to create an internal
# context for parsing, so push an implied context to route
# resolve requests to the document's parser
__GLOBAL_PARSER_CONTEXT.pushImpliedContextFromParser(doc._parser)
self._c_schema = xmlschema.xmlSchemaParse(parser_ctxt)
__GLOBAL_PARSER_CONTEXT.popImpliedContext()

if _LIBXML_VERSION_INT >= 20624:
xmlschema.xmlSchemaFreeParserCtxt(parser_ctxt)
Expand Down

0 comments on commit 9490a09

Please sign in to comment.