From 37e775cc5d145a4e5c573278c694a098b6c10066 Mon Sep 17 00:00:00 2001 From: Emilio Pozuelo Monfort Date: Thu, 16 Jul 2020 13:16:14 +0200 Subject: lib/python: use isinstance rather than types types.TypeFoo are gone in python3. --- lib/python/bugs.py | 26 +++++++++++++------------- lib/python/debian_support.py | 18 +++++++++++------- lib/python/helpers.py | 7 +++++++ lib/python/security_db.py | 7 +++---- lib/python/web_support.py | 24 +++++++++++++----------- 5 files changed, 47 insertions(+), 35 deletions(-) create mode 100644 lib/python/helpers.py (limited to 'lib') diff --git a/lib/python/bugs.py b/lib/python/bugs.py index 0c94d4aff3..f0ef8c9ab7 100644 --- a/lib/python/bugs.py +++ b/lib/python/bugs.py @@ -19,9 +19,10 @@ import debian_support import functools import os import re -import types import hashlib +from helpers import isstring + class Urgency(debian_support.PseudoEnum): pass def listUrgencies(): @@ -55,20 +56,19 @@ class PackageNote: def __init__(self, package, fixed_version, release, urgency): self.id = None self.package = package - if (fixed_version is not None - and type(fixed_version) in types.StringTypes): + if (isstring(fixed_version)): self.fixed_version = debian_support.Version(fixed_version) else: self.fixed_version = fixed_version if release == '': self.release = None else: - if type(release) == types.StringType: + if isstring(release): release = debian_support.internRelease(release) if release is None: raise ValueError("invalid release") self.release = release - if type(urgency) == types.StringType: + if isstring(urgency): urgency = internUrgency(urgency) if urgency is None: raise ValueError("invalid urgency") @@ -166,13 +166,13 @@ class PackageNoteParsed(PackageNote): class PackageNoteNoDSA: def __init__(self, package, release, comment, reason=None): - assert type(package) == types.StringType and package != '' - assert type(release) == types.StringType and release != '' - assert type(comment) == types.StringType + assert isstring(package) and package != '' + assert isstring(release) and release != '' + assert isstring(comment) if not reason: reason = '' else: - assert type(reason) == types.StringType + assert isstring(reason) self.package = package self.release = release self.comment = comment @@ -191,7 +191,7 @@ class BugBase: re_cve_name = re.compile(r'^CVE-\d{4}-\d{4,}$') def __init__(self, fname, lineno, date, name, description, comments): - assert type(fname) in types.StringTypes + assert isstring(fname) lineno = to_integer(lineno) self.source_file = fname self.source_line = lineno @@ -266,8 +266,8 @@ class Bug(BugBase): for n in notes: assert isinstance(n, PackageNote) \ or isinstance(n, PackageNoteNoDSA) - assert len(xref) == 0 or type(xref[0]) == types.StringType - assert type(not_for_us) == types.BooleanType + assert len(xref) == 0 or isstring(xref[0]) + assert isinstance(not_for_us, bool) BugBase.__init__(self, fname, lineno, date, name, description, comments) self.notes = notes @@ -307,7 +307,7 @@ class Bug(BugBase): class BugFromDB(Bug): def __init__(self, cursor, name): - assert type(name) in types.StringTypes + assert isstring(name) def lookup(bug): for r in cursor.execute('SELECT * FROM bugs WHERE name = ?', diff --git a/lib/python/debian_support.py b/lib/python/debian_support.py index a8a2c0ffed..3a79349cae 100644 --- a/lib/python/debian_support.py +++ b/lib/python/debian_support.py @@ -24,13 +24,15 @@ import os.path import re import sys import tempfile -import types + try: from urllib.request import urlopen except ImportError: from urllib2 import urlopen from cStringIO import StringIO +from helpers import isstring + try: from hashlib import sha1 except ImportError: @@ -57,7 +59,7 @@ class ParseError(Exception): """ def __init__(self, filename, lineno, msg): - assert type(lineno) == types.IntType + assert isinstance(lineno, int) self.filename = filename self.lineno = lineno self.msg = msg @@ -86,11 +88,13 @@ class Version: def __init__(self, version): """Creates a new Version object.""" - t = type(version) - if t == types.UnicodeType: - version = version.encode('UTF-8') - else: - assert t == types.StringType, repr(version) + try: + if isinstance(version, unicode): + version = version.encode('UTF-8') + except: + pass + + assert isstring(version), repr(version) assert version != "" self.__asString = version self.__forCompare = _version_normalize_regexp.sub("", version) diff --git a/lib/python/helpers.py b/lib/python/helpers.py new file mode 100644 index 0000000000..1bfcf94375 --- /dev/null +++ b/lib/python/helpers.py @@ -0,0 +1,7 @@ +# helpers.py -- utility functions that don't belong elsewhere + +def isstring(s): + try: + return isinstance(s, basestring) + except NameError: + return isinstance(s, str) diff --git a/lib/python/security_db.py b/lib/python/security_db.py index 5c1d49cf17..23d0beb95b 100644 --- a/lib/python/security_db.py +++ b/lib/python/security_db.py @@ -40,7 +40,6 @@ import os import os.path import re import sys -import types import zlib import config @@ -54,7 +53,7 @@ class InsertError(Exception): def __init__(self, errors): assert len(errors) > 0, errors - assert type(errors) == types.ListType, errors + assert isinstance(errors, list), errors self.errors = errors def __str__(self): @@ -62,12 +61,12 @@ class InsertError(Exception): def mergeLists(a, b): """Merges two lists.""" - if type(a) == types.UnicodeType: + if isstring(a): if a == "": a = [] else: a = a.split(',') - if type(b) == types.UnicodeType: + if isstring(b): if b == "": b = [] else: diff --git a/lib/python/web_support.py b/lib/python/web_support.py index 116cbec2be..37d36d037b 100644 --- a/lib/python/web_support.py +++ b/lib/python/web_support.py @@ -24,12 +24,13 @@ import struct import sys import grp import traceback -import types import urllib import threading import SocketServer import BaseHTTPServer +from helpers import isstring + class ServinvokeError(Exception): pass @@ -164,7 +165,7 @@ class URLFactory: for (key, value) in args.items(): if value is None: continue - if type(value) not in (types.ListType, types.TupleType): + if not isinstance(value, (list, tuple)): value = (value,) for v in value: arglist.append("%s=%s" % (urllib.quote(key), @@ -268,8 +269,6 @@ class VerbatimHTML(HTMLBase): def flatten(self, write): write(self.__contents) -_string_types = (types.StringType, types.UnicodeType) - class Compose(HTMLBase): """Glues a sequence of HTML snippets together, without enclosing it in a tag.""" @@ -278,7 +277,7 @@ class Compose(HTMLBase): def flatten(self, write): for x in self.__contents: - if type(x) in _string_types: + if isstring(x): write(escapeHTML(x)) else: x.flatten(write) @@ -325,7 +324,7 @@ class Tag(HTMLBase): closing = "" % self.__name try: for x in self.contents: - if type(x) in _string_types: + if isstring(x): write(escapeHTML(x)) else: x.flatten(write) @@ -494,7 +493,7 @@ def make_menu(convert, *entries): ul = [] append = ul.append for e in entries: - if type(e) == types.TupleType: + if isinstance(e, tuple): (relurl, label) = e append(LI(A(convert(relurl), label))) else: @@ -512,7 +511,7 @@ def make_numbered_list(entries): def make_list(lst, separator=", "): """Creates a list of HTML elements.""" - assert type(lst) != types.StringType + assert not isstring(lst) c = [] if lst: append = c.append @@ -657,9 +656,12 @@ class HTMLResult(Result): buf.write(self.doctype) buf.write('\n') def write_both(s): - if type(s) == types.UnicodeType: - buf.write(s.encode('UTF-8')) - else: + try: + if isinstance(s, unicode): + s = s.encode('UTF-8') + except: + pass + finally: buf.write(s) self.contents.flatten(write_both) buf = buf.getvalue() -- cgit v1.2.3