[FIX] P3: file modes

In P3, files *must* be opened in binary mode when reading or writing
binary data, text mode (default) generates text and thus decodes on
the fly.
This commit is contained in:
Xavier Morel
2017-08-20 23:25:54 +02:00
committed by Olivier Dony
parent b93613066f
commit 3b67fda251
3 changed files with 7 additions and 9 deletions
+4 -3
View File
@@ -10,6 +10,7 @@ from odoo.http import request
import time
import base64
import io
import logging
import os
import lxml.html
@@ -464,20 +465,20 @@ class IrActionsReport(models.Model):
if wkhtmltopdf_obj.header:
head_file_fd, head_file_path = tempfile.mkstemp(suffix='.html', prefix='report.header.tmp.')
temporary_files.append(head_file_path)
with closing(os.fdopen(head_file_fd, 'w')) as head_file:
with closing(os.fdopen(head_file_fd, 'wb')) as head_file:
head_file.write(wkhtmltopdf_obj.header)
local_command_args.extend(['--header-html', head_file_path])
if wkhtmltopdf_obj.footer:
foot_file_fd, foot_file_path = tempfile.mkstemp(suffix='.html', prefix='report.footer.tmp.')
temporary_files.append(foot_file_path)
with closing(os.fdopen(foot_file_fd, 'w')) as foot_file:
with closing(os.fdopen(foot_file_fd, 'wb')) as foot_file:
foot_file.write(wkhtmltopdf_obj.footer)
local_command_args.extend(['--footer-html', foot_file_path])
# Body stuff
content_file_fd, content_file_path = tempfile.mkstemp(suffix='.html', prefix='report.body.tmp.')
temporary_files.append(content_file_path)
with closing(os.fdopen(content_file_fd, 'w')) as content_file:
with closing(os.fdopen(content_file_fd, 'wb')) as content_file:
content_file.write(wkhtmltopdf_obj.content)
try:
+1 -4
View File
@@ -820,10 +820,9 @@ form: module.record_id""" % (xml_id,)
def convert_file(cr, module, filename, idref, mode='update', noupdate=False, kind=None, report=None, pathname=None):
if pathname is None:
pathname = os.path.join(module, filename)
fp = file_open(pathname, mode='rb')
ext = os.path.splitext(filename)[1].lower()
try:
with file_open(pathname, 'rb') as fp:
if ext == '.csv':
convert_csv_import(cr, module, pathname, fp.read(), idref, mode, noupdate)
elif ext == '.sql':
@@ -836,8 +835,6 @@ def convert_file(cr, module, filename, idref, mode='update', noupdate=False, kin
pass # .js files are valid but ignored here.
else:
raise ValueError("Can't load unknown file type %s.", filename)
finally:
fp.close()
def convert_sql_import(cr, fp):
cr.execute(fp.read())
+2 -2
View File
@@ -1000,7 +1000,7 @@ def trans_generate(lang, modules, cr):
def trans_load(cr, filename, lang, verbose=True, module_name=None, context=None):
try:
with file_open(filename) as fileobj:
with file_open(filename, mode='rb') as fileobj:
_logger.info("loading %s", filename)
fileformat = os.path.splitext(filename)[-1][1:].lower()
result = trans_load_data(cr, fileobj, fileformat, lang, verbose=verbose, module_name=module_name, context=context)
@@ -1053,7 +1053,7 @@ def trans_load_data(cr, fileobj, fileformat, lang, lang_name=None, verbose=True,
addons_module, i18n_dir = os.path.split(addons_module_i18n)
addons, module = os.path.split(addons_module)
pot_handle = file_open(os.path.join(
addons, module, i18n_dir, module + '.pot'))
addons, module, i18n_dir, module + '.pot'), mode='rb')
pot_reader = PoFile(pot_handle)
except:
pass