473,395 Members | 1,554 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,395 software developers and data experts.

__builtins__ magic behavior

__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.

Thanks,
Pat

Executing:
{{{
original = __builtins__
if hasattr(original, '__dict__'): original = original.__dict__
copy = original.copy()

# Make the dictionaries different and use new one
copy['foo'] = 'bar'
del copy['len']
del original['chr']
__builtins__ = copy

testcode = '''
try: print foo
except: print "foo not found"
try: print len
except: print "len not found"
try: print chr
except: print "chr not found"
print
'''

x = globals()
assert type(x) is dict
y = {}
y.update(x)
assert y == x

print 'Executing in globals()'
exec testcode in x

print 'Executing in copy'
exec testcode in y
}}}

results in:

Executing in globals()
foo not found
<built-in function len>
chr not found

Executing in copy
bar
len not found
<built-in function chr>
Sep 7 '08 #1
0 1066

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

Similar topics

1
by: Michael Hohn | last post by:
Hi, using the file builtin_check.py with content # Module builtin_check # Inconstency in the binding of __builtins__ def get_binding(name): return locals() def get_global_binding(name):
4
by: Collin Winter | last post by:
Hallo all, As it currently stands, the type of the global __builtins__ differs depending on whether you're in the __main__ namespace (__builtins__ is a module) or not (its a dict). I was...
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...
16
by: per9000 | last post by:
Hi, I recently started working a lot more in python than I have done in the past. And I discovered something that totally removed the pretty pink clouds of beautifulness that had surrounded my...
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: James Stroud | last post by:
Hello all, Say I have the following module # amodule.py print __builtins__ # end of module Then I have the following little helper script:
3
by: loquehumaine | last post by:
Hi there, I'm a newby in python (I know a little in programmation) and I have a lot of questions on builtins but my first one is about modules... I have seen that if I type help() at a prompt,...
3
by: Gabriel Genellina | last post by:
En Sun, 07 Sep 2008 14:00:48 -0300, Patrick Maupin <pmaupin@gmail.comescribió: Python takes some shortcuts when dealing with builtins. I'll just describe what happens (I won't say whether it is...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.