-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
Add codecs.unregister() to unregister a codec search function #86008
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
Comments
Writing an unit test on the Python codecs machinery is facing a practical problem: there is no C nor Python API to unregister a codec search function. It's even documented in a note of the codecs.register() function: "Note: Search function registration is not currently reversible, which may cause problems in some cases, such as unit testing or module reloading." https://docs.python.org/dev/library/codecs.html#codecs.register test_codecs contains a long comment about that:
See bpo-22166 which fixed memory leaks in test_codecs. In 2011, a Python user requested the function Marc-Andre Lemburg explained: "There is no API to unregister a codec search function, since deregistration One simple solution would be to clear the cache (PyInterpreterState.codec_search_cache) when codecs.unregister() removes a search function. I expect that calling unregister() is an uncommon operation, so the performance is not a blocker issue. |
Hai Shi wrote PR 22360 to implement codecs.unregister(). |
On 23.09.2020 14:56, STINNER Victor wrote:
+1 BTW: While you're at it, having a way to access the search function -- Professional Python Services directly from the Experts (#1, Sep 23 2020)
>>> Python Projects, Coaching and Support ... https://www.egenix.com/
>>> Python Product Development ... https://consulting.egenix.com/
________________________________________________________________________ ::: We implement business ideas - efficiently in both time and costs ::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 |
Just found an internal API which already takes care of All that needs to be done is to expose this as codecs.unregister() -- Professional Python Services directly from the Experts (#1, Sep 23 2020)
>>> Python Projects, Coaching and Support ... https://www.egenix.com/
>>> Python Product Development ... https://consulting.egenix.com/
________________________________________________________________________ ::: We implement business ideas - efficiently in both time and costs ::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 |
Yeah, I saw this function, but it's related to the cache, not to the list of search functions.
I didn't hear anyone (ok, apart you) who requested to order search functions. I dislike the idea of exposing it, since it introduces the risk that someone "unregisters" a search function simply by removing it from the list, without invalidating the cache. I prefer to hide the internals to ensure that the cache remains consistent. |
On 23.09.2020 16:49, STINNER Victor wrote:
Ah, right. I just looked at the first occurance of codec_search_path :-)
This has come up in the past from people who wanted to override
Sure, a function would merely return a tuple with the entries, def get_search_path():
return tuple(interp->codec_search_path) For replacing the vanilla setup, this is not needed, since only -- Professional Python Services directly from the Experts (#1, Sep 23 2020)
>>> Python Projects, Coaching and Support ... https://www.egenix.com/
>>> Python Product Development ... https://consulting.egenix.com/
________________________________________________________________________ ::: We implement business ideas - efficiently in both time and costs ::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 |
If add a function for unregistering a codec search function, it would be worth to add also a function for unregistering an error handler. |
Registering an error handler have no refleaks when registering multiple search functions. |
oh, sorry, typo error. multiple search functions-->multiple error handler. |
Although unregistering an error handler may be not so easy, so it is better to open a separate issue for it. |
PR 22360 required 15 iterations (15 commits) and it changes a lot of code. I wouldn't say that it's an "easy (C)" issue. |
codecs.unregister() is needed to write unit tests: see PR 19069 of bpo-39337. I don't see a strong need to add a new codecs.unregister_error() function, excpet for completeness. I don't think that completeness is enough to justify to add the function, but feel free to open a new issue if you consider that it's really needed. |
The function is added, I close the issue. Thanks Hai Shi. |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: