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


Groups > comp.lang.python > #82761

Re: what is wrong with d.clear()?

From Terry Reedy <tjreedy@udel.edu>
Subject Re: what is wrong with d.clear()?
Date 2014-12-22 03:58 -0500
References <CANUu8=iCdMSQ+ToM988e-ri74quNDBHh+pCR0mh_BJkNQQBkKQ@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.17114.1419238703.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 12/21/2014 2:28 AM, shawool wrote:
> where am i going wrong ?

You clear sys.modules, which apparently CPython uses in its normal function.


> Python 3.2.5 (default, Oct  2 2013, 22:58:11)

>>>> d = {}
>>>> import sys
>>>> d = sys.modules
>>>> type(d)
> <class 'dict'>
>>>> dir(d)
> ['__class__', '__contains__', '__delattr__', '__delitem__', '__doc__',
> '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__',
> '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__',
> '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
> '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__',
> '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys',
> 'pop', 'popitem', 'setdefault', 'update', 'values']

dir(d) is the contents of d.__dict__, not d itself, so the above is not 
what you clear.  Just type d to see what is cleared.

>>>> d.clear()
> Traceback (most recent call last):dir(
>    File "<stdin>", line 1, in <module>

There is no code line because the exception in in C code.  3.4.2 gives 
more info: RuntimeError: lost builtins module.  Do upgrade if you can.

In Idle, this executes 'ok', without an exception, because it clears 
sys.modules in the user process, not the Idle process.

>>>> d
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>

However, this throws the user process into a loop, literally.

-- 
Terry Jan Reedy

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


Thread

Re: what is wrong with d.clear()? Terry Reedy <tjreedy@udel.edu> - 2014-12-22 03:58 -0500

csiph-web