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

trying to fix syntax errors in python 3.2 #32

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions markupsafe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
:copyright: (c) 2010 by Armin Ronacher.
:license: BSD, see LICENSE for more details.
"""
from __future__ import unicode_literals
import re
import string
from collections import Mapping
Expand Down Expand Up @@ -39,35 +40,35 @@ class Markup(text_type):
converted into a unicode string and then assumed to be safe:

>>> Markup("Hello <em>World</em>!")
Markup(u'Hello <em>World</em>!')
Markup('Hello <em>World</em>!')
>>> class Foo(object):
... def __html__(self):
... return '<a href="#">foo</a>'
...
>>> Markup(Foo())
Markup(u'<a href="#">foo</a>')
Markup('<a href="#">foo</a>')

If you want object passed being always treated as unsafe you can use the
:meth:`escape` classmethod to create a :class:`Markup` object:

>>> Markup.escape("Hello <em>World</em>!")
Markup(u'Hello &lt;em&gt;World&lt;/em&gt;!')
Markup('Hello &lt;em&gt;World&lt;/em&gt;!')

Operations on a markup string are markup aware which means that all
arguments are passed through the :func:`escape` function:

>>> em = Markup("<em>%s</em>")
>>> em % "foo & bar"
Markup(u'<em>foo &amp; bar</em>')
Markup('<em>foo &amp; bar</em>')
>>> strong = Markup("<strong>%(text)s</strong>")
>>> strong % {'text': '<blink>hacker here</blink>'}
Markup(u'<strong>&lt;blink&gt;hacker here&lt;/blink&gt;</strong>')
Markup('<strong>&lt;blink&gt;hacker here&lt;/blink&gt;</strong>')
>>> Markup("<em>Hello</em> ") + "<foo>"
Markup(u'<em>Hello</em> &lt;foo&gt;')
Markup('<em>Hello</em> &lt;foo&gt;')
"""
__slots__ = ()

def __new__(cls, base=u'', encoding=None, errors='strict'):
def __new__(cls, base='', encoding=None, errors='strict'):
if hasattr(base, '__html__'):
base = base.__html__()
if encoding is None:
Expand Down Expand Up @@ -128,7 +129,7 @@ def unescape(self):
known HTML4 and XHTML entities:

>>> Markup("Main &raquo; <em>About</em>").unescape()
u'Main \xbb <em>About</em>'
'Main \xbb <em>About</em>'
"""
from markupsafe._constants import HTML_ENTITIES
def handle_match(m):
Expand All @@ -142,7 +143,7 @@ def handle_match(m):
return unichr(int(name[1:]))
except ValueError:
pass
return u''
return ''
return _entity_re.sub(handle_match, text_type(self))

def striptags(self):
Expand All @@ -151,9 +152,9 @@ def striptags(self):
normalized to one:

>>> Markup("Main &raquo; <em>About</em>").striptags()
u'Main \xbb About'
'Main \xbb About'
"""
stripped = u' '.join(_striptags_re.sub('', self).split())
stripped = ' '.join(_striptags_re.sub('', self).split())
return Markup(stripped).unescape()

@classmethod
Expand Down
13 changes: 7 additions & 6 deletions markupsafe/tests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import gc
import sys
import unittest
Expand Down Expand Up @@ -49,16 +50,16 @@ def test_tuple_interpol(self):
self.assertEqual(Markup('<em>%s:%s</em>') % (
'<foo>',
'<bar>',
), Markup(u'<em>&lt;foo&gt;:&lt;bar&gt;</em>'))
), Markup('<em>&lt;foo&gt;:&lt;bar&gt;</em>'))

def test_dict_interpol(self):
self.assertEqual(Markup('<em>%(foo)s</em>') % {
'foo': '<foo>',
}, Markup(u'<em>&lt;foo&gt;</em>'))
}, Markup('<em>&lt;foo&gt;</em>'))
self.assertEqual(Markup('<em>%(foo)s:%(bar)s</em>') % {
'foo': '<foo>',
'bar': '<bar>',
}, Markup(u'<em>&lt;foo&gt;:&lt;bar&gt;</em>'))
}, Markup('<em>&lt;foo&gt;:&lt;bar&gt;</em>'))

def test_escaping(self):
# escaping and unescaping
Expand Down Expand Up @@ -128,7 +129,7 @@ def test_all_set(self):
def test_escape_silent(self):
assert escape_silent(None) == Markup()
assert escape(None) == Markup(None)
assert escape_silent('<foo>') == Markup(u'&lt;foo&gt;')
assert escape_silent('<foo>') == Markup('&lt;foo&gt;')

def test_splitting(self):
self.assertEqual(Markup('a b').split(), [
Expand Down Expand Up @@ -156,8 +157,8 @@ def test_markup_leaks(self):
for item in range(1000):
escape("foo")
escape("<foo>")
escape(u"foo")
escape(u"<foo>")
escape("foo")
escape("<foo>")
counts.add(len(gc.get_objects()))
assert len(counts) == 1, 'ouch, c extension seems to leak objects'

Expand Down