Clearing globals in CPython

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Oct 2 04:11:58 EDT 2014


Peter Otten wrote:

> Steven D'Aprano wrote:

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

Nicely done! 



-- 
Steven




More information about the Python-list mailing list