コード例 #1
0
ファイル: test_soup.py プロジェクト: poutant1/InkuMail
 def test_sniffed_xml_encoding(self):
     # A document written in UTF-16LE will be converted by a different
     # code path that sniffs the byte order markers.
     data = b'\xff\xfe<\x00a\x00>\x00\xe1\x00\xe9\x00<\x00/\x00a\x00>\x00'
     dammit = UnicodeDammit(data)
     self.assertEqual("<a>áé</a>", dammit.unicode_markup)
     self.assertEqual("utf-16le", dammit.original_encoding)
コード例 #2
0
ファイル: test_soup.py プロジェクト: poutant1/InkuMail
 def test_smart_quote_substitution(self):
     # MS smart quotes are a common source of frustration, so we
     # give them a special test.
     quotes = b"\x91\x92foo\x93\x94"
     dammit = UnicodeDammit(quotes)
     self.assertEqual(self.sub.substitute_html(dammit.markup),
                      "&lsquo;&rsquo;foo&ldquo;&rdquo;")
コード例 #3
0
ファイル: test_soup.py プロジェクト: poutant1/InkuMail
    def test_detect_html5_style_meta_tag(self):

        for data in (b'<html><meta charset="euc-jp" /></html>',
                     b"<html><meta charset='euc-jp' /></html>",
                     b"<html><meta charset=euc-jp /></html>",
                     b"<html><meta charset=euc-jp/></html>"):
            dammit = UnicodeDammit(data, is_html=True)
            self.assertEqual("euc-jp", dammit.original_encoding)
コード例 #4
0
ファイル: test_soup.py プロジェクト: poutant1/InkuMail
 def test_detwingle_ignores_multibyte_characters(self):
     # Each of these characters has a UTF-8 representation ending
     # in \x93. \x93 is a smart quote if interpreted as
     # Windows-1252. But our code knows to skip over multibyte
     # UTF-8 characters, so they'll survive the process unscathed.
     for tricky_unicode_char in (
             "\N{LATIN SMALL LIGATURE OE}",  # 2-byte char '\xc5\x93'
             "\N{LATIN SUBSCRIPT SMALL LETTER X}",  # 3-byte char '\xe2\x82\x93'
             "\xf0\x90\x90\x93",  # This is a CJK character, not sure which one.
     ):
         input = tricky_unicode_char.encode("utf8")
         self.assertTrue(input.endswith(b'\x93'))
         output = UnicodeDammit.detwingle(input)
         self.assertEqual(output, input)
コード例 #5
0
    def prepare_markup(self,
                       markup,
                       user_specified_encoding=None,
                       document_declared_encoding=None):
        """
        :return: A 3-tuple (markup, original encoding, encoding
        declared within markup).
        """
        if isinstance(markup, str):
            return markup, None, None, False

        try_encodings = [user_specified_encoding, document_declared_encoding]
        dammit = UnicodeDammit(markup, try_encodings, is_html=True)
        return (dammit.markup, dammit.original_encoding,
                dammit.declared_html_encoding,
                dammit.contains_replacement_characters)
コード例 #6
0
ファイル: _htmlparser.py プロジェクト: poutant1/InkuMail
    def prepare_markup(self,
                       markup,
                       user_specified_encoding=None,
                       document_declared_encoding=None,
                       exclude_encodings=None):
        """
        :return: A 4-tuple (markup, original encoding, encoding
        declared within markup, whether any characters had to be
        replaced with REPLACEMENT CHARACTER).
        """
        if isinstance(markup, str):
            yield (markup, None, None, False)
            return

        try_encodings = [user_specified_encoding, document_declared_encoding]
        dammit = UnicodeDammit(markup,
                               try_encodings,
                               is_html=True,
                               exclude_encodings=exclude_encodings)
        yield (dammit.markup, dammit.original_encoding,
               dammit.declared_html_encoding,
               dammit.contains_replacement_characters)
コード例 #7
0
ファイル: test_soup.py プロジェクト: poutant1/InkuMail
    def test_detwingle(self):
        # Here's a UTF8 document.
        utf8 = ("\N{SNOWMAN}" * 3).encode("utf8")

        # Here's a Windows-1252 document.
        windows_1252 = (
            "\N{LEFT DOUBLE QUOTATION MARK}Hi, I like Windows!"
            "\N{RIGHT DOUBLE QUOTATION MARK}").encode("windows_1252")

        # Through some unholy alchemy, they've been stuck together.
        doc = utf8 + windows_1252 + utf8

        # The document can't be turned into UTF-8:
        self.assertRaises(UnicodeDecodeError, doc.decode, "utf8")

        # Unicode, Dammit thinks the whole document is Windows-1252,
        # and decodes it into "☃☃☃“Hi, I like Windows!”☃☃☃"

        # But if we run it through fix_embedded_windows_1252, it's fixed:

        fixed = UnicodeDammit.detwingle(doc)
        self.assertEqual("☃☃☃“Hi, I like Windows!”☃☃☃", fixed.decode("utf8"))
コード例 #8
0
ファイル: test_soup.py プロジェクト: poutant1/InkuMail
    def test_last_ditch_entity_replacement(self):
        # This is a UTF-8 document that contains bytestrings
        # completely incompatible with UTF-8 (ie. encoded with some other
        # encoding).
        #
        # Since there is no consistent encoding for the document,
        # Unicode, Dammit will eventually encode the document as UTF-8
        # and encode the incompatible characters as REPLACEMENT
        # CHARACTER.
        #
        # If chardet is installed, it will detect that the document
        # can be converted into ISO-8859-1 without errors. This happens
        # to be the wrong encoding, but it is a consistent encoding, so the
        # code we're testing here won't run.
        #
        # So we temporarily disable chardet if it's present.
        doc = b"""\357\273\277<?xml version="1.0" encoding="UTF-8"?>
<html><b>\330\250\330\252\330\261</b>
<i>\310\322\321\220\312\321\355\344</i></html>"""
        chardet = bs4.dammit.chardet
        try:
            bs4.dammit.chardet = None
            with warnings.catch_warnings(record=True) as w:
                dammit = UnicodeDammit(doc)
                self.assertEqual(True, dammit.contains_replacement_characters)
                self.assertTrue("\ufffd" in dammit.unicode_markup)

                soup = BeautifulSoup(doc, "html.parser")
                self.assertTrue(soup.contains_replacement_characters)

                msg = w[0].message
                self.assertTrue(isinstance(msg, UnicodeWarning))
                self.assertTrue(
                    "Some characters could not be decoded" in str(msg))
        finally:
            bs4.dammit.chardet = chardet
コード例 #9
0
ファイル: test_soup.py プロジェクト: poutant1/InkuMail
 def test_ignore_inappropriate_codecs(self):
     utf8_data = "Räksmörgås".encode("utf-8")
     dammit = UnicodeDammit(utf8_data, ["iso-8859-8"])
     self.assertEqual(dammit.original_encoding, 'utf-8')
コード例 #10
0
ファイル: test_soup.py プロジェクト: poutant1/InkuMail
 def test_smart_quotes_to_xml_entities(self):
     markup = b"<foo>\x91\x92\x93\x94</foo>"
     dammit = UnicodeDammit(markup, smart_quotes_to="xml")
     self.assertEqual(dammit.unicode_markup,
                      "<foo>&#x2018;&#x2019;&#x201C;&#x201D;</foo>")
コード例 #11
0
ファイル: test_soup.py プロジェクト: poutant1/InkuMail
 def test_smart_quotes_to_html_entities(self):
     markup = b"<foo>\x91\x92\x93\x94</foo>"
     dammit = UnicodeDammit(markup, smart_quotes_to="html")
     self.assertEqual(dammit.unicode_markup,
                      "<foo>&lsquo;&rsquo;&ldquo;&rdquo;</foo>")
コード例 #12
0
ファイル: test_soup.py プロジェクト: poutant1/InkuMail
 def test_smart_quotes_to_ascii(self):
     markup = b"<foo>\x91\x92\x93\x94</foo>"
     dammit = UnicodeDammit(markup, smart_quotes_to="ascii")
     self.assertEqual(dammit.unicode_markup, """<foo>''""</foo>""")
コード例 #13
0
ファイル: test_soup.py プロジェクト: poutant1/InkuMail
 def test_detect_utf8(self):
     utf8 = b"\xc3\xa9"
     dammit = UnicodeDammit(utf8)
     self.assertEqual(dammit.unicode_markup, '\xe9')
     self.assertEqual(dammit.original_encoding, 'utf-8')
コード例 #14
0
ファイル: test_soup.py プロジェクト: poutant1/InkuMail
 def test_ignore_invalid_codecs(self):
     utf8_data = "Räksmörgås".encode("utf-8")
     for bad_encoding in ['.utf8', '...', 'utF---16.!']:
         dammit = UnicodeDammit(utf8_data, [bad_encoding])
         self.assertEqual(dammit.original_encoding, 'utf-8')
コード例 #15
0
ファイル: test_soup.py プロジェクト: poutant1/InkuMail
 def test_smart_quotes_to_unicode(self):
     markup = b"<foo>\x91\x92\x93\x94</foo>"
     dammit = UnicodeDammit(markup)
     self.assertEqual(dammit.unicode_markup,
                      "<foo>\u2018\u2019\u201c\u201d</foo>")
コード例 #16
0
ファイル: test_soup.py プロジェクト: poutant1/InkuMail
 def test_dont_see_smart_quotes_where_there_are_none(self):
     utf_8 = b"\343\202\261\343\203\274\343\202\277\343\202\244 Watch"
     dammit = UnicodeDammit(utf_8)
     self.assertEqual(dammit.original_encoding, 'utf-8')
     self.assertEqual(dammit.unicode_markup.encode("utf-8"), utf_8)
コード例 #17
0
ファイル: test_soup.py プロジェクト: poutant1/InkuMail
 def test_convert_hebrew(self):
     hebrew = b"\xed\xe5\xec\xf9"
     dammit = UnicodeDammit(hebrew, ["iso-8859-8"])
     self.assertEqual(dammit.original_encoding, 'iso-8859-8')
     self.assertEqual(dammit.unicode_markup, '\u05dd\u05d5\u05dc\u05e9')