Merge pull request #21512 from edx/BOM-96-2

BOM-96
This commit is contained in:
Ayub
2019-09-02 14:26:20 +05:00
committed by GitHub
8 changed files with 18 additions and 11 deletions

View File

@@ -146,7 +146,9 @@ class UniversalNewlineIterator(object):
"""
Replace CR and CRLF with LF within `string`.
"""
return string.replace('\r\n', '\n').replace('\r', '\n')
if six.PY2:
return string.replace('\r\n', '\n').replace('\r', '\n')
return string.replace('\r\n', '\n').replace('\r', '\n').encode('utf-8')
def generate_lines(self):
"""

View File

@@ -728,7 +728,10 @@ class LoncapaProblem(object):
Main method called externally to get the HTML to be rendered for this capa Problem.
"""
self.do_targeted_feedback(self.tree)
html = contextualize_text(etree.tostring(self._extract_html(self.tree)), self.context)
html = contextualize_text(
etree.tostring(self._extract_html(self.tree)).decode('utf-8'),
self.context
)
return html
def handle_input_ajax(self, data):

View File

@@ -26,7 +26,7 @@ def make_hashkey(seed):
Generate a string key by hashing
"""
h = hashlib.md5()
h.update(str(seed))
h.update(six.b(str(seed)))
return h.hexdigest()

View File

@@ -472,7 +472,7 @@ class StackTraceCounter(object):
"""
# pylint: disable=broad-except
stack = traceback.extract_stack()[:-2]
stack = [tuple(item) for item in traceback.extract_stack()[:-2]]
if self._top_of_stack in stack:
stack = stack[stack.index(self._top_of_stack):]
@@ -484,7 +484,6 @@ class StackTraceCounter(object):
safe_args.append(repr(arg))
except Exception as exc:
safe_args.append('<un-repr-able value: {}'.format(exc))
safe_kwargs = {}
for key, kwarg in kwargs.items():
try:

View File

@@ -519,7 +519,6 @@ def generate_random_string(length):
char for char in string.ascii_uppercase + string.digits + string.ascii_lowercase
if char not in 'aAeEiIoOuU1l'
]
return ''.join((random.choice(chars) for i in range(length)))

View File

@@ -235,7 +235,10 @@ class ReportStore(object):
compatibility.
"""
for row in rows:
yield [six.text_type(item).encode('utf-8') for item in row]
if six.PY2:
yield [six.text_type(item).encode('utf-8') for item in row]
else:
yield [six.text_type(item) for item in row]
class DjangoStorageReportStore(ReportStore):
@@ -283,7 +286,8 @@ class DjangoStorageReportStore(ReportStore):
"""
output_buffer = ContentFile('')
# Adding unicode signature (BOM) for MS Excel 2013 compatibility
output_buffer.write(codecs.BOM_UTF8)
if six.PY2:
output_buffer.write(codecs.BOM_UTF8)
csvwriter = csv.writer(output_buffer)
csvwriter.writerows(self._get_utf8_encoded_rows(rows))
output_buffer.seek(0)

View File

@@ -383,9 +383,9 @@ class TestReportMixin(object):
to four decimal places.
"""
extracted = {}
for key, value in dictionary.items():
for key in list(dictionary):
try:
float(value)
float(dictionary[key])
extracted[key] = round(float(dictionary.pop(key)), 4)
except ValueError:
pass

View File

@@ -1463,7 +1463,7 @@ class TestCourseSurveyReport(TestReportMixin, InstructorTaskCourseTestCase):
with report_store.storage.open(report_path) as csv_file:
csv_file_data = csv_file.read()
# Removing unicode signature (BOM) from the beginning
csv_file_data = csv_file_data.decode("utf-8-sig").encode("utf-8")
csv_file_data = csv_file_data.decode("utf-8-sig")
for data in expected_data:
self.assertIn(data, csv_file_data)