Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'from:addr:yahoo.co.uk': 0.04; 'cpython': 0.05; 'interpreter': 0.05; 'importerror:': 0.07; 'interpreter.': 0.07; 'reason,': 0.07; 'think,': 0.07; 'variables': 0.07; 'broke': 0.09; 'exit': 0.09; 'global,': 0.09; 'lawrence': 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; 'language.': 0.14; 'builtins': 0.16; 'command.': 0.16; 'jython,': 0.16; 'nameerror:': 0.16; 'namespace.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'remain,': 0.16; 'subject:Clearing': 0.16; 'language': 0.16; 'wrote:': 0.18; 'obviously': 0.18; 'variable': 0.18; 'working.': 0.19; 'import': 0.22; 'header:User-Agent:1': 0.23; 'defined': 0.27; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; "skip:' 10": 0.31; '"",': 0.31; '>>>>': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'file': 0.32; '(most': 0.33; 'but': 0.35; 'functions.': 0.36; "i'll": 0.36; 'so,': 0.37; 'expected': 0.38; 'easiest': 0.38; 'stopped': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'recent': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'name': 0.63; 'our': 0.64; 'more': 0.64; 'charset:windows-1252': 0.65; 'special': 0.74; '*appears*': 0.84; 'functions:': 0.84; 'received:2': 0.84; 'recover': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: Clearing globals in CPython Date: Wed, 01 Oct 2014 19:17:03 +0100 References: <542c2527$0$13013$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: host-2-100-226-53.as13285.net User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.1.2 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 52 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1412187452 news.xs4all.nl 2938 [2001:888:2000:d::a6]:37963 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:78563 On 01/10/2014 17:00, 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 "", line 1, in > 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 "", line 1, in > 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? > > We obviously need a Restart Command. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence