Python codecs.decode() Examples

The following are 30 code examples of codecs.decode(). 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: formparser.py    From lambda-packs with MIT License 6 votes vote down vote up
def __init__(self, stream_factory=None, charset='utf-8', errors='replace',
                 max_form_memory_size=None, cls=None, buffer_size=64 * 1024):
        self.charset = charset
        self.errors = errors
        self.max_form_memory_size = max_form_memory_size
        self.stream_factory = default_stream_factory if stream_factory is None else stream_factory
        self.cls = MultiDict if cls is None else cls

        # make sure the buffer size is divisible by four so that we can base64
        # decode chunk by chunk
        assert buffer_size % 4 == 0, 'buffer size has to be divisible by 4'
        # also the buffer size has to be at least 1024 bytes long or long headers
        # will freak out the system
        assert buffer_size >= 1024, 'buffer size has to be at least 1KB'

        self.buffer_size = buffer_size 
Example #2
Source File: formparser.py    From recruit with Apache License 2.0 6 votes vote down vote up
def __init__(
        self,
        stream_factory=None,
        charset="utf-8",
        errors="replace",
        max_form_memory_size=None,
        cls=None,
        buffer_size=64 * 1024,
    ):
        self.charset = charset
        self.errors = errors
        self.max_form_memory_size = max_form_memory_size
        self.stream_factory = (
            default_stream_factory if stream_factory is None else stream_factory
        )
        self.cls = MultiDict if cls is None else cls

        # make sure the buffer size is divisible by four so that we can base64
        # decode chunk by chunk
        assert buffer_size % 4 == 0, "buffer size has to be divisible by 4"
        # also the buffer size has to be at least 1024 bytes long or long headers
        # will freak out the system
        assert buffer_size >= 1024, "buffer size has to be at least 1KB"

        self.buffer_size = buffer_size 
Example #3
Source File: repr.py    From recruit with Apache License 2.0 6 votes vote down vote up
def __call__(self, topic=None):
        if topic is None:
            sys.stdout._write("<span class=help>%s</span>" % repr(self))
            return
        import pydoc

        pydoc.help(topic)
        rv = sys.stdout.reset()
        if isinstance(rv, bytes):
            rv = rv.decode("utf-8", "ignore")
        paragraphs = _paragraph_re.split(rv)
        if len(paragraphs) > 1:
            title = paragraphs[0]
            text = "\n\n".join(paragraphs[1:])
        else:  # pragma: no cover
            title = "Help"
            text = paragraphs[0]
        sys.stdout._write(HELP_HTML % {"title": title, "text": text}) 
Example #4
Source File: repr.py    From jbox with MIT License 6 votes vote down vote up
def __call__(self, topic=None):
        if topic is None:
            sys.stdout._write('<span class=help>%s</span>' % repr(self))
            return
        import pydoc
        pydoc.help(topic)
        rv = sys.stdout.reset()
        if isinstance(rv, bytes):
            rv = rv.decode('utf-8', 'ignore')
        paragraphs = _paragraph_re.split(rv)
        if len(paragraphs) > 1:
            title = paragraphs[0]
            text = '\n\n'.join(paragraphs[1:])
        else:  # pragma: no cover
            title = 'Help'
            text = paragraphs[0]
        sys.stdout._write(HELP_HTML % {'title': title, 'text': text}) 
Example #5
Source File: formparser.py    From jbox with MIT License 6 votes vote down vote up
def __init__(self, stream_factory=None, charset='utf-8', errors='replace',
                 max_form_memory_size=None, cls=None, buffer_size=64 * 1024):
        self.stream_factory = stream_factory
        self.charset = charset
        self.errors = errors
        self.max_form_memory_size = max_form_memory_size
        if stream_factory is None:
            stream_factory = default_stream_factory
        if cls is None:
            cls = MultiDict
        self.cls = cls

        # make sure the buffer size is divisible by four so that we can base64
        # decode chunk by chunk
        assert buffer_size % 4 == 0, 'buffer size has to be divisible by 4'
        # also the buffer size has to be at least 1024 bytes long or long headers
        # will freak out the system
        assert buffer_size >= 1024, 'buffer size has to be at least 1KB'

        self.buffer_size = buffer_size 
Example #6
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 #7
Source File: test_codecs.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_bug1251300(self):
        # Decoding with unicode_internal used to not correctly handle "code
        # points" above 0x10ffff on UCS-4 builds.
        if sys.maxunicode > 0xffff:
            ok = [
                ("\x00\x10\xff\xff", u"\U0010ffff"),
                ("\x00\x00\x01\x01", u"\U00000101"),
                ("", u""),
            ]
            not_ok = [
                "\x7f\xff\xff\xff",
                "\x80\x00\x00\x00",
                "\x81\x00\x00\x00",
                "\x00",
                "\x00\x00\x00\x00\x00",
            ]
            for internal, uni in ok:
                if sys.byteorder == "little":
                    internal = "".join(reversed(internal))
                self.assertEqual(uni, internal.decode("unicode_internal"))
            for internal in not_ok:
                if sys.byteorder == "little":
                    internal = "".join(reversed(internal))
                self.assertRaises(UnicodeDecodeError, internal.decode,
                    "unicode_internal") 
Example #8
Source File: test_crypto.py    From pyaff4 with Apache License 2.0 6 votes vote down vote up
def testDecrypt(self):

        g = rdflib.Graph()
        g.parse(data=keybagturtle, format="turtle")
        kb = keybag.PasswordWrappedKeyBag.load(g)

        key = "password"
        kek = digest.pbkdf2_hmac("sha256", key, kb.salt, kb.iterations, kb.keySizeBytes);
        vek = aes_unwrap_key(kek, kb.wrappedKey)

        key1 = vek[0:16]
        key2 = vek[16:]
        tweak = codecs.decode('00', 'hex')

        cipher = python_AES.new((key1, key2), python_AES.MODE_XTS)
        text = cipher.decrypt(target_ciphertext, tweak)

        self.assertEqual(src[0:len(src)], text[0:len(src)]) 
Example #9
Source File: test_codecs.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_lone_surrogates(self):
        tests = [
            ('a+2AE-b', u'a\ud801b'),
            ('a+2AE\xe1b', u'a\ufffdb'),
            ('a+2AE', u'a\ufffd'),
            ('a+2AEA-b', u'a\ufffdb'),
            ('a+2AH-b', u'a\ufffdb'),
            ('a+IKzYAQ-b', u'a\u20ac\ud801b'),
            ('a+IKzYAQ\xe1b', u'a\u20ac\ufffdb'),
            ('a+IKzYAQA-b', u'a\u20ac\ufffdb'),
            ('a+IKzYAd-b', u'a\u20ac\ufffdb'),
            ('a+IKwgrNgB-b', u'a\u20ac\u20ac\ud801b'),
            ('a+IKwgrNgB\xe1b', u'a\u20ac\u20ac\ufffdb'),
            ('a+IKwgrNgB', u'a\u20ac\u20ac\ufffd'),
            ('a+IKwgrNgBA-b', u'a\u20ac\u20ac\ufffdb'),
        ]
        for raw, expected in tests:
            try:
                self.assertEqual(raw.decode('utf-7', 'replace'), expected)
            except:
                print 'raw=%r' % raw
                raise 
Example #10
Source File: CAN-USB-stick.py    From openplotter with GNU General Public License v2.0 6 votes vote down vote up
def SendCommandtoSerial(self, TXs):
			crc = 0
			#start = codecs.decode('1002', 'hex_codec')
			start = (0x10, 0x02)
			ende = codecs.decode('1003', 'hex_codec')
			ende = (0x10, 0x03)
			i = 0
			while i < TXs[1] + 2:
				crc += TXs[i]
				i += 1
			crc = (256 - crc) & 255
			TXs[TXs[1] + 2] = crc
			i = 0
			self.ser.write(chr(start[0]))
			self.ser.write(chr(start[1]))
			while i < TXs[1] + 3:
				self.ser.write(chr(TXs[i]))
				#print format(TXs[i], '02x')
				if TXs[i] == 0x10:
					self.ser.write(chr(TXs[i]))
				i += 1
			self.ser.write(chr(ende[0]))
			self.ser.write(chr(ende[1])) 
Example #11
Source File: repr.py    From lambda-packs with MIT License 6 votes vote down vote up
def __call__(self, topic=None):
        if topic is None:
            sys.stdout._write('<span class=help>%s</span>' % repr(self))
            return
        import pydoc
        pydoc.help(topic)
        rv = sys.stdout.reset()
        if isinstance(rv, bytes):
            rv = rv.decode('utf-8', 'ignore')
        paragraphs = _paragraph_re.split(rv)
        if len(paragraphs) > 1:
            title = paragraphs[0]
            text = '\n\n'.join(paragraphs[1:])
        else:  # pragma: no cover
            title = 'Help'
            text = paragraphs[0]
        sys.stdout._write(HELP_HTML % {'title': title, 'text': text}) 
Example #12
Source File: test_codecs.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_ascii(self):
        # Set D (directly encoded characters)
        set_d = ('ABCDEFGHIJKLMNOPQRSTUVWXYZ'
                 'abcdefghijklmnopqrstuvwxyz'
                 '0123456789'
                 '\'(),-./:?')
        self.assertEqual(set_d.encode(self.encoding), set_d)
        self.assertEqual(set_d.decode(self.encoding), set_d)
        # Set O (optional direct characters)
        set_o = ' !"#$%&*;<=>@[]^_`{|}'
        self.assertEqual(set_o.encode(self.encoding), set_o)
        self.assertEqual(set_o.decode(self.encoding), set_o)
        # +
        self.assertEqual(u'a+b'.encode(self.encoding), 'a+-b')
        self.assertEqual('a+-b'.decode(self.encoding), u'a+b')
        # White spaces
        ws = ' \t\n\r'
        self.assertEqual(ws.encode(self.encoding), ws)
        self.assertEqual(ws.decode(self.encoding), ws)
        # Other ASCII characters
        other_ascii = ''.join(sorted(set(chr(i) for i in range(0x80)) -
                                     set(set_d + set_o + '+' + ws)))
        self.assertEqual(other_ascii.encode(self.encoding),
                         '+AAAAAQACAAMABAAFAAYABwAIAAsADAAOAA8AEAARABIAEwAU'
                         'ABUAFgAXABgAGQAaABsAHAAdAB4AHwBcAH4Afw-') 
Example #13
Source File: test_codecs.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_errors(self):
        tests = [
            (b'\xff', u'\ufffd'),
            (b'\x00A\xff', u'A\ufffd'),
            (b'\x00A\x00B\x00C\x00DZ', u'ABCD\ufffd'),
            (b'\xd8\x00', u'\ufffd'),
            (b'\xd8\x00\xdc', u'\ufffd'),
            (b'\xd8\x00\x00A', u'\ufffdA'),
            (b'\xdc\x00\x00A', u'\ufffdA'),
        ]
        for raw, expected in tests:
            try:
                with self.assertRaises(UnicodeDecodeError):
                    codecs.utf_16_be_decode(raw, 'strict', True)
                self.assertEqual(raw.decode('utf-16be', 'replace'), expected)
            except:
                print 'raw=%r' % raw
                raise 
Example #14
Source File: credstash.py    From credstash with Apache License 2.0 6 votes vote down vote up
def seal_aes_ctr_legacy(key_service, secret, digest_method=DEFAULT_DIGEST):
    """
    Encrypts `secret` using the key service.
    You can decrypt with the companion method `open_aes_ctr_legacy`.
    """
    # generate a a 64 byte key.
    # Half will be for data encryption, the other half for HMAC
    key, encoded_key = key_service.generate_key_data(64)
    ciphertext, hmac = _seal_aes_ctr(
        secret, key, LEGACY_NONCE, digest_method,
    )
    return {
        'key': b64encode(encoded_key).decode('utf-8'),
        'contents': b64encode(ciphertext).decode('utf-8'),
        'hmac': codecs.encode(hmac, "hex_codec"),
        'digest': digest_method,
    } 
Example #15
Source File: test_codecs.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_errors(self):
        tests = [
            (b'\xff', u'\ufffd'),
            (b'A\x00Z', u'A\ufffd'),
            (b'A\x00B\x00C\x00D\x00Z', u'ABCD\ufffd'),
            (b'\x00\xd8', u'\ufffd'),
            (b'\x00\xd8A', u'\ufffd'),
            (b'\x00\xd8A\x00', u'\ufffdA'),
            (b'\x00\xdcA\x00', u'\ufffdA'),
        ]
        for raw, expected in tests:
            try:
                with self.assertRaises(UnicodeDecodeError):
                    codecs.utf_16_le_decode(raw, 'strict', True)
                self.assertEqual(raw.decode('utf-16le', 'replace'), expected)
            except:
                print 'raw=%r' % raw
                raise 
Example #16
Source File: types.py    From IOTA_demo with MIT License 6 votes vote down vote up
def as_bytes(self, errors='strict', codec=AsciiTrytesCodec.name):
    # type: (Text, Text) -> binary_type
    """
    Converts the TryteString into a byte string.

    :param errors:
      How to handle trytes that can't be converted:
        - 'strict':   raise an exception (recommended).
        - 'replace':  replace with '?'.
        - 'ignore':   omit the tryte from the result.

    :param codec:
      Which codec to use:

      - 'binary': Converts each sequence of 5 trits into a byte with
        the same value (this is usually what you want).
      - 'ascii': Uses the legacy ASCII codec.

    :raise:
      - :py:class:`iota.codecs.TrytesDecodeError` if the trytes cannot
        be decoded into bytes.
    """
    # In Python 2, :py:func:`decode` does not accept keyword arguments.
    return decode(self._trytes, codec, errors) 
Example #17
Source File: test_codecs.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_decode(self):
        #sanity
        new_str = codecs.decode("abc")
        self.assertEqual(new_str, u'abc') 
Example #18
Source File: test_codecs.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_lookup_encodings(self):
        try:
            self.assertEqual('07FF'.decode('hex')  , '\x07\xff')
        except LookupError:
            # if we don't have encodings then this will fail so
            # make sure we're failing because we don't have encodings
            self.assertRaises(ImportError, __import__, 'encodings') 
Example #19
Source File: test_uu.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_decode(self):
        with open(self.tmpin, 'w') as f:
            f.write(encodedtextwrapped % (0o644, self.tmpout))

        with open(self.tmpin, 'r') as f:
            uu.decode(f)

        with open(self.tmpout, 'r') as f:
            s = f.read()
        self.assertEqual(s, plaintext)
        # XXX is there an xp way to verify the mode? 
Example #20
Source File: types.py    From IOTA_demo with MIT License 5 votes vote down vote up
def __bytes__(self):
    """
    Converts the TryteString into an ASCII representation.

    Note: This does not decode the trytes into bytes/characters; it
    only returns an ASCII representation of the trytes themselves!

    If you want to...
    - ... decode trytes into bytes: use :py:meth:`as_bytes`.
    - ... decode trytes into Unicode: use :py:meth:`as_string`.
    """
    return binary_type(self._trytes) 
Example #21
Source File: types.py    From IOTA_demo with MIT License 5 votes vote down vote up
def __str__(self):
    """
    Same as :py:meth:`__bytes__`, except this method returns a unicode
    string.
    """
    # This causes infinite recursion in Python 2.
    # return binary_type(self).decode('ascii')
    return binary_type(self._trytes).decode('ascii') 
Example #22
Source File: repr.py    From lambda-packs with MIT License 5 votes vote down vote up
def regex_repr(self, obj):
        pattern = repr(obj.pattern)
        if PY2:
            pattern = pattern.decode('string-escape', 'ignore')
        else:
            pattern = codecs.decode(pattern, 'unicode-escape', 'ignore')
        if pattern[:1] == 'u':
            pattern = 'ur' + pattern[1:]
        else:
            pattern = 'r' + pattern
        return u're.compile(<span class="string regex">%s</span>)' % pattern 
Example #23
Source File: gpo.py    From pywerview with GNU General Public License v3.0 5 votes vote down vote up
def get_gpttmpl(self, gpttmpl_path):
        content_io = StringIO()

        gpttmpl_path_split = gpttmpl_path.split('\\')
        target = self._domain_controller
        share = gpttmpl_path_split[3]
        file_name = '\\'.join(gpttmpl_path_split[4:])

        smb_connection = SMBConnection(remoteName=target, remoteHost=target)
        # TODO: kerberos login
        smb_connection.login(self._user, self._password, self._domain,
                             self._lmhash, self._nthash)

        smb_connection.connectTree(share)
        smb_connection.getFile(share, file_name, content_io.write)

        try:
            content = codecs.decode(content_io.getvalue(), 'utf_16_le')[1:].replace('\r', '')
        except UnicodeDecodeError:
            content = content_io.getvalue().replace('\r', '')

        gpttmpl_final = GptTmpl(list())
        for l in content.split('\n'):
            if l.startswith('['):
                section_name = l.strip('[]').replace(' ', '').lower()
                setattr(gpttmpl_final, section_name, Policy(list()))
            elif '=' in l:
                property_name, property_values = [x.strip() for x in l.split('=')]
                if ',' in property_values:
                    property_values = property_values.split(',')
                try:
                    setattr(getattr(gpttmpl_final, section_name), property_name, property_values)
                except UnicodeEncodeError:
                    property_name = property_name.encode('utf-8')
                    setattr(getattr(gpttmpl_final, section_name), property_name, property_values)

        return gpttmpl_final 
Example #24
Source File: test_uu.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_decodetwice(self):
        # Verify that decode() will refuse to overwrite an existing file
        with open(self.tmpin, 'wb') as f:
            f.write(encodedtextwrapped % (0o644, self.tmpout))
        with open(self.tmpin, 'r') as f:
            uu.decode(f)

        with open(self.tmpin, 'r') as f:
            self.assertRaises(uu.Error, uu.decode, f) 
Example #25
Source File: test_codecs.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_bom(self):
        d = codecs.getincrementaldecoder("utf-8-sig")()
        s = u"spam"
        self.assertEqual(d.decode(s.encode("utf-8-sig")), s) 
Example #26
Source File: test_codecs.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_raw(self):
        decode = codecs.escape_decode
        for b in range(256):
            b = chr(b)
            if b != '\\':
                self.assertEqual(decode(b + '0'), (b + '0', 2)) 
Example #27
Source File: test_uu.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_decode_filename(self):
        with open(self.tmpin, 'w') as f:
            f.write(encodedtextwrapped % (0o644, self.tmpout))

        uu.decode(self.tmpin)

        with open(self.tmpout, 'r') as f:
            s = f.read()
        self.assertEqual(s, plaintext) 
Example #28
Source File: test_codecs.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_escape(self):
        decode = codecs.escape_decode
        check = coding_checker(self, decode)
        check(b"[\\\n]", b"[]")
        check(br'[\"]', b'["]')
        check(br"[\']", b"[']")
        check(br"[\\]", br"[\]")
        check(br"[\a]", b"[\x07]")
        check(br"[\b]", b"[\x08]")
        check(br"[\t]", b"[\x09]")
        check(br"[\n]", b"[\x0a]")
        check(br"[\v]", b"[\x0b]")
        check(br"[\f]", b"[\x0c]")
        check(br"[\r]", b"[\x0d]")
        check(br"[\7]", b"[\x07]")
        check(br"[\8]", br"[\8]")
        check(br"[\78]", b"[\x078]")
        check(br"[\41]", b"[!]")
        check(br"[\418]", b"[!8]")
        check(br"[\101]", b"[A]")
        check(br"[\1010]", b"[A0]")
        check(br"[\501]", b"[A]")
        check(br"[\x41]", b"[A]")
        check(br"[\X41]", br"[\X41]")
        check(br"[\x410]", b"[A0]")
        for b in range(256):
            b = chr(b)
            if b not in '\n"\'\\abtnvfr01234567x':
                check('\\' + b, '\\' + b) 
Example #29
Source File: test_codecs.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_errors(self):
        decode = codecs.escape_decode
        self.assertRaises(ValueError, decode, br"\x")
        self.assertRaises(ValueError, decode, br"[\x]")
        self.assertEqual(decode(br"[\x]\x", "ignore"), (b"[]", 6))
        self.assertEqual(decode(br"[\x]\x", "replace"), (b"[?]?", 6))
        self.assertRaises(ValueError, decode, br"\x0")
        self.assertRaises(ValueError, decode, br"[\x0]")
        self.assertEqual(decode(br"[\x0]\x0", "ignore"), (b"[]", 8))
        self.assertEqual(decode(br"[\x0]\x0", "replace"), (b"[?]?", 8)) 
Example #30
Source File: test_codecs.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_errors(self):
        tests = [
            ('\xe1b', u'\ufffdb'),
            ('a\xe1b', u'a\ufffdb'),
            ('a\xe1\xe1b', u'a\ufffd\ufffdb'),
            ('a+IK', u'a\ufffd'),
            ('a+IK-b', u'a\ufffdb'),
            ('a+IK,b', u'a\ufffdb'),
            ('a+IKx', u'a\u20ac\ufffd'),
            ('a+IKx-b', u'a\u20ac\ufffdb'),
            ('a+IKwgr', u'a\u20ac\ufffd'),
            ('a+IKwgr-b', u'a\u20ac\ufffdb'),
            ('a+IKwgr,', u'a\u20ac\ufffd'),
            ('a+IKwgr,-b', u'a\u20ac\ufffd-b'),
            ('a+IKwgrB', u'a\u20ac\u20ac\ufffd'),
            ('a+IKwgrB-b', u'a\u20ac\u20ac\ufffdb'),
            ('a+/,+IKw-b', u'a\ufffd\u20acb'),
            ('a+//,+IKw-b', u'a\ufffd\u20acb'),
            ('a+///,+IKw-b', u'a\uffff\ufffd\u20acb'),
            ('a+////,+IKw-b', u'a\uffff\ufffd\u20acb'),
            ('a+IKw-b\xe1', u'a\u20acb\ufffd'),
            ('a+IKw\xe1b', u'a\u20ac\ufffdb'),
        ]
        for raw, expected in tests:
            try:
                with self.assertRaises(UnicodeDecodeError):
                    codecs.utf_7_decode(raw, 'strict', True)
                self.assertEqual(raw.decode('utf-7', 'replace'), expected)
            except:
                print 'raw=%r' % raw
                raise