Clearing globals in CPython

Peter Otten __peter__ at web.de
Wed Oct 1 13:24:05 EDT 2014


Steven D'Aprano wrote:

> Out of curiosity, I ran:
> 
> globals().clear()
> 
> in the interactive interpreter. It broke much more than I expected!
> Built-ins were no longer available, and import stopped working.
> 
> I expected that global variables would be all lost, but built-ins would
> remain, since they don't live in the global namespace. I was wrong:
> 
>>>> globals().clear()
>>>> x = len([])
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> NameError: name 'len' is not defined
> 
> The reason, I think, is that CPython has a special __builtins__ global
> variable that the interpreter uses to access built-in functions. What
> *appears* to be happening is that if that __builtins__ global is missing,
> CPython can not access the built-ins.
> 
> (Supporting this interpretation: IronPython, like CPython, has a
> __builtins__ global, and behaves the same when the globals() are cleared.
> Jython, which does not have a __builtins__ global, does not.)
> 
> Well that's okay, I thought to myself, I'll just import the built-in
> functions:
> 
>>>> from builtins import len  # use '__builtin__' in Python 2
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> ImportError: __import__ not found
> 
> Oops.
> 
> So, with no built-ins available, import no longer works. That makes things
> rather tricky.
> 
> Obviously the easiest way to recover is to exit the current session and
> restart it, but as a challenge, can we recover from this state?

$ python3
Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> globals().clear()
>>> import that
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: __import__ not found
>>> object = 1 .__class__.__bases__[0]
>>> Quitter = [c for c in object.__subclasses__() if c.__name__ == 
"Quitter"][0]
>>> __builtins__ = Quitter.__call__.__globals__["__builtins__"]
>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!





More information about the Python-list mailing list