Skip to main content

Implements a unicode subclass that supports HTML strings:

>>> from markupsafe import Markup, escape
>>> escape("<script>alert(document.cookie);</script>")
Markup(u'&lt;script&gt;alert(document.cookie);&lt;/script&gt;')
>>> tmpl = Markup("<em>%s</em>")
>>> tmpl % "Peter > Lustig"
Markup(u'<em>Peter &gt; Lustig</em>')

If you want to make an object unicode that is not yet unicode but don’t want to lose the taint information, you can use the soft_unicode function:

>>> from markupsafe import soft_unicode
>>> soft_unicode(42)
u'42'
>>> soft_unicode(Markup('foo'))
Markup(u'foo')

Objects can customize their HTML markup equivalent by overriding the __html__ function:

>>> class Foo(object):
...  def __html__(self):
...   return '<strong>Nice</strong>'
...
>>> escape(Foo())
Markup(u'<strong>Nice</strong>')
>>> Markup(Foo())
Markup(u'<strong>Nice</strong>')

Since MarkupSafe 0.10 there is now also a separate escape function called escape_silent that returns an empty string for None for consistency with other systems that return empty strings for None when escaping (for instance Pylons’ webhelpers).

If you also want to use this for the escape method of the Markup object, you can create your own subclass that does that:

from markupsafe import Markup, escape_silent as escape

class SilentMarkup(Markup):
    __slots__ = ()

    @classmethod
    def escape(cls, s):
        return cls(escape(s))

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

MarkupSafe-0.11.tar.gz (10.9 kB view details)

Uploaded Source

File details

Details for the file MarkupSafe-0.11.tar.gz.

File metadata

  • Download URL: MarkupSafe-0.11.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for MarkupSafe-0.11.tar.gz
Algorithm Hash digest
SHA256 f19442168c44db14b7093d1f93444233b05271dd76b4bb96ff8007ef83ac8855
MD5 48d445941c16d6aa55caf8e148fc0911
BLAKE2b-256 ee51df4a007a19f3de1b733adac77da11fa45f1ec5089549a7cc2d67195bb2c1

See more details on using hashes here.

Release history Release notifications | RSS feed

3.0.3

89 files

3.0.2

61 files

3.0.1

61 files

3.0.0

61 files

2.1.5

60 files

2.1.4

60 files

2.1.3

60 files

2.1.2

50 files

2.1.1

40 files

2.1.0

40 files

2.0.1

69 files

2.0.0

34 files

1.1.1

52 files

1.1.0

28 files

1.0

1 file

0.23

1 file

0.22

1 file

0.21

1 file

0.20

1 file

0.19

1 file

0.18

1 file

0.17

1 file

0.16

1 file

0.15

1 file

0.14

1 file

0.13

1 file

0.12

1 file

This release

0.11 This release

1 file

0.9.3

1 file

0.9.2

1 file

0.9.1

1 file

0.9

1 file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page