Edgewall Software

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

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

alternative patch that doesn't do weird things with the context

  • 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(context, 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    namespace = context.frames[0]
     56    mod = __import__(module, None, None, ['__all__'])
     57    if hasattr(mod, '__all__'):
     58        members = mod.__all__
     59    else:
     60        members = [x for x in mod.__dict__.items()
     61                   if not x.startswith('_')]
     62    for member in members:
     63        namespace[member] = getattr(mod, member)
     64
     65
    3766class Code(object):
    3867    """Abstract base class for the `Expression` and `Suite` classes."""
    3968    __slots__ = ['source', 'code', 'ast', '_globals']
     
    270299            '_lookup_name': cls.lookup_name,
    271300            '_lookup_attr': cls.lookup_attr,
    272301            '_lookup_item': cls.lookup_item,
     302            '_star_import_compat': _star_import_compat,
    273303            'UndefinedError': UndefinedError,
    274304        }
    275305    globals = classmethod(globals)
     
    520550            [self.visit(x) for x in node.subs]
    521551        )
    522552
     553    def visitFrom(self, node):
     554        if not has_star_import_bug or node.names != [('*', None)]:
     555            return node
     556        # this is a python 2.x bug.  Only if we have a broken python
     557        # version we have to apply that hack.
     558        return ast.Discard(ast.CallFunc(
     559            ast.Name('_star_import_compat'),
     560            [ast.Name('__data__'), ast.Const(node.modname)], None, None
     561        ), lineno=node.lineno)
     562
    523563    # Statements
    524564
    525565    def visitAssert(self, node):