Skip to content

Fix logger level not restored correctly for empty byte sequences#701

Merged
Ousret merged 1 commit intojawah:masterfrom
bysiber:fix/explain-logger-level-restore
Mar 6, 2026
Merged

Fix logger level not restored correctly for empty byte sequences#701
Ousret merged 1 commit intojawah:masterfrom
bysiber:fix/explain-logger-level-restore

Conversation

@bysiber
Copy link
Copy Markdown
Contributor

@bysiber bysiber commented Feb 20, 2026

Summary

Fix incorrect logger level restoration after from_bytes(b"", explain=True).

Problem

When from_bytes() is called with explain=True on an empty byte sequence, the early-return path restores the logger level with:

logger.setLevel(previous_logger_level or logging.WARNING)

The default logger level is NOTSET (integer 0). Because 0 is falsy, the or expression evaluates to logging.WARNING (30) instead of 0:

>>> 0 or logging.WARNING
30

This permanently changes the charset_normalizer logger from NOTSET to WARNING. The effect: any user-configured handler expecting DEBUG or INFO messages from the library will silently stop receiving them after a single from_bytes(b"", explain=True) call.

All other exit paths in the same function correctly restore the level without the or fallback:

logger.setLevel(previous_logger_level)  # correct (lines 468, 486, 498, 540)

Only the empty-sequence path at line 81 had the bug.

Fix

Remove the or logging.WARNING fallback so it behaves the same as every other exit path.

When from_bytes() is called with explain=True on an empty byte
sequence, the logger level was restored using:

    logger.setLevel(previous_logger_level or logging.WARNING)

If the logger was at NOTSET (level 0, the default), the 'or'
expression evaluates to logging.WARNING (30) instead of 0,
permanently changing the logger level from NOTSET to WARNING.

All other exit paths in the same function correctly use just
previous_logger_level without the 'or' fallback.
@bysiber bysiber requested a review from Ousret as a code owner February 20, 2026 15:20
Copy link
Copy Markdown
Member

@Ousret Ousret left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@Ousret Ousret merged commit 6a8503f into jawah:master Mar 6, 2026
1 check passed
@Ousret Ousret mentioned this pull request Mar 6, 2026
Ousret added a commit that referenced this pull request Mar 6, 2026
## [3.4.5](3.4.4...3.4.5) (2026-03-06)

### Changed
- Update `setuptools` constraint to `setuptools>=68,<=82`.
- Raised upper bound of mypyc for the optional pre-built extension to v1.19.1

### Fixed
- Add explicit link to lib math in our optimized build. (#692)
- Logger level not restored correctly for empty byte sequences. (#701)
- TypeError when passing bytearray to from_bytes. (#703)

### Misc
- Applied safe micro-optimizations in both our noise detector and language detector.
- Rewrote the `query_yes_no` function (inside CLI) to avoid using ambiguous licensed code.
- Added `cd.py` submodule into mypyc optional compilation to reduce further the performance impact.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants