Example #1
0
 def test_bad_types(self):
     bytes_literal = (b'col1,col2\n' b'1,a\n' b'2,b\n' b'3,c\n')
     if PY2:
         bytes_stream = io.BytesIO(bytes_literal)
         text_stream = io.TextIOWrapper(bytes_stream, encoding='ascii')
         with self.assertRaises(TypeError):
             reader = _from_csv_iterable(text_stream, 'ascii')
     else:
         bytes_stream = io.BytesIO(bytes_literal)
         with self.assertRaises((csv.Error, TypeError)):
             reader = _from_csv_iterable(bytes_stream, 'ascii')
             list(reader)  # Trigger evaluation.
Example #2
0
    def test_utf8(self):
        stream = self.get_stream(
            (
                b'col1,col2\n'
                b'1,\xce\xb1\n'  # '\xce\xb1'         -> α (Greek alpha)
                b'2,\xe0\xa5\x90\n'  # '\xe0\xa5\x90'     -> ॐ (Devanagari Om)
                b'3,\xf0\x9d\x94\xb8\n'  # '\xf0\x9d\x94\xb8' -> 𝔸 (mathematical double-struck A)
            ),
            encoding='utf-8')

        reader = _from_csv_iterable(stream, encoding='utf-8')

        try:
            double_struck_a = chr(0x1d538)  # 𝔸
        except ValueError:
            double_struck_a = chr(0xd835) + chr(0xdd38)  # 𝔸
            # Above, we use a "surrogate pair" to support older
            # "narrow" (2-byte character) builds of Python.

        expected = [
            ['col1', 'col2'],
            ['1', chr(0x003b1)],  # α
            ['2', chr(0x00950)],  # ॐ
            ['3', double_struck_a],  # 𝔸
        ]
        self.assertEqual(list(reader), expected)
Example #3
0
    def test_ascii(self):
        stream = self.get_stream((b'col1,col2\n'
                                  b'1,a\n'
                                  b'2,b\n'
                                  b'3,c\n'),
                                 encoding='ascii')

        reader = _from_csv_iterable(stream, encoding='ascii')
        expected = [
            ['col1', 'col2'],
            ['1', 'a'],
            ['2', 'b'],
            ['3', 'c'],
        ]
        self.assertEqual(list(reader), expected)
Example #4
0
    def test_fmtparams(self):
        stream = self.get_stream((b'|col1| |col2|\n'
                                  b'|1| |a|\n'
                                  b'|2| |b|\n'
                                  b'|3| |c|\n'),
                                 encoding='utf-8')

        fmtparams = {'delimiter': ' ', 'quotechar': '|'}
        reader = _from_csv_iterable(stream, encoding='utf-8', **fmtparams)
        expected = [
            ['col1', 'col2'],
            ['1', 'a'],
            ['2', 'b'],
            ['3', 'c'],
        ]
        self.assertEqual(list(reader), expected)
Example #5
0
    def test_iso88591(self):
        stream = self.get_stream(
            (
                b'col1,col2\n'
                b'1,\xe6\n'  # '\xe6' -> æ (ash)
                b'2,\xf0\n'  # '\xf0' -> ð (eth)
                b'3,\xfe\n'  # '\xfe' -> þ (thorn)
            ),
            encoding='iso8859-1')

        reader = _from_csv_iterable(stream, encoding='iso8859-1')
        expected = [
            ['col1', 'col2'],
            ['1', chr(0xe6)],  # chr(0xe6) -> æ
            ['2', chr(0xf0)],  # chr(0xf0) -> ð
            ['3', chr(0xfe)],  # chr(0xfe) -> þ
        ]
        self.assertEqual(list(reader), expected)
Example #6
0
 def test_empty_file(self):
     stream = self.get_stream(b'', encoding='ascii')
     reader = _from_csv_iterable(stream, encoding='ascii')
     expected = []
     self.assertEqual(list(reader), expected)