|
3 | 3 | from xml.sax import SAXParseException, handler |
4 | 4 | from xml.sax.handler import LexicalHandler |
5 | 5 | from xml.sax.saxutils import XMLGenerator |
6 | | -from xml.sax.xmlreader import Locator |
| 6 | +from xml.sax.xmlreader import AttributesImpl, Locator |
7 | 7 |
|
8 | 8 | from defusedxml.sax import make_parser |
9 | 9 |
|
@@ -107,9 +107,9 @@ def __init__( |
107 | 107 | super().__init__(out, encoding, short_empty_elements, results) |
108 | 108 |
|
109 | 109 | def startElement(self, name, attrs): |
110 | | - new_attrs = attrs |
| 110 | + new_attrs: AttributesImpl = attrs |
111 | 111 | if self.event_match_result() and name in self.name_attributes_map: |
112 | | - new_attrs = self.name_attributes_map[name] |
| 112 | + new_attrs = AttributesImpl(attrs._attrs | self.name_attributes_map[name]) |
113 | 113 | self.add_change(self._my_locator.getLineNumber()) |
114 | 114 | super().startElement(name, new_attrs) |
115 | 115 |
|
@@ -150,19 +150,18 @@ def apply( |
150 | 150 | return None |
151 | 151 |
|
152 | 152 | diff = "" |
153 | | - with open(file_context.file_path, "r") as original: |
154 | | - # don't calculate diff if no changes were reported |
155 | | - # TODO there's a failure potential here for very large files |
156 | | - diff = ( |
157 | | - create_diff( |
| 153 | + # don't calculate diff if no changes were reported |
| 154 | + if changes: |
| 155 | + with open(file_context.file_path, "r") as original: |
| 156 | + # TODO there's a failure potential here for very large files |
| 157 | + diff = create_diff( |
158 | 158 | original.readlines(), |
159 | 159 | output_file.readlines(), |
160 | 160 | ) |
161 | | - if changes |
162 | | - else "" |
163 | | - ) |
164 | 161 |
|
165 | | - if not context.dry_run: |
| 162 | + # don't write anything if no changes were issued |
| 163 | + # avoids simply formatting the file |
| 164 | + if changes and not context.dry_run: |
166 | 165 | with open(file_context.file_path, "w+b") as original: |
167 | 166 | # mmap can't map empty files, write something first |
168 | 167 | original.write(b"a") |
|
0 commit comments