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:
parent
650b23ada8
commit
ceb8d749f8
@ -67,14 +67,18 @@ def main(argv=None):
|
|||||||
'output filename that ends in ' + extensions)
|
'output filename that ends in ' + extensions)
|
||||||
|
|
||||||
if args.input == '-':
|
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.
|
# Dummy filename in the current directory.
|
||||||
base_url='<stdin>')
|
base_url='<stdin>')
|
||||||
else:
|
else:
|
||||||
source = HTML(args.input, encoding=args.encoding)
|
source = HTML(args.input, encoding=args.encoding)
|
||||||
|
|
||||||
if args.output == '-':
|
if args.output == '-':
|
||||||
args.output = sys.stdout
|
stdout = sys.stdout
|
||||||
|
stdout = getattr(stdout, 'buffer', stdout)
|
||||||
|
args.output = stdout
|
||||||
|
|
||||||
getattr(source, 'write_' + args.format)(
|
getattr(source, 'write_' + args.format)(
|
||||||
args.output, stylesheets=args.stylesheet)
|
args.output, stylesheets=args.stylesheet)
|
||||||
|
@ -23,11 +23,16 @@ Python 2/3 compatibility.
|
|||||||
|
|
||||||
from __future__ import division, unicode_literals
|
from __future__ import division, unicode_literals
|
||||||
|
|
||||||
|
import io
|
||||||
import sys
|
import sys
|
||||||
import email
|
import email
|
||||||
|
import contextlib
|
||||||
|
|
||||||
|
|
||||||
if sys.version_info[0] >= 3:
|
PY3 = sys.version_info[0] >= 3
|
||||||
|
|
||||||
|
|
||||||
|
if PY3:
|
||||||
# Python 3
|
# Python 3
|
||||||
from urllib.parse import urljoin, urlparse, unquote_to_bytes
|
from urllib.parse import urljoin, urlparse, unquote_to_bytes
|
||||||
from urllib.request import urlopen, Request
|
from urllib.request import urlopen, Request
|
||||||
@ -37,6 +42,7 @@ if sys.version_info[0] >= 3:
|
|||||||
xrange = range
|
xrange = range
|
||||||
iteritems = dict.items
|
iteritems = dict.items
|
||||||
|
|
||||||
|
|
||||||
def urlopen_contenttype(url):
|
def urlopen_contenttype(url):
|
||||||
"""Return (file_obj, mime_type, encoding)"""
|
"""Return (file_obj, mime_type, encoding)"""
|
||||||
result = urlopen(url)
|
result = urlopen(url)
|
||||||
@ -46,6 +52,7 @@ if sys.version_info[0] >= 3:
|
|||||||
# Using here result.fp gives 'ValueError: read of closed file'
|
# Using here result.fp gives 'ValueError: read of closed file'
|
||||||
return result, mime_type, charset
|
return result, mime_type, charset
|
||||||
|
|
||||||
|
|
||||||
def parse_email(data):
|
def parse_email(data):
|
||||||
if isinstance(data, bytes):
|
if isinstance(data, bytes):
|
||||||
data = data.decode('utf8')
|
data = data.decode('utf8')
|
||||||
@ -61,9 +68,11 @@ else:
|
|||||||
xrange = xrange
|
xrange = xrange
|
||||||
iteritems = dict.iteritems
|
iteritems = dict.iteritems
|
||||||
|
|
||||||
|
|
||||||
def array(typecode, initializer):
|
def array(typecode, initializer):
|
||||||
return _array(typecode.encode('ascii'), initializer)
|
return _array(typecode.encode('ascii'), initializer)
|
||||||
|
|
||||||
|
|
||||||
def urlopen_contenttype(url):
|
def urlopen_contenttype(url):
|
||||||
"""Return (file_obj, mime_type, encoding)"""
|
"""Return (file_obj, mime_type, encoding)"""
|
||||||
result = urlopen(url)
|
result = urlopen(url)
|
||||||
@ -72,11 +81,13 @@ else:
|
|||||||
charset = info.getparam('charset')
|
charset = info.getparam('charset')
|
||||||
return result.fp, mime_type, charset
|
return result.fp, mime_type, charset
|
||||||
|
|
||||||
|
|
||||||
def unquote_to_bytes(data):
|
def unquote_to_bytes(data):
|
||||||
if isinstance(data, unicode):
|
if isinstance(data, unicode):
|
||||||
data = data.encode('ascii')
|
data = data.encode('ascii')
|
||||||
return unquote(data)
|
return unquote(data)
|
||||||
|
|
||||||
|
|
||||||
def parse_email(data):
|
def parse_email(data):
|
||||||
if isinstance(data, unicode):
|
if isinstance(data, unicode):
|
||||||
data = data.encode('utf8')
|
data = data.encode('utf8')
|
||||||
|
@ -36,7 +36,7 @@ import pystacia
|
|||||||
|
|
||||||
from .testing_utils import (
|
from .testing_utils import (
|
||||||
resource_filename, assert_no_logs, TEST_UA_STYLESHEET)
|
resource_filename, assert_no_logs, TEST_UA_STYLESHEET)
|
||||||
from ..compat import urljoin
|
from ..compat import urljoin, PY3
|
||||||
from .. import HTML, CSS
|
from .. import HTML, CSS
|
||||||
from .. import __main__
|
from .. import __main__
|
||||||
|
|
||||||
@ -86,9 +86,15 @@ def monkey_patch_stdio(input_bytes=b''):
|
|||||||
old_stdin = sys.stdin
|
old_stdin = sys.stdin
|
||||||
old_stdout = sys.stdout
|
old_stdout = sys.stdout
|
||||||
try:
|
try:
|
||||||
sys.stdin = io.BytesIO(input_bytes)
|
stdin = io.BytesIO(input_bytes)
|
||||||
sys.stdout = io.BytesIO()
|
stdout = io.BytesIO()
|
||||||
yield sys.stdout
|
if PY3:
|
||||||
|
sys.stdin = io.TextIOWrapper(stdin)
|
||||||
|
sys.stdout = io.TextIOWrapper(stdout)
|
||||||
|
else:
|
||||||
|
sys.stdin = stdin
|
||||||
|
sys.stdout = stdout
|
||||||
|
yield stdout
|
||||||
finally:
|
finally:
|
||||||
sys.stdin = old_stdin
|
sys.stdin = old_stdin
|
||||||
sys.stdout = old_stdout
|
sys.stdout = old_stdout
|
||||||
@ -296,7 +302,7 @@ def test_command_line_render():
|
|||||||
|
|
||||||
with monkey_patch_stdio() as stdout:
|
with monkey_patch_stdio() as stdout:
|
||||||
run('--format png combined.html -')
|
run('--format png combined.html -')
|
||||||
assert stdout.getvalue() == png_bytes
|
assert stdout.getvalue() == png_bytes
|
||||||
|
|
||||||
with monkey_patch_stdio(combined):
|
with monkey_patch_stdio(combined):
|
||||||
run('- out11.png')
|
run('- out11.png')
|
||||||
@ -305,4 +311,4 @@ def test_command_line_render():
|
|||||||
|
|
||||||
with monkey_patch_stdio(combined) as stdout:
|
with monkey_patch_stdio(combined) as stdout:
|
||||||
run('--format png - -')
|
run('--format png - -')
|
||||||
assert stdout.getvalue() == png_bytes
|
assert stdout.getvalue() == png_bytes
|
||||||
|
Loading…
Reference in New Issue
Block a user