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
Broken encoding modules are silently skipped. #72192
In Lib/encodings/init.py, search_function() looks up an encoding by name. It loads the encoding by importing the corresponding module in the encodings package. If there's an ImportError while importing that module then the encoding gets silently ignored. I noticed this while working on issue bpo-16384.
While I don't have any direct interest in the matter, I expect that it would be more useful to users if we issued a warning or some other printed message when an encoding is ignored.
Steve, this is not a correct patch. The purpose of skipping ImportErrors is to give other search functions a chance to find and provide a working codec. Your patch causes this to break.
Thinking about this some, we could go ahead and document somewhere that if a codec wants to tell the search function that it cannot work due to some missing dependency, it will have to raise an ImportError from the codec module level (instead of just lettering ImportError bubble up), possibly reraising the error to assure this.
Is it deliberate that inner import errors should be treated as if the codec is missing? Another search function isn't going to help in that case unless it finds the same encoding in another place. I didn't see any evidence of that, and apparently neither did Eric (but I was bitten by a misspelt import within my new encodings/oem.py giving a confusing message).
I don't think any encoding modules in the stdlib should fail to import. This change ensures that we hear about it if they do fail.
If a search function gets an ImportError, it interprets this as "codec is not available/working", and then gives other search functions a chance to find a working one.
I reverted the change but wrote the wrong issue number. Also added a comment so the next person to encounter this realises it is the correct behaviour.
Activity
ericsnowcurrently commentedon Sep 7, 2016
In Lib/encodings/init.py, search_function() looks up an encoding by name. It loads the encoding by importing the corresponding module in the encodings package. If there's an ImportError while importing that module then the encoding gets silently ignored. I noticed this while working on issue bpo-16384.
While I don't have any direct interest in the matter, I expect that it would be more useful to users if we issued a warning or some other printed message when an encoding is ignored.
python-dev commentedon Sep 8, 2016
New changeset 2b2e7b39bb68 by Steve Dower in branch 'default':
Issue bpo-28005: Allow ImportErrors in encoding implementation to propagate.
https://hg.python.org/cpython/rev/2b2e7b39bb68
malemburg commentedon Sep 8, 2016
Steve, this is not a correct patch. The purpose of skipping ImportErrors is to give other search functions a chance to find and provide a working codec. Your patch causes this to break.
malemburg commentedon Sep 8, 2016
Thinking about this some, we could go ahead and document somewhere that if a codec wants to tell the search function that it cannot work due to some missing dependency, it will have to raise an ImportError from the codec module level (instead of just lettering ImportError bubble up), possibly reraising the error to assure this.
zooba commentedon Sep 8, 2016
Is it deliberate that inner import errors should be treated as if the codec is missing? Another search function isn't going to help in that case unless it finds the same encoding in another place. I didn't see any evidence of that, and apparently neither did Eric (but I was bitten by a misspelt import within my new encodings/oem.py giving a confusing message).
I don't think any encoding modules in the stdlib should fail to import. This change ensures that we hear about it if they do fail.
malemburg commentedon Sep 9, 2016
Yes, that's intended. See e.g. the mbcs codec.
If a search function gets an ImportError, it interprets this as "codec is not available/working", and then gives other search functions a chance to find a working one.
zooba commentedon Sep 9, 2016
I reverted the change but wrote the wrong issue number. Also added a comment so the next person to encounter this realises it is the correct behaviour.
New changeset 4e80a157ea66 by Steve Dower in branch 'default':
Revert bpo-27959: ImportError within an encoding module should also skip the encoding
https://hg.python.org/cpython/rev/4e80a157ea66