Drops basestring and unicode built-ins from utils
These do not exist in Python 3. Calls were wrapped with `NameError`
try-except blocks, but that's not enough when it comes to type
annotation checking. Therefore it's simpler to get rid of them instead
of convincing mypy they still exist.
@@ -622,10 +622,6 @@ def quote(string):
def ascii(text):
""" Transliterate special unicode characters into pure ascii """
- try:
- if not isinstance(text, unicode):
- text = unicode(text)
- except NameError:
if not isinstance(text, str):
text = str(text)
return unicodedata.normalize('NFKD', text).encode('ascii', 'ignore')
@@ -1595,14 +1591,9 @@ class StructuredField(object):
""" Load the StructuredField from a string """
if version is not None:
self.version(version)
- # Make sure we got a text, convert to unicode if necessary
- try:
- if not isinstance(text, basestring):
- raise StructuredFieldError(
- "Invalid StructuredField, expecting string or unicode")
- if not isinstance(text, unicode):
+ # Make sure we got a text, convert from bytes if necessary
+ if isinstance(text, bytes):
text = text.decode("utf8")
- except NameError:
if not isinstance(text, str):
raise StructuredFieldError(
"Invalid StructuredField, expecting string")
@@ -1662,14 +1653,11 @@ class StructuredField(object):
def set(self, section, content, item=None):
""" Update content of given section or section item """
# Convert to string if necessary, keep lists untouched
- if not isinstance(content, list):
- try:
- if not isinstance(content, basestring):
- content = unicode(content)
- elif not isinstance(content, unicode):
+ if isinstance(content, list):
+ pass
+ elif isinstance(content, bytes):
content = content.decode("utf8")
- except BaseException:
- if not isinstance(content, str):
+ elif not isinstance(content, str):
content = str(content)
# Set the whole section content
if item is None:
