From a754bf46f1cb05051f92395661ccd830357cb66a Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 1 Apr 2019 18:34:50 +0530 Subject: [PATCH] py3: Replace use of StringIO --- src/calibre/devices/cli.py | 19 ++++++++++--------- src/calibre/ebooks/pml/pmlconverter.py | 7 ++++--- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/calibre/devices/cli.py b/src/calibre/devices/cli.py index 8dd8ca282b..e90a88bce1 100755 --- a/src/calibre/devices/cli.py +++ b/src/calibre/devices/cli.py @@ -7,14 +7,15 @@ Provides a command-line interface to ebook devices. For usage information run the script. """ -import StringIO, sys, time, os +import sys, time, os from optparse import OptionParser -from calibre import __version__, __appname__, human_readable, fsync +from calibre import __version__, __appname__, human_readable, fsync, prints from calibre.devices.errors import ArgumentError, DeviceError, DeviceLocked from calibre.customize.ui import device_plugins from calibre.devices.scanner import DeviceScanner from calibre.utils.config import device_prefs +from polyglot.io import PolyglotBytesIO MINIMUM_COL_WIDTH = 12 # : Minimum width of columns in ls output @@ -127,13 +128,13 @@ def ls(dev, path, recurse=False, human_readable_size=False, ll=False, cols=0): c += 1 return rowwidths - output = StringIO.StringIO() + output = PolyglotBytesIO() if path.endswith("/") and len(path) > 1: path = path[:-1] dirs = dev.list(path, recurse) for dir in dirs: if recurse: - print(dir[0] + ":", file=output) + prints(dir[0] + ":", file=output) lsoutput, lscoloutput = [], [] files = dir[1] maxlen = 0 @@ -154,7 +155,7 @@ def ls(dev, path, recurse=False, human_readable_size=False, ll=False, cols=0): size = str(file.size) if human_readable_size: size = file.human_readable_size - print(file.mode_string, ("%"+str(maxlen)+"s")%size, file.modification_time, name, file=output) + prints(file.mode_string, ("%"+str(maxlen)+"s")%size, file.modification_time, name, file=output) if not ll and len(lsoutput) > 0: trytable = [] for colwidth in range(MINIMUM_COL_WIDTH, cols): @@ -176,10 +177,10 @@ def ls(dev, path, recurse=False, human_readable_size=False, ll=False, cols=0): for r in range(len(trytable)): for c in range(len(trytable[r])): padding = rowwidths[c] - len(trytable[r][c]) - print(trytablecol[r][c], "".ljust(padding), end=' ', file=output) - print(file=output) - print(file=output) - listing = output.getvalue().rstrip()+ "\n" + prints(trytablecol[r][c], "".ljust(padding), end=' ', file=output) + prints(file=output) + prints(file=output) + listing = output.getvalue().rstrip().decode('utf-8') + "\n" output.close() return listing diff --git a/src/calibre/ebooks/pml/pmlconverter.py b/src/calibre/ebooks/pml/pmlconverter.py index 6259fdec7b..da748469c2 100644 --- a/src/calibre/ebooks/pml/pmlconverter.py +++ b/src/calibre/ebooks/pml/pmlconverter.py @@ -10,7 +10,7 @@ __docformat__ = 'restructuredtext en' import os import re -import StringIO +import io from copy import deepcopy from calibre import my_unichr, prepare_string_for_xml @@ -558,8 +558,9 @@ class PML_HTMLizer(object): else: indent_state['et'] = False - # Must use StringIO, cStringIO does not support unicode - line = StringIO.StringIO(line) + if isinstance(line, bytes): + line = line.decode('utf-8') + line = io.StringIO(line) parsed.append(self.start_line()) c = line.read(1)