From eceb17365357d33ed1bad8378bbe22feb241f655 Mon Sep 17 00:00:00 2001 From: Christophe Simonis Date: Thu, 24 Aug 2017 15:17:24 +0200 Subject: [PATCH] [P3] *: stop using cStringIO --- odoo/addons/base/ir/ir_actions_report.py | 14 ++++++-------- odoo/tools/pdf.py | 6 +++--- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/odoo/addons/base/ir/ir_actions_report.py b/odoo/addons/base/ir/ir_actions_report.py index d7adaf42363..fa7365f1b50 100644 --- a/odoo/addons/base/ir/ir_actions_report.py +++ b/odoo/addons/base/ir/ir_actions_report.py @@ -1,8 +1,8 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. from odoo import api, fields, models, tools, SUPERUSER_ID, _ -from odoo.exceptions import MissingError, UserError, ValidationError, AccessError -from odoo.tools.safe_eval import safe_eval, test_python_expr +from odoo.exceptions import UserError, AccessError +from odoo.tools.safe_eval import safe_eval from odoo.tools.misc import find_in_path from odoo.tools import config from odoo.sql_db import TestCursor @@ -22,7 +22,6 @@ from lxml import etree from contextlib import closing from distutils.version import LooseVersion from reportlab.graphics.barcode import createBarcodeDrawing -from cStringIO import StringIO from PyPDF2 import PdfFileWriter, PdfFileReader @@ -103,7 +102,6 @@ class IrActionsReport(models.Model): attachment = fields.Char(string='Save as Attachment Prefix', help='This is the filename of the attachment used to store the printing result. Keep empty to not save the printed reports. You can use a python expression with the object and time variables.') - @api.multi def associated_view(self): """Used in the ir.actions.report form view in order to search naively after the view(s) @@ -512,7 +510,7 @@ class IrActionsReport(models.Model): # In wkhtmltopdf has been called, we need to split the pdf in order to call the postprocess method. if pdf_content: - pdf_content_stream = StringIO(pdf_content) + pdf_content_stream = io.BytesIO(pdf_content) # Build a record_map mapping id -> record record_map = {r.id: r for r in self.env[self.model].browse([res_id for res_id in res_ids if res_id])} @@ -540,7 +538,7 @@ class IrActionsReport(models.Model): attachment_writer = PdfFileWriter() for j in range(num, to): attachment_writer.addPage(reader.getPage(j)) - stream = StringIO() + stream = io.BytesIO() attachment_writer.write(stream) if res_ids[i] and res_ids[i] not in save_in_attachment: self.postprocess_pdf_report(record_map[res_ids[i]], stream) @@ -552,14 +550,14 @@ class IrActionsReport(models.Model): if self.attachment_use: for attachment_id in save_in_attachment.values(): content = base64.decodestring(attachment_id.datas) - streams.append(StringIO(content)) + streams.append(io.BytesIO(content)) # Build the final pdf. writer = PdfFileWriter() for stream in streams: reader = PdfFileReader(stream) writer.appendPagesFromReader(reader) - result_stream = StringIO() + result_stream = io.BytesIO() streams.append(result_stream) writer.write(result_stream) result = result_stream.getvalue() diff --git a/odoo/tools/pdf.py b/odoo/tools/pdf.py index 7f6f244ce45..85e4005e69e 100644 --- a/odoo/tools/pdf.py +++ b/odoo/tools/pdf.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. -from cStringIO import StringIO +import io from PyPDF2 import PdfFileWriter, PdfFileReader @@ -11,10 +11,10 @@ def merge_pdf(pdf_data): ''' writer = PdfFileWriter() for document in pdf_data: - reader = PdfFileReader(StringIO(document), strict=False) + reader = PdfFileReader(io.BytesIO(document), strict=False) for page in range(0, reader.getNumPages()): writer.addPage(reader.getPage(page)) - _buffer = StringIO() + _buffer = io.BytesIO() writer.write(_buffer) merged_pdf = _buffer.getvalue() _buffer.close()