comparison genshi/template/eval.py @ 739:6de290dec976 trunk

Another Python 2.3 fix in the wake of #221.
author cmlenz
date Fri, 06 Jun 2008 15:52:33 +0000
parents a1e8b24bc1bb
children 52219748e5c1 5d8ca3f527af
comparison
equal deleted inserted replaced
738:3b8a38fcc1ab 739:6de290dec976
31 __all__ = ['Code', 'Expression', 'Suite', 'LenientLookup', 'StrictLookup', 31 __all__ = ['Code', 'Expression', 'Suite', 'LenientLookup', 'StrictLookup',
32 'Undefined', 'UndefinedError'] 32 'Undefined', 'UndefinedError']
33 __docformat__ = 'restructuredtext en' 33 __docformat__ = 'restructuredtext en'
34 34
35 # Check for a Python 2.4 bug in the eval loop 35 # Check for a Python 2.4 bug in the eval loop
36 has_star_import_bug = False
36 try: 37 try:
37 class _FakeMapping(object): 38 class _FakeMapping(object):
38 __getitem__ = __setitem__ = lambda *a: None 39 __getitem__ = __setitem__ = lambda *a: None
39 exec 'from sys import *' in {}, _FakeMapping() 40 exec 'from sys import *' in {}, _FakeMapping()
40 except (SystemError, TypeError): 41 except SystemError:
41 has_star_import_bug = True 42 has_star_import_bug = True
42 else: 43 except TypeError:
43 has_star_import_bug = False 44 pass # Python 2.3
44 del _FakeMapping 45 del _FakeMapping
45 46
46 def _star_import_patch(mapping, modname): 47 def _star_import_patch(mapping, modname):
47 """This function is used as helper if a Python version with a broken 48 """This function is used as helper if a Python version with a broken
48 star-import opcode is in use. 49 star-import opcode is in use.
517 def visitFrom(self, node): 518 def visitFrom(self, node):
518 if not has_star_import_bug or node.names != [('*', None)]: 519 if not has_star_import_bug or node.names != [('*', None)]:
519 # This is a Python 2.4 bug. Only if we have a broken Python 520 # This is a Python 2.4 bug. Only if we have a broken Python
520 # version we have to apply the hack 521 # version we have to apply the hack
521 return node 522 return node
522 return ast.Discard(ast.CallFunc( 523 new_node = ast.Discard(ast.CallFunc(
523 ast.Name('_star_import_patch'), 524 ast.Name('_star_import_patch'),
524 [ast.Name('__data__'), ast.Const(node.modname)], None, None 525 [ast.Name('__data__'), ast.Const(node.modname)], None, None
525 ), lineno=node.lineno) 526 ))
527 if hasattr(node, 'lineno'): # No lineno in Python 2.3
528 new_node.lineno = node.lineno
529 return new_node
526 530
527 def visitFunction(self, node): 531 def visitFunction(self, node):
528 args = [] 532 args = []
529 if hasattr(node, 'decorators'): 533 if hasattr(node, 'decorators'):
530 args.append(self.visit(node.decorators)) 534 args.append(self.visit(node.decorators))
Copyright (C) 2012-2017 Edgewall Software