Skip to content

Commit

Permalink
Drop Python 2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
tiran committed Mar 4, 2021
1 parent 06d7ab2 commit 2be0182
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 78 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["2.7", "3.5", "3.6", "3.7", "3.8", "3.9"]
python-version: ["3.6", "3.7", "3.8", "3.9"]
steps:
- uses: "actions/checkout@v2"
- uses: "actions/setup-python@v2"
Expand Down
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ defusedxml 0.7.0rc2
- Use GitHub Actions instead of TravisCI
- Restore ``ElementTree`` attribute of ``xml.etree`` module after patching


defusedxml 0.7.0rc1
-------------------

Expand Down
22 changes: 5 additions & 17 deletions defusedxml/ElementTree.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# defusedxml
#
# Copyright (c) 2013 by Christian Heimes <christian@python.org>
# Copyright (c) 2013-2020 by Christian Heimes <christian@python.org>
# Licensed to PSF under a Contributor Agreement.
# See https://www.python.org/psf/license for licensing details.
"""Defused xml.etree.ElementTree facade
Expand All @@ -13,14 +13,7 @@
from xml.etree.ElementTree import parse as _parse
from xml.etree.ElementTree import tostring

from .common import PY3

if PY3:
import importlib
else:
from xml.etree.ElementTree import XMLParser as _XMLParser
from xml.etree.ElementTree import iterparse as _iterparse
from xml.etree.ElementTree import ParseError
import importlib


from .common import (
Expand Down Expand Up @@ -68,8 +61,7 @@ def _get_py3_cls():
return _XMLParser, _iterparse, ParseError


if PY3:
_XMLParser, _iterparse, ParseError = _get_py3_cls()
_XMLParser, _iterparse, ParseError = _get_py3_cls()

_sentinel = object()

Expand All @@ -84,8 +76,7 @@ def __init__(
forbid_entities=True,
forbid_external=True,
):
# Python 2.x old style class
_XMLParser.__init__(self, target=target, encoding=encoding)
super().__init__(target=target, encoding=encoding)
if html is not _sentinel:
# the 'html' argument has been deprecated and ignored in all
# supported versions of Python. Python 3.8 finally removed it.
Expand All @@ -101,10 +92,7 @@ def __init__(
self.forbid_dtd = forbid_dtd
self.forbid_entities = forbid_entities
self.forbid_external = forbid_external
if PY3:
parser = self.parser
else:
parser = self._parser
parser = self.parser
if self.forbid_dtd:
parser.StartDoctypeDeclHandler = self.defused_start_doctype_decl
if self.forbid_entities:
Expand Down
10 changes: 5 additions & 5 deletions defusedxml/common.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# defusedxml
#
# Copyright (c) 2013 by Christian Heimes <christian@python.org>
# Copyright (c) 2013-2020 by Christian Heimes <christian@python.org>
# Licensed to PSF under a Contributor Agreement.
# See https://www.python.org/psf/license for licensing details.
"""Common constants, exceptions and helpe functions
"""
import sys
import xml.parsers.expat

PY3 = sys.version_info[0] == 3
PY3 = True

# Fail early when pyexpat is not installed correctly
if not hasattr(xml.parsers.expat, "ParserCreate"):
Expand All @@ -26,7 +26,7 @@ class DTDForbidden(DefusedXmlException):
"""Document type definition is forbidden"""

def __init__(self, name, sysid, pubid):
super(DTDForbidden, self).__init__()
super().__init__()
self.name = name
self.sysid = sysid
self.pubid = pubid
Expand All @@ -40,7 +40,7 @@ class EntitiesForbidden(DefusedXmlException):
"""Entity definition is forbidden"""

def __init__(self, name, value, base, sysid, pubid, notation_name):
super(EntitiesForbidden, self).__init__()
super().__init__()
self.name = name
self.value = value
self.base = base
Expand All @@ -57,7 +57,7 @@ class ExternalReferenceForbidden(DefusedXmlException):
"""Resolving an external reference is forbidden"""

def __init__(self, context, base, sysid, pubid):
super(ExternalReferenceForbidden, self).__init__()
super().__init__()
self.context = context
self.base = base
self.sysid = sysid
Expand Down
4 changes: 2 additions & 2 deletions defusedxml/expatreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(
forbid_entities=True,
forbid_external=True,
):
_ExpatParser.__init__(self, namespaceHandling, bufsize)
super().__init__(namespaceHandling, bufsize)
self.forbid_dtd = forbid_dtd
self.forbid_entities = forbid_entities
self.forbid_external = forbid_external
Expand All @@ -46,7 +46,7 @@ def defused_external_entity_ref_handler(self, context, base, sysid, pubid):
raise ExternalReferenceForbidden(context, base, sysid, pubid)

def reset(self):
_ExpatParser.reset(self)
super().reset()
parser = self._parser
if self.forbid_dtd:
parser.StartDoctypeDeclHandler = self.defused_start_doctype_decl
Expand Down
33 changes: 12 additions & 21 deletions defusedxml/xmlrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,14 @@

import io

from .common import DTDForbidden, EntitiesForbidden, ExternalReferenceForbidden, PY3

if PY3:
__origin__ = "xmlrpc.client"
from xmlrpc.client import ExpatParser
from xmlrpc import client as xmlrpc_client
from xmlrpc import server as xmlrpc_server
from xmlrpc.client import gzip_decode as _orig_gzip_decode
from xmlrpc.client import GzipDecodedResponse as _OrigGzipDecodedResponse
else:
__origin__ = "xmlrpclib"
from xmlrpclib import ExpatParser
import xmlrpclib as xmlrpc_client

xmlrpc_server = None
from xmlrpclib import gzip_decode as _orig_gzip_decode
from xmlrpclib import GzipDecodedResponse as _OrigGzipDecodedResponse
from .common import DTDForbidden, EntitiesForbidden, ExternalReferenceForbidden

__origin__ = "xmlrpc.client"
from xmlrpc.client import ExpatParser
from xmlrpc import client as xmlrpc_client
from xmlrpc import server as xmlrpc_server
from xmlrpc.client import gzip_decode as _orig_gzip_decode
from xmlrpc.client import GzipDecodedResponse as _OrigGzipDecodedResponse

try:
import gzip
Expand Down Expand Up @@ -87,7 +78,7 @@ def __init__(self, response, limit=None):
if limit >= 0 and len(data) > limit:
raise ValueError("max payload length exceeded")
self.stringio = io.BytesIO(data)
gzip.GzipFile.__init__(self, mode="rb", fileobj=self.stringio)
super().__init__(mode="rb", fileobj=self.stringio)

def read(self, n):
if self.limit >= 0:
Expand All @@ -99,16 +90,16 @@ def read(self, n):
raise ValueError("max payload length exceeded")
return data
else:
return gzip.GzipFile.read(self, n)
return super().read(n)

def close(self):
gzip.GzipFile.close(self)
super().close()
self.stringio.close()


class DefusedExpatParser(ExpatParser):
def __init__(self, target, forbid_dtd=False, forbid_entities=True, forbid_external=True):
ExpatParser.__init__(self, target)
super().__init__(target)
self.forbid_dtd = forbid_dtd
self.forbid_entities = forbid_entities
self.forbid_external = forbid_external
Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
[tool.black]
line-length = 98
# black does not yet support py39
target-version = ['py27', 'py35', 'py36', 'py37', 'py38']
target-version = ['py36', 'py37', 'py38']
6 changes: 1 addition & 5 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
from __future__ import absolute_import
import sys
from distutils.core import Command
import subprocess
Expand Down Expand Up @@ -52,15 +51,12 @@ def run(self):
"License :: OSI Approved :: Python Software Foundation License",
"Natural Language :: English",
"Programming Language :: Python",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Topic :: Text Processing :: Markup :: XML",
],
python_requires=">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*",
python_requires=">=3.6",
)
18 changes: 3 additions & 15 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
ExternalReferenceForbidden,
NotSupportedError,
)
from defusedxml.common import PY3


if sys.version_info < (3, 7):
warnings.filterwarnings("once", category=DeprecationWarning)
Expand Down Expand Up @@ -62,11 +60,7 @@


class DefusedTestCase(unittest.TestCase):

if PY3:
content_binary = False
else:
content_binary = True
content_binary = False

xml_dtd = os.path.join(HERE, "xmltestdata", "dtd.xml")
xml_external = os.path.join(HERE, "xmltestdata", "external.xml")
Expand Down Expand Up @@ -280,19 +274,13 @@ class TestDefusedSax(BaseTests):
dtd_external_ref = True

def parse(self, xmlfile, **kwargs):
if PY3:
result = io.StringIO()
else:
result = io.BytesIO()
result = io.StringIO()
handler = XMLGenerator(result)
self.module.parse(xmlfile, handler, **kwargs)
return result.getvalue()

def parseString(self, xmlstring, **kwargs):
if PY3:
result = io.StringIO()
else:
result = io.BytesIO()
result = io.StringIO()
handler = XMLGenerator(result)
self.module.parseString(xmlstring, handler, **kwargs)
return result.getvalue()
Expand Down
11 changes: 1 addition & 10 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = py27,py35,py36,py37,py38,py39,py310,black,pep8py2,pep8py3,doc
envlist = py36,py37,py38,py39,py310,black,pep8py3,doc
skip_missing_interpreters = true

[testenv]
Expand All @@ -23,13 +23,6 @@ commands = black --check --verbose \
{toxinidir}/defusedxml/
deps = black

[testenv:pep8py2]
basepython = python2.7
deps =
flake8
commands =
{envpython} -m flake8

[testenv:pep8py3]
basepython = python3
deps =
Expand All @@ -56,8 +49,6 @@ max-line-length = 98

[gh-actions]
python =
2.7: py27, pep8py2
3.5: py35
3.6: py36
3.7: py37
3.8: py38, pep8py3, doc
Expand Down

0 comments on commit 2be0182

Please sign in to comment.