Skip to content

Commit

Permalink
Simplify wrap_file_object (#1037)
Browse files Browse the repository at this point in the history
Remove code that is specific to Python 2.
  • Loading branch information
mportesdev committed Jul 13, 2023
1 parent a0d5fc6 commit 6d6ec6d
Showing 1 changed file with 3 additions and 12 deletions.
15 changes: 3 additions & 12 deletions bandit/formatters/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,9 @@


def wrap_file_object(fileobj):
"""Handle differences in Python 2 and 3 around writing bytes."""
# If it's not an instance of IOBase, we're probably using Python 2 and
# that is less finnicky about writing text versus bytes to a file.
if not isinstance(fileobj, io.IOBase):
return fileobj

# At this point we're using Python 3 and that will mangle text written to
# a file written in bytes mode. So, let's check if the file can handle
# text as opposed to bytes.
"""If the fileobj passed in cannot handle text, use TextIOWrapper
to handle the conversion.
"""
if isinstance(fileobj, io.TextIOBase):
return fileobj

# Finally, we've determined that the fileobj passed in cannot handle text,
# so we use TextIOWrapper to handle the conversion for us.
return io.TextIOWrapper(fileobj)

0 comments on commit 6d6ec6d

Please sign in to comment.