diff --git a/tests/test_api.py b/tests/test_api.py index 16def4b69..cc65249f5 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -52,7 +52,9 @@ def _test_resource(class_, basename, check, **kwargs): check(class_(relative_path, **kwargs)) kwargs.pop('base_url', None) check(class_(string=content, base_url=relative_filename, **kwargs)) - encoding = kwargs.get('encoding') or 'utf-8' + encoding = kwargs.pop('encoding', 'utf-8') + with open(absolute_filename, 'r', encoding=encoding) as fd: + check(class_(file_obj=fd, **kwargs)) check(class_(string=content.decode(encoding), # unicode base_url=relative_filename, **kwargs)) with pytest.raises(TypeError): diff --git a/weasyprint/__init__.py b/weasyprint/__init__.py index 47aa905fb..52094a372 100644 --- a/weasyprint/__init__.py +++ b/weasyprint/__init__.py @@ -162,10 +162,12 @@ def __init__(self, guess=None, filename=None, url=None, file_obj=None, if isinstance(source, str): result = html5lib.parse(source, namespaceHTMLElements=False) else: - result = html5lib.parse( - source, override_encoding=encoding, - transport_encoding=protocol_encoding, - namespaceHTMLElements=False) + kwargs = {'namespaceHTMLElements': False} + if protocol_encoding is not None: + kwargs['transport_encoding'] = protocol_encoding + if encoding is not None: + kwargs['override_encoding'] = encoding + result = html5lib.parse(source, **kwargs) self.base_url = _find_base_url(result, base_url) self.url_fetcher = url_fetcher self.media_type = media_type @@ -281,12 +283,12 @@ def __init__(self, guess=None, filename=None, url=None, file_obj=None, base_url=base_url, url_fetcher=url_fetcher, check_css_mime_type=_check_mime_type) with result as (source_type, source, base_url, protocol_encoding): - if source_type == 'string' and not isinstance(source, bytes): + if source_type == 'file_obj': + source = source.read() + if isinstance(source, str): # unicode, no encoding stylesheet = tinycss2.parse_stylesheet(source) else: - if source_type == 'file_obj': - source = source.read() stylesheet, encoding = tinycss2.parse_stylesheet_bytes( source, environment_encoding=encoding, protocol_encoding=protocol_encoding)