Commits

Christian Heimes  committed 5e309f1 Merge

merge

  • Participants
  • Parent commits 4824a08, 32024df

Comments (0)

Files changed (9)

 *.pyo
 *.pyd
 *~
-
+__pycache__

File defusedxml/ElementTree.py

 
 
 from .common import (DTDForbidden, EntitiesForbidden,
-                     ExternalEntitiesForbidden)
+                     ExternalEntitiesForbidden, _generate_etree_functions)
 
 
 def _get_python_classes():
-    """Python 3.3 hides the pure Python code but defusedxml requires it
+    """Python 3.3 hides the pure Python code but defusedxml requires it.
 
-    The code is based on test.support.import_fresh_module
+    The code is based on test.support.import_fresh_module().
     """
     global _XMLParser, _iterparse, _IterParseIterator, ParseError
     pymodname = "xml.etree.ElementTree"
 # aliases
 XMLTreeBuilder = XMLParse = DefusedXMLParser
 
-
-def parse(source, parser=None, forbid_dtd=False, forbid_entities=True):
-    if parser is None:
-        parser = DefusedXMLParser(target=_TreeBuilder(),
-                                  forbid_dtd=forbid_dtd,
-                                  forbid_entities=forbid_entities)
-    return _parse(source, parser)
-
-if PY3:
-    def iterparse(source, events=None, parser=None, forbid_dtd=False,
-                  forbid_entities=True):
-        close_source = False
-        if not hasattr(source, "read"):
-            source = open(source, "rb")
-            close_source = True
-        if not parser:
-            parser = DefusedXMLParser(target=_TreeBuilder())
-        return _IterParseIterator(source, events, parser, close_source)
-else:
-    def iterparse(source, events=None, parser=None, forbid_dtd=False,
-                  forbid_entities=True):
-        if parser is None:
-            parser = DefusedXMLParser(target=_TreeBuilder())
-        return _iterparse(source, events, parser)
-
-
-def fromstring(text, forbid_dtd=False, forbid_entities=True):
-    parser = DefusedXMLParser(target=_TreeBuilder(),
-                              forbid_dtd=forbid_dtd,
-                              forbid_entities=forbid_entities)
-    parser.feed(text)
-    return parser.close()
-
+parse, iterparse, fromstring = _generate_etree_functions(DefusedXMLParser,
+        _TreeBuilder, _IterParseIterator, _parse, _iterparse)
 XML = fromstring

File defusedxml/cElementTree.py

 from xml.etree.ElementTree import iterparse as _iterparse
 
 from .ElementTree import DefusedXMLParser, _IterParseIterator
-from .common import PY3
+from .common import PY3, _generate_etree_functions
 
 __origin__ = "xml.etree.cElementTree"
 
 XMLTreeBuilder = XMLParse = DefusedXMLParser
 
-def parse(source, parser=None, forbid_dtd=False, forbid_entities=True):
-    if parser is None:
-        parser = DefusedXMLParser(target=_TreeBuilder(),
-                                  forbid_dtd=forbid_dtd,
-                                  forbid_entities=forbid_entities)
-    return _parse(source, parser)
-
-
-if PY3:
-    def iterparse(source, events=None, parser=None, forbid_dtd=False,
-                  forbid_entities=True):
-        close_source = False
-        if not hasattr(source, "read"):
-            source = open(source, "rb")
-            close_source = True
-        if not parser:
-            parser = DefusedXMLParser(target=_TreeBuilder())
-        return _IterParseIterator(source, events, parser, close_source)
-else:
-    def iterparse(source, events=None, parser=None, forbid_dtd=False,
-                  forbid_entities=True):
-        if parser is None:
-            parser = DefusedXMLParser(target=_TreeBuilder())
-        return _iterparse(source, events, parser)
-
-
-def fromstring(text, forbid_dtd=False, forbid_entities=True):
-    parser = DefusedXMLParser(target=_TreeBuilder(),
-                              forbid_dtd=forbid_dtd,
-                              forbid_entities=forbid_entities)
-    parser.feed(text)
-    return parser.close()
-
+parse, iterparse, fromstring = _generate_etree_functions(DefusedXMLParser,
+        _TreeBuilder, _IterParseIterator, _parse, _iterparse)
 XML = fromstring

File defusedxml/common.py

             continue
         value = getattr(srcmod, name)
         setattr(dstmod, name, value)
+
+
+def _generate_etree_functions(DefusedXMLParser, _TreeBuilder,
+            _IterParseIterator, _parse, _iterparse):
+    """Factory for functions needed by etree, dependent on whether
+    cElementTree or ElementTree is used."""
+    def parse(source, parser=None, forbid_dtd=False, forbid_entities=True):
+        if parser is None:
+            parser = DefusedXMLParser(target=_TreeBuilder(),
+                                      forbid_dtd=forbid_dtd,
+                                      forbid_entities=forbid_entities)
+        return _parse(source, parser)
+
+    def iterparse(source, events=None, parser=None, forbid_dtd=False,
+                forbid_entities=True):
+      if PY3:
+          close_source = False
+          if not hasattr(source, "read"):
+              source = open(source, "rb")
+              close_source = True
+          if not parser:
+              parser = DefusedXMLParser(target=_TreeBuilder())
+          return _IterParseIterator(source, events, parser, close_source)
+      else:
+          if parser is None:
+              parser = DefusedXMLParser(target=_TreeBuilder())
+          return _iterparse(source, events, parser)
+
+    def fromstring(text, forbid_dtd=False, forbid_entities=True):
+        parser = DefusedXMLParser(target=_TreeBuilder(),
+                                  forbid_dtd=forbid_dtd,
+                                  forbid_entities=forbid_entities)
+        parser.feed(text)
+        return parser.close()
+
+    return parse, iterparse, fromstring

File defusedxml/expatbuilder.py

 
 class DefusedExpatBuilder(_ExpatBuilder):
     def __init__(self, options=None, forbid_dtd=False, forbid_entities=True):
-        if PY3:
-            super().__init__(options)
-        else:
-            # Python 2.x old style class
-            _ExpatBuilder.__init__(self, options)
+        _ExpatBuilder.__init__(self, options)
         self.forbid_dtd = forbid_dtd
         self.forbid_entities = forbid_entities
 
         raise ExternalEntitiesForbidden(systemId, publicId)
 
     def install(self, parser):
-        if PY3:
-            super().install(parser)
-        else:
-            # Python 2.x old style class
-            _ExpatBuilder.install(self, parser)
+        _ExpatBuilder.install(self, parser)
 
         if self.forbid_dtd:
             parser.StartDoctypeDeclHandler = self.start_doctype_decl
     """Document builder that supports namespaces."""
 
     def install(self, parser):
-        # Python 3.x code doesn't use super() :(
         DefusedExpatBuilder.install(self, parser)
         if self._options.namespace_declarations:
             parser.StartNamespaceDeclHandler = (
                 self.start_namespace_decl_handler)
 
     def reset(self):
-        if PY3:
-            super().reset()
-        else:
-            DefusedExpatBuilder.reset(self)
+        DefusedExpatBuilder.reset(self)
         self._initNamespaces()
 
 
     Document node.
     """
     if namespaces:
-        builder = DefusedExpatBuilderNS(forbid_dtd=forbid_dtd,
-                                        forbid_entities=forbid_entities)
+        build_builder = DefusedExpatBuilderNS
     else:
-        builder = DefusedExpatBuilder(forbid_dtd=forbid_dtd,
-                                      forbid_entities=forbid_entities)
+        build_builder = DefusedExpatBuilder
+    builder = build_builder(forbid_dtd=forbid_dtd,
+                            forbid_entities=forbid_entities)
     return builder.parseString(string)

File defusedxml/expatreader.py

 
     def __init__(self, forbid_dtd=False, forbid_entities=True,
                  *args, **kwargs):
-        if PY3:
-            super().__init__(*args, **kwargs)
-        else:
-            # Python 2.x old style class
-            _ExpatParser.__init__(self, *args, **kwargs)
+        _ExpatParser.__init__(self, *args, **kwargs)
         self.forbid_dtd = forbid_dtd
         self.forbid_entities = forbid_entities
 
         raise ExternalEntitiesForbidden(systemId, publicId)
 
     def reset(self):
-        if PY3:
-            super().reset()
-        else:
-            # Python 2.x old style class
-            _ExpatParser.reset(self)
+        _ExpatParser.reset(self)
         parser = self._parser
         if self.forbid_dtd:
             parser.StartDoctypeDeclHandler = self.start_doctype_decl

File defusedxml/lxml.py

                 raise EntitiesForbidden(entity.name)
 
 
-def parse(source, parser=None, base_url=None, forbid_dtd=False, forbid_entities=True):
+def parse(source, parser=None, base_url=None, forbid_dtd=False,
+          forbid_entities=True):
     if parser is None:
         parser = getDefaultParser()
     elementtree = _etree.parse(source, parser, base_url=base_url)
     return elementtree
 
 
-def fromstring(text, parser=None, base_url=None, forbid_dtd=False, forbid_entities=True):
+def fromstring(text, parser=None, base_url=None, forbid_dtd=False,
+               forbid_entities=True):
     if parser is None:
         parser = getDefaultParser()
     rootelement = _etree.fromstring(text, parser, base_url=base_url)
 #!/usr/bin/env python
-from __future__ import print_function, absolute_import, division
+from __future__ import absolute_import
 import sys
 from distutils.core import setup, Command
 import subprocess
 import sys
 import unittest
 import io
-#from StringIO import StringIO
 
 from xml.sax.saxutils import XMLGenerator
 
 
 
 class BaseTests(unittest.TestCase):
+
     module = None
 
     if PY3:
 
     dtd_external_ref = False
 
-
     xml_dtd = os.path.join(HERE, "xmltestdata", "dtd.xml")
     xml_external = os.path.join(HERE, "xmltestdata", "external.xml")
     xml_quadratic = os.path.join(HERE, "xmltestdata", "quadratic.xml")
 
     def test_dtd_with_external_ref(self):
         if self.dtd_external_ref:
-            self.assertRaises(ExternalEntitiesForbidden, self.parse, self.xml_dtd)
+            self.assertRaises(ExternalEntitiesForbidden, self.parse,
+                              self.xml_dtd)
         else:
             self.parse(self.xml_dtd)
 
     def test_external_ref(self):
-        self.assertRaises(ExternalEntitiesForbidden, self.parse, self.xml_external,
-                          forbid_entities=False)
+        self.assertRaises(ExternalEntitiesForbidden, self.parse,
+                          self.xml_external, forbid_entities=False)
 
 
 class TestDefusedcElementTree(BaseTests):