我们从Python开源项目中,提取了以下24个代码示例,用于说明如何使用xml.parsers.expat.ErrorString()。
def feed(self, data, isFinal = 0): if not self._parsing: self.reset() self._parsing = 1 self._cont_handler.startDocument() try: # The isFinal parameter is internal to the expat reader. # If it is set to true, expat will check validity of the entire # document. When feeding chunks, they are not normally final - # except when invoked from close. self._parser.Parse(data, isFinal) except expat.error, e: exc = SAXParseException(expat.ErrorString(e.code), e, self) # FIXME: when to invoke error()? self._err_handler.fatalError(exc)
def feed(self, data, isFinal = 0): if not self._parsing: self.reset() self._parsing = 1 self._cont_handler.startDocument() try: # The isFinal parameter is internal to the expat reader. # If it is set to true, expat will check validity of the entire # document. When feeding chunks, they are not normally final - # except when invoked from close. self._parser.Parse(data, isFinal) except expat.error as e: exc = SAXParseException(expat.ErrorString(e.code), e, self) # FIXME: when to invoke error()? self._err_handler.fatalError(exc)
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)
def _render_xml(self, xml_document, **kwargs): # Prepare the xml object to be processed by jinja2 self.log.debug('Rendering XML object') try: self.template_images = dict() self._prepare_template_tags(xml_document) template_string = self._unescape_entities(xml_document.toxml()) jinja_template = self.environment.from_string(template_string) result = jinja_template.render(**kwargs) result = self._encode_escape_chars(result) final_xml = parseString(result.encode('ascii', 'xmlcharrefreplace')) if self.template_images: self.replace_images(final_xml) return final_xml except ExpatError as e: near = result.split('\n')[e.lineno -1][e.offset-50:e.offset+50] raise ExpatError('ExpatError "%s" at line %d, column %d\nNear of: "[...]%s[...]"' % \ (ErrorString(e.code), e.lineno, e.offset, near)) except: self.log.error('Error rendering template:\n%s', xml_document.toprettyxml(), exc_info=True) self.log.error('Unescaped template was:\n{}'.format(template_string)) raise finally: self.log.debug('Rendering xml object finished')
def test_parse_again(self): parser = expat.ParserCreate() file = BytesIO(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)