Merge pull request #444 from cjmayo/isinstance

Remove or replace uses of isinstance()
This commit is contained in:
Chris Mayo 2020-07-08 19:55:29 +01:00 committed by GitHub
commit a977e4d712
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 9 additions and 14 deletions

View file

@ -234,8 +234,7 @@ class FileUrl(urlbase.UrlBase):
with links to the files."""
if self.is_directory():
data = get_index_html(get_files(self.get_os_filename()))
if isinstance(data, str):
data = data.encode("iso8859-1", "ignore")
data = data.encode("iso8859-1", "ignore")
else:
data = super().read_content()
return data

View file

@ -70,7 +70,7 @@ else:
def path_safe(path):
"""Ensure path string is compatible with the platform file system encoding."""
if isinstance(path, str) and not os.path.supports_unicode_filenames:
if path and not os.path.supports_unicode_filenames:
path = path.encode(FSCODING, "replace").decode(FSCODING)
return path

View file

@ -276,10 +276,6 @@ class RobotFileParser:
useragent,
url,
)
if not isinstance(useragent, str):
useragent = useragent.encode("ascii", "ignore")
if not isinstance(url, str):
url = url.encode("ascii", "ignore")
if self.disallow_all:
log.debug(LOG_CHECK, " ... disallow all.")
return False

View file

@ -39,12 +39,12 @@ def ascii_safe(s):
"""Get ASCII string without raising encoding errors. Unknown
characters of the given encoding will be ignored.
@param s: the Unicode string to be encoded
@type s: unicode or None
@return: encoded ASCII version of s, or None if s was None
@rtype: string
@param s: the string to be encoded
@type s: string or None
@return: version of s containing only ASCII characters, or None if s was None
@rtype: string or None
"""
if isinstance(s, str):
if s:
s = s.encode('ascii', 'ignore').decode('ascii')
return s

View file

@ -183,7 +183,7 @@ def idna_encode(host):
to RFC 3490.
@raise: UnicodeError if hostname is not properly IDN encoded.
"""
if host and isinstance(host, str):
if host:
try:
host.encode('ascii')
return host, False
@ -255,7 +255,7 @@ def url_fix_common_typos(url):
def url_fix_mailto_urlsplit(urlparts):
"""Split query part of mailto url if found."""
sep = b"?" if isinstance(urlparts[2], bytes) else "?"
sep = "?"
if sep in urlparts[2]:
urlparts[2], urlparts[3] = urlparts[2].split(sep, 1)