How do you create constants?/immutable dicts

KevinL darius at bofh.net.au
Mon Nov 6 14:15:54 EST 2000


mxProxy is cool - I'm planning on using it - but useless for the particular 
issue at hand.  You can't use it as a namespace.  That's not a limitation of 
mxProxy, it's a limitation of python itself - python requires a true internal 
dictionary for any namespace.  Um - you can test it with this:

$ python1.6
Python 1.6a2 (#3, Jul 24 2000, 11:19:08)  [GCC 2.95.2 19991024 (release)] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
Copyright 1995-2000 Corporation for National Research Initiatives (CNRI)
>>> import mxProxy
>>> text = """print str(globals().keys())"""
>>> exec text
['__doc__', 'text', '__builtins__', '__name__', 'mxProxy', 'p']
>>> exec text in {}
['__builtins__']
>>> p = mxProxy.Proxy({})
>>> exec text in p
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: exec 2nd/3rd args must be dict or None

Boom.  I'm not allowed to try and protect that dictionary by making it 
anything other than a dictionary - so I've got to make many copies of the same 
dictionary, and allow people to scribble all over their own copy (and 
re-initialise it if it gets broken, which means re-binding the method, which 
is painful speed-wise).

See, at the moment, anyone who logs in can execute code (well, actually, at 
the moment you'd have to convince me you mean well before I let you ;), but 
there's a lot of trust that people doing so don't munge up the "global" 
namespace tied to the particular function.  If the namespace was immutable, 
that worry goes away.

I'm not actually using exec, btw, I'm using the new module, but the effect is 
the same - you can't bind in anything other than a dictionary as a namespace.

KL

>>> Alex wrote
> 
> Perhaps this is close to what you want:
> 
> http://starship.python.net/crew/lemburg/mxProxy.html
> 
> Alex.





More information about the Python-list mailing list