Nick Coghlan <ncogh...@gmail.com> added the comment:

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.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue32145>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to