As it has been mentioned there is no guarantee that your variable will even
be finalized (or even destroyed) after the frame finishes. For example, if
your variable goes into a reference cycle for whatever reason it may not be
cleared until a GC run happens (and in some situations it may not even be
cleared at any point). The language gives you no guarantees over when or
how objects will be finalized or destroyed and any attempt at relying on
specific behaviour is deemed to fail because it can change between versions
and implementations.



On Thu, 28 Apr 2022, 14:14 Malthe, <mbo...@gmail.com> wrote:

> Consider this example code:
>
> def test():
>     a = A()
>
> test()
>
> Currently, the locals (i.e. `a`) are cleared only after the function
> has returned:
>
> If we attach a finalizer to `a` immediately after the declaration then
> the frame stack available via `sys._getframe()` inside the finalizer
> function does not include the frame used to evaluate the function
> (i.e. with the code object of the `test` function).
>
> The nearest frame is that of the top-level module (where we make the
> call to the function).
>
> This is in practical terms no different than:
>
> def test():
>     return A()
>
> test()
>
> There's no way to distinguish between the two cases even though in the
> second example, the object is dropped only after the frame (used to
> evaluate the function) has been cleared.
>
> The effect I am trying to achieve is:
>
> def test():
>     a = A()
>     del a
>
> Here's a use-case to motivate this need:
>
> In Airflow, we're considering introducing some "magic" to help users write:
>
> with DAG(...):
>     # some code here
>
> That is, without declaring a top-level variable such as `dag`.
>
> However, we can't detect the following situation:
>
> def create():
>     with DAG(...) as dag:
>         # some code here
>
> create()
>
> The DAG is not returned from the function but nevertheless, we can't
> distinguish between this code and the correct version:
>
> def create():
>     with DAG(...) as dag:
>         # some code here
>     return dag
>
> In this case, calling `create` will then "return" the DAG and of
> course, without a variable assignment, the finalizer will be called –
> but now we can detect this.
>
> I'm thinking that it ought to be possible to clear out
> `frame->localsplus` before leaving the function frame.
>
> I played around with "ceval.c" and only got segfaults. It's
> complicated machinery :-)
>
> Thoughts?
> _______________________________________________
> Python-Dev mailing list -- python-dev@python.org
> To unsubscribe send an email to python-dev-le...@python.org
> https://mail.python.org/mailman3/lists/python-dev.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-dev@python.org/message/D5HCLMN42SIRRUHWPU566R7YYAVLCAEN/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/BUT34WUMBSQHKASHDTRSZI5H7GSUAX72/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to