Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String type is returned in Python 2.7 when content is a newline #1053

Closed
Anteru opened this issue Aug 31, 2019 · 1 comment
Closed

String type is returned in Python 2.7 when content is a newline #1053

Anteru opened this issue Aug 31, 2019 · 1 comment
Labels
S-minor severity: minor T-bug type: a bug X-imported imported from Bitbucket

Comments

@Anteru
Copy link
Collaborator

Anteru commented Aug 31, 2019

(Original issue 1349 created by None on 2017-05-09T04:32:19.850113+00:00)

I've reported this issue in the Trac Project with Pygments 2.2. It can be reproduced in Python 2.7 with:

#!python

import io

from pygments.formatters.html import HtmlFormatter
from pygments.lexers import get_lexer_by_name


lexer_options = {'stripnl': False}
lexer_name = 'ipython2'

content = """
"""

out = io.StringIO()
lexer = get_lexer_by_name(lexer_name, **lexer_options)
formatter = HtmlFormatter(nowrap=True)
formatter.format(lexer.get_tokens(content), out)

assert '\n' == out.getvalue()

An error results: TypeError: unicode argument expected, got 'str'

The exception is not seen if adding a single whitespace before the newline. In html.py, the code branches to if line is there is a single whitespace and newline, but branches to else: yield 1, lsep if there's only a newline. The returned newline (lsep) seems to be a byte string rather than a unicode string (str type rather than unicode type in Python 2.7). I haven't tested with Python 3/

A successful workaround is to specify lineseparator as a unicode string.

#!diff

- formatter = HtmlFormatter(nowrap=True)
+ formatter = HtmlFormatter(nowrap=True, lineseparator=u'\n')

@Anteru Anteru added T-bug type: a bug X-imported imported from Bitbucket S-minor severity: minor labels Aug 31, 2019
@Anteru
Copy link
Collaborator Author

Anteru commented Aug 31, 2019

(Original comment by rjollos on 2017-05-09T04:34:18.112643+00:00)

Forgot to login before reporting. I'm the reporter.

See also: https://trac.edgewall.org/ticket/12046#comment:13

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-minor severity: minor T-bug type: a bug X-imported imported from Bitbucket
Projects
None yet
Development

No branches or pull requests

1 participant