Skip to content

Commit

Permalink
closes #14, crash on non-utf8 py files
Browse files Browse the repository at this point in the history
  • Loading branch information
tarpas committed Jan 4, 2018
1 parent 31fe409 commit ba69b81
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
4 changes: 4 additions & 0 deletions test/print1250r.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# anything
# -- coding:cp1250 --

print("š")
5 changes: 5 additions & 0 deletions test/test_testmon.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ def func():
assert {os.path.relpath(a.strpath, testdir.tmpdir.strpath):
checksum_coverage(Module(file_name=a.strpath).blocks, [2])} == deps

def test_detect_encoding():
from testmon import testmon_core
with open('test/print1250r.py', 'rb') as f:
testmon_core.detect_encoding(f.readline() + f.readline()) == 'cp1250'


@pytest.mark.xfail
def test_testmon_recursive(testdir, monkeypatch):
Expand Down
28 changes: 18 additions & 10 deletions testmon/testmon_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@
import sqlite3
import sys
import textwrap
import zlib

import coverage

from testmon.process_code import checksum_coverage
from testmon.process_code import Module
import codecs
import re

coding_re = re.compile(b'coding[=:]\s*([-\w.]+)')

if sys.version_info > (3,):
buffer = memoryview
Expand Down Expand Up @@ -150,15 +153,20 @@ def get_variant_inifile(inifile):

def read_file_with_checksum(absfilename):
hasher = hashlib.sha1()
try:
with open(absfilename) as afile:
source = afile.read()
except UnicodeDecodeError:
raise Exception("""You are hitting https://github.com/tarpas/pytest-testmon/issues/14"""
""" when reading {}""".format(absfilename))

hasher.update(encode(source))
return source, hasher.hexdigest()
with open(absfilename, 'rb') as afile:
source = b''.join([afile.readline(), afile.readline()])
encoding = detect_encoding(source)
source = source + afile.read()
hasher.update(source)
return source.decode(encoding), hasher.hexdigest()


def detect_encoding(beginning):
result = coding_re.search(beginning)
if result:
return result.group(1).decode('ascii')
else:
return 'utf-8'


def parse_file(filename, rootdir, source_code):
Expand Down

0 comments on commit ba69b81

Please sign in to comment.