Python codecs.escape_encode() Examples

The following are 30 code examples of codecs.escape_encode(). 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 Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 6 votes vote down vote up
def test_escape_encode(self):
        tests = [
            (b'', (b'', 0)),
            (b'foobar', (b'foobar', 6)),
            (b'spam\0eggs', (b'spam\\x00eggs', 9)),
            (b'a\'b', (b"a\\'b", 3)),
            (b'b\\c', (b'b\\\\c', 3)),
            (b'c\nd', (b'c\\nd', 3)),
            (b'd\re', (b'd\\re', 3)),
            (b'f\x7fg', (b'f\\x7fg', 3)),
        ]
        for data, output in tests:
            with self.subTest(data=data):
                self.assertEqual(codecs.escape_encode(data), output)
        self.assertRaises(TypeError, codecs.escape_encode, 'spam')
        self.assertRaises(TypeError, codecs.escape_encode, bytearray(b'spam')) 
Example #2
Source File: string_escape.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0] 
Example #3
Source File: fields.py    From gramfuzz with MIT License 5 votes vote down vote up
def build(self, pre=None, shortest=False):
        """Build the ``Quote`` instance

        :param list pre: The prerequisites list
        :param bool shortest: Whether or not the shortest reference-chain (most minimal) version of the field should be generated.
        """
        res = super(Q, self).build(pre, shortest=shortest)

        if self.escape:
            return self._repr_escape(res)
        elif self.html_js_escape:
            res, _ = codecs.escape_encode(res)
            return (b"'" + res.replace(b"<", b"\\x3c").replace(b">", b"\\x3e") + b"'")
        else:
            return b"".join([self.quote, res, self.quote]) 
Example #4
Source File: string_escape.py    From datafari with Apache License 2.0 5 votes vote down vote up
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0] 
Example #5
Source File: string_escape.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0] 
Example #6
Source File: string_escape.py    From PhonePi_SampleServer with MIT License 5 votes vote down vote up
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0] 
Example #7
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_encode_length(self):
        with support.check_warnings(('unicode_internal codec has been '
                                     'deprecated', DeprecationWarning)):
            # Issue 3739
            encoder = codecs.getencoder("unicode_internal")
            self.assertEqual(encoder("a")[1], 1)
            self.assertEqual(encoder("\xe9\u0142")[1], 2)

            self.assertEqual(codecs.escape_encode(br'\x00')[1], 4)

# From http://www.gnu.org/software/libidn/draft-josefsson-idn-test-vectors.html 
Example #8
Source File: string_escape.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0] 
Example #9
Source File: string_escape.py    From syntheticmass with Apache License 2.0 5 votes vote down vote up
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0] 
Example #10
Source File: string_escape.py    From ImageFusion with MIT License 5 votes vote down vote up
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0] 
Example #11
Source File: string_escape.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0] 
Example #12
Source File: string_escape.py    From RevitBatchProcessor with GNU General Public License v3.0 5 votes vote down vote up
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0] 
Example #13
Source File: string_escape.py    From PokemonGo-DesktopMap with MIT License 5 votes vote down vote up
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0] 
Example #14
Source File: string_escape.py    From unity-python with MIT License 5 votes vote down vote up
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0] 
Example #15
Source File: string_escape.py    From canape with GNU General Public License v3.0 5 votes vote down vote up
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0] 
Example #16
Source File: string_escape.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0] 
Example #17
Source File: string_escape.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0] 
Example #18
Source File: string_escape.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0] 
Example #19
Source File: testcase_mutation.py    From httpninja with Apache License 2.0 5 votes vote down vote up
def asciiHex(HTTPRequestHolderObjs):
    result = []
    for HTTPRequestHolderObj in HTTPRequestHolderObjs:
        initHTTPReq = HTTPRequestHolderObj.rawHTTPRequest
        initAdditionalInfo = HTTPRequestHolderObj.additionalInfo
        for c in range(0, 256):
            asciiChar = chr(c)
            asciiHex = "{:02x}".format(c)
            additionalInfo = 'ASCII [Code: {} - Char: {} - Hex: {}]'.format(c, codecs.escape_encode(asciiChar), asciiHex)
            if initAdditionalInfo != '':
                additionalInfo = initAdditionalInfo + ' - ' + additionalInfo
            HTTPRequestHolderObjTemp = HTTPRequestHolder.HTTPRequestHolder(Template(initHTTPReq).safe_substitute(asciiHex=asciiHex),additionalInfo)
            result.append(HTTPRequestHolderObjTemp)
    return result 
Example #20
Source File: testcase_mutation.py    From httpninja with Apache License 2.0 5 votes vote down vote up
def unicodeChar(HTTPRequestHolderObjs):
    result = []
    for HTTPRequestHolderObj in HTTPRequestHolderObjs:
        initHTTPReq = HTTPRequestHolderObj.rawHTTPRequest
        initAdditionalInfo = HTTPRequestHolderObj.additionalInfo
        for u in range(0, 65535):
            unicodeChar = int2bytes(u)
            additionalInfo = 'UNICODE [Code: {} - Char: {}]'.format(u, codecs.escape_encode(unicodeChar))
            if initAdditionalInfo != '':
                additionalInfo = initAdditionalInfo + ' - ' + additionalInfo
            HTTPRequestHolderObjTemp = HTTPRequestHolder.HTTPRequestHolder(Template(initHTTPReq).safe_substitute(asciiChar=asciiChar),additionalInfo)
            result.append(HTTPRequestHolderObjTemp)
    return result 
Example #21
Source File: string_escape.py    From meddle with MIT License 5 votes vote down vote up
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0] 
Example #22
Source File: string_escape.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0] 
Example #23
Source File: test_codecs.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_escape_encode(self):
        #sanity checks
        value, length = codecs.escape_encode("abba")
        self.assertEqual(value, "abba")
        self.assertEqual(length, 4)

        value, length = codecs.escape_encode("ab\a\b\t\n\r\f\vba")
        self.assertEqual(value, 'ab\\x07\\x08\\t\\n\\r\\x0c\\x0bba')
        if is_cli: #http://ironpython.codeplex.com/workitem/27899
            self.assertEqual(length, 26)
        else:
            self.assertEqual(length, 11)

        value, length = codecs.escape_encode("\\a")
        self.assertEqual(value, "\\\\a")
        if is_cli: #http://ironpython.codeplex.com/workitem/27899
            self.assertEqual(length, 3)
        else:
            self.assertEqual(length, 2)
        
        l = []
        for i in xrange(256):
            l.append(chr(i))
            
        value, length = codecs.escape_encode(''.join(l))
        self.assertEqual(value, '\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b\\x0c\\r\\x0e\\x0f\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a\\x1b\\x1c\\x1d\\x1e\\x1f !"#$%&\\\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\\x7f\\x80\\x81\\x82\\x83\\x84\\x85\\x86\\x87\\x88\\x89\\x8a\\x8b\\x8c\\x8d\\x8e\\x8f\\x90\\x91\\x92\\x93\\x94\\x95\\x96\\x97\\x98\\x99\\x9a\\x9b\\x9c\\x9d\\x9e\\x9f\\xa0\\xa1\\xa2\\xa3\\xa4\\xa5\\xa6\\xa7\\xa8\\xa9\\xaa\\xab\\xac\\xad\\xae\\xaf\\xb0\\xb1\\xb2\\xb3\\xb4\\xb5\\xb6\\xb7\\xb8\\xb9\\xba\\xbb\\xbc\\xbd\\xbe\\xbf\\xc0\\xc1\\xc2\\xc3\\xc4\\xc5\\xc6\\xc7\\xc8\\xc9\\xca\\xcb\\xcc\\xcd\\xce\\xcf\\xd0\\xd1\\xd2\\xd3\\xd4\\xd5\\xd6\\xd7\\xd8\\xd9\\xda\\xdb\\xdc\\xdd\\xde\\xdf\\xe0\\xe1\\xe2\\xe3\\xe4\\xe5\\xe6\\xe7\\xe8\\xe9\\xea\\xeb\\xec\\xed\\xee\\xef\\xf0\\xf1\\xf2\\xf3\\xf4\\xf5\\xf6\\xf7\\xf8\\xf9\\xfa\\xfb\\xfc\\xfd\\xfe\\xff')
        
        if is_cli: #http://ironpython.codeplex.com/workitem/27899
            self.assertEqual(length, 735)
        else:
            self.assertEqual(length, 256) 
Example #24
Source File: string_escape.py    From BinderFilter with MIT License 5 votes vote down vote up
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0] 
Example #25
Source File: string_escape.py    From Computable with MIT License 5 votes vote down vote up
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0] 
Example #26
Source File: string_escape.py    From oss-ftp with MIT License 5 votes vote down vote up
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0] 
Example #27
Source File: testcase_mutation.py    From httpninja with Apache License 2.0 5 votes vote down vote up
def asciiChar(HTTPRequestHolderObjs):
    result = []
    for HTTPRequestHolderObj in HTTPRequestHolderObjs:
        initHTTPReq = HTTPRequestHolderObj.rawHTTPRequest
        initAdditionalInfo = HTTPRequestHolderObj.additionalInfo
        for c in range(0, 256):
            asciiChar = chr(c)
            additionalInfo = 'ASCII [Code: {} - Char: {}]'.format(c, codecs.escape_encode(asciiChar))
            if initAdditionalInfo != '':
                additionalInfo = initAdditionalInfo + ' - ' + additionalInfo
            HTTPRequestHolderObjTemp = HTTPRequestHolder.HTTPRequestHolder(Template(initHTTPReq).safe_substitute(asciiChar=asciiChar),additionalInfo)
            result.append(HTTPRequestHolderObjTemp)
    return result 
Example #28
Source File: string_escape.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0] 
Example #29
Source File: string_escape.py    From pmatic with GNU General Public License v2.0 5 votes vote down vote up
def encode(self, input, final=False):
        return codecs.escape_encode(input, self.errors)[0] 
Example #30
Source File: test_codecs.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_encode_length(self):
        with support.check_warnings(('unicode_internal codec has been '
                                     'deprecated', DeprecationWarning)):
            # Issue 3739
            encoder = codecs.getencoder("unicode_internal")
            self.assertEqual(encoder("a")[1], 1)
            self.assertEqual(encoder("\xe9\u0142")[1], 2)

            self.assertEqual(codecs.escape_encode(br'\x00')[1], 4)

# From http://www.gnu.org/software/libidn/draft-josefsson-idn-test-vectors.html