You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
unknown error handlers should be reported early #81569
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2019-06-26.00:13:23.084>created_at=<Date2019-06-24.14:10:52.129>labels= ['3.9', 'expert-unicode']
title='unknown error handlers should be reported early'updated_at=<Date2020-04-07.14:07:50.672>user='https://github.com/hroncok'
Getting an error handler is expensive compared to the time to encode text/decode bytes. Python loads the error handler lazily to provide best performances. Text codecs are performance critical for Python.
If we add a check, it should only be enabled in development (python3 -X dev) and/or debug mode (./configure --with-pydebug).
Activity
hroncok commentedon Jun 24, 2019
I was just bit by specifying an nonexisitng error handler for bytes.decode() without noticing.
Consider this code:
Nobody notices that the error handler doesn't exist.
However:
The error is only noticeable once there is an error in the data.
While nobody could possibly mistake 'Boom, Shaka Laka, Boom!' for a valid error handler, I was bit by this:
Which in fact should have been
Yet I wasn't notified, because the bytes in question were actually decodeable as valid utf-8.
I suggest that unknown error handler should rise an exception immediately like this:
vstinner commentedon Jun 24, 2019
Getting an error handler is expensive compared to the time to encode text/decode bytes. Python loads the error handler lazily to provide best performances. Text codecs are performance critical for Python.
If we add a check, it should only be enabled in development (python3 -X dev) and/or debug mode (./configure --with-pydebug).
vstinner commentedon Jun 24, 2019
bench.py: microbenchmark to measure the overhead of PR 14341.
vstinner commentedon Jun 25, 2019
New changeset 22eb689 by Victor Stinner in branch 'master':
bpo-37388: Development mode check encoding and errors (GH-14341)
22eb689
vstinner commentedon Jun 25, 2019
New changeset ed076ed by Victor Stinner in branch 'master':
bpo-37388: Add PyUnicode_Decode(str, 0) fast-path (GH-14385)
ed076ed
vstinner commentedon Jun 26, 2019
I compared ref (commit e1a63c4) to patch (commit ed076ed). I ran bench.py using:
# edit Makefile.pre.in to use:
PROFILE_TASK=-m test.regrtest --pgo test_unicode test_codecs
./configure --enable-optimizations && make
./python -m venv env && env/bin/python -m pip install -U pyperf
./env/bin/python bench.py -o <file>.json
The overhead of my change is around 1 ns, 4 ns (on 106 ns) in the worst case (5B).
The change "looks" faster on the 1000B case, but it's likely a glitch of PGO compilation which is not really deterministic.
vstinner commentedon Jun 26, 2019
Ok, the encoding and errors are now checked in almost all cases if you enable the development mode using -X dev command line option.
vstinner commentedon Apr 7, 2020
New changeset d8acf0d by Victor Stinner in branch 'master':
bpo-37388: Don't check encoding/errors during finalization (GH-19409)
d8acf0d