Sat, 31 Oct 2015 11:55:35 +0100
Some refinements to the miscellaneous checker.
--- a/Documentation/Help/source.qhp Fri Oct 30 18:18:48 2015 +0100 +++ b/Documentation/Help/source.qhp Sat Oct 31 11:55:35 2015 +0100 @@ -8148,6 +8148,7 @@ <keyword name="MiscellaneousChecker.__checkPrintStatements" id="MiscellaneousChecker.__checkPrintStatements" ref="eric6.Plugins.CheckerPlugins.CodeStyleChecker.MiscellaneousChecker.html#MiscellaneousChecker.__checkPrintStatements" /> <keyword name="MiscellaneousChecker.__checkTuple" id="MiscellaneousChecker.__checkTuple" ref="eric6.Plugins.CheckerPlugins.CodeStyleChecker.MiscellaneousChecker.html#MiscellaneousChecker.__checkTuple" /> <keyword name="MiscellaneousChecker.__error" id="MiscellaneousChecker.__error" ref="eric6.Plugins.CheckerPlugins.CodeStyleChecker.MiscellaneousChecker.html#MiscellaneousChecker.__error" /> + <keyword name="MiscellaneousChecker.__getCoding" id="MiscellaneousChecker.__getCoding" ref="eric6.Plugins.CheckerPlugins.CodeStyleChecker.MiscellaneousChecker.html#MiscellaneousChecker.__getCoding" /> <keyword name="MiscellaneousChecker.__getFields" id="MiscellaneousChecker.__getFields" ref="eric6.Plugins.CheckerPlugins.CodeStyleChecker.MiscellaneousChecker.html#MiscellaneousChecker.__getFields" /> <keyword name="MiscellaneousChecker.__ignoreCode" id="MiscellaneousChecker.__ignoreCode" ref="eric6.Plugins.CheckerPlugins.CodeStyleChecker.MiscellaneousChecker.html#MiscellaneousChecker.__ignoreCode" /> <keyword name="MiscellaneousChecker.__reportInvalidSyntax" id="MiscellaneousChecker.__reportInvalidSyntax" ref="eric6.Plugins.CheckerPlugins.CodeStyleChecker.MiscellaneousChecker.html#MiscellaneousChecker.__reportInvalidSyntax" />
--- a/Documentation/Source/eric6.Plugins.CheckerPlugins.CodeStyleChecker.MiscellaneousChecker.html Fri Oct 30 18:18:48 2015 +0100 +++ b/Documentation/Source/eric6.Plugins.CheckerPlugins.CodeStyleChecker.MiscellaneousChecker.html Sat Oct 31 11:55:35 2015 +0100 @@ -90,6 +90,9 @@ <td><a href="#MiscellaneousChecker.__error">__error</a></td> <td>Private method to record an issue.</td> </tr><tr> +<td><a href="#MiscellaneousChecker.__getCoding">__getCoding</a></td> +<td>Private method to get the defined coding of the source.</td> +</tr><tr> <td><a href="#MiscellaneousChecker.__getFields">__getFields</a></td> <td>Private method to extract the format field information.</td> </tr><tr> @@ -195,6 +198,21 @@ <dd> arguments for the message </dd> +</dl><a NAME="MiscellaneousChecker.__getCoding" ID="MiscellaneousChecker.__getCoding"></a> +<h4>MiscellaneousChecker.__getCoding</h4> +<b>__getCoding</b>(<i></i>) +<p> + Private method to get the defined coding of the source. +</p><dl> +<dt>Returns:</dt> +<dd> +tuple containing the line number and the coding +</dd> +</dl><dl> +<dt>Return Type:</dt> +<dd> +tuple of int and str +</dd> </dl><a NAME="MiscellaneousChecker.__getFields" ID="MiscellaneousChecker.__getFields"></a> <h4>MiscellaneousChecker.__getFields</h4> <b>__getFields</b>(<i>string</i>)
--- 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)
--- a/changelog Fri Oct 30 18:18:48 2015 +0100 +++ b/changelog Sat Oct 31 11:55:35 2015 +0100 @@ -16,6 +16,10 @@ multiple CPUs/CPU-Cores -- added a code complexity checker iaw. McCabe to the code style checker + -- added miscellaneous checks to the code style checker (coding + comment, copyright, blind except, print statements, one element + tuples, __future__ imports, old style string formats, string + format strings) - Debugger -- added signal handlers for the Python debug clients - Editor