-
Notifications
You must be signed in to change notification settings - Fork 5
/
codegen.py
1347 lines (1165 loc) · 45.4 KB
/
codegen.py
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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# License
# Copyright (c) 2008, Armin Ronacher
# Significant changes made 2014-2024 by CensoredUsername
# All rights reserved.
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
# - Redistributions of source code must retain the above copyright notice, this list of
# conditions and the following disclaimer.
# - Redistributions in binary form must reproduce the above copyright notice, this list of
# conditions and the following disclaimer in the documentation and/or other materials
# provided with the distribution.
# - Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to
# endorse or promote products derived from this software without specific prior written
# permission.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# Taken from http://github.com/jonathaneunice/codegen
"""
codegen
~~~~~~~
Extension to ast that allow ast -> python code generation.
:copyright: Copyright 2008 by Armin Ronacher.
:license: BSD.
"""
import sys
PY3 = sys.version_info >= (3, 0)
# These might not exist, so we put them equal to NoneType
Try = TryExcept = TryFinally = YieldFrom = MatMult = Await = Num = Constant = type(None)
from ast import *
if PY3:
unicode = str
long = int
else:
bytes = str
str = unicode
# for 3.6-3.11 f-strings get lexed really weirdly and need additional effort.
BROKEN_FSTRINGS = (3, 12) > sys.version_info >= (3, 6)
class Sep(object):
# Performs the common pattern of returning a different symbol the first
# time the object is called
def __init__(self, last, first=''):
self.last = last
self.first = first
self.begin = True
def __call__(self):
if self.begin:
self.begin = False
return self.first
return self.last
def to_source(node, indent_with=' ' * 4, add_line_information=False, correct_line_numbers=False):
"""This function can convert a node tree back into python sourcecode.
This is useful for debugging purposes, especially if you're dealing with
custom asts not generated by python itself.
It could be that the sourcecode is evaluable when the AST itself is not
compilable / evaluable. The reason for this is that the AST contains some
more data than regular sourcecode does, which is dropped during
conversion.
Each level of indentation is replaced with `indent_with`. Per default this
parameter is equal to four spaces as suggested by PEP 8, but it might be
adjusted to match the application's styleguide.
If `add_line_information` is set to `True` comments for the line numbers
of the nodes are added to the output. This can be used to spot wrong line
number information of statement nodes.
"""
if correct_line_numbers:
if hasattr(node, 'lineno'):
return SourceGenerator(indent_with, add_line_information, True, node.lineno).process(node)
else:
return SourceGenerator(indent_with, add_line_information, True).process(node)
else:
return SourceGenerator(indent_with, add_line_information).process(node)
class QuoteAnalyzer(NodeVisitor):
"""python 3.6 introduced format strings. Which are really nice, except for that it disallowed
reusing the quote symbol used to open format strings. So now we need to figure out which
quotes need to be used where, often before the child contents are analyzed."""
QUOTES = ('"', "'", '"""', "'''")
def __init__(self):
# stack of sets of disallowed quotes
self.disallowed_quote_stack = []
def process(self, node):
self.quote_queue = []
self.visit(node)
self.quote_queue.reverse()
return self.quote_queue
# since 3.6
def visit_FormattedValue(self, node):
self.handle_fstring([node])
# since 3.6
def visit_JoinedStr(self, node):
self.handle_fstring(node.values)
def handle_fstring(self, contents):
# contents of either a JoinedStr node or a lone FormattedValue node
# we need to read these out in order, so we push a result obj first
result = []
self.quote_queue.append(result)
# push a set onto our stack
string_contents = []
self.disallowed_quote_stack.append(set())
# visit expression nodes, and collect string segments
self.fstring_visit_children(contents, string_contents)
string_contents = "".join(string_contents)
disallowed_quotes = self.disallowed_quote_stack.pop()
if self.disallowed_quote_stack:
# outermost f-string can use escapes in its string parts
# others cannot so we need to also exempt any quotes used in them
disallowed_quotes.add(i for i in self.QUOTES if i in string_contents)
can_escape = False
else:
can_escape = True
# disallow use of any quotes used in this string as delimiter in surrounding f-strings
for delimiter in self.QUOTES:
if delimiter in string_contents:
for entry in self.disallowed_quote_stack:
entry.add(delimiter)
try:
delimiter = next(i for i in self.QUOTES if i not in disallowed_quotes)
except StopIteration:
raise ValueError("Unrepresentable AST given (3.6-3.11 limit nesting of f-strings)")
result[:] = (delimiter, can_escape)
def fstring_visit_children(self, children, string_contents):
# this is separated out as it recurses during format_spec handling
for child in children:
if child.__class__.__name__ == "FormattedValue":
# visit the expression
self.visit(child.value)
# and visit format_spec. Format_spec is weird.
# format_spec is itself also a JoinedStr, so we get to do this dance again
if child.format_spec:
self.fstring_visit_children(child.format_spec.values, string_contents)
else:
# Constant node, possibly containing quotes
string_contents.append(child.value)
# leaf string nodes
# until 3.8
def visit_Str(self, node):
self.handle_leaf(node.s)
# until 3.8
def visit_Bytes(self, node):
self.handle_leaf(node.s)
# since 3.6
def visit_Constant(self, node):
self.handle_leaf(node.value)
def handle_leaf(self, content):
if not self.disallowed_quote_stack:
# not inside an f-string, we don't care
return
stringified = repr(content)
assert "\\" not in stringified, "Cannot use escapes inside an f-string expression?"
for delimiter in self.QUOTES:
if delimiter in stringified:
for entry in self.disallowed_quote_stack:
entry.add(delimiter)
class SourceGenerator(NodeVisitor):
"""This visitor is able to transform a well formed syntax tree into python
sourcecode. For more details have a look at the docstring of the
`node_to_source` function.
"""
COMMA = ', '
COLON = ': '
ASSIGN = ' = '
SEMICOLON = '; '
ARROW = ' -> '
WALRUS = ' := '
BOOLOP_SYMBOLS = {
And: (' and ', 5),
Or: (' or ', 4)
}
BINOP_SYMBOLS = {
Add: (' + ', 12),
Sub: (' - ', 12),
Mult: (' * ', 13),
MatMult: (' @ ', 13),
Div: (' / ', 13),
FloorDiv: (' // ', 13),
Mod: (' % ', 13),
Pow: (' ** ', 15),
LShift: (' << ', 11),
RShift: (' >> ', 11),
BitOr: (' | ', 8),
BitAnd: (' & ', 10),
BitXor: (' ^ ', 9)
}
CMPOP_SYMBOLS = {
Eq: (' == ', 7),
Gt: (' > ', 7),
GtE: (' >= ', 7),
In: (' in ', 7),
Is: (' is ', 7),
IsNot: (' is not ', 7),
Lt: (' < ', 7),
LtE: (' <= ', 7),
NotEq: (' != ', 7),
NotIn: (' not in ', 7)
}
UNARYOP_SYMBOLS = {
Invert: ('~', 14),
Not: ('not ', 6),
UAdd: ('+', 14),
USub: ('-', 14)
}
BLOCK_NODES = (If, For, While, With, Try, TryExcept, TryFinally,
FunctionDef, ClassDef)
def __init__(self, indent_with, add_line_information=False, correct_line_numbers=False, line_number=1):
self.result = []
self.indent_with = indent_with
self.add_line_information = add_line_information
self.indentation = 0
self.new_lines = 0
# precedence_stack: what precedence level are we on, could we safely newline before and is this operator left-to-right
self.precedence_stack = [[0, False, None]]
self.correct_line_numbers = correct_line_numbers
# The current line number we *think* we are on. As in it's most likely
# the line number of the last node we passed which can differ when
# the ast is broken
self.line_number = line_number
# Can we insert a newline here without having to escape it?
# (are we between delimiting characters)
self.can_newline = False
# after a colon, we don't have to print a semi colon. set to 1 when self.body() is called,
# set to 2 or 0 when it's actually used. set to 0 at the end of the body
self.after_colon = 0
# reset by a call to self.newline, set by the first call to write() afterwards
# determines if we have to print the newlines and indent
self.indented = False
# the amount of newlines to be printed
self.newlines = 0
# force the printing of a proper newline (and not a semicolon)
self.force_newline = False
# Python 3.6-3.11: f-strings aren't properly recursive, so we need to analyze them if they
# appear
self.quote_analysis = None
# keep track if we're inside an fstring
self.fstring_depth = 0
def process(self, node):
self.analyze_fstrings(node)
self.visit(node)
result = ''.join(self.result)
self.result = []
return result
def analyze_fstrings(self, node):
if BROKEN_FSTRINGS:
self.quote_analysis = QuoteAnalyzer().process(node)
# Precedence management
def prec_start(self, value, ltr=None):
newline = self.can_newline
if value < self.precedence_stack[-1][0]:
self.write('(')
self.can_newline = True
if ltr == False:
value += 1
self.precedence_stack.append([value, newline, ltr])
def prec_middle(self, level=None):
if level is not None:
self.precedence_stack[-1][0] = level
elif self.precedence_stack[-1][2]:
self.precedence_stack[-1][0] += 1
elif self.precedence_stack[-1][2] is False:
self.precedence_stack[-1][0] -= 1
def prec_end(self):
precedence, newline, ltr = self.precedence_stack.pop()
if ltr:
precedence -= 1
if precedence < self.precedence_stack[-1][0]:
self.write(')')
self.can_newline = newline
def paren_start(self, symbol='('):
self.precedence_stack.append([0, self.can_newline, None])
self.write(symbol)
self.can_newline = True
def paren_end(self, symbol=')'):
_, self.can_newline, _ = self.precedence_stack.pop()
self.write(symbol)
# convenience functions
def write(self, x):
# ignore empty writes
if not x:
return
# Before we write, we must check if newlines have been queued.
# If this is the case, we have to handle them properly
if self.correct_line_numbers:
if not self.indented:
self.new_lines = max(self.new_lines, 1 if self.force_newline else 0)
self.force_newline = False
if self.new_lines:
# we have new lines to print
if self.after_colon == 2:
self.result.append(';'+'\\\n' * self.new_lines)
else:
self.after_colon = 0
self.result.append('\n' * self.new_lines)
self.result.append(self.indent_with * self.indentation)
elif self.after_colon == 1:
# we're directly after a block-having statement and can write on the same line
self.after_colon = 2
self.result.append(' ')
elif self.result:
# we're after any statement. or at the start of the file
self.result.append(self.SEMICOLON)
self.indented = True
elif self.new_lines > 0:
if self.can_newline:
self.result.append('\n' * self.new_lines)
self.result.append(self.indent_with * (self.indentation + 1))
else:
self.result.append('\\\n' * self.new_lines)
self.result.append(self.indent_with * (self.indentation + 1))
self.new_lines = 0
elif self.new_lines:
# normal behaviour
self.result.append('\n' * self.new_lines)
self.result.append(self.indent_with * self.indentation)
self.new_lines = 0
self.result.append(x)
def newline(self, node=None, extra=0, force=False):
if not self.correct_line_numbers:
self.new_lines = max(self.new_lines, 1 + extra)
if not self.result:
self.new_lines = 0
if node is not None and self.add_line_information:
self.write('# line: %s' % node.lineno)
self.new_lines = 1
else:
if extra:
#Ignore extra
return
self.indented = False
if node is None:
# else/finally statement. insert one true newline. body is implicit
self.force_newline = True
self.new_lines += 1
self.line_number += 1
elif force:
# statement with a block: needs a true newline before it
self.force_newline = True
self.new_lines += node.lineno - self.line_number
self.line_number = node.lineno
else:
# block-less statement: needs a semicolon, colon, or newline in front of it
self.new_lines += node.lineno - self.line_number
self.line_number = node.lineno
def maybe_break(self, node):
if self.correct_line_numbers:
self.new_lines += node.lineno - self.line_number
self.line_number = node.lineno
def body(self, statements):
self.force_newline = any(isinstance(i, self.BLOCK_NODES) for i in statements)
self.indentation += 1
self.after_colon = 1
for stmt in statements:
self.visit(stmt)
self.indentation -= 1
self.force_newline = True
self.after_colon = 0 # do empty blocks even exist?
def body_or_else(self, node):
self.body(node.body)
if node.orelse:
self.newline()
self.write('else:')
self.body(node.orelse)
def visit_bare(self, node):
# this node is allowed to be a bare tuple
if isinstance(node, Tuple):
self.visit_Tuple(node, False)
else:
self.visit(node)
def visit_bareyield(self, node):
if isinstance(node, Yield):
self.visit_Yield(node, False)
elif isinstance(node, YieldFrom):
self.visit_YieldFrom(node, False)
else:
self.visit_bare(node)
def decorators(self, node):
for decorator in node.decorator_list:
self.newline(decorator, force=True)
self.write('@')
self.visit(decorator)
if node.decorator_list:
self.newline()
else:
self.newline(node, force=True)
# Module
def visit_Module(self, node):
self.generic_visit(node)
self.write('\n')
self.line_number += 1
# Statements
def visit_Assert(self, node):
self.newline(node)
self.write('assert ')
self.visit(node.test)
if node.msg:
self.write(self.COMMA)
self.visit(node.msg)
def visit_Assign(self, node):
self.newline(node)
for target in node.targets:
self.visit_bare(target)
self.write(self.ASSIGN)
self.visit_bareyield(node.value)
def visit_AugAssign(self, node):
self.newline(node)
self.visit_bare(node.target)
self.write(self.BINOP_SYMBOLS[type(node.op)][0].rstrip() + self.ASSIGN.lstrip())
self.visit_bareyield(node.value)
def visit_Await(self, node):
self.maybe_break(node)
self.prec_start(16, True)
self.prec_middle()
self.write('await ')
self.visit(node.value)
self.prec_end()
def visit_ImportFrom(self, node):
self.newline(node)
self.write('from ')
self.write('%s%s' % ('.' * node.level, node.module or ''))
self.write(' import ')
sep = Sep(self.COMMA)
for item in node.names:
self.write(sep())
self.visit(item)
def visit_Import(self, node):
self.newline(node)
self.write('import ')
sep = Sep(self.COMMA)
for item in node.names:
self.write(sep())
self.visit(item)
def visit_Exec(self, node):
self.newline(node)
self.write('exec ')
self.visit(node.body)
if node.globals:
self.write(' in ')
self.visit(node.globals)
if node.locals:
self.write(self.COMMA)
self.visit(node.locals)
def visit_Expr(self, node):
self.newline(node)
self.visit_bareyield(node.value)
def visit_AsyncFunctionDef(self, node):
self.visit_FunctionDef(node, True)
def visit_FunctionDef(self, node, async_=False):
self.newline(extra=1)
# first decorator line number will be used
self.decorators(node)
if async_:
self.write('async ')
self.write('def ')
self.write(node.name)
self.paren_start()
self.visit_arguments(node.args)
self.paren_end()
if hasattr(node, 'returns') and node.returns is not None:
self.write(self.ARROW)
self.visit(node.returns)
self.write(':')
self.body(node.body)
def visit_arguments(self, node):
sep = Sep(self.COMMA)
padding = [None] * (len(node.args) - len(node.defaults))
if hasattr(node, 'kwonlyargs'):
for arg, default in zip(node.args, padding + node.defaults):
self.write(sep())
self.visit(arg)
if default is not None:
self.write('=')
self.visit(default)
if node.vararg is not None:
self.write(sep())
if hasattr(node, 'varargannotation'):
if node.varargannotation is None:
self.write('*' + node.vararg)
else:
self.maybe_break(node.varargannotation)
self.write('*' + node.vararg)
self.write(self.COLON)
self.visit(node.varargannotation)
else:
self.maybe_break(node.vararg)
self.write('*')
self.visit(node.vararg)
elif node.kwonlyargs:
self.write(sep() + '*')
for arg, default in zip(node.kwonlyargs, node.kw_defaults):
self.write(sep())
self.visit(arg)
if default is not None:
self.write('=')
self.visit(default)
if node.kwarg is not None:
self.write(sep())
if hasattr(node, 'kwargannotation'):
if node.kwargannotation is None:
self.write('**' + node.kwarg)
else:
self.maybe_break(node.kwargannotation)
self.write('**' + node.kwarg)
self.write(self.COLON)
self.visit(node.kwargannotation)
else:
self.maybe_break(node.kwarg)
self.write('**')
self.visit(node.kwarg)
else:
for arg, default in zip(node.args, padding + node.defaults):
self.write(sep())
self.visit(arg)
if default is not None:
self.write('=')
self.visit(default)
if node.vararg is not None:
self.write(sep())
self.write('*' + node.vararg)
if node.kwarg is not None:
self.write(sep())
self.write('**' + node.kwarg)
def visit_arg(self, node):
# Py3 only
self.maybe_break(node)
self.write(node.arg)
if node.annotation is not None:
self.write(self.COLON)
self.visit(node.annotation)
def visit_keyword(self, node):
self.maybe_break(node.value)
if node.arg is not None:
self.write(node.arg + '=')
else:
self.write('**')
self.visit(node.value)
def visit_ClassDef(self, node):
self.newline(extra=2)
# first decorator line number will be used
self.decorators(node)
self.write('class %s' % node.name)
if (node.bases or (hasattr(node, 'keywords') and node.keywords) or
(hasattr(node, 'starargs') and (node.starargs or node.kwargs))):
self.paren_start()
sep = Sep(self.COMMA)
for base in node.bases:
self.write(sep())
self.visit(base)
# XXX: the if here is used to keep this module compatible
# with python 2.6.
if hasattr(node, 'keywords'):
for keyword in node.keywords:
self.write(sep())
self.visit(keyword)
if hasattr(node, 'starargs'):
if node.starargs is not None:
self.write(sep())
self.maybe_break(node.starargs)
self.write('*')
self.visit(node.starargs)
if node.kwargs is not None:
self.write(sep())
self.maybe_break(node.kwargs)
self.write('**')
self.visit(node.kwargs)
self.paren_end()
self.write(':')
self.body(node.body)
def visit_If(self, node):
self.newline(node, force=True)
self.write('if ')
self.visit(node.test)
self.write(':')
self.body(node.body)
while True:
if len(node.orelse) == 1 and isinstance(node.orelse[0], If):
node = node.orelse[0]
self.newline(node.test, force=True)
self.write('elif ')
self.visit(node.test)
self.write(':')
self.body(node.body)
else:
if node.orelse:
self.newline()
self.write('else:')
self.body(node.orelse)
break
def visit_AsyncFor(self, node):
self.visit_For(node, True)
def visit_For(self, node, async_=False):
self.newline(node, force=True)
if async_:
self.write('async ')
self.write('for ')
self.visit_bare(node.target)
self.write(' in ')
self.visit(node.iter)
self.write(':')
self.body_or_else(node)
def visit_While(self, node):
self.newline(node, force=True)
self.write('while ')
self.visit(node.test)
self.write(':')
self.body_or_else(node)
def visit_AsyncWith(self, node):
self.visit_With(node, True)
def visit_With(self, node, async_=False):
self.newline(node, force=True)
if async_:
self.write('async ')
self.write('with ')
if hasattr(node, 'items'):
sep = Sep(self.COMMA)
for item in node.items:
self.write(sep())
self.visit_withitem(item)
else:
# in python 2, similarly to the elif statement, multiple nested context managers
# are generally the multi-form of a single with statement
self.visit_withitem(node)
while len(node.body) == 1 and isinstance(node.body[0], With):
node = node.body[0]
self.write(self.COMMA)
self.visit_withitem(node)
self.write(':')
self.body(node.body)
def visit_withitem(self, node):
self.visit(node.context_expr)
if node.optional_vars is not None:
self.write(' as ')
self.visit(node.optional_vars)
def visit_Pass(self, node):
self.newline(node)
self.write('pass')
def visit_Print(self, node):
# XXX: python 2 only
self.newline(node)
self.write('print ')
sep = Sep(self.COMMA)
if node.dest is not None:
self.write(' >> ')
self.visit(node.dest)
sep()
for value in node.values:
self.write(sep())
self.visit(value)
if not node.nl:
self.write(',')
def visit_Delete(self, node):
self.newline(node)
self.write('del ')
sep = Sep(self.COMMA)
for target in node.targets:
self.write(sep())
self.visit(target)
def visit_Try(self, node):
# Python 3 only. exploits the fact that TryExcept uses the same attribute names
self.visit_TryExcept(node)
if node.finalbody:
self.newline()
self.write('finally:')
self.body(node.finalbody)
def visit_TryExcept(self, node):
self.newline(node, force=True)
self.write('try:')
self.body(node.body)
for handler in node.handlers:
self.visit(handler)
if node.orelse:
self.newline()
self.write('else:')
self.body(node.orelse)
def visit_TryFinally(self, node):
# Python 2 only
if len(node.body) == 1 and isinstance(node.body[0], TryExcept):
self.visit_TryExcept(node.body[0])
else:
self.newline(node, force=True)
self.write('try:')
self.body(node.body)
self.newline()
self.write('finally:')
self.body(node.finalbody)
def visit_ExceptHandler(self, node):
self.newline(node, force=True)
self.write('except')
if node.type:
self.write(' ')
self.visit(node.type)
if node.name:
self.write(' as ')
# Compatability
if isinstance(node.name, AST):
self.visit(node.name)
else:
self.write(node.name)
self.write(':')
self.body(node.body)
def visit_Global(self, node):
self.newline(node)
self.write('global ' + self.COMMA.join(node.names))
def visit_Nonlocal(self, node):
self.newline(node)
self.write('nonlocal ' + self.COMMA.join(node.names))
def visit_Return(self, node):
self.newline(node)
if node.value is not None:
self.write('return ')
self.visit(node.value)
else:
self.write('return')
def visit_Break(self, node):
self.newline(node)
self.write('break')
def visit_Continue(self, node):
self.newline(node)
self.write('continue')
def visit_Raise(self, node):
# XXX: Python 2.6 / 3.0 compatibility
self.newline(node)
if hasattr(node, 'exc') and node.exc is not None:
self.write('raise ')
self.visit(node.exc)
if node.cause is not None:
self.write(' from ')
self.visit(node.cause)
elif hasattr(node, 'type') and node.type is not None:
self.write('raise ')
self.visit(node.type)
if node.inst is not None:
self.write(self.COMMA)
self.visit(node.inst)
if node.tback is not None:
self.write(self.COMMA)
self.visit(node.tback)
else:
self.write('raise')
# Expressions
def visit_Attribute(self, node):
self.maybe_break(node)
# Edge case: due to the use of \d*[.]\d* for floats \d*[.]\w*, you have
# to put parenthesis around an integer literal do get an attribute from it
if isinstance(node.value, Num) or (isinstance(node.value, Constant) and isinstance(node.value.value, int)):
self.paren_start()
self.visit(node.value)
self.paren_end()
else:
self.prec_start(17)
self.visit(node.value)
self.prec_end()
self.write('.' + node.attr)
def visit_Call(self, node):
self.maybe_break(node)
#need to put parenthesis around numbers being called (this makes no sense)
if isinstance(node.func, Num):
self.paren_start()
self.visit_Num(node.func)
self.paren_end()
else:
self.prec_start(17)
self.visit(node.func)
self.prec_end()
# special case generator expressions as only argument
if (len(node.args) == 1 and isinstance(node.args[0], GeneratorExp) and
not node.keywords and hasattr(node, 'starargs') and
not node.starargs and not node.kwargs):
self.visit_GeneratorExp(node.args[0])
return
self.paren_start()
sep = Sep(self.COMMA)
for arg in node.args:
self.write(sep())
self.maybe_break(arg)
self.visit(arg)
for keyword in node.keywords:
self.write(sep())
self.visit(keyword)
if hasattr(node, 'starargs'):
if node.starargs is not None:
self.write(sep())
self.maybe_break(node.starargs)
self.write('*')
self.visit(node.starargs)
if node.kwargs is not None:
self.write(sep())
self.maybe_break(node.kwargs)
self.write('**')
self.visit(node.kwargs)
self.paren_end()
def visit_Name(self, node):
self.maybe_break(node)
self.write(node.id)
# new in 3.6.
def visit_Constant(self, node):
# catchall for ast.Num/ast.Str/ast.Bytes/ast.NameConstant/ast.Ellipsis
self.maybe_break(node)
# a bunch of these get special behaviour.
if isinstance(node.value, str):
self.handle_string(node.value, False, kind=node.kind)
elif isinstance(node.value, bytes):
self.handle_string(node.value, True)
elif isinstance(node.value, (int, complex, long)):
self.handle_num(node.value)
else:
# NameConstant, Ellipsis
self.write(repr(node.value))
# introduced 3.4. Deprecated 3.8
def visit_NameConstant(self, node):
self.maybe_break(node)
self.write(repr(node.value))
# deprecated in 3.8. contains both bytes/unicode in py2, only unicode in py3
def visit_Str(self, node):
self.maybe_break(node)
self.handle_string(node.s, False)
# introduced 3.0. Deprecated 3.8
def visit_Bytes(self, node):
self.maybe_break(node)
self.handle_string(node.s, True)
# deprecated 3.8
def visit_Num(self, node):
self.maybe_break(node)
self.handle_num(node.n)
# deprecated 3.8
def visit_Ellipsis(self, node):
# Ellipsis has no lineno information
self.write('...')
def handle_string(self, string, isbytes=False, kind=None):
if kind is not None:
self.write(kind)
if self.fstring_depth and BROKEN_FSTRINGS:
# avoid anything fancy inside f-strings
self.write(repr(node.s))
return
if isbytes:
newline_count = node.s.count('\n'.encode('utf-8'))
else:
newline_count = node.s.count('\n')
# heuristic, expand when more than 1 newline and when at least 80%
# of the characters aren't newlines
expand = newline_count > 1 and len(node.s) > 5 * newline_count
if self.correct_line_numbers:
# Also check if we have enougn newlines to expand in if we're going for correct line numbers
if self.after_colon:
# Although this makes little sense just after a colon
expand = expand and self.new_lines > newline_count
else:
expand = expand and self.new_lines >= newline_count
if expand and (not self.correct_line_numbers or self.new_lines >= newline_count):
if self.correct_line_numbers:
self.new_lines -= newline_count
a = repr(node.s)
delimiter = a[-1]
header, content = a[:-1].split(delimiter, 1)
lines = []
chain = False
for i in content.split('\\n'):
if chain:
i = lines.pop() + i