Plugins/CheckerPlugins/CodeStyleChecker/MiscellaneousChecker.py

changeset 4515
d7cebe39ffba
parent 4511
b5e4e7efa904
child 4539
beac8323fa9e
--- a/Plugins/CheckerPlugins/CodeStyleChecker/MiscellaneousChecker.py	Fri Oct 30 18:18:48 2015 +0100
+++ b/Plugins/CheckerPlugins/CodeStyleChecker/MiscellaneousChecker.py	Sat Oct 31 11:55:35 2015 +0100
@@ -193,6 +193,20 @@
         for check in self.__checkers:
             check()
     
+    def __getCoding(self):
+        """
+        Private method to get the defined coding of the source.
+        
+        @return tuple containing the line number and the coding
+        @rtype tuple of int and str
+        """
+        for lineno, line in enumerate(self.__source[:2]):
+            matched = re.search('coding[:=]\s*([-\w_.]+)', line, re.IGNORECASE)
+            if matched:
+                return lineno, matched.group(1)
+        else:
+            return 0, ""
+    
     def __checkCoding(self):
         """
         Private method to check the presence of a coding line and valid
@@ -205,12 +219,10 @@
                      for e in self.__args.get(
                      "CodingChecker", self.__defaultArgs["CodingChecker"])
                      .split(",")]
-        for lineno, line in enumerate(self.__source[:2]):
-            matched = re.search('coding[:=]\s*([-\w.]+)', line, re.IGNORECASE)
-            if matched:
-                if encodings and matched.group(1).lower() not in encodings:
-                    self.__error(lineno, 0, "M102", matched.group(1))
-                break
+        lineno, coding = self.__getCoding()
+        if coding:
+            if coding.lower() not in encodings:
+                self.__error(lineno, 0, "M102", coding)
         else:
             self.__error(0, 0, "M101")
     
@@ -339,14 +351,18 @@
         """
         Private method to check string format strings.
         """
+        coding = self.__getCoding()[1]
+        if not coding:
+            # default to utf-8
+            coding = "utf-8"
+        
         visitor = TextVisitor()
         visitor.visit(self.__tree)
         for node in visitor.nodes:
             text = node.s
             if sys.version_info[0] > 2 and isinstance(text, bytes):
                 try:
-                    # TODO: Maybe decode using file encoding?
-                    text = text.decode('utf-8')
+                    text = text.decode(coding)
                 except UnicodeDecodeError:
                     continue
             fields, implicit, explicit = self.__getFields(text)

eric ide

mercurial