Skip to content

Fix bare except in StringEncoded: use specific Unicode exception types - #1179

Open
koteshyelamati wants to merge 1 commit into
construct:masterfrom
koteshyelamati:master
Open

koteshyelamati wants to merge 1 commit into
construct:masterfrom
koteshyelamati:master

Conversation

@koteshyelamati

Copy link
Copy Markdown

In StringEncoded._decode and StringEncoded._encode, the bare except: clauses catch BaseException (including KeyboardInterrupt and SystemExit). Replace with the specific exceptions that bytes.decode() and str.encode() actually raise.

_decode:

# Before
try:
    return obj.decode(self.encoding)
except:
    raise StringError(...)

# After
try:
    return obj.decode(self.encoding)
except (UnicodeDecodeError, LookupError):
    raise StringError(...)

_encode:

# Before
try:
    return obj.encode(self.encoding)
except:
    raise StringError(...)

# After
try:
    return obj.encode(self.encoding)
except (UnicodeEncodeError, LookupError):
    raise StringError(...)
  • UnicodeDecodeError/UnicodeEncodeError: raised when bytes/string cannot be decoded/encoded with the given codec
  • LookupError: raised when self.encoding is an unknown/unsupported codec name

Both are Exception subclasses, so KeyboardInterrupt and SystemExit will now propagate correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant