RFC: Proposal: Deterministic Object Destruction
On Thu, 01 Mar 2018 22:46:56 -0700, Ian Kelly wrote:
> On Thu, Mar 1, 2018 at 10:35 PM, Chris Angelico <rosuav at gmail.com>
>> On Fri, Mar 2, 2018 at 4:16 PM, Ian Kelly <ian.g.kelly at gmail.com>
>>> On Wed, Feb 28, 2018 at 8:00 PM, Chris Angelico <rosuav at gmail.com>
>>>> Not off hand, but I can provide an EXTREMELY real-world example of a
>>>> fairly tight loop: exceptions. An exception has a reference to the
>>>> local variables it came from, and those locals may well include the
>>>> exception itself:
>>>> except Exception as e:
>>>> The ZeroDivisionError has a reference to the locals, and 'e' in the
>>>> locals refers to that very exception object.
>>> The problem with this example of course is that the variable 'e' is
>>> scoped to the except block and automatically del'ed when it exits.
>> Or, to be more accurate: The language design acknowledges that this
>> reference cycle is a fundamental problem, and the *solution* is that
>> there's an implicit "e = None; del e" at the end of the except block.
>> You can easily defeat that protection with "except Exception as ee: e =
>> ee", if you want to demonstrate the cycle.
> Yes, but how often does this happen in practice? This situation was
> billed as "an EXTREMELY real-world example". In the real world, most of
> the time when you have an exception, you log it or handle it, and then
> you discard it.
What difference does that make it? For the period of time you are in the
except block, you have a cycle. Nobody said the cycle has to persist for
a long time.
In other words, every single time you catch an exception in Python 3, you
have the situation Chris was asked to demonstrate. That seems very common
to me :-)