Skip to content

Fix #3076 - Handle None object before HTML escaping - #3078

Merged
Anteru merged 4 commits into
pygments:masterfrom
styx0x6:issue-3076
Apr 2, 2026
Merged

Anteru merged 4 commits into
pygments:masterfrom
styx0x6:issue-3076

Conversation

@styx0x6

@styx0x6 styx0x6 commented Mar 30, 2026 •

Copy link
Copy Markdown
Contributor

My proposal to fix issue #3076.

Handle None object before HTML escaping regarding commit 0f97e7c.

Various text-only attributes using html.escape may also be NoneType objects, which seems not to be supported by html.escape which uses built-in replace function.

Handling None object will avoid further of this kind of error.

Fixes #3076.

@birkenfeld

Copy link
Copy Markdown
Member

Thanks for working on a fix!

I don't think this will work as expected, we don't want a passed None to appear in the output as stringified "None".

Better to skip the escape() call if it's not a string.

@birkenfeld

Copy link
Copy Markdown
Member

Also, looks like a few new tests are needed 😁

@styx0x6

styx0x6 commented Mar 30, 2026 •

Copy link
Copy Markdown
Contributor Author

Hi,

You are right, also tought about this solution afterwards, string casting might have other side-effect. It's effectivelly more clean to skip the escape on None values. Will come back with an update.

@styx0x6

styx0x6 commented Mar 30, 2026

Copy link
Copy Markdown
Contributor Author

Here it is. Ready to run tests again @birkenfeld .

@styx0x6 styx0x6 changed the title Fix #3076 - Force to string before HTML escaping Fix #3076 - Handle None object before HTML escaping Mar 30, 2026

@2bndy5 2bndy5 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be painfully clear:
An empty string is not identical to a None type.

>>> any_str = ""
>>> True if any_str else False
False
>>> True if any_str is not None else False
True
>>> no_val = None
>>> True if no_val else False
False
>>> True if no_val is not None else False
False

I'm not sure if this bears any significance in the usage here. But the issue you're trying to solve is a dynamic typing problem. It makes sense to be stricter with your type checking, no?

@birkenfeld

Copy link
Copy Markdown
Member

It doesn't make sense to gratuitously break people's code, no.

@styx0x6

styx0x6 commented Mar 31, 2026

Copy link
Copy Markdown
Contributor Author

Hi @2bndy5 ,

That's right, an empty string is not the same as a NoneType object, but whatever, regarding the business logic of the code line here, it will just say that an empty string will not pass through the html.escape, which is not a security issue.

The important point here is to not let None being treated by html.escape. And that will fix the bug.

We might force the type checking but it would change nothing. Let's see that as avoiding useless code treatment and performance optimization 🙂 .

@Anteru

Anteru commented Mar 31, 2026 •

Copy link
Copy Markdown
Collaborator

Just to be clear, it's people passing in None, not getting None from the options, as those all default to '', right? I'd rather have a helper function, html_escape or similar, in our utility functions, which is:

def html_escape(string):
    """Return a safe version of the passed string, and '' if None"""
    if string is not None:
        return html.escape(string)
    return ''

So we can test this in isolation. Eventually, we can even add strong types to it (str | None) in this case. Then we can test this function (as trivial as it is ...) and test it in combination.

@2bndy5

2bndy5 commented Mar 31, 2026

Copy link
Copy Markdown

Using a blanket true if cond else false makes sense as long as html.escape() has no special handling of empty strings. And I see that this is not a concern (see html.escape() source). However, this could become a future regression if html.escape() ever changes about handling an empty string.

I agree that treating None and an empty string similarly could prove to be an optimization. Setting aside how empty iterators are handled, we could go further in the name of optimization: The html.escape() call should be after the else keyword, for the sake of various Python interpreters.

# ensure `html.escape()` is only processed when needed by the interpreter
self.filename = _filename if not _filename else html.escape(_filename)
# interpreter stops at `else` when it is None or an empty string

I'm not trying to block anything up here. I'm only trying to be helpful.

@birkenfeld

Copy link
Copy Markdown
Member

@2bndy5 the order of the clauses does not matter, the if-else expression first evaluates the condition and then exactly one of the cases, depending on the result.

@styx0x6 the function suggested by @Anteru sounds like a good idea.

@styx0x6

styx0x6 commented Mar 31, 2026

Copy link
Copy Markdown
Contributor Author

Thanks all for your help and discussion.

I will try to offer a new version asap.

@birkenfeld

Copy link
Copy Markdown
Member

No problem, and thank you for actually contributing instead of letting Claude run wild. It's a breath of fresh air these days 🤣

@styx0x6

styx0x6 commented Mar 31, 2026 •

Copy link
Copy Markdown
Contributor Author

Here is an update, with trivial unit tests this time.

Also I discovered another clean-up code to do in html.py and svg.py, using an old escape_html function hardcoded directly, which seems useless now. I think it's better to clean-up and use the util function now. I'll set a new PR for the next feature release for that in order to not interact with this bugfix.

(@birkenfeld, I understand you, thinking the same. Claude is like a colleague, helpful to ask a question sometimes, but not here to do the job for you :) ).

Any comment is welcome.

@Anteru

Anteru commented Mar 31, 2026

Copy link
Copy Markdown
Collaborator

Looks good to me. This will go into a 2.20.1 release, thanks again! Also good catch on the escape_html thing. I agree that should go away or be unified.

@Anteru Anteru added this to the 2.20.1 milestone Mar 31, 2026
@ilyagr ilyagr mentioned this pull request Apr 1, 2026
2 tasks done
@Anteru
Anteru merged commit 7d7ab7f into pygments:master Apr 2, 2026
15 checks passed
@styx0x6
styx0x6 deleted the issue-3076 branch April 2, 2026 19:43
Anteru added a commit that referenced this pull request May 3, 2026
Update #3078 - Force any non-NoneType to string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mkdocs build fails in HtmlFormatter with 'NoneType' object has no attribute 'replace'

4 participants