py3: replace more cStringIO

This commit is contained in:
Kovid Goyal 2019-03-30 14:46:04 +05:30
parent f149427104
commit dec0a11ec0
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 18 additions and 15 deletions

View File

@ -11,10 +11,10 @@ Test the CLI of the calibre database management tool
'''
import csv
import unittest
from cStringIO import StringIO
from calibre.db.cli.cmd_check_library import _print_check_library_results
from polyglot.builtins import iteritems
from polyglot.io import PolyglotBytesIO
class Checker(object):
@ -32,12 +32,12 @@ class PrintCheckLibraryResultsTest(unittest.TestCase):
check = ('dummy_check', 'Dummy Check')
def test_prints_nothing_if_no_errors(self):
stdout = StringIO()
stdout = PolyglotBytesIO()
checker = Checker(dict.fromkeys(self.check))
_print_check_library_results(checker, self.check, as_csv=False, out=stdout)
self.assertEqual(stdout.getvalue(), '')
self.assertEqual(stdout.getvalue(), b'')
_print_check_library_results(checker, self.check, as_csv=True, out=stdout)
self.assertEqual(stdout.getvalue(), '')
self.assertEqual(stdout.getvalue(), b'')
def test_human_readable_output(self):
"""
@ -48,10 +48,10 @@ class PrintCheckLibraryResultsTest(unittest.TestCase):
data = [['first', 'second']]
checker = Checker(dict.fromkeys(self.check))
setattr(checker, self.check[0], data)
stdout = StringIO()
stdout = PolyglotBytesIO()
_print_check_library_results(checker, self.check, out=stdout, as_csv=False)
result = stdout.getvalue().split('\n')
result = stdout.getvalue().decode('utf-8', 'replace').split('\n')
self.assertEqual(len(result), len(data)+2)
self.assertEqual(result[0], self.check[1])
@ -70,10 +70,10 @@ class PrintCheckLibraryResultsTest(unittest.TestCase):
data = [['first', 'second']]
checker = Checker(dict.fromkeys(self.check))
setattr(checker, self.check[0], data)
stdout = StringIO()
stdout = PolyglotBytesIO()
_print_check_library_results(checker, self.check, as_csv=True, out=stdout)
result = stdout.getvalue().split('\n')
result = stdout.getvalue().decode('utf-8', 'replace').split('\n')
parsed_result = [l for l in csv.reader(result) if l]
self.assertEqual(parsed_result, [[self.check[1], data[0][0], data[0][1]]])
@ -84,10 +84,10 @@ class PrintCheckLibraryResultsTest(unittest.TestCase):
data = [['I, Caesar', 'second']]
checker = Checker(dict.fromkeys(self.check))
setattr(checker, self.check[0], data)
stdout = StringIO()
stdout = PolyglotBytesIO()
_print_check_library_results(checker, self.check, as_csv=True, out=stdout)
result = stdout.getvalue().split('\n')
result = stdout.getvalue().decode('utf-8', 'replace').split('\n')
parsed_result = [l for l in csv.reader(result) if l]
self.assertEqual(parsed_result, [[self.check[1], data[0][0], data[0][1]]])

View File

@ -7,7 +7,6 @@ __license__ = 'GPL v3'
__copyright__ = '2014, Kovid Goyal <kovid at kovidgoyal.net>'
import re, io, weakref, sys
from cStringIO import StringIO
from PyQt5.Qt import (
pyqtSignal, QVBoxLayout, QHBoxLayout, QPlainTextEdit, QLabel, QFontMetrics,
@ -24,6 +23,7 @@ from calibre.utils.icu import capitalize, upper, lower, swapcase
from calibre.utils.titlecase import titlecase
from calibre.utils.localization import localize_user_manual_link
from polyglot.builtins import iteritems, unicode_type
from polyglot.io import PolyglotBytesIO
user_functions = JSONConfig('editor-search-replace-functions')
@ -68,7 +68,7 @@ class Function(object):
self.match_index = 0
self.boss = get_boss()
self.data = {}
self.debug_buf = StringIO()
self.debug_buf = PolyglotBytesIO()
self.functions = {name:func.mod for name, func in iteritems(functions()) if func.mod is not None}
def __hash__(self):
@ -123,6 +123,8 @@ class DebugOutput(Dialog):
b.setIcon(QIcon(I('edit-copy.png')))
def show_log(self, name, text):
if isinstance(text, bytes):
text = text.decode('utf-8', 'replace')
self.setWindowTitle(_('Debug output from %s') % name)
self.text.setPlainText(self.windowTitle() + '\n\n' + text)
self.log_text = text

View File

@ -6,7 +6,7 @@ __license__ = 'GPL v3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'
import cStringIO, sys
import sys
from functools import partial
from threading import Thread
@ -21,6 +21,7 @@ from calibre.utils.smtp import config as smtp_prefs
from calibre.gui2 import error_dialog, question_dialog
from polyglot.builtins import unicode_type
from polyglot.binary import as_hex_unicode, from_hex_unicode
from polyglot.io import PolyglotBytesIO
class TestEmail(QDialog):
@ -193,7 +194,7 @@ class SendEmail(QWidget, Ui_Form):
def test_email_settings(self, to):
opts = smtp_prefs().parse()
from calibre.utils.smtp import sendmail, create_mail
buf = cStringIO.StringIO()
buf = PolyglotBytesIO()
debug_out = partial(prints, file=buf)
oout, oerr = sys.stdout, sys.stderr
sys.stdout = sys.stderr = buf
@ -209,7 +210,7 @@ class SendEmail(QWidget, Ui_Form):
except:
import traceback
tb = traceback.format_exc()
tb += '\n\nLog:\n' + buf.getvalue()
tb += '\n\nLog:\n' + buf.getvalue().decode('utf-8', 'replace')
finally:
sys.stdout, sys.stderr = oout, oerr
return tb