[elbe-devel] [PATCH 2/4] python3: Remove urllib compatibility imports

Torben Hohn torben.hohn at linutronix.de
Wed Oct 28 14:58:26 CET 2020


On Fri, Oct 02, 2020 at 07:46:18PM +0200, bage at linutronix.de wrote:
> From: Bastian Germann <bage at linutronix.de>
> 
> Elbe does not need the Python 2 imports.
> Remove them which makes the code less noisy.
> 
> Signed-off-by: Bastian Germann <bage at linutronix.de>

Reviewed-by: Torben Hohn <torben.hohn at linutronix.de>

> ---
>  elbepack/archivedir.py       |  6 +-----
>  elbepack/commands/control.py |  8 ++------
>  elbepack/commands/prjrepo.py |  8 ++------
>  elbepack/debinstaller.py     | 15 +--------------
>  elbepack/elbexml.py          | 14 ++++++--------
>  elbepack/pbuilder.py         |  6 ++----
>  elbepack/soapclient.py       |  8 ++------
>  elbepack/xmlpreprocess.py    |  7 ++-----
>  8 files changed, 18 insertions(+), 54 deletions(-)
> 
> diff --git a/elbepack/archivedir.py b/elbepack/archivedir.py
> index 4b9ace9a9..983798c52 100644
> --- a/elbepack/archivedir.py
> +++ b/elbepack/archivedir.py
> @@ -8,11 +8,7 @@ import os
>  import re
>  import sys
>  
> -# The urlparse module is renamed to urllib.parse in Python 3.
> -try:
> -    from urllib.parse import urljoin,urlparse
> -except ImportError:
> -    from urlparse import urljoin,urlparse
> +from urllib.parse import urljoin,urlparse
>  
>  from base64 import encodestring, standard_b64decode
>  from subprocess import CalledProcessError
> diff --git a/elbepack/commands/control.py b/elbepack/commands/control.py
> index 50a3f01a3..ebffad6d8 100644
> --- a/elbepack/commands/control.py
> +++ b/elbepack/commands/control.py
> @@ -8,13 +8,9 @@
>  import socket
>  import sys
>  
> +from http.client import BadStatusLine
>  from optparse import (OptionParser, OptionGroup)
> -try:
> -    from urllib.error import URLError
> -    from http.client import BadStatusLine
> -except ImportError:
> -    from urllib2 import URLError
> -    from httplib import BadStatusLine
> +from urllib.error import URLError
>  
>  from suds import WebFault
>  
> diff --git a/elbepack/commands/prjrepo.py b/elbepack/commands/prjrepo.py
> index dff01b2d7..17073482c 100644
> --- a/elbepack/commands/prjrepo.py
> +++ b/elbepack/commands/prjrepo.py
> @@ -8,13 +8,9 @@
>  import socket
>  import sys
>  
> +from http.client import BadStatusLine
>  from optparse import (OptionParser, OptionGroup)
> -try:
> -    from urllib.error import URLError
> -    from http.client import BadStatusLine
> -except ImportError:
> -    from urllib2 import URLError
> -    from httplib import BadStatusLine
> +from urllib.error import URLError
>  
>  from suds import WebFault
>  
> diff --git a/elbepack/debinstaller.py b/elbepack/debinstaller.py
> index c1dfb8ab5..d880e5e34 100644
> --- a/elbepack/debinstaller.py
> +++ b/elbepack/debinstaller.py
> @@ -7,21 +7,8 @@ import sys
>  import os
>  import re
>  
> -# different module names in python 2 and 3
> -try:
> -    import urllib.request
> -
> -    # when running inside pylint this import fails
> -    # disable no-member here
> -    #
> -    # pylint: disable=no-member
> -
> -    urlopen = urllib.request.urlopen
> -except ImportError:
> -    import urllib2
> -    urlopen = urllib2.urlopen
> -
>  from shutil import copyfile
> +from urllib.request import urlopen
>  
>  from gpg import core
>  from gpg.constants import PROTOCOL_OpenPGP
> diff --git a/elbepack/elbexml.py b/elbepack/elbexml.py
> index 4ea4fb58e..50126753d 100644
> --- a/elbepack/elbexml.py
> +++ b/elbepack/elbexml.py
> @@ -10,19 +10,17 @@
>  
>  import os
>  import re
> +
> +from urllib.error import URLError
> +from urllib.request import (urlopen, install_opener, build_opener,
> +                            HTTPPasswordMgrWithDefaultRealm,
> +                            HTTPBasicAuthHandler)
> +
>  from elbepack.treeutils import etree
>  from elbepack.validate import validate_xml
>  from elbepack.xmldefaults import ElbeDefaults
>  
>  from elbepack.version import elbe_version, is_devel
> -try:
> -    from urllib.request import (urlopen, install_opener, build_opener,
> -                                HTTPPasswordMgrWithDefaultRealm,
> -                                HTTPBasicAuthHandler)
> -    from urllib.error import URLError
> -except ImportError:
> -    from urllib2 import (urlopen, install_opener, build_opener, URLError,
> -                         HTTPPasswordMgrWithDefaultRealm, HTTPBasicAuthHandler)
>  
>  class ValidationError(Exception):
>      def __init__(self, validation):
> diff --git a/elbepack/pbuilder.py b/elbepack/pbuilder.py
> index 5cae2b986..d13ef82af 100644
> --- a/elbepack/pbuilder.py
> +++ b/elbepack/pbuilder.py
> @@ -7,10 +7,8 @@
>  # SPDX-License-Identifier: GPL-3.0-or-later
>  
>  import os
> -try:
> -    from urllib.request import urlopen
> -except ImportError:
> -    from urllib2 import urlopen
> +
> +from urllib.request import urlopen
>  
>  from elbepack.filesystem import Filesystem
>  
> diff --git a/elbepack/soapclient.py b/elbepack/soapclient.py
> index 7652a033e..81ef8fe0f 100644
> --- a/elbepack/soapclient.py
> +++ b/elbepack/soapclient.py
> @@ -14,12 +14,8 @@ import os
>  import fnmatch
>  
>  from datetime import datetime
> -try:
> -    from urllib.error import URLError
> -    from http.client import BadStatusLine
> -except ImportError:
> -    from urllib2 import URLError
> -    from httplib import BadStatusLine
> +from http.client import BadStatusLine
> +from urllib.error import URLError
>  
>  import deb822   # package for dealing with Debian related data
>  
> diff --git a/elbepack/xmlpreprocess.py b/elbepack/xmlpreprocess.py
> index f0ea733a0..a6f5276ff 100644
> --- a/elbepack/xmlpreprocess.py
> +++ b/elbepack/xmlpreprocess.py
> @@ -8,15 +8,12 @@
>  import os
>  import re
>  import sys
> -try:
> -    from urllib.request import urlopen
> -    from urllib.error import HTTPError
> -except ImportError:
> -    from urllib2 import urlopen, HTTPError
>  
>  from tempfile import NamedTemporaryFile
>  from optparse import OptionGroup
>  from itertools import islice
> +from urllib.error import HTTPError
> +from urllib.request import urlopen
>  
>  from lxml import etree
>  from lxml.etree import XMLParser, parse, Element
> -- 
> 2.28.0
> 
> _______________________________________________
> elbe-devel mailing list
> elbe-devel at linutronix.de
> https://lists.linutronix.de/mailman/listinfo/elbe-devel

-- 
Torben Hohn
Linutronix GmbH | Bahnhofstrasse 3 | D-88690 Uhldingen-Mühlhofen
Phone: +49 7556 25 999 18; Fax.: +49 7556 25 999 99

Hinweise zum Datenschutz finden Sie hier (Informations on data privacy 
can be found here): https://linutronix.de/kontakt/Datenschutz.php

Linutronix GmbH | Firmensitz (Registered Office): Uhldingen-Mühlhofen | 
Registergericht (Registration Court): Amtsgericht Freiburg i.Br., HRB700 
806 | Geschäftsführer (Managing Directors): Heinz Egger, Thomas Gleixner


More information about the elbe-devel mailing list