This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: contextlib.ExitStack abuses __self__
Type: performance Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: jdemeyer, ncoghlan, yselivanov
Priority: normal Keywords: patch

Created on 2018-04-12 05:24 by jdemeyer, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 6456 merged jdemeyer, 2018-04-12 05:28
Messages (5)
msg315212 - (view) Author: Jeroen Demeyer (jdemeyer) * (Python triager) Date: 2018-04-12 05:24
In contextlib, there is code which roughly looks like

        def _exit_wrapper(exc_type, exc, tb):
            return cm_exit(cm, exc_type, exc, tb)
        _exit_wrapper.__self__ = cm

This creates a new function _exit_wrapper from a given function cm_exit by prepending the __self__ attribute to *args. Now this is exactly what a method does too.

It would be better to use an actual method for this: it's cleaner, faster and it doesn't abuse a double-underscore attribute. The latter will actually break with PEP 575, as __self__ will become a special name  instead of an arbitrary attribute.
msg315221 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2018-04-12 12:25
Yury, could you double check the async exit stack change in the PR? I think it's fine since the bound method just passes back the underlying coroutine and the tests all still pass, but a second opinion would be good :)
msg315239 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2018-04-13 03:29
Yep, I think this is a good fix!
msg315243 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2018-04-13 12:22
New changeset 23ab5ee667a9b29014f6f7f01797c611f63ff743 by Nick Coghlan (jdemeyer) in branch 'master':
bpo-33265: use an actual method instead of a method-like function in ExitStack (GH-6456)
https://github.com/python/cpython/commit/23ab5ee667a9b29014f6f7f01797c611f63ff743
msg315244 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2018-04-13 12:25
Classifying this as a minor performance enhancement, since methods are much simpler objects than full Python level closures.

Thanks!
History
Date User Action Args
2022-04-11 14:58:59adminsetgithub: 77446
2018-04-13 12:25:09ncoghlansetstatus: open -> closed
versions: + Python 3.8
type: performance
messages: + msg315244

resolution: fixed
stage: patch review -> resolved
2018-04-13 12:22:50ncoghlansetmessages: + msg315243
2018-04-13 03:29:00yselivanovsetmessages: + msg315239
2018-04-12 12:25:27ncoghlansetnosy: + yselivanov
messages: + msg315221
2018-04-12 06:40:24eric.smithsetnosy: + ncoghlan
2018-04-12 05:28:43jdemeyersetkeywords: + patch
stage: patch review
pull_requests: + pull_request6151
2018-04-12 05:24:51jdemeyercreate