Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #78560

Re: Clearing globals in CPython

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'cpython': 0.05; 'explicitly': 0.05; 'interpreter': 0.05; 'preferably': 0.05; 'explicit': 0.07; 'importerror:': 0.07; 'interpreter.': 0.07; 'python3': 0.07; 'reason,': 0.07; 'think,': 0.07; 'variables': 0.07; 'broke': 0.09; 'exit': 0.09; 'global,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'restart': 0.09; 'works.': 0.09; 'python': 0.11; 'builtins': 0.16; 'guess.': 0.16; 'jython,': 0.16; 'nameerror:': 0.16; 'namespace.': 0.16; 'peters': 0.16; 'readability': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'remain,': 0.16; 'rules.': 0.16; 'subject:Clearing': 0.16; 'wrote:': 0.18; 'obviously': 0.18; 'variable': 0.18; 'working.': 0.19; '>>>': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; "aren't": 0.24; 'pass': 0.26; 'defined': 0.27; 'header:X-Complaints-To:1': 0.27; 'idea': 0.28; 'tim': 0.29; 'errors': 0.30; "skip:' 10": 0.31; '"",': 0.31; '>>>>': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'file': 0.32; 'linux': 0.33; '(most': 0.33; 'cases': 0.33; 'but': 0.35; 'there': 0.35; 'functions.': 0.36; "i'll": 0.36; 'should': 0.36; 'so,': 0.37; 'expected': 0.38; 'skip:o 20': 0.38; 'easiest': 0.38; 'skip:. 20': 0.38; 'stopped': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'recent': 0.39; 'does': 0.39; 'bad': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'received:org': 0.40; 'easy': 0.60; 'flat': 0.60; 'break': 0.61; 'simple': 0.61; "you're": 0.61; 'first': 0.61; 'name': 0.63; 'face': 0.64; 'more': 0.64; 'great': 0.65; 'beautiful': 0.68; 'obvious': 0.74; 'special': 0.74; '*appears*': 0.84; '2014,': 0.84; 'beats': 0.84; 'functions:': 0.84; 'zen': 0.84; 'recover': 0.91; 'refuse': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Clearing globals in CPython
Date Wed, 01 Oct 2014 19:24:05 +0200
Organization None
References <542c2527$0$13013$c3e8da3$5496439d@news.astraweb.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p57bd83da.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.14526.1412184262.18130.python-list@python.org> (permalink)
Lines 80
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1412184262 news.xs4all.nl 2888 [2001:888:2000:d::a6]:47091
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:78560

Show key headers only | View raw


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!

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Clearing globals in CPython Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-02 02:00 +1000
  Re: Clearing globals in CPython Chris Angelico <rosuav@gmail.com> - 2014-10-02 02:14 +1000
  Re: Clearing globals in CPython Chris Kaynor <ckaynor@zindagigames.com> - 2014-10-01 10:08 -0700
  Re: Clearing globals in CPython Peter Otten <__peter__@web.de> - 2014-10-01 19:24 +0200
    Re: Clearing globals in CPython Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-02 18:11 +1000
      Re: Clearing globals in CPython Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-10-02 09:30 +0100
    Re: Clearing globals in CPython jakubo <jakubo@example.org> - 2014-10-06 09:05 +0000
      Re: Clearing globals in CPython Steven D'Aprano <steve@pearwood.info> - 2014-10-06 09:08 +0000
        Re: Clearing globals in CPython Chris Angelico <rosuav@gmail.com> - 2014-10-06 20:30 +1100
  Re: Clearing globals in CPython Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-10-01 19:17 +0100
  Re: Clearing globals in CPython Terry Reedy <tjreedy@udel.edu> - 2014-10-01 14:44 -0400
  Re: Clearing globals in CPython Marco Buttu <marco.buttu@gmail.com> - 2014-10-01 18:39 +0200
  Re: Clearing globals in CPython Grant Edwards <invalid@invalid.invalid> - 2014-10-02 14:07 +0000
    Re: Clearing globals in CPython Chris Angelico <rosuav@gmail.com> - 2014-10-03 00:12 +1000
    Re: Clearing globals in CPython Stefan Behnel <stefan_ml@behnel.de> - 2014-10-03 07:26 +0200

csiph-web