This file is indexed.

/usr/share/pyshared/sourcecodegen/visitor.py is in python-sourcecodegen 0.6.14-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
import sys

from compiler import ast

from cStringIO import StringIO

version = sys.version_info[:3]

def triple_quote(doc):
    return '"""%s"""' % doc.replace('"""', '\"\"\"')

def format_argnames(argnames):
    return ", ".join(
        isinstance(name, tuple) and "(%s)" % format_argnames(name) or name \
        for name in argnames)

def format_ass(node):
    if isinstance(node, ast.AssTuple):
        return "(%s)" % ", ".join(format_ass(ass) for ass in node)
    return node.name

class prioritized(object):
    def __init__(self, generator, priority):
        self.generator = generator
        self.priority = priority

    def __iter__(self):
        return self.generator

def prioritize(priority):
    def decorator(func):
        def visit(self, node):
            return prioritized(func(self, node), priority)
        return visit
    return decorator

def unary(symbol, priority):
    @prioritize(prioritize)
    def visit(self, node):
        yield symbol
        child = self.visit(node.expr)
        if child.priority < priority:
            yield '('
            yield child
            yield ')'
        else:
            yield child
    return visit

def binary(symbol, priority):
    @prioritize(prioritize)
    def visit(self, node):
        left = self.visit(node.left)
        right = self.visit(node.right)

        if left.priority < priority and right.priority < priority:
            yield '('
            yield left
            yield ' %s ' % symbol
            yield right
            yield ')'
        else:
            yield left
            yield ' %s ' % symbol
            yield right
            
    return visit

def n_ary(symbol, priority):
    @prioritize(prioritize)
    def visit(self, node):
        yield '('
        for condition in tuple(node)[:-1]:
            yield self.visit(condition)
            yield ' %s ' % symbol
        yield self.visit(tuple(node)[-1])
        yield ')'
    return visit

class CodeStream(object):
    def __init__(self, indentation_string="\t"):
        self.indentation_string = indentation_string
        self.indentation = 0
        self.stream = StringIO()
        self.clear = True

    def __call__(self, value):
        if value is None:
            return self.write(None)

        if isinstance(value, basestring):
            return self.out(value)

        if isinstance(value, tuple) and self.clear is False:
            self.write()
            self.clear = True
            self.indentation += 1
            self(value)
            self.indentation -= 1
        else:
            for part in value:
                self(part)

    def write(self, text=None):
        if text or self.clear == False:
            self.out(text)
            self.stream.write('\n')
        self.clear = True
    
    def out(self, text):
        if self.clear is True:
            indentation = self.indentation_string * self.indentation
            self.stream.write(indentation)
            self.clear = False
        self.stream.write(text or "")

    def getvalue(self):
        return self.stream.getvalue()
    
class ASTVisitor(object):
    def __init__(self, tree):
        self.tree = tree

    def __call__(self):
        stream = CodeStream()
        stream(self.visit(self.tree))            
        return stream.getvalue()
        
    def visit(self, node):
        name = node.__class__.__name__

        try:
            func = getattr(self, 'visit%s' % name)
        except AttributeError:
            raise NotImplementedError(
                "Unable to visit `%s`." % repr(node))

        gen = func(node)

        if isinstance(gen, prioritized):
            return gen

        return prioritized(gen, 0)

    def visitModule(self, node):
        if node.doc is not None:
            yield triple_quote(node.doc)
            yield None

        for node in self.visit(node.node):
            yield node

    def visitStmt(self, node):
        yield tuple(self.visit(child) for child in node.nodes if child is not None)
        
    def visitIf(self, node):
        for index, test in enumerate(node.tests):
            if index == 0:
                yield "if "
            else:
                yield "elif "

            condition, statement = test

            yield self.visit(condition)
            yield ":"
            yield self.visit(statement),
            
        if node.else_:
            yield "else:"
            yield self.visit(node.else_),

    @prioritize(-3)
    def visitName(self, node):
        yield node.name

    def visitPass(self, node):
        yield "pass"
        yield None

    def visitDiscard(self, node):
        yield self.visit(node.expr)
        yield None

    def visitAssign(self, node):
        for index, ass in enumerate(tuple(node.nodes)):
            yield self.visit(ass)
                
            if index < len(tuple(node.nodes)) - 1:
                yield " = "
        yield " = "
        yield self.visit(node.expr)
        yield None

    def visitAssName(self, node):
        if node.flags == 'OP_DELETE':
            yield "del "
        yield node.name
        if node.flags == 'OP_DELETE':
            yield None

    def visitFunction(self, node):
        if node.decorators:
            yield self.visit(node.decorators)

        yield "def %s(" % node.name

        argnames = list(node.argnames)
        if argnames:
            if node.kwargs:
                kwargs = argnames.pop()
            if node.varargs:
                varargs = argnames.pop()

            if node.defaults:
                yield format_argnames(argnames[:-len(node.defaults)])
                for index, default in enumerate(node.defaults):
                    name = argnames[index-len(node.defaults)]
                    if len(argnames) > len(node.defaults) or index > 0:
                        yield ", %s=" % name
                    else:
                        yield "%s=" % name
                    yield self.visit(default)
            else:
                yield format_argnames(argnames)

        if node.varargs:
            if len(node.argnames) > 1:
                yield ", "
            yield "*%s" % varargs

        if node.kwargs:
            if len(node.argnames) > 1 or node.varargs:
                yield ", "
            yield "**%s" % kwargs

        yield "):"

        if node.doc:
            yield triple_quote(node.doc),

        yield self.visit(node.code),

    @prioritize(0)
    def visitConst(self, node):
        yield repr(node.value)

    def visitDecorators(self, node):
        for decorator in tuple(node):
            yield '@'
            yield self.visit(decorator)
            yield None

    def visitCallFunc(self, node):
        yield self.visit(node.node)
        yield '('
        for arg in tuple(node.args)[:-1]:
            yield self.visit(arg)
            yield ", "
        if node.args:
            yield self.visit(node.args[-1])
        if node.star_args:
            if node.args:
                yield ", *"
            else:
                yield "*"
            yield self.visit(node.star_args)
        if node.dstar_args:
            if node.args:
                yield ", **"
            else:
                yield "**"
            yield self.visit(node.dstar_args)
        yield ")"

    def visitKeyword(self, node):
        yield "%s=" % node.name
        yield self.visit(node.expr)

    def visitAssTuple(self, node):
        first = node
        while isinstance(first, ast.AssTuple):
            first = first.nodes[0]
        if first.flags == 'OP_DELETE':
            yield "del "
        yield format_ass(node)
        if first.flags == 'OP_DELETE':
            yield None

    @prioritize(0)
    def visitTuple(self, node):
        yield "("
        for index, item in enumerate(tuple(node)):
            yield self.visit(item)
            if index < len(tuple(node)) - 1:
                yield ", "
        if len(node.nodes) == 1:
            yield ", "
        yield ")"

    def visitGenExpr(self, node):
        yield "("
        yield self.visit(node.code)
        yield ")"

    def visitListComp(self, node):
        yield "["
        yield self.visitGenExprInner(node)
        yield "]"

    def visitGenExprInner(self, node):
        yield self.visit(node.expr)
        for qual in node.quals:
            yield self.visit(qual)

    def visitGenExprFor(self, node):
        yield " for "
        yield self.visit(node.assign)
        yield " in "
        yield self.visit(node.iter)
        for _if in node.ifs:
            yield self.visit(_if)

    def visitGenExprIf(self, node):
        yield " if "
        yield self.visit(node.test)

    def visitListCompFor(self, node):
        yield " for "
        yield self.visit(node.assign)
        yield " in "
        yield self.visit(node.list)
        for _if in node.ifs:
            yield self.visit(_if)
            
    def visitListCompIf(self, node):
        yield " if "
        yield self.visit(node.test)

    def visitCompare(self, node):
        yield self.visit(node.expr)
        for op, expr in node.ops:
            yield ' %s ' % op
            yield self.visit(expr)

    def visitImport(self, node):
        yield "import "
        for index, (name, alias) in enumerate(node.names):
            yield name
            if alias is not None:
                yield " as %s" % alias
            if index < len(node.names) - 1:
                yield ", "
        yield None

    def visitFrom(self, node):
        yield "from %s import " % node.modname
        for index, (name, alias) in enumerate(node.names):
            yield name
            if alias is not None:
                yield " as %s" % alias
            if index < len(node.names) - 1:
                yield ", "
        yield None

    def visitReturn(self, node):
        yield "return "
        yield self.visit(node.value)
        yield None

    def visitBreak(self, node):
        yield "break"
        yield None

    def visitContinue(self, node):
        yield "continue"
        yield None

    def visitWhile(self, node):
        yield "while "
        yield self.visit(node.test)
        yield ":"
 
        yield self.visit(node.body),

        if node.else_ is not None:
            yield "else:"
            yield self.visit(node.else_),

    def visitTryExcept(self, node):
        yield "try:"
        yield self.visit(node.body),
        for cls, var, body in node.handlers:
            yield "except"
            if cls is not None:
                yield " "
                yield self.visit(cls)
            if var is not None:
                if cls is None:
                    yield " "
                else:
                    yield ", "
                yield self.visit(var)
            yield ":"
            yield self.visit(body),

        if node.else_:
            yield "else:"
            yield self.visit(node.else_),
    
    def visitTryFinally(self, node):
        if version < (2,5):
            yield "try:"
            yield self.visit(node.body),
            yield "finally:"
            yield self.visit(node.final),
        else:
            yield self.visit(node.body)
            yield "finally:"
            yield self.visit(node.final),

    def visitClass(self, node):
        yield "class %s" % node.name

        if node.bases:
            yield "("
            for index, base in enumerate(node.bases):
                yield self.visit(base)
                if index < len(node.bases) - 1:
                    yield ", "
            yield ")"
        yield ":"
        
        if node.doc:
            yield triple_quote(node.doc), self.visit(node.code)
        else:
            yield self.visit(node.code)

    @prioritize(-2)
    def visitLambda(self, node):
        yield "lambda"
        argnames = list(node.argnames)
        if argnames:
            yield " "
            if node.kwargs:
                kwargs = argnames.pop()
            if node.varargs:
                varargs = argnames.pop()

            if node.defaults:
                yield format_argnames(argnames[:-len(node.defaults)])
                for index, default in enumerate(node.defaults):
                    name = argnames[index-len(node.defaults)]
                    yield ", %s=" % name
                    yield self.visit(default)
            else:
                yield format_argnames(argnames)

        offset = (node.varargs or 0) + (node.kwargs or 0)

        if node.varargs:
            if len(node.argnames) > offset:
                yield ", "
            yield "*%s" % varargs

        if node.kwargs:
            if node.varargs or len(node.argnames) > offset:
                yield ", "
            yield "**%s" % kwargs

        yield ": "
        yield self.visit(node.code)

    def visitGetattr(self, node):
        yield self.visit(node.expr)
        yield ".%s" % node.attrname

    def visitAssAttr(self, node):
        if node.flags == 'OP_DELETE':
            yield "del "
        yield self.visit(node.expr)
        yield ".%s" % node.attrname
        if node.flags == 'OP_DELETE':
            yield None

    def visitSubscript(self, node):
        if node.flags == 'OP_DELETE':
            yield "del "
        yield self.visit(node.expr)
        yield '['
        for index, sub in enumerate(node.subs):
            if isinstance(sub, ast.Sliceobj):
                for i, slice in enumerate(tuple(sub)):
                    yield self.visit(slice)
                    if i < len(tuple(sub)) - 1:
                        yield ":"
            else:
                yield self.visit(sub)
            if index < len(node.subs) - 1:
                yield ', '
        yield ']'
        if node.flags == 'OP_DELETE':
            yield None

    def visitSlice(self, node):
        if node.flags == 'OP_DELETE':
            yield "del "
        yield self.visit(node.expr)
        yield '['
        if node.lower:
            yield self.visit(node.lower)
        yield ':'
        if node.upper:
            yield self.visit(node.upper)
        yield ']'
        if node.flags == 'OP_DELETE':
            yield None

    def visitSliceobj(self, node):
        yield 'slice('
        for index, item in enumerate(tuple(node)):
            yield self.visit(item)
            if index < len(tuple(node)) - 1:
                yield ", "
        yield ')'

    def visitExec(self, node):
        yield "exec "
        yield self.visit(node.expr)
        if node.locals:
            yield " in "
            yield self.visit(node.locals)
        if node.globals:
            yield ", "
            yield self.visit(node.globals)
        yield None

    def visitAssert(self, node):
        yield "assert "
        yield self.visit(node.test)
        if node.fail is not None:
            yield ", "
            yield self.visit(node.fail)
        yield None

    def visitRaise(self, node):
        yield "raise "
        yield self.visit(node.expr1)
        if node.expr2:
            yield ", "
            yield self.visit(node.expr2)
        if node.expr3:
            yield ", "
            yield self.visit(node.expr3)

    def visitPrintnl(self, node):
        yield "print "
        if node.dest is not None:
            yield ">> "
            yield self.visit(node.dest)
            yield ", "
        for index, expr in enumerate(tuple(node.nodes)):
            if expr is None:
                continue
            yield self.visit(expr)
            if index < len(tuple(node.nodes)) - 1 and node.nodes[index+1] is not None:
                yield ", "
        yield None

    def visitIfExp(self, node):
        yield self.visit(node.then)
        yield " if "
        yield self.visit(node.test)
        yield " else "
        yield self.visit(node.else_)

    def visitWith(self, node):
        raise NotImplementedError(
            "The `with` keyword is not supported.")

    def visitAugAssign(self, node):
        yield self.visit(node.node)
        yield " %s " % node.op
        yield self.visit(node.expr)
        yield None

    def visitList(self, node):
        yield '['
        for index, item in enumerate(node.nodes):
            yield self.visit(item)
            if index < len(node.nodes) - 1:
                yield ", "
        yield ']'

    def visitDict(self, node):
        yield '{'
        for index, (expr, value) in enumerate(node.items):
            yield self.visit(expr)
            yield ': '
            yield self.visit(value)
            if index < len(node.items) - 1:
                yield ", "
        yield '}'

    def visitFor(self, node):
        yield "for %s in " % format_ass(node.assign)
        yield self.visit(node.list)
        yield ":"
        yield self.visit(node.body)
        if node.else_ is not None:
            yield "else:"
            yield self.visit(node.else_)

    def visitYield(self, node):
        yield "yield "
        yield self.visit(node.value)
        yield None

    visitPower = binary('**', 10)
    visitInvert = unary('~', 9)
    visitUnaryAdd = unary('+', 8)
    visitUnarySub = unary('-', 8)
    visitMul = binary('*', 7)
    visitMod = binary('%', 7)    
    visitDiv = binary('/', 7)

    visitAdd = binary('+', 6)
    visitSub = binary('-', 6)

    visitLeftShift = binary('<<', 5)
    visitRightShift = binary('>>', 5)

    visitBitand = n_ary('&', 4)
    visitBitxor = n_ary('^', 3)
    visitBitor = n_ary('|', 2)
    
    visitNot = unary('not ', 1)
    visitAnd = n_ary('and', 0)
    visitOr = n_ary('or', -1)