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-leave@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/
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-leave@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/