!C99Shell v. 2.0 [PHP 7 Update] [25.02.2019]!

Software: Apache. PHP/7.3.33 

uname -a: Linux web25.us.cloudlogin.co 5.10.237-xeon-hst #1 SMP Mon May 5 15:10:04 UTC 2025 x86_64 

uid=233359(alpastrology) gid=888(tty) groups=888(tty),33(tape) 

Safe-mode: OFF (not secure)

/usr/local/python-3.2/lib/python3.2/test/   drwxr-xr-x
Free 6182.22 GB of 6263.42 GB (98.7%)
Home    Back    Forward    UPDIR    Refresh    Search    Buffer    Encoder    Tools    Proc.    FTP brute    Sec.    SQL    PHP-code    Update    Feedback    Self remove    Logout    


Viewing file:     test_codeccallbacks.py (32.54 KB)      -rw-r--r--
Select action/file-type:
(+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
import test.support, unittest
import sys, codecs, html.entities, unicodedata

class PosReturn:
    # this can be used for configurable callbacks

    def __init__(self):
        self.pos = 0

    def handle(self, exc):
        oldpos = self.pos
        realpos = oldpos
        if realpos<0:
            realpos = len(exc.object) + realpos
        # if we don't advance this time, terminate on the next call
        # otherwise we'd get an endless loop
        if realpos <= exc.start:
            self.pos = len(exc.object)
        return ("<?>", oldpos)

# A UnicodeEncodeError object with a bad start attribute
class BadStartUnicodeEncodeError(UnicodeEncodeError):
    def __init__(self):
        UnicodeEncodeError.__init__(self, "
ascii", "", 0, 1, "bad")
        self.start = []

# A UnicodeEncodeError object with a bad object attribute
class BadObjectUnicodeEncodeError(UnicodeEncodeError):
    def __init__(self):
        UnicodeEncodeError.__init__(self, "
ascii", "", 0, 1, "bad")
        self.object = []

# A UnicodeDecodeError object without an end attribute
class NoEndUnicodeDecodeError(UnicodeDecodeError):
    def __init__(self):
        UnicodeDecodeError.__init__(self, "
ascii", bytearray(b""), 0, 1, "bad")
        del self.end

# A UnicodeDecodeError object with a bad object attribute
class BadObjectUnicodeDecodeError(UnicodeDecodeError):
    def __init__(self):
        UnicodeDecodeError.__init__(self, "
ascii", bytearray(b""), 0, 1, "bad")
        self.object = []

# A UnicodeTranslateError object without a start attribute
class NoStartUnicodeTranslateError(UnicodeTranslateError):
    def __init__(self):
        UnicodeTranslateError.__init__(self, "", 0, 1, "
bad")
        del self.start

# A UnicodeTranslateError object without an end attribute
class NoEndUnicodeTranslateError(UnicodeTranslateError):
    def __init__(self):
        UnicodeTranslateError.__init__(self,  "", 0, 1, "
bad")
        del self.end

# A UnicodeTranslateError object without an object attribute
class NoObjectUnicodeTranslateError(UnicodeTranslateError):
    def __init__(self):
        UnicodeTranslateError.__init__(self, "", 0, 1, "
bad")
        del self.object

class CodecCallbackTest(unittest.TestCase):

    def test_xmlcharrefreplace(self):
        # replace unencodable characters which numeric character entities.
        # For ascii, latin-1 and charmaps this is completely implemented
        # in C and should be reasonably fast.
        s = "
\u30b9\u30d1\u30e2 \xe4nd eggs"
        self.assertEqual(
            s.encode("
ascii", "xmlcharrefreplace"),
            b"
&#12473;&#12497;&#12514; &#228;nd eggs"
        
)
        
self.assertEqual(
            
s.encode("latin-1""xmlcharrefreplace"),
            
b"&#12473;&#12497;&#12514; \xe4nd eggs"
        
)

    
def test_xmlcharnamereplace(self):
        
# This time use a named character entity for unencodable
        # characters, if one is available.

        
def xmlcharnamereplace(exc):
            if 
not isinstance(excUnicodeEncodeError):
                
raise TypeError("don't know how to handle %r" exc)
            
= []
            for 
c in exc.object[exc.start:exc.end]:
                try:
                    
l.append("&%s;" html.entities.codepoint2name[ord(c)])
                
except KeyError:
                    
l.append("&#%d;" ord(c))
            return (
"".join(l), exc.end)

        
codecs.register_error(
            
"test.xmlcharnamereplace"xmlcharnamereplace)

        
sin "\xab\u211c\xbb = \u2329\u1234\u20ac\u232a"
        
sout b"&laquo;&real;&raquo; = &lang;&#4660;&euro;&rang;"
        
self.assertEqual(sin.encode("ascii""test.xmlcharnamereplace"), sout)
        
sout b"\xab&real;\xbb = &lang;&#4660;&euro;&rang;"
        
self.assertEqual(sin.encode("latin-1""test.xmlcharnamereplace"), sout)
        
sout b"\xab&real;\xbb = &lang;&#4660;\xa4&rang;"
        
self.assertEqual(sin.encode("iso-8859-15""test.xmlcharnamereplace"), sout)

    
def test_uninamereplace(self):
        
# We're using the names from the unicode database this time,
        # and we're doing "syntax highlighting" here, i.e. we include
        # the replaced text in ANSI escape sequences. For this it is
        # useful that the error handler is not called for every single
        # unencodable character, but for a complete sequence of
        # unencodable characters, otherwise we would output many
        # unnecessary escape sequences.

        
def uninamereplace(exc):
            if 
not isinstance(excUnicodeEncodeError):
                
raise TypeError("don't know how to handle %r" exc)
            
= []
            for 
c in exc.object[exc.start:exc.end]:
                
l.append(unicodedata.name(c"0x%x" ord(c)))
            return (
"\033[1m%s\033[0m" ", ".join(l), exc.end)

        
codecs.register_error(
            
"test.uninamereplace"uninamereplace)

        
sin "\xac\u1234\u20ac\u8000"
        
sout b"\033[1mNOT SIGN, ETHIOPIC SYLLABLE SEE, EURO SIGN, CJK UNIFIED IDEOGRAPH-8000\033[0m"
        
self.assertEqual(sin.encode("ascii""test.uninamereplace"), sout)

        
sout b"\xac\033[1mETHIOPIC SYLLABLE SEE, EURO SIGN, CJK UNIFIED IDEOGRAPH-8000\033[0m"
        
self.assertEqual(sin.encode("latin-1""test.uninamereplace"), sout)

        
sout b"\xac\033[1mETHIOPIC SYLLABLE SEE\033[0m\xa4\033[1mCJK UNIFIED IDEOGRAPH-8000\033[0m"
        
self.assertEqual(sin.encode("iso-8859-15""test.uninamereplace"), sout)

    
def test_backslashescape(self):
        
# Does the same as the "unicode-escape" encoding, but with different
        # base encodings.
        
sin "a\xac\u1234\u20ac\u8000"
        
if sys.maxunicode 0xffff:
            
sin += chr(sys.maxunicode)
        
sout b"a\\xac\\u1234\\u20ac\\u8000"
        
if sys.maxunicode 0xffff:
            
sout += bytes("\\U%08x" sys.maxunicode"ascii")
        
self.assertEqual(sin.encode("ascii""backslashreplace"), sout)

        
sout b"a\xac\\u1234\\u20ac\\u8000"
        
if sys.maxunicode 0xffff:
            
sout += bytes("\\U%08x" sys.maxunicode"ascii")
        
self.assertEqual(sin.encode("latin-1""backslashreplace"), sout)

        
sout b"a\xac\\u1234\xa4\\u8000"
        
if sys.maxunicode 0xffff:
            
sout += bytes("\\U%08x" sys.maxunicode"ascii")
        
self.assertEqual(sin.encode("iso-8859-15""backslashreplace"), sout)

    
def test_decoding_callbacks(self):
        
# This is a test for a decoding callback handler
        # that allows the decoding of the invalid sequence
        # "\xc0\x80" and returns "\x00" instead of raising an error.
        # All other illegal sequences will be handled strictly.
        
def relaxedutf8(exc):
            if 
not isinstance(excUnicodeDecodeError):
                
raise TypeError("don't know how to handle %r" exc)
            if 
exc.object[exc.start:exc.start+2] == b"\xc0\x80":
                return (
"\x00"exc.start+2# retry after two bytes
            
else:
                
raise exc

        codecs
.register_error("test.relaxedutf8"relaxedutf8)

        
# all the "\xc0\x80" will be decoded to "\x00"
        
sin b"a\x00b\xc0\x80c\xc3\xbc\xc0\x80\xc0\x80"
        
sout "a\x00b\x00c\xfc\x00\x00"
        
self.assertEqual(sin.decode("utf-8""test.relaxedutf8"), sout)

        
# "\xc0\x81" is not valid and a UnicodeDecodeError will be raised
        
sin b"\xc0\x80\xc0\x81"
        
self.assertRaises(UnicodeDecodeErrorsin.decode,
                          
"utf-8""test.relaxedutf8")

    
def test_charmapencode(self):
        
# For charmap encodings the replacement string will be
        # mapped through the encoding again. This means, that
        # to be able to use e.g. the "replace" handler, the
        # charmap has to have a mapping for "?".
        
charmap dict((ord(c), bytes(2*c.upper(), 'ascii')) for c in "abcdefgh")
        
sin "abc"
        
sout b"AABBCC"
        
self.assertEqual(codecs.charmap_encode(sin"strict"charmap)[0], sout)

        
sin "abcA"
        
self.assertRaises(UnicodeErrorcodecs.charmap_encodesin"strict"charmap)

        
charmap[ord("?")] = b"XYZ"
        
sin "abcDEF"
        
sout b"AABBCCXYZXYZXYZ"
        
self.assertEqual(codecs.charmap_encode(sin"replace"charmap)[0], sout)

        
charmap[ord("?")] = "XYZ" # wrong type in mapping
        
self.assertRaises(TypeErrorcodecs.charmap_encodesin"replace"charmap)

    
def test_decodeunicodeinternal(self):
        
self.assertRaises(
            
UnicodeDecodeError,
            
b"\x00\x00\x00\x00\x00".decode,
            
"unicode-internal",
        )
        if 
sys.maxunicode 0xffff:
            
def handler_unicodeinternal(exc):
                if 
not isinstance(excUnicodeDecodeError):
                    
raise TypeError("don't know how to handle %r" exc)
                return (
"\x01"1)

            
self.assertEqual(
                
b"\x00\x00\x00\x00\x00".decode("unicode-internal""ignore"),
                
"\u0000"
            
)

            
self.assertEqual(
                
b"\x00\x00\x00\x00\x00".decode("unicode-internal""replace"),
                
"\u0000\ufffd"
            
)

            
codecs.register_error("test.hui"handler_unicodeinternal)

            
self.assertEqual(
                
b"\x00\x00\x00\x00\x00".decode("unicode-internal""test.hui"),
                
"\u0000\u0001\u0000"
            
)

    
def test_callbacks(self):
        
def handler1(exc):
            
range(exc.startexc.end)
            if 
isinstance(excUnicodeEncodeError):
                
= ["<%d>" ord(exc.object[pos]) for pos in r]
            
elif isinstance(excUnicodeDecodeError):
                
= ["<%d>" exc.object[pos] for pos in r]
            else:
                
raise TypeError("don't know how to handle %r" exc)
            return (
"[%s]" "".join(l), exc.end)

        
codecs.register_error("test.handler1"handler1)

        
def handler2(exc):
            if 
not isinstance(excUnicodeDecodeError):
                
raise TypeError("don't know how to handle %r" exc)
            
= ["<%d>" exc.object[pos] for pos in range(exc.startexc.end)]
            return (
"[%s]" "".join(l), exc.end+1# skip one character

        
codecs.register_error("test.handler2"handler2)

        
b"\x00\x81\x7f\x80\xff"

        
self.assertEqual(
            
s.decode("ascii""test.handler1"),
            
"\x00[<129>]\x7f[<128>][<255>]"
        
)
        
self.assertEqual(
            
s.decode("ascii""test.handler2"),
            
"\x00[<129>][<128>]"
        
)

        
self.assertEqual(
            
b"\\u3042\u3xxx".decode("unicode-escape""test.handler1"),
            
"\u3042[<92><117><51>]xxx"
        
)

        
self.assertEqual(
            
b"\\u3042\u3xx".decode("unicode-escape""test.handler1"),
            
"\u3042[<92><117><51>]xx"
        
)

        
self.assertEqual(
            
codecs.charmap_decode(b"abc""test.handler1", {ord("a"): "z"})[0],
            
"z[<98>][<99>]"
        
)

        
self.assertEqual(
            
"g\xfc\xdfrk".encode("ascii""test.handler1"),
            
b"g[<252><223>]rk"
        
)

        
self.assertEqual(
            
"g\xfc\xdf".encode("ascii""test.handler1"),
            
b"g[<252><223>]"
        
)

    
def test_longstrings(self):
        
# test long strings to check for memory overflow problems
        
errors = [ "strict""ignore""replace""xmlcharrefreplace",
                   
"backslashreplace"]
        
# register the handlers under different names,
        # to prevent the codec from recognizing the name
        
for err in errors:
            
codecs.register_error("test." errcodecs.lookup_error(err))
        
1000
        errors 
+= [ "test." err for err in errors ]
        for 
uni in s*for s in ("x""\u3042""a\xe4") ]:
            for 
enc in ("ascii""latin-1""iso-8859-1""iso-8859-15",
                        
"utf-8""utf-7""utf-16""utf-32"):
                for 
err in errors:
                    try:
                        
uni.encode(encerr)
                    
except UnicodeError:
                        
pass

    def check_exceptionobjectargs
(selfexctypeargsmsg):
        
# Test UnicodeError subclasses: construction, attribute assignment and __str__ conversion
        # check with one missing argument
        
self.assertRaises(TypeErrorexctype, *args[:-1])
        
# check with one argument too much
        
self.assertRaises(TypeErrorexctype, *(args + ["too much"]))
        
# check with one argument of the wrong type
        
wrongargs = [ "spam"b"eggs"b"spam"421.0None ]
        for 
i in range(len(args)):
            for 
wrongarg in wrongargs:
                if 
type(wrongargis type(args[i]):
                    continue
                
# build argument array
                
callargs = []
                for 
j in range(len(args)):
                    if 
i==j:
                        
callargs.append(wrongarg)
                    else:
                        
callargs.append(args[i])
                
self.assertRaises(TypeErrorexctype, *callargs)

        
# check with the correct number and type of arguments
        
exc exctype(*args)
        
self.assertEqual(str(exc), msg)

    
def test_unicodeencodeerror(self):
        
self.check_exceptionobjectargs(
            
UnicodeEncodeError,
            [
"ascii""g\xfcrk"12"ouch"],
            
"'ascii' codec can't encode character '\\xfc' in position 1: ouch"
        
)
        
self.check_exceptionobjectargs(
            
UnicodeEncodeError,
            [
"ascii""g\xfcrk"14"ouch"],
            
"'ascii' codec can't encode characters in position 1-3: ouch"
        
)
        
self.check_exceptionobjectargs(
            
UnicodeEncodeError,
            [
"ascii""\xfcx"01"ouch"],
            
"'ascii' codec can't encode character '\\xfc' in position 0: ouch"
        
)
        
self.check_exceptionobjectargs(
            
UnicodeEncodeError,
            [
"ascii""\u0100x"01"ouch"],
            
"'ascii' codec can't encode character '\\u0100' in position 0: ouch"
        
)
        
self.check_exceptionobjectargs(
            
UnicodeEncodeError,
            [
"ascii""\uffffx"01"ouch"],
            
"'ascii' codec can't encode character '\\uffff' in position 0: ouch"
        
)
        if 
sys.maxunicode 0xffff:
            
self.check_exceptionobjectargs(
                
UnicodeEncodeError,
                [
"ascii""\U00010000x"01"ouch"],
                
"'ascii' codec can't encode character '\\U00010000' in position 0: ouch"
            
)

    
def test_unicodedecodeerror(self):
        
self.check_exceptionobjectargs(
            
UnicodeDecodeError,
            [
"ascii"bytearray(b"g\xfcrk"), 12"ouch"],
            
"'ascii' codec can't decode byte 0xfc in position 1: ouch"
        
)
        
self.check_exceptionobjectargs(
            
UnicodeDecodeError,
            [
"ascii"bytearray(b"g\xfcrk"), 13"ouch"],
            
"'ascii' codec can't decode bytes in position 1-2: ouch"
        
)

    
def test_unicodetranslateerror(self):
        
self.check_exceptionobjectargs(
            
UnicodeTranslateError,
            [
"g\xfcrk"12"ouch"],
            
"can't translate character '\\xfc' in position 1: ouch"
        
)
        
self.check_exceptionobjectargs(
            
UnicodeTranslateError,
            [
"g\u0100rk"12"ouch"],
            
"can't translate character '\\u0100' in position 1: ouch"
        
)
        
self.check_exceptionobjectargs(
            
UnicodeTranslateError,
            [
"g\uffffrk"12"ouch"],
            
"can't translate character '\\uffff' in position 1: ouch"
        
)
        if 
sys.maxunicode 0xffff:
            
self.check_exceptionobjectargs(
                
UnicodeTranslateError,
                [
"g\U00010000rk"12"ouch"],
                
"can't translate character '\\U00010000' in position 1: ouch"
            
)
        
self.check_exceptionobjectargs(
            
UnicodeTranslateError,
            [
"g\xfcrk"13"ouch"],
            
"can't translate characters in position 1-2: ouch"
        
)

    
def test_badandgoodstrictexceptions(self):
        
# "strict" complains about a non-exception passed in
        
self.assertRaises(
            
TypeError,
            
codecs.strict_errors,
            
42
        
)
        
# "strict" complains about the wrong exception type
        
self.assertRaises(
            
Exception,
            
codecs.strict_errors,
            
Exception("ouch")
        )

        
# If the correct exception is passed in, "strict" raises it
        
self.assertRaises(
            
UnicodeEncodeError,
            
codecs.strict_errors,
            
UnicodeEncodeError("ascii""\u3042"01"ouch")
        )

    
def test_badandgoodignoreexceptions(self):
        
# "ignore" complains about a non-exception passed in
        
self.assertRaises(
           
TypeError,
           
codecs.ignore_errors,
           
42
        
)
        
# "ignore" complains about the wrong exception type
        
self.assertRaises(
           
TypeError,
           
codecs.ignore_errors,
           
UnicodeError("ouch")
        )
        
# If the correct exception is passed in, "ignore" returns an empty replacement
        
self.assertEqual(
            
codecs.ignore_errors(
                
UnicodeEncodeError("ascii""\u3042"01"ouch")),
            (
""1)
        )
        
self.assertEqual(
            
codecs.ignore_errors(
                
UnicodeDecodeError("ascii"bytearray(b"\xff"), 01"ouch")),
            (
""1)
        )
        
self.assertEqual(
            
codecs.ignore_errors(
                
UnicodeTranslateError("\u3042"01"ouch")),
            (
""1)
        )

    
def test_badandgoodreplaceexceptions(self):
        
# "replace" complains about a non-exception passed in
        
self.assertRaises(
           
TypeError,
           
codecs.replace_errors,
           
42
        
)
        
# "replace" complains about the wrong exception type
        
self.assertRaises(
           
TypeError,
           
codecs.replace_errors,
           
UnicodeError("ouch")
        )
        
self.assertRaises(
            
TypeError,
            
codecs.replace_errors,
            
BadObjectUnicodeEncodeError()
        )
        
self.assertRaises(
            
TypeError,
            
codecs.replace_errors,
            
BadObjectUnicodeDecodeError()
        )
        
# With the correct exception, "replace" returns an "?" or "\ufffd" replacement
        
self.assertEqual(
            
codecs.replace_errors(
                
UnicodeEncodeError("ascii""\u3042"01"ouch")),
            (
"?"1)
        )
        
self.assertEqual(
            
codecs.replace_errors(
                
UnicodeDecodeError("ascii"bytearray(b"\xff"), 01"ouch")),
            (
"\ufffd"1)
        )
        
self.assertEqual(
            
codecs.replace_errors(
                
UnicodeTranslateError("\u3042"01"ouch")),
            (
"\ufffd"1)
        )

    
def test_badandgoodxmlcharrefreplaceexceptions(self):
        
# "xmlcharrefreplace" complains about a non-exception passed in
        
self.assertRaises(
           
TypeError,
           
codecs.xmlcharrefreplace_errors,
           
42
        
)
        
# "xmlcharrefreplace" complains about the wrong exception types
        
self.assertRaises(
           
TypeError,
           
codecs.xmlcharrefreplace_errors,
           
UnicodeError("ouch")
        )
        
# "xmlcharrefreplace" can only be used for encoding
        
self.assertRaises(
            
TypeError,
            
codecs.xmlcharrefreplace_errors,
            
UnicodeDecodeError("ascii"bytearray(b"\xff"), 01"ouch")
        )
        
self.assertRaises(
            
TypeError,
            
codecs.xmlcharrefreplace_errors,
            
UnicodeTranslateError("\u3042"01"ouch")
        )
        
# Use the correct exception
        
cs = (019109910099910009999100000x3042)
        
"".join(chr(c) for c in cs)
        
self.assertEqual(
            
codecs.xmlcharrefreplace_errors(
                
UnicodeEncodeError("ascii"s0len(s), "ouch")
            ),
            (
"".join("&#%d;" ord(c) for c in s), len(s))
        )

    
def test_badandgoodbackslashreplaceexceptions(self):
        
# "backslashreplace" complains about a non-exception passed in
        
self.assertRaises(
           
TypeError,
           
codecs.backslashreplace_errors,
           
42
        
)
        
# "backslashreplace" complains about the wrong exception types
        
self.assertRaises(
           
TypeError,
           
codecs.backslashreplace_errors,
           
UnicodeError("ouch")
        )
        
# "backslashreplace" can only be used for encoding
        
self.assertRaises(
            
TypeError,
            
codecs.backslashreplace_errors,
            
UnicodeDecodeError("ascii"bytearray(b"\xff"), 01"ouch")
        )
        
self.assertRaises(
            
TypeError,
            
codecs.backslashreplace_errors,
            
UnicodeTranslateError("\u3042"01"ouch")
        )
        
# Use the correct exception
        
self.assertEqual(
            
codecs.backslashreplace_errors(
                
UnicodeEncodeError("ascii""\u3042"01"ouch")),
            (
"\\u3042"1)
        )
        
self.assertEqual(
            
codecs.backslashreplace_errors(
                
UnicodeEncodeError("ascii""\x00"01"ouch")),
            (
"\\x00"1)
        )
        
self.assertEqual(
            
codecs.backslashreplace_errors(
                
UnicodeEncodeError("ascii""\xff"01"ouch")),
            (
"\\xff"1)
        )
        
self.assertEqual(
            
codecs.backslashreplace_errors(
                
UnicodeEncodeError("ascii""\u0100"01"ouch")),
            (
"\\u0100"1)
        )
        
self.assertEqual(
            
codecs.backslashreplace_errors(
                
UnicodeEncodeError("ascii""\uffff"01"ouch")),
            (
"\\uffff"1)
        )
        
# 1 on UCS-4 builds, 2 on UCS-2
        
len_wide len("\U00010000")
        
self.assertEqual(
            
codecs.backslashreplace_errors(
                
UnicodeEncodeError("ascii""\U00010000",
                                   
0len_wide"ouch")),
            (
"\\U00010000"len_wide)
        )
        
self.assertEqual(
            
codecs.backslashreplace_errors(
                
UnicodeEncodeError("ascii""\U0010ffff",
                                   
0len_wide"ouch")),
            (
"\\U0010ffff"len_wide)
        )
        
# Lone surrogates (regardless of unicode width)
        
self.assertEqual(
            
codecs.backslashreplace_errors(
                
UnicodeEncodeError("ascii""\ud800"01"ouch")),
            (
"\\ud800"1)
        )
        
self.assertEqual(
            
codecs.backslashreplace_errors(
                
UnicodeEncodeError("ascii""\udfff"01"ouch")),
            (
"\\udfff"1)
        )

    
def test_badhandlerresults(self):
        
results = ( 42"foo", (1,2,3), ("foo"13), ("foo"None), ("foo",), ("foo"13), ("foo"None), ("foo",) )
        
encs = ("ascii""latin-1""iso-8859-1""iso-8859-15")

        for 
res in results:
            
codecs.register_error("test.badhandler"lambda xres)
            for 
enc in encs:
                
self.assertRaises(
                    
TypeError,
                    
"\u3042".encode,
                    
enc,
                    
"test.badhandler"
                
)
            for (
encbytesin (
                (
"ascii"b"\xff"),
                (
"utf-8"b"\xff"),
                (
"utf-7"b"+x-"),
                (
"unicode-internal"b"\x00"),
            ):
                
self.assertRaises(
                    
TypeError,
                    
bytes.decode,
                    
enc,
                    
"test.badhandler"
                
)

    
def test_lookup(self):
        
self.assertEqual(codecs.strict_errorscodecs.lookup_error("strict"))
        
self.assertEqual(codecs.ignore_errorscodecs.lookup_error("ignore"))
        
self.assertEqual(codecs.strict_errorscodecs.lookup_error("strict"))
        
self.assertEqual(
            
codecs.xmlcharrefreplace_errors,
            
codecs.lookup_error("xmlcharrefreplace")
        )
        
self.assertEqual(
            
codecs.backslashreplace_errors,
            
codecs.lookup_error("backslashreplace")
        )

    
def test_unencodablereplacement(self):
        
def unencrepl(exc):
            if 
isinstance(excUnicodeEncodeError):
                return (
"\u4242"exc.end)
            else:
                
raise TypeError("don't know how to handle %r" exc)
        
codecs.register_error("test.unencreplhandler"unencrepl)
        for 
enc in ("ascii""iso-8859-1""iso-8859-15"):
            
self.assertRaises(
                
UnicodeEncodeError,
                
"\u4242".encode,
                
enc,
                
"test.unencreplhandler"
            
)

    
def test_badregistercall(self):
        
# enhance coverage of:
        # Modules/_codecsmodule.c::register_error()
        # Python/codecs.c::PyCodec_RegisterError()
        
self.assertRaises(TypeErrorcodecs.register_error42)
        
self.assertRaises(TypeErrorcodecs.register_error"test.dummy"42)

    
def test_badlookupcall(self):
        
# enhance coverage of:
        # Modules/_codecsmodule.c::lookup_error()
        
self.assertRaises(TypeErrorcodecs.lookup_error)

    
def test_unknownhandler(self):
        
# enhance coverage of:
        # Modules/_codecsmodule.c::lookup_error()
        
self.assertRaises(LookupErrorcodecs.lookup_error"test.unknown")

    
def test_xmlcharrefvalues(self):
        
# enhance coverage of:
        # Python/codecs.c::PyCodec_XMLCharRefReplaceErrors()
        # and inline implementations
        
= (151050100500100050001000050000)
        if 
sys.maxunicode>=100000:
            
+= (1000005000001000000)
        
"".join([chr(x) for x in v])
        
codecs.register_error("test.xmlcharrefreplace"codecs.xmlcharrefreplace_errors)
        for 
enc in ("ascii""iso-8859-15"):
            for 
err in ("xmlcharrefreplace""test.xmlcharrefreplace"):
                
s.encode(encerr)

    
def test_decodehelper(self):
        
# enhance coverage of:
        # Objects/unicodeobject.c::unicode_decode_call_errorhandler()
        # and callers
        
self.assertRaises(LookupErrorb"\xff".decode"ascii""test.unknown")

        
def baddecodereturn1(exc):
            return 
42
        codecs
.register_error("test.baddecodereturn1"baddecodereturn1)
        
self.assertRaises(TypeErrorb"\xff".decode"ascii""test.baddecodereturn1")
        
self.assertRaises(TypeErrorb"\\".decode"unicode-escape""test.baddecodereturn1")
        
self.assertRaises(TypeErrorb"\\x0".decode"unicode-escape""test.baddecodereturn1")
        
self.assertRaises(TypeErrorb"\\x0y".decode"unicode-escape""test.baddecodereturn1")
        
self.assertRaises(TypeErrorb"\\Uffffeeee".decode"unicode-escape""test.baddecodereturn1")
        
self.assertRaises(TypeErrorb"\\uyyyy".decode"raw-unicode-escape""test.baddecodereturn1")

        
def baddecodereturn2(exc):
            return (
"?"None)
        
codecs.register_error("test.baddecodereturn2"baddecodereturn2)
        
self.assertRaises(TypeErrorb"\xff".decode"ascii""test.baddecodereturn2")

        
handler PosReturn()
        
codecs.register_error("test.posreturn"handler.handle)

        
# Valid negative position
        
handler.pos = -1
        self
.assertEqual(b"\xff0".decode("ascii""test.posreturn"), "<?>0")

        
# Valid negative position
        
handler.pos = -2
        self
.assertEqual(b"\xff0".decode("ascii""test.posreturn"), "<?><?>")

        
# Negative position out of bounds
        
handler.pos = -3
        self
.assertRaises(IndexErrorb"\xff0".decode"ascii""test.posreturn")

        
# Valid positive position
        
handler.pos 1
        self
.assertEqual(b"\xff0".decode("ascii""test.posreturn"), "<?>0")

        
# Largest valid positive position (one beyond end of input)
        
handler.pos 2
        self
.assertEqual(b"\xff0".decode("ascii""test.posreturn"), "<?>")

        
# Invalid positive position
        
handler.pos 3
        self
.assertRaises(IndexErrorb"\xff0".decode"ascii""test.posreturn")

        
# Restart at the "0"
        
handler.pos 6
        self
.assertEqual(b"\\uyyyy0".decode("raw-unicode-escape""test.posreturn"), "<?>0")

        class 
D(dict):
            
def __getitem__(selfkey):
                
raise ValueError
        self
.assertRaises(UnicodeErrorcodecs.charmap_decodeb"\xff""strict", {0xffNone})
        
self.assertRaises(ValueErrorcodecs.charmap_decodeb"\xff""strict"D())
        
self.assertRaises(TypeErrorcodecs.charmap_decodeb"\xff""strict", {0xff0x110000})

    
def test_encodehelper(self):
        
# enhance coverage of:
        # Objects/unicodeobject.c::unicode_encode_call_errorhandler()
        # and callers
        
self.assertRaises(LookupError"\xff".encode"ascii""test.unknown")

        
def badencodereturn1(exc):
            return 
42
        codecs
.register_error("test.badencodereturn1"badencodereturn1)
        
self.assertRaises(TypeError"\xff".encode"ascii""test.badencodereturn1")

        
def badencodereturn2(exc):
            return (
"?"None)
        
codecs.register_error("test.badencodereturn2"badencodereturn2)
        
self.assertRaises(TypeError"\xff".encode"ascii""test.badencodereturn2")

        
handler PosReturn()
        
codecs.register_error("test.posreturn"handler.handle)

        
# Valid negative position
        
handler.pos = -1
        self
.assertEqual("\xff0".encode("ascii""test.posreturn"), b"<?>0")

        
# Valid negative position
        
handler.pos = -2
        self
.assertEqual("\xff0".encode("ascii""test.posreturn"), b"<?><?>")

        
# Negative position out of bounds
        
handler.pos = -3
        self
.assertRaises(IndexError"\xff0".encode"ascii""test.posreturn")

        
# Valid positive position
        
handler.pos 1
        self
.assertEqual("\xff0".encode("ascii""test.posreturn"), b"<?>0")

        
# Largest valid positive position (one beyond end of input
        
handler.pos 2
        self
.assertEqual("\xff0".encode("ascii""test.posreturn"), b"<?>")

        
# Invalid positive position
        
handler.pos 3
        self
.assertRaises(IndexError"\xff0".encode"ascii""test.posreturn")

        
handler.pos 0

        
class D(dict):
            
def __getitem__(selfkey):
                
raise ValueError
        
for err in ("strict""replace""xmlcharrefreplace""backslashreplace""test.posreturn"):
            
self.assertRaises(UnicodeErrorcodecs.charmap_encode"\xff"err, {0xffNone})
            
self.assertRaises(ValueErrorcodecs.charmap_encode"\xff"errD())
            
self.assertRaises(TypeErrorcodecs.charmap_encode"\xff"err, {0xff300})

    
def test_translatehelper(self):
        
# enhance coverage of:
        # Objects/unicodeobject.c::unicode_encode_call_errorhandler()
        # and callers
        # (Unfortunately the errors argument is not directly accessible
        # from Python, so we can't test that much)
        
class D(dict):
            
def __getitem__(selfkey):
                
raise ValueError
        
#self.assertRaises(ValueError, "\xff".translate, D())
        
self.assertRaises(TypeError"\xff".translate, {0xffsys.maxunicode+1})
        
self.assertRaises(TypeError"\xff".translate, {0xff: ()})

    
def test_bug828737(self):
        
charmap = {
            
ord("&"): "&amp;",
            
ord("<"): "&lt;",
            
ord(">"): "&gt;",
            
ord('"'): "&quot;",
        }

        for 
n in (1101001000):
            
text 'abc<def>ghi'*n
            text
.translate(charmap)

    
def test_mutatingdecodehandler(self):
        
baddata = [
            (
"ascii"b"\xff"),
            (
"utf-7"b"++"),
            (
"utf-8",  b"\xff"),
            (
"utf-16"b"\xff"),
            (
"utf-32"b"\xff"),
            (
"unicode-escape"b"\\u123g"),
            (
"raw-unicode-escape"b"\\u123g"),
            (
"unicode-internal"b"\xff"),
        ]

        
def replacing(exc):
            if 
isinstance(excUnicodeDecodeError):
                
exc.object 42
                
return ("\u4242"0)
            else:
                
raise TypeError("don't know how to handle %r" exc)
        
codecs.register_error("test.replacing"replacing)
        for (
encodingdatain baddata:
            
self.assertRaises(TypeErrordata.decodeencoding"test.replacing")

        
def mutating(exc):
            if 
isinstance(excUnicodeDecodeError):
                
exc.object[:] = b""
                
return ("\u4242"0)
            else:
                
raise TypeError("don't know how to handle %r" exc)
        
codecs.register_error("test.mutating"mutating)
        
# If the decoder doesn't pick up the modified input the following
        # will lead to an endless loop
        
for (encodingdatain baddata:
            
self.assertRaises(TypeErrordata.decodeencoding"test.replacing")

def test_main():
    
test.support.run_unittest(CodecCallbackTest)

if 
__name__ == "__main__":
    
test_main()

:: Command execute ::

Enter:
 
Select:
 

:: Search ::
  - regexp 

:: Upload ::
 
[ Read-Only ]

:: Make Dir ::
 
[ Read-Only ]
:: Make File ::
 
[ Read-Only ]

:: Go Dir ::
 
:: Go File ::
 

--[ c99shell v. 2.0 [PHP 7 Update] [25.02.2019] maintained by KaizenLouie | C99Shell Github | Generation time: 0.0127 ]--