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.

Author ncoghlan
Recipients Denaun, barry, docs@python, ncoghlan
Date 2017-12-29.13:40:02
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1514554802.65.0.213398074469.issue32145@psf.upfronthosting.co.za>
In-reply-to
Content
I'm not clear on what you mean about allowing arbitrary names for the instance creation function - at that point we're back to subclasses not being able to use the default `pop_all()` implementation at all, and needing to duplicate the state transfer logic in the subclass (which we don't provide a public API to do, since the `_exit_callbacks` queue is deliberately private).

However, I'm thinking we could potentially change `pop_all` *itself* to accept a target object:

    def pop_all(self, *, target=None):
        """Preserve the context stack by transferring it to a new instance

        If a *target* stack is given, it must be either an `ExitStack`
        instance, or else provide a callback registration method akin to `ExitStack.callback`.
        """
        if target is None:
            target = type(self)()
        exit_callbacks = self._exit_callbacks
        self._exit_callbacks = deque()
        if isinstance(target, ExitStack):
            target._exit_callbacks = exit_callbacks
        else:
            for cb in exit_callbacks:
                target.callback(cb)
        return target

The recipe fix would then be to make `Callback.cancel()` look like:

    def cancel(self):
        # We deliberately *don't* clean up the cancelled callback
        self.pop_all(target=ExitStack())

(Tangent: https://bugs.python.org/issue32445 suggests adding a small optimisation to `ExitStack.callback` to skip adding the wrapper when `args` and `kwds` are both empty)
History
Date User Action Args
2017-12-29 13:40:02ncoghlansetrecipients: + ncoghlan, barry, docs@python, Denaun
2017-12-29 13:40:02ncoghlansetmessageid: <1514554802.65.0.213398074469.issue32145@psf.upfronthosting.co.za>
2017-12-29 13:40:02ncoghlanlinkissue32145 messages
2017-12-29 13:40:02ncoghlancreate