Python codecs.EncodedFile() Examples

The following are 30 code examples of codecs.EncodedFile(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module codecs , or try the search function .
Example #1
Source File: test_codecs.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_all(self):
        api = (
            "encode", "decode",
            "register", "CodecInfo", "Codec", "IncrementalEncoder",
            "IncrementalDecoder", "StreamReader", "StreamWriter", "lookup",
            "getencoder", "getdecoder", "getincrementalencoder",
            "getincrementaldecoder", "getreader", "getwriter",
            "register_error", "lookup_error",
            "strict_errors", "replace_errors", "ignore_errors",
            "xmlcharrefreplace_errors", "backslashreplace_errors",
            "open", "EncodedFile",
            "iterencode", "iterdecode",
            "BOM", "BOM_BE", "BOM_LE",
            "BOM_UTF8", "BOM_UTF16", "BOM_UTF16_BE", "BOM_UTF16_LE",
            "BOM_UTF32", "BOM_UTF32_BE", "BOM_UTF32_LE",
            "BOM32_BE", "BOM32_LE", "BOM64_BE", "BOM64_LE",  # Undocumented
            "StreamReaderWriter", "StreamRecoder",
        )
        self.assertEqual(sorted(api), sorted(codecs.__all__))
        for api in codecs.__all__:
            getattr(codecs, api) 
Example #2
Source File: test_codecs.py    From ironpython3 with Apache License 2.0 6 votes vote down vote up
def test_all(self):
        api = (
            "encode", "decode",
            "register", "CodecInfo", "Codec", "IncrementalEncoder",
            "IncrementalDecoder", "StreamReader", "StreamWriter", "lookup",
            "getencoder", "getdecoder", "getincrementalencoder",
            "getincrementaldecoder", "getreader", "getwriter",
            "register_error", "lookup_error",
            "strict_errors", "replace_errors", "ignore_errors",
            "xmlcharrefreplace_errors", "backslashreplace_errors",
            "open", "EncodedFile",
            "iterencode", "iterdecode",
            "BOM", "BOM_BE", "BOM_LE",
            "BOM_UTF8", "BOM_UTF16", "BOM_UTF16_BE", "BOM_UTF16_LE",
            "BOM_UTF32", "BOM_UTF32_BE", "BOM_UTF32_LE",
            "BOM32_BE", "BOM32_LE", "BOM64_BE", "BOM64_LE",  # Undocumented
            "StreamReaderWriter", "StreamRecoder",
        )
        self.assertCountEqual(api, codecs.__all__)
        for api in codecs.__all__:
            getattr(codecs, api) 
Example #3
Source File: test_codecs.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def test_all(self):
        api = (
            "encode", "decode",
            "register", "CodecInfo", "Codec", "IncrementalEncoder",
            "IncrementalDecoder", "StreamReader", "StreamWriter", "lookup",
            "getencoder", "getdecoder", "getincrementalencoder",
            "getincrementaldecoder", "getreader", "getwriter",
            "register_error", "lookup_error",
            "strict_errors", "replace_errors", "ignore_errors",
            "xmlcharrefreplace_errors", "backslashreplace_errors",
            "namereplace_errors",
            "open", "EncodedFile",
            "iterencode", "iterdecode",
            "BOM", "BOM_BE", "BOM_LE",
            "BOM_UTF8", "BOM_UTF16", "BOM_UTF16_BE", "BOM_UTF16_LE",
            "BOM_UTF32", "BOM_UTF32_BE", "BOM_UTF32_LE",
            "BOM32_BE", "BOM32_LE", "BOM64_BE", "BOM64_LE",  # Undocumented
            "StreamReaderWriter", "StreamRecoder",
        )
        self.assertCountEqual(api, codecs.__all__)
        for api in codecs.__all__:
            getattr(codecs, api) 
Example #4
Source File: test_codecs.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 6 votes vote down vote up
def test_all(self):
        api = (
            "encode", "decode",
            "register", "CodecInfo", "Codec", "IncrementalEncoder",
            "IncrementalDecoder", "StreamReader", "StreamWriter", "lookup",
            "getencoder", "getdecoder", "getincrementalencoder",
            "getincrementaldecoder", "getreader", "getwriter",
            "register_error", "lookup_error",
            "strict_errors", "replace_errors", "ignore_errors",
            "xmlcharrefreplace_errors", "backslashreplace_errors",
            "namereplace_errors",
            "open", "EncodedFile",
            "iterencode", "iterdecode",
            "BOM", "BOM_BE", "BOM_LE",
            "BOM_UTF8", "BOM_UTF16", "BOM_UTF16_BE", "BOM_UTF16_LE",
            "BOM_UTF32", "BOM_UTF32_BE", "BOM_UTF32_LE",
            "BOM32_BE", "BOM32_LE", "BOM64_BE", "BOM64_LE",  # Undocumented
            "StreamReaderWriter", "StreamRecoder",
        )
        self.assertCountEqual(api, codecs.__all__)
        for api in codecs.__all__:
            getattr(codecs, api) 
Example #5
Source File: test_codecs.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_encodedfile(self):
        f = io.BytesIO(b"\xc3\xbc")
        with codecs.EncodedFile(f, "latin-1", "utf-8") as ef:
            self.assertEqual(ef.read(), b"\xfc")
        self.assertTrue(f.closed) 
Example #6
Source File: test.py    From pyRevit with GNU General Public License v3.0 5 votes vote down vote up
def test_decode_error(self):
        """Make sure the specified error-handling mode is obeyed on readers."""
        file = EncodedFile(BytesIO(u'Löwis,2,3'.encode('iso-8859-1')),
                           data_encoding='iso-8859-1')
        reader = csv.reader(file, encoding='ascii', errors='ignore')
        self.assertEqual(list(reader)[0][0], 'Lwis') 
Example #7
Source File: test.py    From pyRevit with GNU General Public License v3.0 5 votes vote down vote up
def test_decode_error_dictreader(self):
        """Make sure the error-handling mode is obeyed on DictReaders."""
        file = EncodedFile(BytesIO(u'name,height,weight\nLöwis,2,3'.encode('iso-8859-1')),
                           data_encoding='iso-8859-1')
        reader = csv.DictReader(file, encoding='ascii', errors='ignore')
        self.assertEqual(list(reader)[0]['name'], 'Lwis') 
Example #8
Source File: test_codecs.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_recoding(self):
        f = StringIO.StringIO()
        f2 = codecs.EncodedFile(f, "unicode_internal", "utf-8")
        f2.write(u"a")
        f2.close()
        # Python used to crash on this at exit because of a refcount
        # bug in _codecsmodule.c

# From RFC 3492 
Example #9
Source File: test_codecs.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_basic(self):
        f = StringIO.StringIO('\xed\x95\x9c\n\xea\xb8\x80')
        ef = codecs.EncodedFile(f, 'utf-16-le', 'utf-8')
        self.assertEqual(ef.read(), '\\\xd5\n\x00\x00\xae')

        f = StringIO.StringIO()
        ef = codecs.EncodedFile(f, 'utf-8', 'latin1')
        ef.write('\xc3\xbc')
        self.assertEqual(f.getvalue(), '\xfc') 
Example #10
Source File: test_codecs.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_encodedfile(self):
        f = StringIO.StringIO("\xc3\xbc")
        with codecs.EncodedFile(f, "latin-1", "utf-8") as ef:
            self.assertEqual(ef.read(), "\xfc") 
Example #11
Source File: test_codecs.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_recoding(self):
        f = io.BytesIO()
        f2 = codecs.EncodedFile(f, "unicode_internal", "utf-8")
        f2.write("a")
        f2.close()
        # Python used to crash on this at exit because of a refcount
        # bug in _codecsmodule.c

        self.assertTrue(f.closed)

# From RFC 3492 
Example #12
Source File: test_codecs.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_basic(self):
        f = io.BytesIO(b'\xed\x95\x9c\n\xea\xb8\x80')
        ef = codecs.EncodedFile(f, 'utf-16-le', 'utf-8')
        self.assertEqual(ef.read(), b'\\\xd5\n\x00\x00\xae')

        f = io.BytesIO()
        ef = codecs.EncodedFile(f, 'utf-8', 'latin-1')
        ef.write(b'\xc3\xbc')
        self.assertEqual(f.getvalue(), b'\xfc') 
Example #13
Source File: test_codecs.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_encodedfile(self):
        f = io.BytesIO(b"\xc3\xbc")
        with codecs.EncodedFile(f, "latin-1", "utf-8") as ef:
            self.assertEqual(ef.read(), b"\xfc")
        self.assertTrue(f.closed) 
Example #14
Source File: test_codecs.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def test_recoding(self):
        f = StringIO.StringIO()
        f2 = codecs.EncodedFile(f, "unicode_internal", "utf-8")
        f2.write(u"a")
        f2.close()
        # Python used to crash on this at exit because of a refcount
        # bug in _codecsmodule.c

# From RFC 3492 
Example #15
Source File: test_codecs.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_recoding(self):
        f = StringIO.StringIO()
        f2 = codecs.EncodedFile(f, "unicode_internal", "utf-8")
        # f2.write(u"a")
        # Must be bytes in Jython (and probably should have been in CPython)
        f2.write(b"\x00\x00\x00\x61")
        f2.close()
        # Python used to crash on this at exit because of a refcount
        # bug in _codecsmodule.c

# From RFC 3492 
Example #16
Source File: test_codecs.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_basic(self):
        f = StringIO.StringIO('\xed\x95\x9c\n\xea\xb8\x80')
        ef = codecs.EncodedFile(f, 'utf-16-le', 'utf-8')
        self.assertEqual(ef.read(), '\\\xd5\n\x00\x00\xae')

        f = StringIO.StringIO()
        ef = codecs.EncodedFile(f, 'utf-8', 'latin1')
        ef.write('\xc3\xbc')
        self.assertEqual(f.getvalue(), '\xfc') 
Example #17
Source File: test_codecs.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_encodedfile(self):
        f = StringIO.StringIO("\xc3\xbc")
        with codecs.EncodedFile(f, "latin-1", "utf-8") as ef:
            self.assertEqual(ef.read(), "\xfc") 
Example #18
Source File: tree.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _parse_fits(self, iterator, extnum, config):
        for start, tag, data, pos in iterator:
            if tag == 'STREAM':
                if start:
                    warn_unknown_attrs(
                        'STREAM', data.keys(), config, pos,
                        ['type', 'href', 'actuate', 'encoding', 'expires',
                         'rights'])
                    href = data['href']
                    encoding = data.get('encoding', None)
                else:
                    break

        if not href.startswith(('http', 'ftp', 'file')):
            vo_raise(
                "The vo package only supports remote data through http, "
                "ftp or file",
                self._config, self._pos, NotImplementedError)

        fd = urllib.request.urlopen(href)
        if encoding is not None:
            if encoding == 'gzip':
                fd = gzip.GzipFile(href, 'r', fileobj=fd)
            elif encoding == 'base64':
                fd = codecs.EncodedFile(fd, 'base64')
            else:
                vo_raise(
                    f"Unknown encoding type '{encoding}'",
                    self._config, self._pos, NotImplementedError)

        hdulist = fits.open(fd)

        array = hdulist[int(extnum)].data
        if array.dtype != self.array.dtype:
            warn_or_raise(W19, W19, (), self._config, self._pos)

        return array 
Example #19
Source File: test_codecs.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_recoding(self):
        f = StringIO.StringIO()
        f2 = codecs.EncodedFile(f, "unicode_internal", "utf-8")
        # f2.write(u"a")
        # Must be bytes in Jython (and probably should have been in CPython)
        f2.write(b"\x00\x00\x00\x61")
        f2.close()
        # Python used to crash on this at exit because of a refcount
        # bug in _codecsmodule.c

# From RFC 3492 
Example #20
Source File: test_codecs.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_basic(self):
        f = StringIO.StringIO('\xed\x95\x9c\n\xea\xb8\x80')
        ef = codecs.EncodedFile(f, 'utf-16-le', 'utf-8')
        self.assertEqual(ef.read(), '\\\xd5\n\x00\x00\xae')

        f = StringIO.StringIO()
        ef = codecs.EncodedFile(f, 'utf-8', 'latin1')
        ef.write('\xc3\xbc')
        self.assertEqual(f.getvalue(), '\xfc') 
Example #21
Source File: test_codecs.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_encodedfile(self):
        f = StringIO.StringIO("\xc3\xbc")
        with codecs.EncodedFile(f, "latin-1", "utf-8") as ef:
            self.assertEqual(ef.read(), "\xfc") 
Example #22
Source File: test_codecs.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_recoding(self):
        f = StringIO.StringIO()
        f2 = codecs.EncodedFile(f, "unicode_internal", "utf-8")
        f2.write(u"a")
        f2.close()
        # Python used to crash on this at exit because of a refcount
        # bug in _codecsmodule.c

# From RFC 3492 
Example #23
Source File: svgfig.py    From earthengine with MIT License 5 votes vote down vote up
def save(self, fileName=None, encoding="utf-8", compresslevel=None):
    """Save to a file for viewing.  Note that svg.save() overwrites the file named _default_fileName.

    fileName        default=None            note that _default_fileName will be overwritten if
                                            no fileName is specified. If the extension
                                            is ".svgz" or ".gz", the output will be gzipped
    encoding        default="utf-8"       file encoding (default is Unicode)
    compresslevel   default=None            if a number, the output will be gzipped with that
                                            compression level (1-9, 1 being fastest and 9 most
                                            thorough)
    """
    fileName = self.interpret_fileName(fileName)

    if compresslevel != None or re.search("\.svgz$", fileName, re.I) or re.search("\.gz$", fileName, re.I):
      import gzip
      if compresslevel == None:
        f = gzip.GzipFile(fileName, "w")
      else:
        f = gzip.GzipFile(fileName, "w", compresslevel)

      f = codecs.EncodedFile(f, "utf-8", encoding)
      f.write(self.standalone_xml())
      f.close()

    else:
      f = codecs.open(fileName, "w", encoding=encoding)
      f.write(self.standalone_xml())
      f.close() 
Example #24
Source File: test_codecs.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_recoding(self):
        f = StringIO.StringIO()
        f2 = codecs.EncodedFile(f, "unicode_internal", "utf-8")
        f2.write(u"a")
        f2.close()
        # Python used to crash on this at exit because of a refcount
        # bug in _codecsmodule.c

# From RFC 3492 
Example #25
Source File: test_codecs.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_basic(self):
        f = StringIO.StringIO('\xed\x95\x9c\n\xea\xb8\x80')
        ef = codecs.EncodedFile(f, 'utf-16-le', 'utf-8')
        self.assertEqual(ef.read(), '\\\xd5\n\x00\x00\xae')

        f = StringIO.StringIO()
        ef = codecs.EncodedFile(f, 'utf-8', 'latin1')
        ef.write('\xc3\xbc')
        self.assertEqual(f.getvalue(), '\xfc') 
Example #26
Source File: test_codecs.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_encodedfile(self):
        f = StringIO.StringIO("\xc3\xbc")
        with codecs.EncodedFile(f, "latin-1", "utf-8") as ef:
            self.assertEqual(ef.read(), "\xfc") 
Example #27
Source File: test_codecs.py    From BinderFilter with MIT License 5 votes vote down vote up
def test_recoding(self):
        f = StringIO.StringIO()
        f2 = codecs.EncodedFile(f, "unicode_internal", "utf-8")
        f2.write(u"a")
        f2.close()
        # Python used to crash on this at exit because of a refcount
        # bug in _codecsmodule.c

# From RFC 3492 
Example #28
Source File: test_codecs.py    From BinderFilter with MIT License 5 votes vote down vote up
def test_basic(self):
        f = StringIO.StringIO('\xed\x95\x9c\n\xea\xb8\x80')
        ef = codecs.EncodedFile(f, 'utf-16-le', 'utf-8')
        self.assertEqual(ef.read(), '\\\xd5\n\x00\x00\xae')

        f = StringIO.StringIO()
        ef = codecs.EncodedFile(f, 'utf-8', 'latin1')
        ef.write('\xc3\xbc')
        self.assertEqual(f.getvalue(), '\xfc') 
Example #29
Source File: test_codecs.py    From BinderFilter with MIT License 5 votes vote down vote up
def test_encodedfile(self):
        f = StringIO.StringIO("\xc3\xbc")
        with codecs.EncodedFile(f, "latin-1", "utf-8") as ef:
            self.assertEqual(ef.read(), "\xfc") 
Example #30
Source File: test.py    From pyRevit with GNU General Public License v3.0 5 votes vote down vote up
def test_unicode_read(self):
        f = EncodedFile(BytesIO((u"Martin von Löwis,"
                                 u"Marc André Lemburg,"
                                 u"Guido van Rossum,"
                                 u"François Pinard\r\n").encode('iso-8859-1')),
                        data_encoding='iso-8859-1')
        reader = csv.reader(f, encoding='iso-8859-1')
        self.assertEqual(list(reader), [[u"Martin von Löwis",
                                         u"Marc André Lemburg",
                                         u"Guido van Rossum",
                                         u"François Pinard"]])