How to set custom locals for function call?

Chris Angelico rosuav at gmail.com
Tue Sep 1 20:31:30 EDT 2020


On Wed, Sep 2, 2020 at 10:23 AM Andras Tantos
<python-list at andras.tantosonline.com> wrote:
> I did see these macros in the CPython source code. What it seems to
> imply is that if I wanted to do what I intend, that is to hook every
> local variable assignment, I would have to modify the AST. That seems
> rather more effort than simply supplying my own locals dictionary. I
> will try to look myself as well, but do you have an inkling as to how
> complex would it be to convince CPython to optimize locals only if no
> custom locals is provided during exec?
>
> To my untrained eye, it seems to be an unsafe optimization that breaks
> the contract of the exec API.

By the time you're calling exec, it's too late to decide whether to
optimize, since you're using a precompiled code object. If you want
that behaviour, try exec'ing a string of code instead.

ChrisA


More information about the Python-list mailing list