1
1
mirror of https://github.com/Kozea/WeasyPrint.git synced 2024-10-05 00:21:15 +03:00

Fix stdio monkey-patching on Python 3.

This commit is contained in:
Simon Sapin 2012-02-23 12:50:08 +01:00
parent 650b23ada8
commit ceb8d749f8
3 changed files with 30 additions and 9 deletions

View File

@ -67,14 +67,18 @@ def main(argv=None):
'output filename that ends in ' + extensions)
if args.input == '-':
source = HTML(file_obj=sys.stdin, encoding=args.encoding,
stdin = sys.stdin
stdin = getattr(stdin, 'buffer', stdin)
source = HTML(file_obj=stdin, encoding=args.encoding,
# Dummy filename in the current directory.
base_url='<stdin>')
else:
source = HTML(args.input, encoding=args.encoding)
if args.output == '-':
args.output = sys.stdout
stdout = sys.stdout
stdout = getattr(stdout, 'buffer', stdout)
args.output = stdout
getattr(source, 'write_' + args.format)(
args.output, stylesheets=args.stylesheet)

View File

@ -23,11 +23,16 @@ Python 2/3 compatibility.
from __future__ import division, unicode_literals
import io
import sys
import email
import contextlib
if sys.version_info[0] >= 3:
PY3 = sys.version_info[0] >= 3
if PY3:
# Python 3
from urllib.parse import urljoin, urlparse, unquote_to_bytes
from urllib.request import urlopen, Request
@ -37,6 +42,7 @@ if sys.version_info[0] >= 3:
xrange = range
iteritems = dict.items
def urlopen_contenttype(url):
"""Return (file_obj, mime_type, encoding)"""
result = urlopen(url)
@ -46,6 +52,7 @@ if sys.version_info[0] >= 3:
# Using here result.fp gives 'ValueError: read of closed file'
return result, mime_type, charset
def parse_email(data):
if isinstance(data, bytes):
data = data.decode('utf8')
@ -61,9 +68,11 @@ else:
xrange = xrange
iteritems = dict.iteritems
def array(typecode, initializer):
return _array(typecode.encode('ascii'), initializer)
def urlopen_contenttype(url):
"""Return (file_obj, mime_type, encoding)"""
result = urlopen(url)
@ -72,11 +81,13 @@ else:
charset = info.getparam('charset')
return result.fp, mime_type, charset
def unquote_to_bytes(data):
if isinstance(data, unicode):
data = data.encode('ascii')
return unquote(data)
def parse_email(data):
if isinstance(data, unicode):
data = data.encode('utf8')

View File

@ -36,7 +36,7 @@ import pystacia
from .testing_utils import (
resource_filename, assert_no_logs, TEST_UA_STYLESHEET)
from ..compat import urljoin
from ..compat import urljoin, PY3
from .. import HTML, CSS
from .. import __main__
@ -86,9 +86,15 @@ def monkey_patch_stdio(input_bytes=b''):
old_stdin = sys.stdin
old_stdout = sys.stdout
try:
sys.stdin = io.BytesIO(input_bytes)
sys.stdout = io.BytesIO()
yield sys.stdout
stdin = io.BytesIO(input_bytes)
stdout = io.BytesIO()
if PY3:
sys.stdin = io.TextIOWrapper(stdin)
sys.stdout = io.TextIOWrapper(stdout)
else:
sys.stdin = stdin
sys.stdout = stdout
yield stdout
finally:
sys.stdin = old_stdin
sys.stdout = old_stdout
@ -296,7 +302,7 @@ def test_command_line_render():
with monkey_patch_stdio() as stdout:
run('--format png combined.html -')
assert stdout.getvalue() == png_bytes
assert stdout.getvalue() == png_bytes
with monkey_patch_stdio(combined):
run('- out11.png')
@ -305,4 +311,4 @@ def test_command_line_render():
with monkey_patch_stdio(combined) as stdout:
run('--format png - -')
assert stdout.getvalue() == png_bytes
assert stdout.getvalue() == png_bytes