Docs say changing locals() is undefined behavior.  This only worked because at top level, gloabls and locals are the same.  It is safe to modify globals, so do that instead.