You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.4 KiB

4 years ago
  1. # defusedxml
  2. #
  3. # Copyright (c) 2013 by Christian Heimes <christian@python.org>
  4. # Licensed to PSF under a Contributor Agreement.
  5. # See http://www.python.org/psf/license for licensing details.
  6. """Defused xml.sax
  7. """
  8. from __future__ import print_function, absolute_import
  9. from xml.sax import InputSource as _InputSource
  10. from xml.sax import ErrorHandler as _ErrorHandler
  11. from . import expatreader
  12. __origin__ = "xml.sax"
  13. def parse(source, handler, errorHandler=_ErrorHandler(), forbid_dtd=False,
  14. forbid_entities=True, forbid_external=True):
  15. parser = make_parser()
  16. parser.setContentHandler(handler)
  17. parser.setErrorHandler(errorHandler)
  18. parser.forbid_dtd = forbid_dtd
  19. parser.forbid_entities = forbid_entities
  20. parser.forbid_external = forbid_external
  21. parser.parse(source)
  22. def parseString(string, handler, errorHandler=_ErrorHandler(),
  23. forbid_dtd=False, forbid_entities=True,
  24. forbid_external=True):
  25. from io import BytesIO
  26. if errorHandler is None:
  27. errorHandler = _ErrorHandler()
  28. parser = make_parser()
  29. parser.setContentHandler(handler)
  30. parser.setErrorHandler(errorHandler)
  31. parser.forbid_dtd = forbid_dtd
  32. parser.forbid_entities = forbid_entities
  33. parser.forbid_external = forbid_external
  34. inpsrc = _InputSource()
  35. inpsrc.setByteStream(BytesIO(string))
  36. parser.parse(inpsrc)
  37. def make_parser(parser_list=[]):
  38. return expatreader.create_parser()