| | 37 | # check for a python 2.4 bug in the ceval loop. |
| | 38 | try: |
| | 39 | class _FakeMapping(object): |
| | 40 | __getitem__ = __setitem__ = lambda *a: None |
| | 41 | exec 'from sys import *' in {}, _FakeMapping() |
| | 42 | except SystemError: |
| | 43 | has_star_import_bug = True |
| | 44 | else: |
| | 45 | has_star_import_bug = False |
| | 46 | del _FakeMapping |
| | 47 | |
| | 48 | |
| | 49 | def _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 | |
| | 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 | |