mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Fix prints()
This commit is contained in:
parent
4d40be05f3
commit
1d33b3dc0c
@ -18,14 +18,28 @@ def is_binary(stream):
|
|||||||
def prints(*a, **kw):
|
def prints(*a, **kw):
|
||||||
' Print either unicode or bytes to either binary or text mode streams '
|
' Print either unicode or bytes to either binary or text mode streams '
|
||||||
stream = kw.get('file', sys.stdout)
|
stream = kw.get('file', sys.stdout)
|
||||||
|
sep, end = kw.get('sep'), kw.get('end')
|
||||||
|
if sep is None:
|
||||||
|
sep = ' '
|
||||||
|
if end is None:
|
||||||
|
end = '\n'
|
||||||
if is_binary(stream):
|
if is_binary(stream):
|
||||||
encoding = getattr(stream, 'encoding', None) or 'utf-8'
|
encoding = getattr(stream, 'encoding', None) or 'utf-8'
|
||||||
a = tuple(as_bytes(x, encoding=encoding) for x in a)
|
a = tuple(as_bytes(x, encoding=encoding) for x in a)
|
||||||
kw['sep'] = as_bytes(kw.get('sep', b' '))
|
sep = as_bytes(sep)
|
||||||
kw['end'] = as_bytes(kw.get('end', b'\n'))
|
end = as_bytes(end)
|
||||||
else:
|
else:
|
||||||
a = tuple(as_unicode(x, errors='replace') for x in a)
|
a = tuple(as_unicode(x, errors='replace') for x in a)
|
||||||
kw['sep'] = as_unicode(kw.get('sep', ' '))
|
sep = as_unicode(sep)
|
||||||
kw['end'] = as_unicode(kw.get('end', '\n'))
|
end = as_unicode(end)
|
||||||
|
for x in a:
|
||||||
return print(*a, **kw)
|
stream.write(x)
|
||||||
|
if sep:
|
||||||
|
stream.write(sep)
|
||||||
|
if end:
|
||||||
|
stream.write(end)
|
||||||
|
if kw.get('flush'):
|
||||||
|
try:
|
||||||
|
stream.flush()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
Loading…
x
Reference in New Issue
Block a user