Edgewall Software

Ticket #221: genshi-py24-star-import-3.patch

File genshi-py24-star-import-3.patch, 2.5 KB (added by aronacher, 15 years ago)

This patch doesn't expect a context object any more and fixes a bug with modules without all deifned. However for performance reasons it implements update() on Context

  • genshi/template/base.py

     
    244244        """
    245245        return [(key, self.get(key)) for key in self.keys()]
    246246
     247    def update(self, mapping):
     248        """Update the context from the mapping provided."""
     249        self.frames[0].update(mapping)
     250
    247251    def push(self, data):
    248252        """Push a new scope on the stack.
    249253       
  • genshi/template/eval.py

     
    3434__docformat__ = 'restructuredtext en'
    3535
    3636
     37# check for a python 2.4 bug in the ceval loop.
     38try:
     39    class _FakeMapping(object):
     40        __getitem__ = __setitem__ = lambda *a: None
     41    exec 'from sys import *' in {}, _FakeMapping()
     42except SystemError:
     43    has_star_import_bug = True
     44else:
     45    has_star_import_bug = False
     46del _FakeMapping
     47
     48
     49def _star_import_compat(namespace, module):
     50    """This function is used as helper callback if a Python version
     51    with a broken star-import opcode is in use.
     52
     53    :see: has_star_import_bug
     54    """
     55    mod = __import__(module, None, None, ['__all__'])
     56    if hasattr(mod, '__all__'):
     57        members = mod.__all__
     58    else:
     59        members = (x for x in mod.__dict__ if not x.startswith('_'))
     60    namespace.update((m, getattr(mod, m)) for m in members)
     61
     62
    3763class Code(object):
    3864    """Abstract base class for the `Expression` and `Suite` classes."""
    3965    __slots__ = ['source', 'code', 'ast', '_globals']
     
    270296            '_lookup_name': cls.lookup_name,
    271297            '_lookup_attr': cls.lookup_attr,
    272298            '_lookup_item': cls.lookup_item,
     299            '_star_import_compat': _star_import_compat,
    273300            'UndefinedError': UndefinedError,
    274301        }
    275302    globals = classmethod(globals)
     
    520547            [self.visit(x) for x in node.subs]
    521548        )
    522549
     550    def visitFrom(self, node):
     551        if not has_star_import_bug or node.names != [('*', None)]:
     552            return node
     553        # this is a python 2.x bug.  Only if we have a broken python
     554        # version we have to apply that hack.
     555        return ast.Discard(ast.CallFunc(
     556            ast.Name('_star_import_compat'),
     557            [ast.Name('__data__'), ast.Const(node.modname)], None, None
     558        ), lineno=node.lineno)
     559
    523560    # Statements
    524561
    525562    def visitAssert(self, node):