Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wrong logging level applied when setting kwarg explain to True #146

Merged
merged 5 commits into from Nov 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

- [Short description of non-trivial change.]

### Fixed
- Wrong logging level applied when setting kwarg `explain` to True (PR #146)

## [2.0.8](https://github.com/Ousret/charset_normalizer/compare/2.0.7...2.0.8) (2021-11-24)
### Changed
- Improvement over Vietnamese detection (PR #126)
Expand Down
6 changes: 6 additions & 0 deletions charset_normalizer/api.py
Expand Up @@ -68,7 +68,9 @@ def from_bytes(
)

if explain:
previous_logger_level = logger.level # type: int
logger.addHandler(explain_handler)
logger.setLevel(logging.INFO)

length = len(sequences) # type: int

Expand All @@ -78,6 +80,7 @@ def from_bytes(
)
if explain:
logger.removeHandler(explain_handler)
logger.setLevel(previous_logger_level or logging.WARNING)
return CharsetMatches([CharsetMatch(sequences, "utf_8", 0.0, False, [], "")])

if cp_isolation is not None:
Expand Down Expand Up @@ -419,6 +422,7 @@ def from_bytes(
)
if explain:
logger.removeHandler(explain_handler)
logger.setLevel(previous_logger_level)
return CharsetMatches([results[encoding_iana]])

if encoding_iana == sig_encoding:
Expand All @@ -428,6 +432,7 @@ def from_bytes(
)
if explain:
logger.removeHandler(explain_handler)
logger.setLevel(previous_logger_level)
return CharsetMatches([results[encoding_iana]])

if len(results) == 0:
Expand Down Expand Up @@ -458,6 +463,7 @@ def from_bytes(

if explain:
logger.removeHandler(explain_handler)
logger.setLevel(previous_logger_level)

return results

Expand Down
2 changes: 1 addition & 1 deletion tests/test_logging.py
Expand Up @@ -10,7 +10,7 @@ def setup(self):
self.logger = logging.getLogger("charset_normalizer")
self.logger.handlers.clear()
self.logger.addHandler(logging.NullHandler())
self.logger.level = None
self.logger.level = logging.WARNING

def test_explain_true_behavior(self, caplog):
test_sequence = b'This is a test sequence of bytes that should be sufficient'
Expand Down