By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,996 Members | 1,498 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,996 IT Pros & Developers. It's quick & easy.

Is access to locals() of "parent" namespace possible?

P: n/a
I want to give a user the possibility of "restarting" an interactive
session, by removing all the objects defined by her since the
beginning. The way I make this possible is by having a "function"
that can be called during the interactive session using locals() as an
argument, as follows:

restart(locals())

It works. However, I would like to make this "friendlier", i.e.
requiring the user to simply type

restart()

and have the code extract out the value of locals() of the "parent"
namespace. I thought it might be possible using sys._getframe, but I
have not been able to figure out why.

Any help would be appreciated.
André
==================

The code below defines the class I used. The interpreter is started
via

#--
exec user_code in symbols
#--

where user_code is

#--
interp = SingleConsole()
interp.interact()
#--
# InteractiveConsole is very similar to the class of the same name
# in module code.py of the standard library

class SingleConsole(InteractiveConsole):
'''SingleConsole are isolated one from another'''
def __init__(self, locals={}, filename="Isolated console"):
self.locals = locals
self.locals['restart'] = self.restart
InteractiveConsole.__init__(self, self.locals,
filename=filename)

def restart(self, loc):
"""Call this function as follows: restart(locals())

Used to restart an interpreter session, removing all
variables
and functions introduced by the user, but leaving Crunchy
specific
ones in."""
to_delete = set()
# We can't iterate over a dict while changing its size; we do
it
# in two steps; first identify the objects to be deleted while
# iterating over the dict; then iterate over a set while
removing
# the objects in the dict.
for x in loc:
if x not in ['__builtins__', 'crunchy', 'restart']:
to_delete.add(x)
for x in to_delete:
del loc[x]
return

Jul 27 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Fri, 2007-07-27 at 20:46 +0000, André wrote:
I want to give a user the possibility of "restarting" an interactive
session, by removing all the objects defined by her since the
beginning. The way I make this possible is by having a "function"
that can be called during the interactive session using locals() as an
argument, as follows:

restart(locals())

It works. However, I would like to make this "friendlier", i.e.
requiring the user to simply type

restart()

and have the code extract out the value of locals() of the "parent"
namespace. I thought it might be possible using sys._getframe, but I
have not been able to figure out why.
inspect.currentframe(1).f_locals

The same caveats for locals() apply here: Modifications to this
dictionary may or may not be visible to the caller.

HTH,

--
Carsten Haese
http://informixdb.sourceforge.net
Jul 27 '07 #2

P: n/a
On Jul 27, 6:01 pm, Carsten Haese <cars...@uniqsys.comwrote:
On Fri, 2007-07-27 at 20:46 +0000, André wrote:
I want to give a user the possibility of "restarting" an interactive
session, by removing all the objects defined by her since the
beginning. The way I make this possible is by having a "function"
that can be called during the interactive session using locals() as an
argument, as follows:
restart(locals())
It works. However, I would like to make this "friendlier", i.e.
requiring the user to simply type
restart()
and have the code extract out the value of locals() of the "parent"
namespace. I thought it might be possible using sys._getframe, but I
have not been able to figure out why.

inspect.currentframe(1).f_locals

The same caveats for locals() apply here: Modifications to this
dictionary may or may not be visible to the caller.

HTH,

--
Carsten Haesehttp://informixdb.sourceforge.net
Thanks, it worked!

André

Jul 27 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.