Plugins/CheckerPlugins/CodeStyleChecker/MiscellaneousChecker.py

changeset 4515
d7cebe39ffba
parent 4511
b5e4e7efa904
child 4539
beac8323fa9e
equal deleted inserted replaced
4514:e410e76d6538 4515:d7cebe39ffba
191 return 191 return
192 192
193 for check in self.__checkers: 193 for check in self.__checkers:
194 check() 194 check()
195 195
196 def __getCoding(self):
197 """
198 Private method to get the defined coding of the source.
199
200 @return tuple containing the line number and the coding
201 @rtype tuple of int and str
202 """
203 for lineno, line in enumerate(self.__source[:2]):
204 matched = re.search('coding[:=]\s*([-\w_.]+)', line, re.IGNORECASE)
205 if matched:
206 return lineno, matched.group(1)
207 else:
208 return 0, ""
209
196 def __checkCoding(self): 210 def __checkCoding(self):
197 """ 211 """
198 Private method to check the presence of a coding line and valid 212 Private method to check the presence of a coding line and valid
199 encodings. 213 encodings.
200 """ 214 """
203 217
204 encodings = [e.lower().strip() 218 encodings = [e.lower().strip()
205 for e in self.__args.get( 219 for e in self.__args.get(
206 "CodingChecker", self.__defaultArgs["CodingChecker"]) 220 "CodingChecker", self.__defaultArgs["CodingChecker"])
207 .split(",")] 221 .split(",")]
208 for lineno, line in enumerate(self.__source[:2]): 222 lineno, coding = self.__getCoding()
209 matched = re.search('coding[:=]\s*([-\w.]+)', line, re.IGNORECASE) 223 if coding:
210 if matched: 224 if coding.lower() not in encodings:
211 if encodings and matched.group(1).lower() not in encodings: 225 self.__error(lineno, 0, "M102", coding)
212 self.__error(lineno, 0, "M102", matched.group(1))
213 break
214 else: 226 else:
215 self.__error(0, 0, "M101") 227 self.__error(0, 0, "M101")
216 228
217 def __checkCopyright(self): 229 def __checkCopyright(self):
218 """ 230 """
337 349
338 def __checkFormatString(self): 350 def __checkFormatString(self):
339 """ 351 """
340 Private method to check string format strings. 352 Private method to check string format strings.
341 """ 353 """
354 coding = self.__getCoding()[1]
355 if not coding:
356 # default to utf-8
357 coding = "utf-8"
358
342 visitor = TextVisitor() 359 visitor = TextVisitor()
343 visitor.visit(self.__tree) 360 visitor.visit(self.__tree)
344 for node in visitor.nodes: 361 for node in visitor.nodes:
345 text = node.s 362 text = node.s
346 if sys.version_info[0] > 2 and isinstance(text, bytes): 363 if sys.version_info[0] > 2 and isinstance(text, bytes):
347 try: 364 try:
348 # TODO: Maybe decode using file encoding? 365 text = text.decode(coding)
349 text = text.decode('utf-8')
350 except UnicodeDecodeError: 366 except UnicodeDecodeError:
351 continue 367 continue
352 fields, implicit, explicit = self.__getFields(text) 368 fields, implicit, explicit = self.__getFields(text)
353 if implicit: 369 if implicit:
354 if node in visitor.calls: 370 if node in visitor.calls:

eric ide

mercurial