Mercurial > genshi > mirror
comparison genshi/template/eval.py @ 1023:2036193f89e7 trunk
Add support for Python 3.4 AST (support for NameConstants and changes to existing to arguments node attributes).
author | hodgestar |
---|---|
date | Sun, 16 Feb 2014 18:46:15 +0000 |
parents | 31bbb6f9e87b |
children | a5e09a7ba12d |
comparison
equal
deleted
inserted
replaced
1022:500573200533 | 1023:2036193f89e7 |
---|---|
22 from genshi.template.astutil import ASTTransformer, ASTCodeGenerator, \ | 22 from genshi.template.astutil import ASTTransformer, ASTCodeGenerator, \ |
23 _ast, parse | 23 _ast, parse |
24 from genshi.template.base import TemplateRuntimeError | 24 from genshi.template.base import TemplateRuntimeError |
25 from genshi.util import flatten | 25 from genshi.util import flatten |
26 | 26 |
27 from genshi.compat import get_code_params, build_code_chunk, IS_PYTHON2 | 27 from genshi.compat import get_code_params, build_code_chunk, isstring, \ |
28 IS_PYTHON2 | |
28 | 29 |
29 __all__ = ['Code', 'Expression', 'Suite', 'LenientLookup', 'StrictLookup', | 30 __all__ = ['Code', 'Expression', 'Suite', 'LenientLookup', 'StrictLookup', |
30 'Undefined', 'UndefinedError'] | 31 'Undefined', 'UndefinedError'] |
31 __docformat__ = 'restructuredtext en' | 32 __docformat__ = 'restructuredtext en' |
32 | 33 |
493 """ | 494 """ |
494 | 495 |
495 def __init__(self): | 496 def __init__(self): |
496 self.locals = [CONSTANTS] | 497 self.locals = [CONSTANTS] |
497 | 498 |
499 def _process(self, names, node): | |
500 if not IS_PYTHON2 and isinstance(node, _ast.arg): | |
501 names.add(node.arg) | |
502 elif isstring(node): | |
503 names.add(node) | |
504 elif isinstance(node, _ast.Name): | |
505 names.add(node.id) | |
506 elif isinstance(node, _ast.alias): | |
507 names.add(node.asname or node.name) | |
508 elif isinstance(node, _ast.Tuple): | |
509 for elt in node.elts: | |
510 self._process(names, elt) | |
511 | |
498 def _extract_names(self, node): | 512 def _extract_names(self, node): |
499 names = set() | 513 names = set() |
500 def _process(node): | |
501 if not IS_PYTHON2 and isinstance(node, _ast.arg): | |
502 names.add(node.arg) | |
503 if isinstance(node, _ast.Name): | |
504 names.add(node.id) | |
505 elif isinstance(node, _ast.alias): | |
506 names.add(node.asname or node.name) | |
507 elif isinstance(node, _ast.Tuple): | |
508 for elt in node.elts: | |
509 _process(elt) | |
510 if hasattr(node, 'args'): | 514 if hasattr(node, 'args'): |
511 for arg in node.args: | 515 for arg in node.args: |
512 _process(arg) | 516 self._process(names, arg) |
513 if hasattr(node, 'vararg'): | 517 if hasattr(node, 'vararg'): |
514 names.add(node.vararg) | 518 self._process(names, node.vararg) |
515 if hasattr(node, 'kwarg'): | 519 if hasattr(node, 'kwarg'): |
516 names.add(node.kwarg) | 520 self._process(names, node.kwarg) |
517 elif hasattr(node, 'names'): | 521 elif hasattr(node, 'names'): |
518 for elt in node.names: | 522 for elt in node.names: |
519 _process(elt) | 523 self._process(names, elt) |
520 return names | 524 return names |
521 | 525 |
522 def visit_Str(self, node): | 526 def visit_Str(self, node): |
523 if not isinstance(node.s, unicode): | 527 if not isinstance(node.s, unicode): |
524 try: # If the string is ASCII, return a `str` object | 528 try: # If the string is ASCII, return a `str` object |