RFC: Proposal: Deterministic Object Destruction

Chris Angelico rosuav at gmail.com
Mon Mar 5 06:24:06 EST 2018


On Mon, Mar 5, 2018 at 10:09 PM, Ooomzay <ooomzay at gmail.com> wrote:
> Here is an example of a composite resource using RAII:-
>
> class RAIIFileAccess():
>     def __init__(self, fname):
>         print("%s Opened" % fname)
>     def __del__(self):
>         print("%s Closed" % fname)
>
> class A():
>     def __init__(self):
>         self.res = RAIIFileAccess("a")
>
> class B():
>     def __init__(self):
>         self.res = RAIIFileAccess("b")
>
> class C():
>     def __init__(self):
>         self.a = A()
>         self.b = B()
>
> def main():
>     c = C()
>
> Under this PEP this is all that is needed to guarantee that the files "a"
> and "b" are closed on exit from main or after any exception has been handled.

Okay. And all your PEP needs is for reference count semantics, right?
Okay. I'm going to run this in CPython, with reference semantics. You
guarantee that those files will be closed after an exception is
handled? Right.

>>> def main():
...     c = C()
...     c.do_stuff()
...
>>> main()
a Opened
b Opened
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in main
AttributeError: 'C' object has no attribute 'do_stuff'
>>>


Uhh.... I'm not seeing any messages about the files getting closed.
Maybe exceptions aren't as easy to handle as you think? Or maybe you
just haven't tried any of this (which is obvious from the bug in your
code - but even if I fix that, there's still a problem with exception
handling).

You keep insisting that this is an easy thing. We keep pointing out
that it isn't. Now you're proving that you haven't even attempted any
of this. Do you believe me now? (Probably not.)

ChrisA



More information about the Python-list mailing list