[Checkins] SVN: z3c.pt/trunk/ Wrap 'sys.modules' dictionary in an 'opaque' dictionary class, such that the representation string does not list all loaded modules.

Jacob Holm jh at improva.dk
Tue May 12 04:23:17 EDT 2009


Hi Malthe

Malthe Borch wrote:
> Log message for revision 99833:
>   Wrap 'sys.modules' dictionary in an 'opaque' dictionary class, such that the representation string does not list all loaded modules.
> 


This change won't do what you expect.  __getitem__ and __len__ are 
special methods and are not looked up on the instance.  The reason it 
looks like it is working is that the __init__ method you inherit from 
dict makes a copy of the passed-in dictionary.  It is easy to verify 
this by making a change to sys.modules and showing that this has no 
effect on your sys_modules.

e.g:

sys.modules['foo'] = None
assert len(sys.modules)==len(sys_modules) # fails
sys_modules['foo'] # raises KeyError

- Jacob


> Changed:
>   U   z3c.pt/trunk/CHANGES.txt
>   U   z3c.pt/trunk/src/z3c/pt/pagetemplate.py
> 
> -=-
> Modified: z3c.pt/trunk/CHANGES.txt
> ===================================================================
> --- z3c.pt/trunk/CHANGES.txt	2009-05-11 06:51:33 UTC (rev 99832)
> +++ z3c.pt/trunk/CHANGES.txt	2009-05-11 06:54:42 UTC (rev 99833)
> @@ -1,6 +1,10 @@
>  Changelog
>  ---------
>  
> +- Wrap ``sys.modules`` dictionary in an "opaque" dictionary class,
> +  such that the representation string does not list all loaded
> +  modules. [malthe]
> +
>  1.0b15 (2009/04/24)
>  ~~~~~~~~~~~~~~~~~~~
>  
> 
> Modified: z3c.pt/trunk/src/z3c/pt/pagetemplate.py
> ===================================================================
> --- z3c.pt/trunk/src/z3c/pt/pagetemplate.py	2009-05-11 06:51:33 UTC (rev 99832)
> +++ z3c.pt/trunk/src/z3c/pt/pagetemplate.py	2009-05-11 06:54:42 UTC (rev 99833)
> @@ -20,6 +20,18 @@
>  _marker = object()
>  _expr_cache = {}
>  
> +class opaque_dict(dict):
> +    def __new__(cls, dictionary):
> +        inst = dict.__new__(cls)
> +        inst.__getitem__ = dictionary.__getitem__
> +        inst.__len__ = dictionary.__len__
> +        return inst
> +
> +    def __repr__(self):
> +        return "{...} (%d entries)" % len(self)
> +
> +sys_modules = opaque_dict(sys.modules)
> +
>  def evaluate_expression(pragma, expr):
>      key = "%s(%s)" % (pragma, expr)
>      try:
> @@ -118,7 +130,7 @@
>              path=evaluate_path,
>              exists=evaluate_exists,
>              nothing=None,
> -            modules=sys.modules)
> +            modules=sys_modules)
>  
>  class BaseTemplateFile(BaseTemplate, template.PageTemplateFile):
>      """If ``filename`` is a relative path, the module path of the
> @@ -200,7 +212,7 @@
>              exists=evaluate_exists,
>              options=kwargs,
>              nothing=None,
> -            modules=sys.modules)
> +            modules=sys_modules)
>  
>      def __call__(self, _ob=None, context=None, request=None, **kwargs):
>          kwargs.setdefault('context', context)
> 
> _______________________________________________
> Checkins mailing list
> Checkins at zope.org
> http://mail.zope.org/mailman/listinfo/checkins



More information about the Checkins mailing list