From 3b67fda251099f2cd5c44a1a4fefc37f74a487de Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Thu, 18 May 2017 11:11:14 +0200 Subject: [PATCH] [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. --- odoo/addons/base/ir/ir_actions_report.py | 7 ++++--- odoo/tools/convert.py | 5 +---- odoo/tools/translate.py | 4 ++-- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/odoo/addons/base/ir/ir_actions_report.py b/odoo/addons/base/ir/ir_actions_report.py index 11f7efb03ce..3f81bd13e95 100644 --- a/odoo/addons/base/ir/ir_actions_report.py +++ b/odoo/addons/base/ir/ir_actions_report.py @@ -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: diff --git a/odoo/tools/convert.py b/odoo/tools/convert.py index 86697b2b4d2..57b692f7ae4 100644 --- a/odoo/tools/convert.py +++ b/odoo/tools/convert.py @@ -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()) diff --git a/odoo/tools/translate.py b/odoo/tools/translate.py index 0afb04b3972..f958772514e 100644 --- a/odoo/tools/translate.py +++ b/odoo/tools/translate.py @@ -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