Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed encoding detecting error #60

Closed
wants to merge 1 commit into from
Closed

Conversation

crabant
Copy link

@crabant crabant commented Apr 1, 2017

this patch is for this error, please take a look, thanks.

Traceback (most recent call last):
  File "c:\python27\Lib\runpy.py", line 174, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "c:\python27\Lib\runpy.py", line 72, in _run_code
    exec code in run_globals
  File "D:\virtualenv\Scripts\diff-cover.exe\__main__.py", line 9, in <module>
  File "d:\virtualenv\lib\site-packages\diff_cover\tool.py", line 303, in main
    ignore_unstaged=arg_dict['ignore_unstaged'],
  File "d:\virtualenv\lib\site-packages\diff_cover\tool.py", line 232, in generate_coverage_report
    reporter.generate_report(output_file)
  File "d:\virtualenv\lib\site-packages\diff_cover\report_generator.py", line 216, in generate_report
    report = template.render(self._context())
  File "d:\virtualenv\lib\site-packages\diff_cover\report_generator.py", line 260, in _context
    (src, self._src_path_stats(src)) for src in self.src_paths()
  File "d:\virtualenv\lib\site-packages\diff_cover\report_generator.py", line 260, in <genexpr>
    (src, self._src_path_stats(src)) for src in self.src_paths()
  File "d:\virtualenv\lib\site-packages\diff_cover\report_generator.py", line 323, in _src_path_stats
    snippets = Snippet.load_snippets_html(src_path, violation_lines)
  File "d:\virtualenv\lib\site-packages\diff_cover\snippets.py", line 137, in load_snippets_html
    snippet_list = cls.load_snippets(src_path, violation_lines)
  File "d:\virtualenv\lib\site-packages\diff_cover\snippets.py", line 155, in load_snippets
    contents = src_file.read()
  File "d:\virtualenv\lib\codecs.py", line 314, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf8' codec can't decode bytes in position 9494-9495: invalid continuation byte

@Bachmann1234
Copy link
Owner

I can't merge this for a few reasons.

  1. It breaks existing tests
  2. Its using chardet. It's a fine library but is not appropriate here. The detection code is reading a declared encoding at the top of the file. Chardet reads some bytes and makes a best guess on the encoding used. You use chardet when you do not know the encoding and want to make an educated guess.

Can you tell me a bit about the file (Or perhaps provide it?). Is it python code? Or is it some other language. What encoding is the file using?

@crabant
Copy link
Author

crabant commented Apr 6, 2017

Thank you for the response.
Two reasons for diff-cover failure:

  1. file encoding is ISO-8859
  2. has character //<A1><A7>

the cpp file is:
f.cpp.zip

(virtualenv) :mac $ file f.cpp
f.cpp: c program text, ISO-8859 text
(virtualenv) :mac $ git diff 6e8f05f6a98a0257a7aabc1fe5c04c0aabd5eff3 f.cpp
diff --git a/mac/f.cpp b/mac/f.cpp
index 9c23295..61c8266 100644
--- a/mac/f.cpp
+++ b/mac/f.cpp
@@ -1,12 +1,14 @@
-#include <stdio.h>
-
-
-
-int test_in_f_cpp(int count)
-{
-    if( 1 <= count){
-        printf("a\n");
-    }
-    return 0;
-}
-
+#include <stdio.h>
+
+
+
+int test_in_f_cpp(int count)
+{
+    if( 1 <= count){
+        printf("a\n");
+    }else{
+        printf("b\n");//<A1><A7>
+    }
+    return 0;
+}
+

@Bachmann1234
Copy link
Owner

Ah I see. So CPP does not declare the encoding in the file itself. You can specify it via a command line switch.

So, I think the proper fix for this would be to specify an encoding as an additional option. But for now it would be reasonable to fall back to chardet if the original detecting fails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants