Skip to content

str method argument escaping is inconsistent #401

@davidism

Description

@davidism

Prior to #400, some str methods were overridden using a generic wrapper that called escape on all argument values that were str, then wrapped the result in Markup. This made typing difficult, and was inefficient. When removing the wrapper, I noticed it was only applied to str methods that returned str, not other method that took str arguments. I also noticed that some arguments shouldn't have escape called on them even though they are str.

There are some methods where the escaping makes sense. For example, str.center is similar to adding strings, which should escape the values.

But there are many other places where the escaping is inconsistent or not obvious. For example, __contains__ (and find, index, etc.) is not overridden, but strip is, so you can get the following behavior.

>>> from markupsafe import Markup
>>> s = Markup("<br>")
>>> "<" in s
True
>>> s.strip("<")
Markup("<br>")

strip calls escape on its argument, resulting in the actual call being str.strip(s, "&lt;"). That's definitely not correct, because strip removes any number of each character, in any order, but then intention was obviously to remove the <.

Python 3.9 added removeprefix and removesuffix, which remove the exact string of characters if it is present. But it's not clear if those should escape their argument, should removeprefix("<") remove the literal <, or &lt;?

All methods should at least be consistent. Regardless of which direction we choose, escaping or not, the user can always wrap the argument in Markup or escape to get the other meaning.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy