Python xml.parsers.expat.ParseFile() Examples

The following are 9 code examples of xml.parsers.expat.ParseFile(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module xml.parsers.expat , or try the search function .
Example #1
Source File: test_pyexpat.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_parse_again(self):
        parser = expat.ParserCreate()
        file = StringIO.StringIO(data)
        parser.ParseFile(file)
        # Issue 6676: ensure a meaningful exception is raised when attempting
        # to parse more than one XML document per xmlparser instance,
        # a limitation of the Expat library.
        with self.assertRaises(expat.error) as cm:
            parser.ParseFile(file)
        self.assertEqual(expat.ErrorString(cm.exception.code),
                          expat.errors.XML_ERROR_FINISHED) 
Example #2
Source File: test_pyexpat.py    From BinderFilter with MIT License 5 votes vote down vote up
def test_parse_file(self):
        # Try parsing a file
        out = self.Outputter()
        parser = expat.ParserCreate(namespace_separator='!')
        parser.returns_unicode = 1
        for name in self.handler_names:
            setattr(parser, name, getattr(out, name))
        file = StringIO.StringIO(data)

        parser.ParseFile(file)

        op = out.out
        self.assertEqual(op[0], 'PI: u\'xml-stylesheet\' u\'href="stylesheet.css"\'')
        self.assertEqual(op[1], "Comment: u' comment data '")
        self.assertEqual(op[2], "Notation declared: (u'notation', None, u'notation.jpeg', None)")
        self.assertEqual(op[3], "Unparsed entity decl: (u'unparsed_entity', None, u'entity.file', None, u'notation')")
        self.assertEqual(op[4], "Start element: u'root' {u'attr1': u'value1', u'attr2': u'value2\\u1f40'}")
        self.assertEqual(op[5], "NS decl: u'myns' u'http://www.python.org/namespace'")
        self.assertEqual(op[6], "Start element: u'http://www.python.org/namespace!subelement' {}")
        self.assertEqual(op[7], "Character data: u'Contents of subelements'")
        self.assertEqual(op[8], "End element: u'http://www.python.org/namespace!subelement'")
        self.assertEqual(op[9], "End of NS decl: u'myns'")
        self.assertEqual(op[10], "Start element: u'sub2' {}")
        self.assertEqual(op[11], 'Start of CDATA section')
        self.assertEqual(op[12], "Character data: u'contents of CDATA section'")
        self.assertEqual(op[13], 'End of CDATA section')
        self.assertEqual(op[14], "End element: u'sub2'")
        self.assertEqual(op[15], "External entity ref: (None, u'entity.file', None)")
        self.assertEqual(op[16], "End element: u'root'")

        # Issue 4877: expat.ParseFile causes segfault on a closed file.
        fp = open(test_support.TESTFN, 'wb')
        try:
            fp.close()
            parser = expat.ParserCreate()
            with self.assertRaises(ValueError):
                parser.ParseFile(fp)
        finally:
            test_support.unlink(test_support.TESTFN) 
Example #3
Source File: test_pyexpat.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_parse_file(self):
        # Try parsing a file
        out = self.Outputter()
        parser = expat.ParserCreate(namespace_separator='!')
        parser.returns_unicode = 1
        for name in self.handler_names:
            setattr(parser, name, getattr(out, name))
        file = StringIO.StringIO(data)

        parser.ParseFile(file)

        op = out.out
        self.assertEqual(op[0], 'PI: u\'xml-stylesheet\' u\'href="stylesheet.css"\'')
        self.assertEqual(op[1], "Comment: u' comment data '")
        self.assertEqual(op[2], "Notation declared: (u'notation', None, u'notation.jpeg', None)")
        self.assertEqual(op[3], "Unparsed entity decl: (u'unparsed_entity', None, u'entity.file', None, u'notation')")
        self.assertEqual(op[4], "Start element: u'root' {u'attr1': u'value1', u'attr2': u'value2\\u1f40'}")
        self.assertEqual(op[5], "NS decl: u'myns' u'http://www.python.org/namespace'")
        self.assertEqual(op[6], "Start element: u'http://www.python.org/namespace!subelement' {}")
        self.assertEqual(op[7], "Character data: u'Contents of subelements'")
        self.assertEqual(op[8], "End element: u'http://www.python.org/namespace!subelement'")
        self.assertEqual(op[9], "End of NS decl: u'myns'")
        self.assertEqual(op[10], "Start element: u'sub2' {}")
        self.assertEqual(op[11], 'Start of CDATA section')
        self.assertEqual(op[12], "Character data: u'contents of CDATA section'")
        self.assertEqual(op[13], 'End of CDATA section')
        self.assertEqual(op[14], "End element: u'sub2'")
        self.assertEqual(op[15], "External entity ref: (None, u'entity.file', None)")
        self.assertEqual(op[16], "End element: u'root'")

        # Issue 4877: expat.ParseFile causes segfault on a closed file.
        fp = open(test_support.TESTFN, 'wb')
        try:
            fp.close()
            parser = expat.ParserCreate()
            with self.assertRaises(ValueError):
                parser.ParseFile(fp)
        finally:
            test_support.unlink(test_support.TESTFN) 
Example #4
Source File: test_pyexpat.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_parse_again(self):
        parser = expat.ParserCreate()
        file = StringIO.StringIO(data)
        parser.ParseFile(file)
        # Issue 6676: ensure a meaningful exception is raised when attempting
        # to parse more than one XML document per xmlparser instance,
        # a limitation of the Expat library.
        with self.assertRaises(expat.error) as cm:
            parser.ParseFile(file)
        self.assertEqual(expat.ErrorString(cm.exception.code),
                          expat.errors.XML_ERROR_FINISHED) 
Example #5
Source File: test_pyexpat.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_parse_file(self):
        # Try parsing a file
        out = self.Outputter()
        parser = expat.ParserCreate(namespace_separator='!')
        parser.returns_unicode = 1
        for name in self.handler_names:
            setattr(parser, name, getattr(out, name))
        file = StringIO.StringIO(data)

        parser.ParseFile(file)

        op = out.out
        self.assertEqual(op[0], 'PI: u\'xml-stylesheet\' u\'href="stylesheet.css"\'')
        self.assertEqual(op[1], "Comment: u' comment data '")
        self.assertEqual(op[2], "Notation declared: (u'notation', None, u'notation.jpeg', None)")
        self.assertEqual(op[3], "Unparsed entity decl: (u'unparsed_entity', None, u'entity.file', None, u'notation')")
        self.assertEqual(op[4], "Start element: u'root' {u'attr1': u'value1', u'attr2': u'value2\\u1f40'}")
        self.assertEqual(op[5], "NS decl: u'myns' u'http://www.python.org/namespace'")
        self.assertEqual(op[6], "Start element: u'http://www.python.org/namespace!subelement' {}")
        self.assertEqual(op[7], "Character data: u'Contents of subelements'")
        self.assertEqual(op[8], "End element: u'http://www.python.org/namespace!subelement'")
        self.assertEqual(op[9], "End of NS decl: u'myns'")
        self.assertEqual(op[10], "Start element: u'sub2' {}")
        self.assertEqual(op[11], 'Start of CDATA section')
        self.assertEqual(op[12], "Character data: u'contents of CDATA section'")
        self.assertEqual(op[13], 'End of CDATA section')
        self.assertEqual(op[14], "End element: u'sub2'")
        self.assertEqual(op[15], "External entity ref: (None, u'entity.file', None)")
        self.assertEqual(op[16], "End element: u'root'")

        # Issue 4877: expat.ParseFile causes segfault on a closed file.
        fp = open(test_support.TESTFN, 'wb')
        try:
            fp.close()
            parser = expat.ParserCreate()
            with self.assertRaises(ValueError):
                parser.ParseFile(fp)
        finally:
            test_support.unlink(test_support.TESTFN) 
Example #6
Source File: test_pyexpat.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_parse_again(self):
        parser = expat.ParserCreate()
        file = StringIO.StringIO(data)
        parser.ParseFile(file)
        # Issue 6676: ensure a meaningful exception is raised when attempting
        # to parse more than one XML document per xmlparser instance,
        # a limitation of the Expat library.
        with self.assertRaises(expat.error) as cm:
            parser.ParseFile(file)
        self.assertEqual(expat.ErrorString(cm.exception.code),
                          expat.errors.XML_ERROR_FINISHED) 
Example #7
Source File: test_pyexpat.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_parse_file(self):
        # Try parsing a file
        out = self.Outputter()
        parser = expat.ParserCreate(namespace_separator='!')
        parser.returns_unicode = 1
        for name in self.handler_names:
            setattr(parser, name, getattr(out, name))
        file = StringIO.StringIO(data)

        parser.ParseFile(file)

        op = out.out
        self.assertEqual(op[0], 'PI: u\'xml-stylesheet\' u\'href="stylesheet.css"\'')
        self.assertEqual(op[1], "Comment: u' comment data '")
        self.assertEqual(op[2], "Notation declared: (u'notation', None, u'notation.jpeg', None)")
        self.assertEqual(op[3], "Unparsed entity decl: (u'unparsed_entity', None, u'entity.file', None, u'notation')")
        self.assertEqual(op[4], "Start element: u'root' {u'attr1': u'value1', u'attr2': u'value2\\u1f40'}")
        self.assertEqual(op[5], "NS decl: u'myns' u'http://www.python.org/namespace'")
        self.assertEqual(op[6], "Start element: u'http://www.python.org/namespace!subelement' {}")
        self.assertEqual(op[7], "Character data: u'Contents of subelements'")
        self.assertEqual(op[8], "End element: u'http://www.python.org/namespace!subelement'")
        self.assertEqual(op[9], "End of NS decl: u'myns'")
        self.assertEqual(op[10], "Start element: u'sub2' {}")
        self.assertEqual(op[11], 'Start of CDATA section')
        self.assertEqual(op[12], "Character data: u'contents of CDATA section'")
        self.assertEqual(op[13], 'End of CDATA section')
        self.assertEqual(op[14], "End element: u'sub2'")
        self.assertEqual(op[15], "External entity ref: (None, u'entity.file', None)")
        self.assertEqual(op[16], "End element: u'root'")

        # Issue 4877: expat.ParseFile causes segfault on a closed file.
        fp = open(test_support.TESTFN, 'wb')
        try:
            fp.close()
            parser = expat.ParserCreate()
            with self.assertRaises(ValueError):
                parser.ParseFile(fp)
        finally:
            test_support.unlink(test_support.TESTFN) 
Example #8
Source File: test_pyexpat.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_parse_file(self):
        # Try parsing a file
        out = self.Outputter()
        parser = expat.ParserCreate(namespace_separator='!')
        parser.returns_unicode = 1
        for name in self.handler_names:
            setattr(parser, name, getattr(out, name))
        file = StringIO.StringIO(data)

        parser.ParseFile(file)

        op = out.out
        self.assertEqual(op[0], 'PI: u\'xml-stylesheet\' u\'href="stylesheet.css"\'')
        self.assertEqual(op[1], "Comment: u' comment data '")
        self.assertEqual(op[2], "Notation declared: (u'notation', None, u'notation.jpeg', None)")
        self.assertEqual(op[3], "Unparsed entity decl: (u'unparsed_entity', None, u'entity.file', None, u'notation')")
        self.assertEqual(op[4], "Start element: u'root' {u'attr1': u'value1', u'attr2': u'value2\\u1f40'}")
        self.assertEqual(op[5], "NS decl: u'myns' u'http://www.python.org/namespace'")
        self.assertEqual(op[6], "Start element: u'http://www.python.org/namespace!subelement' {}")
        self.assertEqual(op[7], "Character data: u'Contents of subelements'")
        self.assertEqual(op[8], "End element: u'http://www.python.org/namespace!subelement'")
        self.assertEqual(op[9], "End of NS decl: u'myns'")
        self.assertEqual(op[10], "Start element: u'sub2' {}")
        self.assertEqual(op[11], 'Start of CDATA section')
        self.assertEqual(op[12], "Character data: u'contents of CDATA section'")
        self.assertEqual(op[13], 'End of CDATA section')
        self.assertEqual(op[14], "End element: u'sub2'")
        self.assertEqual(op[15], "External entity ref: (None, u'entity.file', None)")
        self.assertEqual(op[16], "End element: u'root'")

        # Issue 4877: expat.ParseFile causes segfault on a closed file.
        fp = open(test_support.TESTFN, 'wb')
        try:
            fp.close()
            parser = expat.ParserCreate()
            with self.assertRaises(ValueError):
                parser.ParseFile(fp)
        finally:
            test_support.unlink(test_support.TESTFN) 
Example #9
Source File: test_pyexpat.py    From ironpython2 with Apache License 2.0 4 votes vote down vote up
def test_parse_file(self):
        # Try parsing a file
        out = self.Outputter()
        parser = expat.ParserCreate(namespace_separator='!')
        parser.returns_unicode = 1
        for name in self.handler_names:
            setattr(parser, name, getattr(out, name))
        file = StringIO.StringIO(data)

        parser.ParseFile(file)

        op = out.out
        # https://github.com/IronLanguages/ironpython2/issues/464
        if sys.platform == 'cli':
            self.assertEqual(op[0], 'PI: \'xml-stylesheet\' \'href="stylesheet.css"\'')
            self.assertEqual(op[1], "Comment: ' comment data '")
            #self.assertEqual(op[2], "Notation declared: ('notation', None, 'notation.jpeg', None)")
            #self.assertEqual(op[3], "Unparsed entity decl: ('unparsed_entity', None, 'entity.file', None, 'notation')")
            self.assertEqual(op[2], "Start element: 'root' {'attr1': 'value1', 'attr2': u'value2\\u1f40'}")
            self.assertEqual(op[3], "NS decl: 'myns' 'http://www.python.org/namespace'")
            self.assertEqual(op[4], "Start element: 'http://www.python.org/namespace!subelement' {}")
            self.assertEqual(op[5], "Character data: 'Contents of subelements'")
            self.assertEqual(op[6], "End element: 'http://www.python.org/namespace!subelement'")
            self.assertEqual(op[7], "End of NS decl: 'myns'")
            self.assertEqual(op[8], "Start element: 'sub2' {}")
            self.assertEqual(op[9], 'Start of CDATA section')
            self.assertEqual(op[10], "Character data: 'contents of CDATA section'")
            self.assertEqual(op[11], 'End of CDATA section')
            self.assertEqual(op[12], "End element: 'sub2'")
            #self.assertEqual(op[13], "External entity ref: (None, 'entity.file', None)")
            self.assertEqual(op[13], "End element: 'root'")
        else:
            self.assertEqual(op[0], 'PI: u\'xml-stylesheet\' u\'href="stylesheet.css"\'')
            self.assertEqual(op[1], "Comment: u' comment data '")
            self.assertEqual(op[2], "Notation declared: (u'notation', None, u'notation.jpeg', None)")
            self.assertEqual(op[3], "Unparsed entity decl: (u'unparsed_entity', None, u'entity.file', None, u'notation')")
            self.assertEqual(op[4], "Start element: u'root' {u'attr1': u'value1', u'attr2': u'value2\\u1f40'}")
            self.assertEqual(op[5], "NS decl: u'myns' u'http://www.python.org/namespace'")
            self.assertEqual(op[6], "Start element: u'http://www.python.org/namespace!subelement' {}")
            self.assertEqual(op[7], "Character data: u'Contents of subelements'")
            self.assertEqual(op[8], "End element: u'http://www.python.org/namespace!subelement'")
            self.assertEqual(op[9], "End of NS decl: u'myns'")
            self.assertEqual(op[10], "Start element: u'sub2' {}")
            self.assertEqual(op[11], 'Start of CDATA section')
            self.assertEqual(op[12], "Character data: u'contents of CDATA section'")
            self.assertEqual(op[13], 'End of CDATA section')
            self.assertEqual(op[14], "End element: u'sub2'")
            self.assertEqual(op[15], "External entity ref: (None, u'entity.file', None)")
            self.assertEqual(op[16], "End element: u'root'")

        # Issue 4877: expat.ParseFile causes segfault on a closed file.
        fp = open(test_support.TESTFN, 'wb')
        try:
            fp.close()
            parser = expat.ParserCreate()
            with self.assertRaises(ValueError):
                parser.ParseFile(fp)
        finally:
            test_support.unlink(test_support.TESTFN)