__builtins__ magic behavior

Patrick Maupin pmaupin at gmail.com
Sun Sep 7 16:23:30 EDT 2008


On Sep 7, 2:50 pm, "Gabriel Genellina" <gagsl-... at yahoo.com.ar> wrote:
> Inside PyFrame_New, there is a shortcut: if the new frame and
> the previous one share the same globals, then the previous
> builtins are copied into the new frame. Only if the globals
> differ the builtins are searched in globals. From frameobject.c:
>  /* If we share the globals, we share the builtins. Save a
> lookup and a call. */

That was exactly my problem.  Thank you for the cogent explanation.

> If you want to execute some code with modified builtins, do not
> change __builtins__ in the *calling* code, but in the globals that
> you pass to the exec call. That appears to be the most logical
> approach, and the way the developers appear to have expected.

Actually, my use-case is a tiny bit different than this.  I am
dynamically creating module objects, and was going nuts trying
to comprehend the inner workings of behind-the-scenes __builtins__
magic in the module object, not realizing that the magic was
actually in the execution frames.

Thanks,
Pat



More information about the Python-list mailing list