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


Groups > comp.lang.python > #78564

Re: Clearing globals in CPython

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!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; 'syntax': 0.04; 'cpython': 0.05; 'interpreter': 0.05; 'importerror:': 0.07; 'interpreter.': 0.07; 'odd': 0.07; 'reason,': 0.07; 'think,': 0.07; 'variables': 0.07; 'bindings': 0.09; 'broke': 0.09; 'exception.': 0.09; 'exit': 0.09; 'high-level': 0.09; 'hooks': 0.09; 'raises': 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; 'statements': 0.09; 'works.': 0.09; 'python': 0.11; 'jan': 0.12; "'include'": 0.16; 'assembler': 0.16; 'builtins': 0.16; 'imports': 0.16; 'modified.': 0.16; 'namespace.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'remain,': 0.16; 'subject:Clearing': 0.16; 'wrote:': 0.18; 'obviously': 0.18; 'variable': 0.18; 'working.': 0.19; 'machine': 0.22; '>>>': 0.22; 'programming': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'instance,': 0.24; 'sort': 0.25; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'statement': 0.30; 'code': 0.31; "skip:' 10": 0.31; '"",': 0.31; '>>>>': 0.31; "d'aprano": 0.31; 'prints': 0.31; 'steven': 0.31; 'file': 0.32; '(most': 0.33; 'implemented': 0.33; 'skip:_ 10': 0.34; 'except': 0.35; 'but': 0.35; 'accessible': 0.36; 'executing': 0.36; 'functions.': 0.36; 'idle': 0.36; 'keyword': 0.36; 'library.': 0.36; 'like,': 0.36; "i'll": 0.36; 'so,': 0.37; 'expected': 0.38; 'easiest': 0.38; 'stopped': 0.38; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'rather': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'most': 0.60; 'name': 0.63; 'more': 0.64; 'special': 0.74; 'behavior': 0.77; 'functions:': 0.84; 'received:fios.verizon.net': 0.84; 'restores': 0.84; 'recover': 0.91; 'shell,': 0.91; 'received:108': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: Clearing globals in CPython
Date Wed, 01 Oct 2014 14:44:26 -0400
References <542c2527$0$13013$c3e8da3$5496439d@news.astraweb.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-108-16-203-145.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0
In-Reply-To <542c2527$0$13013$c3e8da3$5496439d@news.astraweb.com>
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.14530.1412189121.18130.python-list@python.org> (permalink)
Lines 53
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1412189121 news.xs4all.nl 2940 [2001:888:2000:d::a6]:42220
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:78564

Show key headers only | View raw


On 10/1/2014 12:00 PM, 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.

As you discovered, this reduces the interpreter to a pure syntax machine 
with no name bindings -- sort of like a high-level assembler with no 
access to a function library.

> I expected that global variables would be all lost, but built-ins would
> remain, since they don't live in the global namespace. ...
> The reason, I think, is that CPython has a special __builtins__ global
> variable that the interpreter uses to access built-in functions.

When executing statements from Shell, Idle restores __builtins__.
 >>> globals().clear()
 >>> print(dir())
['__builtins__']
 >>> print(__builtins__)
<long listing>

The odd thing is that executing the same code from a file prints [] and 
then raises "NameError: name '__builtins__' is not defined".

> 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

Most keyword statements are implemented by internal functions not 
accessible from Python except through the statement syntax.  Import is 
an exception.  This is one of the hooks that allow the behavior of 
imports to be modified.  The result is like, for instance, programming 
in C with 'include' disabled.

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


-- 
Terry Jan Reedy

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