Commits

Christian Heimes committed 99f53b7

adapt code to Python 2.6 and 3.x, still WIP

Comments (0)

Files changed (6)

 	$(MAKE) clean
 	@set -e; \
 	for python in $(PYTHONS); do \
-		echo "\n*** $$python without HMAC ***"; \
-		$$python $(SETUPFLAGS) setup.py -q test; \
-	done
-	$(MAKE) clean
-	@set -e; \
-	for python in $(PYTHONS); do \
-		echo "\n*** $$python with HMAC ***"; \
-		$$python $(SETUPFLAGS) setup.py -q build_ext -DSHA3_HMAC_SUPPORT test; \
+		echo "*** $$python ***"; \
+		$$python $(SETUPFLAGS) setup.py build test; \
 	done
 	$(MAKE) clean
 

defusedxml/ElementTree.py

 from xml.etree.ElementTree import iterparse as _iterparse
 from xml.etree.ElementTree import __all__
 
-from .common import DTDForbidden, EntityForbidden, PY3, _wire_module
+from .common import DTDForbidden, EntityForbidden, PY3, PY26
 
 
 __origin__ = "xml.etree.ElementTree"
                  forbid_dtd=False, forbid_entities=True):
         if PY3:
             super().__init__(html, target, encoding)
+        elif PY26:
+            # Python 2.x old style class
+            _XMLParser.__init__(self, html, target)
         else:
             # Python 2.x old style class
             _XMLParser.__init__(self, html, target, encoding)

defusedxml/common.py

 import sys
 
 PY3 = sys.version_info[0] == 3
+PY26 = sys.version_info[:2] == (2, 6)
 
 
 class DefusedXmlException(ValueError):

defusedxml/expatbuilder.py

 
     def reset(self):
         if PY3:
-            super().reset(self)
+            super().reset()
         else:
             DefusedExpatBuilder.reset(self)
         self._initNamespaces()
 #!/usr/bin/env python
 import sys
 import os
-from distutils.core import setup
+from distutils.core import setup, Command
+
+class PyTest(Command):
+    user_options = []
+    def initialize_options(self):
+        pass
+    def finalize_options(self):
+        pass
+    def run(self):
+        import sys,subprocess
+        errno = subprocess.call([sys.executable, "tests.py"])
+        raise SystemExit(errno)
+
 
 long_description = []
 with open("README.txt") as f:
 setup(
     name="defusedxml",
     version="0.1",
+    cmdclass={"test": PyTest},
     packages=["defusedxml"],
     author="Christian Heimes",
     author_email="christian@python.org",
 import sys
 import unittest
 import io
-from StringIO import StringIO
+#from StringIO import StringIO
 
 from xml.sax.saxutils import XMLGenerator
 
 from defusedxml import cElementTree, ElementTree, minidom, pulldom, sax
 from defusedxml import DTDForbidden, EntityForbidden, NotSupportedError
+from defusedxml.common import PY3, PY26
 
 
 try:
                 self.parseString = self.module.fromstring
             else:
                 self.parseString = self.module.parseString
+        if PY26:
+            # TODO
+            self.iterparse = None
         if not hasattr(self, "iterparse"):
             if hasattr(self.module, "iterparse"):
                 self.iterparse = self.module.iterparse
 
     def get_content(self, xmlfile):
-        with io.open(xmlfile, "rb") as f:
+        if PY3:
+            mode = "r"
+        else:
+            mode = "rb"
+        with io.open(xmlfile, mode) as f:
             return f.read()
 
     def test_simple_parse(self):
     iterparse = None
 
     def parse(self, xmlfile, **kwargs):
-        result = StringIO()
+        if PY3:
+            result = io.StringIO()
+        else:
+            result = io.BytesIO()
         handler = XMLGenerator(result)
         self.module.parse(xmlfile, handler, **kwargs)
 
     def parseString(self, xmlstring, **kwargs):
-        result = StringIO()
+        if PY3:
+            result = io.StringIO()
+        else:
+            result = io.BytesIO()
         handler = XMLGenerator(result)
         self.module.parseString(xmlstring, handler, **kwargs)