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-25.23:51:50
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1514245910.29.0.213398074469.issue32145@psf.upfronthosting.co.za>
In-reply-to
Content
Regarding super().__init__(): I added ExitStack to contextlib2 first, so I was thinking in the Py2/3 compatible subset when I wrote the original docs. We can freely change that for the standard library recipes.

Regarding the "how to create a copy" question, while I don't especially like the notion of adding a dedicated semi-public copy method, I think you're right that it may be the best option:

1. Full pickle/copy module support doesn't make sense, since exit stacks are inherently stateful - you can't save them to use later, since you'd need to repeat the setup steps as well, but we don't keep track of what the setup steps actually *were*.

2. `stack.pop_all()` was designed such that if you clone the stack, you ensure that the original stack *won't* call the cleanup callbacks any more. If we were to add a fully public `stack.copy()` method (or `copy.copy(stack)` support via `stack.__copy__()`), then we'd lose that deliberate nudge, and put folks more at risk of "double-free" style bugs, where they run the cleanup functions multiple times.

3. A for-subclasses-only "self._clone()" API could work as follows:

    def _clone(self, new_instance=None):
        if new_instance is None:
            new_instance = type(self)()
        # Clone state here
        return new_instance

Then subclasses could override *just* the instance creation part by doing:

    def _clone(self):
        return super()._clone(self._make_instance())

While also being free to add their own additional state copying code if needed.
History
Date User Action Args
2017-12-25 23:51:50ncoghlansetrecipients: + ncoghlan, barry, docs@python, Denaun
2017-12-25 23:51:50ncoghlansetmessageid: <1514245910.29.0.213398074469.issue32145@psf.upfronthosting.co.za>
2017-12-25 23:51:50ncoghlanlinkissue32145 messages
2017-12-25 23:51:50ncoghlancreate