PageRenderTime 80ms CodeModel.GetById 17ms app.highlight 56ms RepoModel.GetById 1ms app.codeStats 0ms

/logilab-astng-0.23.1/rebuilder.py

#
Python | 864 lines | 745 code | 55 blank | 64 comment | 75 complexity | 97b05e454dcce3504b5a7689f652f558 MD5 | raw file
  1# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
  2# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
  3# copyright 2003-2010 Sylvain Thenault, all rights reserved.
  4# contact mailto:thenault@gmail.com
  5#
  6# This file is part of logilab-astng.
  7#
  8# logilab-astng is free software: you can redistribute it and/or modify it
  9# under the terms of the GNU Lesser General Public License as published by the
 10# Free Software Foundation, either version 2.1 of the License, or (at your
 11# option) any later version.
 12#
 13# logilab-astng is distributed in the hope that it will be useful, but
 14# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 15# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 16# for more details.
 17#
 18# You should have received a copy of the GNU Lesser General Public License along
 19# with logilab-astng. If not, see <http://www.gnu.org/licenses/>.
 20"""this module contains utilities for rebuilding a _ast tree in
 21order to get a single ASTNG representation
 22"""
 23
 24import sys
 25from _ast import (Expr as Discard, Str,
 26    # binary operators
 27    Add, Div, FloorDiv,  Mod, Mult, Pow, Sub, BitAnd, BitOr, BitXor,
 28    LShift, RShift,
 29    # logical operators
 30    And, Or,
 31    # unary operators
 32    UAdd, USub, Not, Invert,
 33    # comparison operators
 34    Eq, Gt, GtE, In, Is, IsNot, Lt, LtE, NotEq, NotIn,
 35    )
 36
 37from logilab.astng.exceptions import ASTNGBuildingException
 38from logilab.astng import nodes as new
 39
 40
 41_BIN_OP_CLASSES = {Add: '+',
 42                   BitAnd: '&',
 43                   BitOr: '|',
 44                   BitXor: '^',
 45                   Div: '/',
 46                   FloorDiv: '//',
 47                   Mod: '%',
 48                   Mult: '*',
 49                   Pow: '**',
 50                   Sub: '-',
 51                   LShift: '<<',
 52                   RShift: '>>'}
 53
 54_BOOL_OP_CLASSES = {And: 'and',
 55                    Or: 'or'}
 56
 57_UNARY_OP_CLASSES = {UAdd: '+',
 58                     USub: '-',
 59                     Not: 'not',
 60                     Invert: '~'}
 61
 62_CMP_OP_CLASSES = {Eq: '==',
 63                   Gt: '>',
 64                   GtE: '>=',
 65                   In: 'in',
 66                   Is: 'is',
 67                   IsNot: 'is not',
 68                   Lt: '<',
 69                   LtE: '<=',
 70                   NotEq: '!=',
 71                   NotIn: 'not in'}
 72
 73CONST_NAME_TRANSFORMS = {'None':  None,
 74                         'True':  True,
 75                         'False': False}
 76
 77REDIRECT = {'arguments': 'Arguments',
 78            'Attribute': 'Getattr',
 79            'comprehension': 'Comprehension',
 80            'Call': 'CallFunc',
 81            'ClassDef': 'Class',
 82            "ListCompFor": 'Comprehension',
 83            "GenExprFor": 'Comprehension',
 84            'excepthandler': 'ExceptHandler',
 85            'Expr': 'Discard',
 86            'FunctionDef': 'Function',
 87            'GeneratorExp': 'GenExpr',
 88            'ImportFrom': 'From',
 89            'keyword': 'Keyword',
 90            'Repr': 'Backquote',
 91            }
 92
 93def _init_set_doc(node, newnode):
 94    newnode.doc = None
 95    try:
 96        if isinstance(node.body[0], Discard) and isinstance(node.body[0].value, Str):
 97            newnode.tolineno = node.body[0].lineno
 98            newnode.doc = node.body[0].value.s
 99            node.body = node.body[1:]
100
101    except IndexError:
102        pass # ast built from scratch
103
104def _lineno_parent(oldnode, newnode, parent):
105    newnode.parent = parent
106    if hasattr(oldnode, 'lineno'):
107        newnode.lineno = oldnode.lineno
108    if hasattr(oldnode, 'col_offset'):
109        newnode.col_offset = oldnode.col_offset
110
111def _set_infos(oldnode, newnode, parent):
112    newnode.parent = parent
113    if hasattr(oldnode, 'lineno'):
114        newnode.lineno = oldnode.lineno
115    if hasattr(oldnode, 'col_offset'):
116        newnode.col_offset = oldnode.col_offset
117    newnode.set_line_info(newnode.last_child()) # set_line_info accepts None
118
119
120
121
122class TreeRebuilder(object):
123    """Rebuilds the _ast tree to become an ASTNG tree"""
124
125    _visit_meths = {}
126    def __init__(self):
127        self.init()
128
129    def init(self):
130        self.asscontext = None
131        self._metaclass = ['']
132        self._global_names = []
133        self._from_nodes = []
134        self._delayed_assattr = []
135
136    def visit(self, node, parent):
137        cls = node.__class__
138        if cls in self._visit_meths:
139            return self._visit_meths[cls](node, parent)
140        else:
141            cls_name = cls.__name__
142            visit_name = 'visit_' + REDIRECT.get(cls_name, cls_name).lower()
143            visit_method = getattr(self, visit_name)
144            self._visit_meths[cls] = visit_method
145            return visit_method(node, parent)
146
147    def _save_assignment(self, node, name=None):
148        """save assignement situation since node.parent is not available yet"""
149        if self._global_names and node.name in self._global_names[-1]:
150            node.root().set_local(node.name, node)
151        else:
152            node.parent.set_local(node.name, node)
153
154
155    def visit_arguments(self, node, parent):
156        """visit a Arguments node by returning a fresh instance of it"""
157        newnode = new.Arguments()
158        _lineno_parent(node, newnode, parent)
159        self.asscontext = "Ass"
160        newnode.args = [self.visit(child, newnode) for child in node.args]
161        self.asscontext = None
162        newnode.defaults = [self.visit(child, newnode) for child in node.defaults]
163        newnode.vararg = node.vararg
164        newnode.kwarg = node.kwarg
165        # save argument names in locals:
166        if node.vararg:
167            newnode.parent.set_local(newnode.vararg, newnode)
168        if node.kwarg:
169            newnode.parent.set_local(newnode.kwarg, newnode)
170        newnode.set_line_info(newnode.last_child())
171        return newnode
172
173    def visit_assattr(self, node, parent):
174        """visit a AssAttr node by returning a fresh instance of it"""
175        assc, self.asscontext = self.asscontext, None
176        newnode = new.AssAttr()
177        _lineno_parent(node, newnode, parent)
178        newnode.expr = self.visit(node.expr, newnode)
179        self.asscontext = assc
180        self._delayed_assattr.append(newnode)
181        newnode.set_line_info(newnode.last_child())
182        return newnode
183
184    def visit_assert(self, node, parent):
185        """visit a Assert node by returning a fresh instance of it"""
186        newnode = new.Assert()
187        _lineno_parent(node, newnode, parent)
188        newnode.test = self.visit(node.test, newnode)
189        if node.msg is not None:
190            newnode.fail = self.visit(node.msg, newnode)
191        newnode.set_line_info(newnode.last_child())
192        return newnode
193
194    def visit_assign(self, node, parent):
195        """visit a Assign node by returning a fresh instance of it"""
196        newnode = new.Assign()
197        _lineno_parent(node, newnode, parent)
198        self.asscontext = "Ass"
199        newnode.targets = [self.visit(child, newnode) for child in node.targets]
200        self.asscontext = None
201        newnode.value = self.visit(node.value, newnode)
202        # set some function or metaclass infos  XXX explain ?
203        klass = newnode.parent.frame()
204        if (isinstance(klass, new.Class)
205            and isinstance(newnode.value, new.CallFunc)
206            and isinstance(newnode.value.func, new.Name)):
207            func_name = newnode.value.func.name
208            for ass_node in newnode.targets:
209                try:
210                    meth = klass[ass_node.name]
211                    if isinstance(meth, new.Function):
212                        if func_name in ('classmethod', 'staticmethod'):
213                            meth.type = func_name
214                        elif func_name == 'classproperty': # see lgc.decorators
215                            meth.type = 'classmethod'
216                        meth.extra_decorators.append(newnode.value)
217                except (AttributeError, KeyError):
218                    continue
219        elif getattr(newnode.targets[0], 'name', None) == '__metaclass__':
220            # XXX check more...
221            self._metaclass[-1] = 'type' # XXX get the actual metaclass
222        newnode.set_line_info(newnode.last_child())
223        return newnode
224
225    def visit_assname(self, node, parent, node_name=None):
226        '''visit a node and return a AssName node'''
227        newnode = new.AssName()
228        _set_infos(node, newnode, parent)
229        newnode.name = node_name
230        self._save_assignment(newnode)
231        return newnode
232
233    def visit_augassign(self, node, parent):
234        """visit a AugAssign node by returning a fresh instance of it"""
235        newnode = new.AugAssign()
236        _lineno_parent(node, newnode, parent)
237        newnode.op = _BIN_OP_CLASSES[node.op.__class__] + "="
238        self.asscontext = "Ass"
239        newnode.target = self.visit(node.target, newnode)
240        self.asscontext = None
241        newnode.value = self.visit(node.value, newnode)
242        newnode.set_line_info(newnode.last_child())
243        return newnode
244
245    def visit_backquote(self, node, parent):
246        """visit a Backquote node by returning a fresh instance of it"""
247        newnode = new.Backquote()
248        _lineno_parent(node, newnode, parent)
249        newnode.value = self.visit(node.value, newnode)
250        newnode.set_line_info(newnode.last_child())
251        return newnode
252
253    def visit_binop(self, node, parent):
254        """visit a BinOp node by returning a fresh instance of it"""
255        newnode = new.BinOp()
256        _lineno_parent(node, newnode, parent)
257        newnode.left = self.visit(node.left, newnode)
258        newnode.right = self.visit(node.right, newnode)
259        newnode.op = _BIN_OP_CLASSES[node.op.__class__]
260        newnode.set_line_info(newnode.last_child())
261        return newnode
262
263    def visit_boolop(self, node, parent):
264        """visit a BoolOp node by returning a fresh instance of it"""
265        newnode = new.BoolOp()
266        _lineno_parent(node, newnode, parent)
267        newnode.values = [self.visit(child, newnode) for child in node.values]
268        newnode.op = _BOOL_OP_CLASSES[node.op.__class__]
269        newnode.set_line_info(newnode.last_child())
270        return newnode
271
272    def visit_break(self, node, parent):
273        """visit a Break node by returning a fresh instance of it"""
274        newnode = new.Break()
275        _set_infos(node, newnode, parent)
276        return newnode
277
278    def visit_callfunc(self, node, parent):
279        """visit a CallFunc node by returning a fresh instance of it"""
280        newnode = new.CallFunc()
281        _lineno_parent(node, newnode, parent)
282        newnode.func = self.visit(node.func, newnode)
283        newnode.args = [self.visit(child, newnode) for child in node.args]
284        if node.starargs is not None:
285            newnode.starargs = self.visit(node.starargs, newnode)
286        if node.kwargs is not None:
287            newnode.kwargs = self.visit(node.kwargs, newnode)
288        newnode.args.extend(self.visit(child, newnode) for child in node.keywords)
289        newnode.set_line_info(newnode.last_child())
290        return newnode
291
292    def visit_class(self, node, parent):
293        """visit a Class node to become astng"""
294        self._metaclass.append(self._metaclass[-1])
295        newnode = new.Class(node.name, None)
296        _lineno_parent(node, newnode, parent)
297        _init_set_doc(node, newnode)
298        newnode.bases = [self.visit(child, newnode) for child in node.bases]
299        newnode.body = [self.visit(child, newnode) for child in node.body]
300        if 'decorator_list' in node._fields and node.decorator_list:# py >= 2.6
301            newnode.decorators = self.visit_decorators(node, newnode)
302        newnode.set_line_info(newnode.last_child())
303        metaclass = self._metaclass.pop()
304        if not newnode.bases:
305            # no base classes, detect new / style old style according to
306            # current scope
307            newnode._newstyle = metaclass == 'type'
308        newnode.parent.frame().set_local(newnode.name, newnode)
309        return newnode
310
311    def visit_const(self, node, parent):
312        """visit a Const node by returning a fresh instance of it"""
313        newnode = new.Const(node.value)
314        _set_infos(node, newnode, parent)
315        return newnode
316
317    def visit_continue(self, node, parent):
318        """visit a Continue node by returning a fresh instance of it"""
319        newnode = new.Continue()
320        _set_infos(node, newnode, parent)
321        return newnode
322
323    def visit_compare(self, node, parent):
324        """visit a Compare node by returning a fresh instance of it"""
325        newnode = new.Compare()
326        _lineno_parent(node, newnode, parent)
327        newnode.left = self.visit(node.left, newnode)
328        newnode.ops = [(_CMP_OP_CLASSES[op.__class__], self.visit(expr, newnode))
329                    for (op, expr) in zip(node.ops, node.comparators)]
330        newnode.set_line_info(newnode.last_child())
331        return newnode
332
333    def visit_comprehension(self, node, parent):
334        """visit a Comprehension node by returning a fresh instance of it"""
335        newnode = new.Comprehension()
336        _lineno_parent(node, newnode, parent)
337        self.asscontext = "Ass"
338        newnode.target = self.visit(node.target, newnode)
339        self.asscontext = None
340        newnode.iter = self.visit(node.iter, newnode)
341        newnode.ifs = [self.visit(child, newnode) for child in node.ifs]
342        newnode.set_line_info(newnode.last_child())
343        return newnode
344
345    def visit_decorators(self, node, parent):
346        """visit a Decorators node by returning a fresh instance of it"""
347        # /!\ node is actually a _ast.Function node while
348        # parent is a astng.nodes.Function node
349        newnode = new.Decorators()
350        _lineno_parent(node, newnode, parent)
351        if 'decorators' in node._fields: # py < 2.6, i.e. 2.5
352            decorators = node.decorators
353        else:
354            decorators= node.decorator_list
355        newnode.nodes = [self.visit(child, newnode) for child in decorators]
356        newnode.set_line_info(newnode.last_child())
357        return newnode
358
359    def visit_delete(self, node, parent):
360        """visit a Delete node by returning a fresh instance of it"""
361        newnode = new.Delete()
362        _lineno_parent(node, newnode, parent)
363        self.asscontext = "Del"
364        newnode.targets = [self.visit(child, newnode) for child in node.targets]
365        self.asscontext = None
366        newnode.set_line_info(newnode.last_child())
367        return newnode
368
369    def visit_dict(self, node, parent):
370        """visit a Dict node by returning a fresh instance of it"""
371        newnode = new.Dict()
372        _lineno_parent(node, newnode, parent)
373        newnode.items = [(self.visit(key, newnode), self.visit(value, newnode))
374                          for key, value in zip(node.keys, node.values)]
375        newnode.set_line_info(newnode.last_child())
376        return newnode
377
378    def visit_dictcomp(self, node, parent):
379        """visit a DictComp node by returning a fresh instance of it"""
380        newnode = new.DictComp()
381        _lineno_parent(node, newnode, parent)
382        newnode.key = self.visit(node.key, newnode)
383        newnode.value = self.visit(node.value, newnode)
384        newnode.generators = [self.visit(child, newnode)
385                              for child in node.generators]
386        newnode.set_line_info(newnode.last_child())
387        return newnode
388
389    def visit_discard(self, node, parent):
390        """visit a Discard node by returning a fresh instance of it"""
391        newnode = new.Discard()
392        _lineno_parent(node, newnode, parent)
393        newnode.value = self.visit(node.value, newnode)
394        newnode.set_line_info(newnode.last_child())
395        return newnode
396
397    def visit_ellipsis(self, node, parent):
398        """visit an Ellipsis node by returning a fresh instance of it"""
399        newnode = new.Ellipsis()
400        _set_infos(node, newnode, parent)
401        return newnode
402
403    def visit_emptynode(self, node, parent):
404        """visit an EmptyNode node by returning a fresh instance of it"""
405        newnode = new.EmptyNode()
406        _set_infos(node, newnode, parent)
407        return newnode
408
409    def visit_excepthandler(self, node, parent):
410        """visit an ExceptHandler node by returning a fresh instance of it"""
411        newnode = new.ExceptHandler()
412        _lineno_parent(node, newnode, parent)
413        if node.type is not None:
414            newnode.type = self.visit(node.type, newnode)
415        if node.name is not None:
416            # /!\ node.name can be a tuple
417            self.asscontext = "Ass"
418            newnode.name = self.visit(node.name, newnode)
419            self.asscontext = None
420        newnode.body = [self.visit(child, newnode) for child in node.body]
421        newnode.set_line_info(newnode.last_child())
422        return newnode
423
424    def visit_exec(self, node, parent):
425        """visit an Exec node by returning a fresh instance of it"""
426        newnode = new.Exec()
427        _lineno_parent(node, newnode, parent)
428        newnode.expr = self.visit(node.body, newnode)
429        if node.globals is not None:
430            newnode.globals = self.visit(node.globals, newnode)
431        if node.locals is not None:
432            newnode.locals = self.visit(node.locals, newnode)
433        newnode.set_line_info(newnode.last_child())
434        return newnode
435
436    def visit_extslice(self, node, parent):
437        """visit an ExtSlice node by returning a fresh instance of it"""
438        newnode = new.ExtSlice()
439        _lineno_parent(node, newnode, parent)
440        newnode.dims = [self.visit(dim, newnode) for dim in node.dims]
441        newnode.set_line_info(newnode.last_child())
442        return newnode
443
444    def visit_for(self, node, parent):
445        """visit a For node by returning a fresh instance of it"""
446        newnode = new.For()
447        _lineno_parent(node, newnode, parent)
448        self.asscontext = "Ass"
449        newnode.target = self.visit(node.target, newnode)
450        self.asscontext = None
451        newnode.iter = self.visit(node.iter, newnode)
452        newnode.body = [self.visit(child, newnode) for child in node.body]
453        newnode.orelse = [self.visit(child, newnode) for child in node.orelse]
454        newnode.set_line_info(newnode.last_child())
455        return newnode
456
457    def visit_from(self, node, parent):
458        """visit a From node by returning a fresh instance of it"""
459        names = [(alias.name, alias.asname) for alias in node.names]
460        newnode = new.From(node.module or '', names, node.level)
461        _set_infos(node, newnode, parent)
462        # store From names to add them to locals after building
463        self._from_nodes.append(newnode)
464        return newnode
465
466    def visit_function(self, node, parent):
467        """visit an Function node to become astng"""
468        self._global_names.append({})
469        newnode = new.Function(node.name, None)
470        _lineno_parent(node, newnode, parent)
471        _init_set_doc(node, newnode)
472        newnode.args = self.visit(node.args, newnode)
473        newnode.body = [self.visit(child, newnode) for child in node.body]
474        if 'decorators' in node._fields: # py < 2.6
475            attr = 'decorators'
476        else:
477            attr = 'decorator_list'
478        decorators = getattr(node, attr)
479        if decorators:
480            newnode.decorators = self.visit_decorators(node, newnode)
481        newnode.set_line_info(newnode.last_child())
482        self._global_names.pop()
483        frame = newnode.parent.frame()
484        if isinstance(frame, new.Class):
485            if newnode.name == '__new__':
486                newnode.type = 'classmethod'
487            else:
488                newnode.type = 'method'
489        if newnode.decorators is not None:
490            for decorator_expr in newnode.decorators.nodes:
491                if isinstance(decorator_expr, new.Name):
492                    if decorator_expr.name in ('classmethod', 'staticmethod'):
493                        newnode.type = decorator_expr.name
494                    elif decorator_expr.name == 'classproperty':
495                        newnode.type = 'classmethod'
496        frame.set_local(newnode.name, newnode)
497        return newnode
498
499    def visit_genexpr(self, node, parent):
500        """visit a GenExpr node by returning a fresh instance of it"""
501        newnode = new.GenExpr()
502        _lineno_parent(node, newnode, parent)
503        newnode.elt = self.visit(node.elt, newnode)
504        newnode.generators = [self.visit(child, newnode) for child in node.generators]
505        newnode.set_line_info(newnode.last_child())
506        return newnode
507
508    def visit_getattr(self, node, parent):
509        """visit a Getattr node by returning a fresh instance of it"""
510        if self.asscontext == "Del":
511            # FIXME : maybe we should reintroduce and visit_delattr ?
512            # for instance, deactivating asscontext
513            newnode = new.DelAttr()
514        elif self.asscontext == "Ass":
515            # FIXME : maybe we should call visit_assattr ?
516            newnode = new.AssAttr()
517            self._delayed_assattr.append(newnode)
518        else:
519            newnode = new.Getattr()
520        _lineno_parent(node, newnode, parent)
521        asscontext, self.asscontext = self.asscontext, None
522        newnode.expr = self.visit(node.value, newnode)
523        self.asscontext = asscontext
524        newnode.attrname = node.attr
525        newnode.set_line_info(newnode.last_child())
526        return newnode
527
528    def visit_global(self, node, parent):
529        """visit an Global node to become astng"""
530        newnode = new.Global(node.names)
531        _set_infos(node, newnode, parent)
532        if self._global_names: # global at the module level, no effect
533            for name in node.names:
534                self._global_names[-1].setdefault(name, []).append(newnode)
535        return newnode
536
537    def visit_if(self, node, parent):
538        """visit a If node by returning a fresh instance of it"""
539        newnode = new.If()
540        _lineno_parent(node, newnode, parent)
541        newnode.test = self.visit(node.test, newnode)
542        newnode.body = [self.visit(child, newnode) for child in node.body]
543        newnode.orelse = [self.visit(child, newnode) for child in node.orelse]
544        newnode.set_line_info(newnode.last_child())
545        return newnode
546
547    def visit_ifexp(self, node, parent):
548        """visit a IfExp node by returning a fresh instance of it"""
549        newnode = new.IfExp()
550        _lineno_parent(node, newnode, parent)
551        newnode.test = self.visit(node.test, newnode)
552        newnode.body = self.visit(node.body, newnode)
553        newnode.orelse = self.visit(node.orelse, newnode)
554        newnode.set_line_info(newnode.last_child())
555        return newnode
556
557    def visit_import(self, node, parent):
558        """visit a Import node by returning a fresh instance of it"""
559        newnode = new.Import()
560        _set_infos(node, newnode, parent)
561        newnode.names = [(alias.name, alias.asname) for alias in node.names]
562        # save import names in parent's locals:
563        for (name, asname) in newnode.names:
564            name = asname or name
565            newnode.parent.set_local(name.split('.')[0], newnode)
566        return newnode
567
568    def visit_index(self, node, parent):
569        """visit a Index node by returning a fresh instance of it"""
570        newnode = new.Index()
571        _lineno_parent(node, newnode, parent)
572        newnode.value = self.visit(node.value, newnode)
573        newnode.set_line_info(newnode.last_child())
574        return newnode
575
576    def visit_keyword(self, node, parent):
577        """visit a Keyword node by returning a fresh instance of it"""
578        newnode = new.Keyword()
579        _lineno_parent(node, newnode, parent)
580        newnode.arg = node.arg
581        newnode.value = self.visit(node.value, newnode)
582        newnode.set_line_info(newnode.last_child())
583        return newnode
584
585    def visit_lambda(self, node, parent):
586        """visit a Lambda node by returning a fresh instance of it"""
587        newnode = new.Lambda()
588        _lineno_parent(node, newnode, parent)
589        newnode.args = self.visit(node.args, newnode)
590        newnode.body = self.visit(node.body, newnode)
591        newnode.set_line_info(newnode.last_child())
592        return newnode
593
594    def visit_list(self, node, parent):
595        """visit a List node by returning a fresh instance of it"""
596        newnode = new.List()
597        _lineno_parent(node, newnode, parent)
598        newnode.elts = [self.visit(child, newnode) for child in node.elts]
599        newnode.set_line_info(newnode.last_child())
600        return newnode
601
602    def visit_listcomp(self, node, parent):
603        """visit a ListComp node by returning a fresh instance of it"""
604        newnode = new.ListComp()
605        _lineno_parent(node, newnode, parent)
606        newnode.elt = self.visit(node.elt, newnode)
607        newnode.generators = [self.visit(child, newnode)
608                              for child in node.generators]
609        newnode.set_line_info(newnode.last_child())
610        return newnode
611
612    def visit_module(self, node, modname, package):
613        """visit a Module node by returning a fresh instance of it"""
614        newnode = new.Module(modname, None)
615        newnode.package = package
616        _lineno_parent(node, newnode, parent=None)
617        _init_set_doc(node, newnode)
618        newnode.body = [self.visit(child, newnode) for child in node.body]
619        newnode.set_line_info(newnode.last_child())
620        return newnode
621
622    def visit_name(self, node, parent):
623        """visit a Name node by returning a fresh instance of it"""
624        # True and False can be assigned to something in py2x, so we have to
625        # check first the asscontext
626        if self.asscontext == "Del":
627            newnode = new.DelName()
628        elif self.asscontext is not None: # Ass
629            assert self.asscontext == "Ass"
630            newnode = new.AssName()
631        elif node.id in CONST_NAME_TRANSFORMS:
632            newnode = new.Const(CONST_NAME_TRANSFORMS[node.id])
633            _set_infos(node, newnode, parent)
634            return newnode
635        else:
636            newnode = new.Name()
637        _lineno_parent(node, newnode, parent)
638        newnode.name = node.id
639        # XXX REMOVE me :
640        if self.asscontext in ('Del', 'Ass'): # 'Aug' ??
641            self._save_assignment(newnode)
642        newnode.set_line_info(newnode.last_child())
643        return newnode
644
645    def visit_bytes(self, node, parent):
646        """visit a Bytes node by returning a fresh instance of Const"""
647        newnode = new.Const(node.s)
648        _set_infos(node, newnode, parent)
649        return newnode
650
651    def visit_num(self, node, parent):
652        """visit a Num node by returning a fresh instance of Const"""
653        newnode = new.Const(node.n)
654        _set_infos(node, newnode, parent)
655        return newnode
656
657    def visit_pass(self, node, parent):
658        """visit a Pass node by returning a fresh instance of it"""
659        newnode = new.Pass()
660        _set_infos(node, newnode, parent)
661        return newnode
662
663    def visit_str(self, node, parent):
664        """visit a Str node by returning a fresh instance of Const"""
665        newnode = new.Const(node.s)
666        _set_infos(node, newnode, parent)
667        return newnode
668
669    def visit_print(self, node, parent):
670        """visit a Print node by returning a fresh instance of it"""
671        newnode = new.Print()
672        _lineno_parent(node, newnode, parent)
673        newnode.nl = node.nl
674        if node.dest is not None:
675            newnode.dest = self.visit(node.dest, newnode)
676        newnode.values = [self.visit(child, newnode) for child in node.values]
677        newnode.set_line_info(newnode.last_child())
678        return newnode
679
680    def visit_raise(self, node, parent):
681        """visit a Raise node by returning a fresh instance of it"""
682        newnode = new.Raise()
683        _lineno_parent(node, newnode, parent)
684        if node.type is not None:
685            newnode.exc = self.visit(node.type, newnode)
686        if node.inst is not None:
687            newnode.inst = self.visit(node.inst, newnode)
688        if node.tback is not None:
689            newnode.tback = self.visit(node.tback, newnode)
690        newnode.set_line_info(newnode.last_child())
691        return newnode
692
693    def visit_return(self, node, parent):
694        """visit a Return node by returning a fresh instance of it"""
695        newnode = new.Return()
696        _lineno_parent(node, newnode, parent)
697        if node.value is not None:
698            newnode.value = self.visit(node.value, newnode)
699        newnode.set_line_info(newnode.last_child())
700        return newnode
701
702    def visit_set(self, node, parent):
703        """visit a Tuple node by returning a fresh instance of it"""
704        newnode = new.Set()
705        _lineno_parent(node, newnode, parent)
706        newnode.elts = [self.visit(child, newnode) for child in node.elts]
707        newnode.set_line_info(newnode.last_child())
708        return newnode
709
710    def visit_setcomp(self, node, parent):
711        """visit a SetComp node by returning a fresh instance of it"""
712        newnode = new.SetComp()
713        _lineno_parent(node, newnode, parent)
714        newnode.elt = self.visit(node.elt, newnode)
715        newnode.generators = [self.visit(child, newnode)
716                              for child in node.generators]
717        newnode.set_line_info(newnode.last_child())
718        return newnode
719
720    def visit_slice(self, node, parent):
721        """visit a Slice node by returning a fresh instance of it"""
722        newnode = new.Slice()
723        _lineno_parent(node, newnode, parent)
724        if node.lower is not None:
725            newnode.lower = self.visit(node.lower, newnode)
726        if node.upper is not None:
727            newnode.upper = self.visit(node.upper, newnode)
728        if node.step is not None:
729            newnode.step = self.visit(node.step, newnode)
730        newnode.set_line_info(newnode.last_child())
731        return newnode
732
733    def visit_subscript(self, node, parent):
734        """visit a Subscript node by returning a fresh instance of it"""
735        newnode = new.Subscript()
736        _lineno_parent(node, newnode, parent)
737        subcontext, self.asscontext = self.asscontext, None
738        newnode.value = self.visit(node.value, newnode)
739        newnode.slice = self.visit(node.slice, newnode)
740        self.asscontext = subcontext
741        newnode.set_line_info(newnode.last_child())
742        return newnode
743
744    def visit_tryexcept(self, node, parent):
745        """visit a TryExcept node by returning a fresh instance of it"""
746        newnode = new.TryExcept()
747        _lineno_parent(node, newnode, parent)
748        newnode.body = [self.visit(child, newnode) for child in node.body]
749        newnode.handlers = [self.visit(child, newnode) for child in node.handlers]
750        newnode.orelse = [self.visit(child, newnode) for child in node.orelse]
751        newnode.set_line_info(newnode.last_child())
752        return newnode
753
754    def visit_tryfinally(self, node, parent):
755        """visit a TryFinally node by returning a fresh instance of it"""
756        newnode = new.TryFinally()
757        _lineno_parent(node, newnode, parent)
758        newnode.body = [self.visit(child, newnode) for child in node.body]
759        newnode.finalbody = [self.visit(n, newnode) for n in node.finalbody]
760        newnode.set_line_info(newnode.last_child())
761        return newnode
762
763    def visit_tuple(self, node, parent):
764        """visit a Tuple node by returning a fresh instance of it"""
765        newnode = new.Tuple()
766        _lineno_parent(node, newnode, parent)
767        newnode.elts = [self.visit(child, newnode) for child in node.elts]
768        newnode.set_line_info(newnode.last_child())
769        return newnode
770
771    def visit_unaryop(self, node, parent):
772        """visit a UnaryOp node by returning a fresh instance of it"""
773        newnode = new.UnaryOp()
774        _lineno_parent(node, newnode, parent)
775        newnode.operand = self.visit(node.operand, newnode)
776        newnode.op = _UNARY_OP_CLASSES[node.op.__class__]
777        newnode.set_line_info(newnode.last_child())
778        return newnode
779
780    def visit_while(self, node, parent):
781        """visit a While node by returning a fresh instance of it"""
782        newnode = new.While()
783        _lineno_parent(node, newnode, parent)
784        newnode.test = self.visit(node.test, newnode)
785        newnode.body = [self.visit(child, newnode) for child in node.body]
786        newnode.orelse = [self.visit(child, newnode) for child in node.orelse]
787        newnode.set_line_info(newnode.last_child())
788        return newnode
789
790    def visit_with(self, node, parent):
791        """visit a With node by returning a fresh instance of it"""
792        newnode = new.With()
793        _lineno_parent(node, newnode, parent)
794        newnode.expr = self.visit(node.context_expr, newnode)
795        self.asscontext = "Ass"
796        if node.optional_vars is not None:
797            newnode.vars = self.visit(node.optional_vars, newnode)
798        self.asscontext = None
799        newnode.body = [self.visit(child, newnode) for child in node.body]
800        newnode.set_line_info(newnode.last_child())
801        return newnode
802
803    def visit_yield(self, node, parent):
804        """visit a Yield node by returning a fresh instance of it"""
805        newnode = new.Yield()
806        _lineno_parent(node, newnode, parent)
807        if node.value is not None:
808            newnode.value = self.visit(node.value, newnode)
809        newnode.set_line_info(newnode.last_child())
810        return newnode
811
812
813class TreeRebuilder3k(TreeRebuilder):
814    """extend and overwrite TreeRebuilder for python3k"""
815
816    def visit_arg(self, node, parent):
817        """visit a arg node by returning a fresh AssName instance"""
818        # the <arg> node is coming from py>=3.0, but we use AssName in py2.x
819        # XXX or we should instead introduce a Arg node in astng ?
820        return self.visit_assname(node, parent, node.arg)
821
822    def visit_excepthandler(self, node, parent):
823        """visit an ExceptHandler node by returning a fresh instance of it"""
824        newnode = new.ExceptHandler()
825        _lineno_parent(node, newnode, parent)
826        if node.type is not None:
827            newnode.type = self.visit(node.type, newnode)
828        if node.name is not None:
829            newnode.name = self.visit_assname(node, newnode, node.name)
830        newnode.body = [self.visit(child, newnode) for child in node.body]
831        newnode.set_line_info(newnode.last_child())
832        return newnode
833
834    def visit_nonlocal(self, node, parent):
835        """visit a Nonlocal node and return a new instance of it"""
836        newnode = new.Nonlocal(node.names)
837        _set_infos(node, newnode, parent)
838        return newnode
839
840    def visit_raise(self, node, parent):
841        """visit a Raise node by returning a fresh instance of it"""
842        newnode = new.Raise()
843        _lineno_parent(node, newnode, parent)
844        # no traceback; anyway it is not used in Pylint
845        if node.exc is not None:
846            newnode.exc = self.visit(node.exc, newnode)
847        if node.cause is not None:
848            newnode.cause = self.visit(node.cause, newnode)
849        newnode.set_line_info(newnode.last_child())
850        return newnode
851
852    def visit_starred(self, node, parent):
853        """visit a Starred node and return a new instance of it"""
854        newnode = new.Starred()
855        _lineno_parent(node, newnode, parent)
856        newnode.value = self.visit(node.value, newnode)
857        newnode.set_line_info(newnode.last_child())
858        return newnode
859
860
861if sys.version_info >= (3, 0):
862    TreeRebuilder = TreeRebuilder3k
863
864