Mercurial > genshi > mirror
comparison genshi/template/eval.py @ 750:52219748e5c1 trunk
Remove some cruft for supporting Python 2.3.
author | cmlenz |
---|---|
date | Mon, 09 Jun 2008 15:19:59 +0000 |
parents | 6de290dec976 |
children | be88c77839fc |
comparison
equal
deleted
inserted
replaced
749:8718e1e94c1c | 750:52219748e5c1 |
---|---|
15 | 15 |
16 import __builtin__ | 16 import __builtin__ |
17 from compiler import ast, parse | 17 from compiler import ast, parse |
18 from compiler.pycodegen import ExpressionCodeGenerator, ModuleCodeGenerator | 18 from compiler.pycodegen import ExpressionCodeGenerator, ModuleCodeGenerator |
19 import new | 19 import new |
20 try: | |
21 set | |
22 except NameError: | |
23 from sets import ImmutableSet as frozenset | |
24 from sets import Set as set | |
25 from textwrap import dedent | 20 from textwrap import dedent |
26 | 21 |
27 from genshi.core import Markup | 22 from genshi.core import Markup |
28 from genshi.template.base import TemplateRuntimeError | 23 from genshi.template.base import TemplateRuntimeError |
29 from genshi.util import flatten | 24 from genshi.util import flatten |
38 class _FakeMapping(object): | 33 class _FakeMapping(object): |
39 __getitem__ = __setitem__ = lambda *a: None | 34 __getitem__ = __setitem__ = lambda *a: None |
40 exec 'from sys import *' in {}, _FakeMapping() | 35 exec 'from sys import *' in {}, _FakeMapping() |
41 except SystemError: | 36 except SystemError: |
42 has_star_import_bug = True | 37 has_star_import_bug = True |
43 except TypeError: | |
44 pass # Python 2.3 | |
45 del _FakeMapping | 38 del _FakeMapping |
46 | 39 |
47 def _star_import_patch(mapping, modname): | 40 def _star_import_patch(mapping, modname): |
48 """This function is used as helper if a Python version with a broken | 41 """This function is used as helper if a Python version with a broken |
49 star-import opcode is in use. | 42 star-import opcode is in use. |
482 def _clone(self, node, *args): | 475 def _clone(self, node, *args): |
483 lineno = getattr(node, 'lineno', None) | 476 lineno = getattr(node, 'lineno', None) |
484 node = node.__class__(*args) | 477 node = node.__class__(*args) |
485 if lineno is not None: | 478 if lineno is not None: |
486 node.lineno = lineno | 479 node.lineno = lineno |
487 if isinstance(node, (ast.Class, ast.Function, ast.Lambda)) or \ | 480 if isinstance(node, (ast.Class, ast.Function, ast.Lambda, |
488 hasattr(ast, 'GenExpr') and isinstance(node, ast.GenExpr): | 481 ast.GenExpr)): |
489 node.filename = '<string>' # workaround for bug in pycodegen | 482 node.filename = '<string>' # workaround for bug in pycodegen |
490 return node | 483 return node |
491 | 484 |
492 def _visitDefault(self, node): | 485 def _visitDefault(self, node): |
493 return node | 486 return node |
518 def visitFrom(self, node): | 511 def visitFrom(self, node): |
519 if not has_star_import_bug or node.names != [('*', None)]: | 512 if not has_star_import_bug or node.names != [('*', None)]: |
520 # This is a Python 2.4 bug. Only if we have a broken Python | 513 # This is a Python 2.4 bug. Only if we have a broken Python |
521 # version we have to apply the hack | 514 # version we have to apply the hack |
522 return node | 515 return node |
523 new_node = ast.Discard(ast.CallFunc( | 516 return ast.Discard(ast.CallFunc( |
524 ast.Name('_star_import_patch'), | 517 ast.Name('_star_import_patch'), |
525 [ast.Name('__data__'), ast.Const(node.modname)], None, None | 518 [ast.Name('__data__'), ast.Const(node.modname)], None, None |
526 )) | 519 ), lineno=node.lineno) |
527 if hasattr(node, 'lineno'): # No lineno in Python 2.3 | |
528 new_node.lineno = node.lineno | |
529 return new_node | |
530 | 520 |
531 def visitFunction(self, node): | 521 def visitFunction(self, node): |
532 args = [] | 522 args = [] |
533 if hasattr(node, 'decorators'): | 523 if hasattr(node, 'decorators'): |
534 args.append(self.visit(node.decorators)) | 524 args.append(self.visit(node.decorators)) |