@@ -90,6 +90,8 @@ def finish(self) -> None:
90
90
class AbstractReporter (metaclass = ABCMeta ):
91
91
def __init__ (self , reports : Reports , output_dir : str ) -> None :
92
92
self .output_dir = output_dir
93
+ if output_dir != '<memory>' :
94
+ stats .ensure_dir_exists (output_dir )
93
95
94
96
@abstractmethod
95
97
def on_file (self , tree : MypyFile , type_map : Dict [Expression , Type ], options : Options ) -> None :
@@ -124,8 +126,6 @@ def __init__(self, reports: Reports, output_dir: str) -> None:
124
126
super ().__init__ (reports , output_dir )
125
127
self .counts = {} # type: Dict[str, Tuple[int, int, int, int]]
126
128
127
- stats .ensure_dir_exists (output_dir )
128
-
129
129
def on_file (self ,
130
130
tree : MypyFile ,
131
131
type_map : Dict [Expression , Type ],
@@ -170,7 +170,6 @@ def __init__(self, reports: Reports, output_dir: str) -> None:
170
170
super ().__init__ (reports , output_dir )
171
171
self .counts = {} # type: Dict[str, Tuple[int, int]]
172
172
self .any_types_counter = {} # type: Dict[str, typing.Counter[int]]
173
- stats .ensure_dir_exists (output_dir )
174
173
175
174
def on_file (self ,
176
175
tree : MypyFile ,
@@ -361,8 +360,6 @@ def __init__(self, reports: Reports, output_dir: str) -> None:
361
360
super ().__init__ (reports , output_dir )
362
361
self .lines_covered = {} # type: Dict[str, List[int]]
363
362
364
- stats .ensure_dir_exists (output_dir )
365
-
366
363
def on_file (self ,
367
364
tree : MypyFile ,
368
365
type_map : Dict [Expression , Type ],
@@ -418,6 +415,10 @@ def __init__(self, reports: Reports, output_dir: str) -> None:
418
415
self .last_xml = None # type: Optional[Any]
419
416
self .files = [] # type: List[FileInfo]
420
417
418
+ # XML doesn't like control characters, but they are sometimes
419
+ # legal in source code (e.g. comments, string literals).
420
+ control_fixer = str .maketrans ('' .join (chr (i ) for i in range (32 )), '?' * 32 )
421
+
421
422
def on_file (self ,
422
423
tree : MypyFile ,
423
424
type_map : Dict [Expression , Type ],
@@ -446,7 +447,7 @@ def on_file(self,
446
447
etree .SubElement (root , 'line' ,
447
448
number = str (lineno ),
448
449
precision = stats .precision_names [status ],
449
- content = line_text .rstrip ('\n ' ),
450
+ content = line_text .rstrip ('\n ' ). translate ( self . control_fixer ) ,
450
451
any_info = self ._get_any_info_for_line (visitor , lineno ))
451
452
# Assumes a layout similar to what XmlReporter uses.
452
453
xslt_path = os .path .relpath ('mypy-html.xslt' , path )
@@ -737,7 +738,6 @@ def on_finish(self) -> None:
737
738
last_xml = self .memory_xml .last_xml
738
739
assert last_xml is not None
739
740
out_path = os .path .join (self .output_dir , 'index.txt' )
740
- stats .ensure_dir_exists (os .path .dirname (out_path ))
741
741
transformed_txt = bytes (self .xslt_txt (last_xml ))
742
742
with open (out_path , 'wb' ) as out_file :
743
743
out_file .write (transformed_txt )
0 commit comments