473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Re: __builtins__ magic behavior

En Sun, 07 Sep 2008 14:00:48 -0300, Patrick Maupin <pm*****@gmail.comescribió:
__builtins__ in 2.5.2 doesn't seem to behave like I remember it did
the last time I did some custom stuff with it, a very long time ago.

This isn't surprising, because of ongoing optimization, but it's hard
to google for '__builtins__' so I didn't really find any documentation
on the current CPython behavior, which in some cases seems quite
strange to me.

The documentation node at http://docs.python.org/ref/naming.html has a
"here be dragons" note on __builtins__, which is nice as far as it
goes, but doesn't provide any help for people wanting to abuse the
current CPython implementation. Additionally, the sentence
immediately above the note, "__builtins__ can be set to a user-created
dictionary to create a weak form of restricted execution" not only
contradicts the note, but in some cases appears not to be true.

When the code below is run, either standalone or by importing it, the
builtins used are different depending on whether exec is used with
globals() or with a copy of globals(). Any explanations for this
behavior would be much appreciated.
Python takes some shortcuts when dealing with builtins. I'll just describe what happens (I won't say whether it is "right" or "wrong").
The exec statement, when given a string source, compiles it and eventually calls PyEval_EvalCodeEx, which creates a new frame using PyFrame_New and finally executes it.
A frame object contains a pointer to the previous frame, the code to be executed, a pointer to the current globals *and* a separate pointer to the current builtins.
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. */
It is this assumption that fails in your code.

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.

--
Gabriel Genellina

Sep 7 '08 #1
3 1344
On Sep 7, 2:50*pm, "Gabriel Genellina" <gagsl-...@yahoo.com.arwrote:
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
Sep 7 '08 #2
Gabriel, could I please ask you to configure your news-reader software or
editor to limit the length of each line of your posts to 70 characters
wide, as per the common standard for email and Usenet? Your lines are
significantly longer than that, including one single line which is 325
characters wide. That forces a lot of backwards and forwards scrolling to
read your posts.

Thank you.

--
Steven
Sep 7 '08 #3
En Sun, 07 Sep 2008 19:30:07 -0300, Steven D'Aprano
<st***@remove-this-cybersource.com.auescribió:
Gabriel, could I please ask you to configure your news-reader software or
editor to limit the length of each line of your posts to 70 characters
wide, as per the common standard for email and Usenet? Your lines are
significantly longer than that, including one single line which is 325
characters wide. That forces a lot of backwards and forwards scrolling to
read your posts.
Fixed, thanks for pointing it out. Opera wraps long lines automatically so
I didn't notice the problem myself.
I can't remember when, or why, I altered the setting (if *I* altered it at
all!)

--
Gabriel Genellina

Sep 10 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: robert | last post by:
As more and more python packages are starting to use the bloomy (Java-ish) 'logging' module in a mood of responsibility and as I am not overly happy with the current "thickener" style of usage, I...
1
by: Adam Hupp | last post by:
I've noticed some unexpected behavior with __builtins__ during module import. It seems that during module import __builtins__ is a dict but at all other times it is a module. For example, if...
0
by: Patrick Maupin | last post by:
__builtins__ in 2.5.2 doesn't seem to behave like I remember it did the last time I did some custom stuff with it, a very long time ago. This isn't surprising, because of ongoing optimization,...
0
by: Lie Ryan | last post by:
On Tue, 30 Sep 2008 16:04:34 -0500, William Purcell wrote: when you pass mydict, it is used as the global variables in the eval, right? Then, you passed a code to eval('...', mydict), sometimes...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.