<s> import autopath <EOL> import types <EOL> from pypy . tool . sourcetools import render_docstr <EOL> def classOfAttribute ( klass , attname ) : <EOL> if attname in klass . __dict__ : <EOL> return klass <EOL> for base in klass . __bases__ : <EOL> ret = classOfAttribute ( base , attname ) <EOL> if ret : <EOL> return ret <EOL> def getAttributes ( klass , ignorelist = [ ] ) : <EOL> return [ name for name in dir ( klass ) if name not in ignorelist ] <EOL> def makeExceptionsTemplate ( f = None ) : <EOL> def enumClassesInOrder ( module ) : <EOL> seen = { } <EOL> ordered = [ ] <EOL> def enumerateOne ( exc ) : <EOL> seen [ exc ] = <NUM_LIT:1> <EOL> for each in exc . __bases__ : <EOL> if each not in seen : <EOL> enumerateOne ( each ) <EOL> ordered . append ( exc ) <EOL> for each in module . __dict__ . values ( ) : <EOL> if isinstance ( each , ( types . ClassType , type ) ) and each not in seen : <EOL> enumerateOne ( each ) <EOL> return ordered <EOL> if not f : <EOL> f = sys . stdout <EOL> import exceptions <EOL> print >> f , render_docstr ( exceptions , "<STR_LIT>" ) <EOL> for exc in enumClassesInOrder ( exceptions ) : <EOL> name = exc . __name__ <EOL> bases = exc . __bases__ <EOL> doc = exc . __doc__ <EOL> bases = [ this . __name__ for this in bases ] <EOL> bases = "<STR_LIT:U+002CU+0020>" . join ( bases ) <EOL> if bases : bases = "<STR_LIT>" % bases <EOL> ignorelist = "<STR_LIT>" . split ( ) <EOL> simple = [ ] <EOL> difficult = [ ] <EOL> for attname in getAttributes ( exc , ignorelist ) : <EOL> if classOfAttribute ( exc , attname ) is exc : <EOL> obj = getattr ( exc , attname ) <EOL> ( simple , difficult ) [ callable ( obj ) ] . append ( ( attname , obj ) ) <EOL> print >> f <EOL> print >> f , "<STR_LIT>" % ( name , bases ) <EOL> if doc : <EOL> print >> f , '<STR_LIT>' % doc <EOL> if not ( simple or difficult or doc ) : <EOL> print >> f , "<STR_LIT>" <EOL> for tup in simple : <EOL> print >> f , "<STR_LIT>" % tup <EOL> for attname , meth in difficult : <EOL> print >> f <EOL> func = globals ( ) . get ( "<STR_LIT>" + attname , None ) <EOL> if not func : <EOL> print >> f , "<STR_LIT>" % ( name , attname , meth ) <EOL> else : <EOL> try : <EOL> for line in func ( exc ) : <EOL> print >> f , "<STR_LIT:U+0020>" + line <EOL> except ValueError , e : <EOL> print >> f , "<STR_LIT>" % e <EOL> print >> f , "<STR_LIT>" % ( name , attname , meth ) <EOL> def tryGenerate__getitem__ ( exc ) : <EOL> for args in ( ) , ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) : <EOL> try : <EOL> sample = exc ( * args ) <EOL> except : <EOL> raise ValueError , "<STR_LIT>" <EOL> if "<STR_LIT:args>" not in sample . __dict__ : <EOL> raise ValueError , "<STR_LIT>" <EOL> if args != sample . args : <EOL> raise ValueError , "<STR_LIT>" <EOL> for i in range ( <NUM_LIT:5> ) : <EOL> try : x = sample [ i ] <EOL> except IndexError : x = <NUM_LIT> <EOL> try : y = args [ i ] <EOL> except IndexError : y = <NUM_LIT> <EOL> if x != y : <EOL> raise ValueError , "<STR_LIT>" <EOL> del sample . args <EOL> try : x = sample [ <NUM_LIT:0> ] <EOL> except : x = <NUM_LIT> <EOL> use_default = x is None <EOL> yield "<STR_LIT>" <EOL> yield "<STR_LIT>" <EOL> if use_default : <EOL> yield "<STR_LIT>" <EOL> yield "<STR_LIT>" <EOL> yield "<STR_LIT>" <EOL> class ProbeObject ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , argpos , maxprobe = None ) : <EOL> self . argpos = argpos <EOL> self . maxprobe = maxprobe <EOL> self . probed = [ ] <EOL> def __getitem__ ( self , idx ) : <EOL> if idx not in self . probed : <EOL> self . probed . append ( idx ) <EOL> if self . maxprobe is not None and idx > self . maxprobe : <EOL> raise IndexError , "<STR_LIT>" % idx <EOL> return "<STR_LIT>" % ( self . argpos , idx ) <EOL> def __repr__ ( self ) : <EOL> if self . probed : <EOL> return "<STR_LIT>" % ( self . argpos , self . probed ) <EOL> else : <EOL> return "<STR_LIT>" % self . argpos <EOL> def __str__ ( self ) : <EOL> return repr ( self ) [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> def __cmp__ ( self , other ) : <EOL> return cmp ( ( self . argpos , self . probed ) , other ) <EOL> def genArgsToTry ( argpos ) : <EOL> args = [ ProbeObject ( argpos ) , <EOL> "<STR_LIT>" % argpos , u"<STR_LIT>" % argpos , <NUM_LIT:1000> + argpos * <NUM_LIT:10> ] <EOL> return args <EOL> def cartesian ( * args ) : <EOL> if len ( args ) == <NUM_LIT:0> : <EOL> yield args <EOL> elif len ( args ) == <NUM_LIT:1> : <EOL> for item in args [ <NUM_LIT:0> ] : <EOL> yield ( item , ) <EOL> else : <EOL> for item in args [ <NUM_LIT:0> ] : <EOL> for other in cartesian ( * args [ <NUM_LIT:1> : ] ) : <EOL> yield ( item , ) + other <EOL> def probeArgCount ( exc , maxprobe = <NUM_LIT:20> ) : <EOL> worksmaybe = [ ] <EOL> for i in range ( maxprobe ) : <EOL> try : <EOL> probe = exc ( * ( i , ) * i ) <EOL> worksmaybe . append ( i ) <EOL> except TypeError , e : <EOL> if not str ( e ) . startswith ( "<STR_LIT>" ) : <EOL> worksmaybe . append ( i ) <EOL> except : <EOL> pass <EOL> return min ( worksmaybe ) , max ( worksmaybe ) <EOL> def refreshArgs ( tup ) : <EOL> res = [ ] <EOL> for arg in tup : <EOL> if type ( arg ) is ProbeObject : <EOL> arg = ProbeObject ( arg . argpos ) <EOL> res . append ( arg ) <EOL> return tuple ( res ) <EOL> def findAllArgs ( exc , maxprobe ) : <EOL> minargs , maxargs = probeArgCount ( exc , maxprobe ) <EOL> res = [ ] <EOL> arglist = tuple ( [ genArgsToTry ( i ) for i in range ( minargs ) ] ) <EOL> for args in cartesian ( * arglist ) : <EOL> try : <EOL> probe = exc ( * args ) <EOL> res . append ( args ) <EOL> works = refreshArgs ( args ) <EOL> break <EOL> except Exception , e : <EOL> continue <EOL> else : <EOL> raise TypeError , "<STR_LIT>" % exc . __name__ <EOL> for i in range ( minargs , maxargs ) : <EOL> for arg in genArgsToTry ( i ) : <EOL> args = works + ( arg , ) <EOL> try : <EOL> probe = exc ( * args ) <EOL> res . append ( args ) <EOL> works = refreshArgs ( args ) <EOL> break <EOL> except : <EOL> continue <EOL> else : <EOL> raise TypeError , "<STR_LIT>" % exc . __name__ <EOL> return minargs , maxargs , res <EOL> def captureAssignments ( exc , args ) : <EOL> """<STR_LIT>""" <EOL> assigned = [ ] <EOL> class WrapExc ( exc ) : <EOL> def __setattr__ ( self , name , obj ) : <EOL> assigned . append ( ( name , obj ) ) <EOL> self . __dict__ [ name ] = obj <EOL> probe = WrapExc ( * args ) <EOL> names = { } <EOL> names [ args ] = "<STR_LIT:args>" <EOL> for i , arg in enumerate ( args ) : <EOL> names [ arg ] = "<STR_LIT>" % i <EOL> if not isinstance ( arg , ProbeObject ) : <EOL> continue <EOL> for subidx in arg . probed : <EOL> names [ arg [ subidx ] ] = "<STR_LIT>" % ( i , subidx ) <EOL> def nameof ( obj ) : <EOL> if obj in names : <EOL> return names [ obj ] <EOL> elif isinstance ( obj , ( tuple , list ) ) : <EOL> stuff = [ nameof ( x ) for x in obj ] <EOL> br = str ( type ( obj ) ( ) ) <EOL> txt = br [ <NUM_LIT:0> ] + "<STR_LIT:U+002CU+0020>" . join ( stuff ) + br [ - <NUM_LIT:1> ] <EOL> names [ obj ] = txt <EOL> else : <EOL> names [ obj ] = "<STR_LIT>" % obj <EOL> return names [ obj ] <EOL> res = [ ] <EOL> for i , ( name , obj ) in enumerate ( assigned ) : <EOL> if isinstance ( obj , ProbeObject ) or name == '<STR_LIT:args>' or obj == None : <EOL> res . append ( "<STR_LIT>" % ( name , nameof ( obj ) ) ) <EOL> else : <EOL> res . append ( "<STR_LIT>" % ( nameof ( obj ) , repr ( type ( obj ) ) [ <NUM_LIT:7> : - <NUM_LIT:2> ] ) ) <EOL> res . append ( "<STR_LIT>" % ( name , nameof ( obj ) ) ) <EOL> res . append ( "<STR_LIT>" ) <EOL> reason = "<STR_LIT>" % ( i - <NUM_LIT:1> , repr ( type ( obj ) ) [ <NUM_LIT:7> : - <NUM_LIT:2> ] , '<STR_LIT:%s>' ) <EOL> reason2 = '<STR_LIT>' . join ( [ "<STR_LIT>" , "<STR_LIT:%s>" % nameof ( obj ) , "<STR_LIT:)>" ] ) <EOL> reason = "<STR_LIT:'>" + reason + "<STR_LIT:'>" + reason2 <EOL> res . append ( "<STR_LIT>" % ( reason ) ) <EOL> return tuple ( res ) <EOL> def tryGenerate__init__ ( exc , maxprobe = <NUM_LIT:20> ) : <EOL> minargs , maxargs , working = findAllArgs ( exc , maxprobe ) <EOL> foldcases = { } <EOL> for args in working : <EOL> singleprog = captureAssignments ( exc , args ) <EOL> for tup in enumerate ( singleprog ) : <EOL> foldcases . setdefault ( tup , [ ] ) <EOL> foldcases [ tup ] . append ( len ( args ) ) <EOL> groupassign = { } <EOL> for ( order , assignment ) , argcounts in foldcases . items ( ) : <EOL> key = tuple ( argcounts ) <EOL> if assignment == "<STR_LIT>" and len ( key ) != maxprobe : <EOL> assignment += "<STR_LIT>" <EOL> key = tuple ( range ( maxprobe ) ) <EOL> groupassign . setdefault ( key , [ ] ) <EOL> groupassign [ key ] . append ( ( order , assignment ) ) <EOL> cases = groupassign . items ( ) <EOL> cases . sort ( ) <EOL> yield "<STR_LIT>" <EOL> yield "<STR_LIT>" <EOL> if len ( cases ) > <NUM_LIT:1> or len ( cases [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) != maxprobe : <EOL> yield "<STR_LIT>" <EOL> for argcounts , ordered_statements in cases : <EOL> ordered_statements . sort ( ) <EOL> trailer = None <EOL> if len ( argcounts ) == maxprobe : <EOL> indent = <NUM_LIT:1> <EOL> else : <EOL> indent = <NUM_LIT:2> <EOL> dense = tuple ( range ( argcounts [ <NUM_LIT:0> ] , argcounts [ - <NUM_LIT:1> ] + <NUM_LIT:1> ) ) == argcounts <EOL> if len ( argcounts ) == <NUM_LIT:1> : <EOL> yield "<STR_LIT>" % argcounts <EOL> if maxargs == minargs : <EOL> trailer = [ "<STR_LIT>" ] <EOL> err_msg = "<STR_LIT>" <EOL> trailer += [ "<STR_LIT>" + str ( argcounts [ <NUM_LIT:0> ] ) + "<STR_LIT>" ] <EOL> elif dense and argcounts [ <NUM_LIT:0> ] == <NUM_LIT:0> : <EOL> yield "<STR_LIT>" % argcounts [ - <NUM_LIT:1> ] <EOL> elif dense and argcounts [ - <NUM_LIT:1> ] == maxprobe - <NUM_LIT:1> : <EOL> yield "<STR_LIT>" % argcounts [ <NUM_LIT:0> ] <EOL> elif dense : <EOL> yield "<STR_LIT>" % ( argcounts [ <NUM_LIT:0> ] , argcounts [ - <NUM_LIT:1> ] ) <EOL> else : <EOL> yield "<STR_LIT>" % ( argcounts , ) <EOL> if len ( ordered_statements ) > <NUM_LIT:0> : <EOL> for order , line in ordered_statements : <EOL> yield indent * "<STR_LIT:U+0020>" + line <EOL> else : <EOL> yield indent * "<STR_LIT:U+0020>" + "<STR_LIT>" <EOL> if trailer : <EOL> for line in trailer : yield line <EOL> def tryGenerate__str__ ( exc , maxprobe = <NUM_LIT:20> ) : <EOL> if exc in known__str__ : <EOL> import inspect <EOL> src = inspect . getsource ( known__str__ [ exc ] ) <EOL> for line in src . split ( "<STR_LIT:\n>" ) : <EOL> yield line <EOL> return <EOL> minargs , maxargs , working = findAllArgs ( exc , maxprobe ) <EOL> simple = False <EOL> arg1_methods = [ ] <EOL> for args in working : <EOL> test = str ( exc ( * args ) ) <EOL> if len ( args ) == <NUM_LIT:0> and test != "<STR_LIT>" : <EOL> break <EOL> if len ( args ) == <NUM_LIT:1> : <EOL> if test == repr ( args [ <NUM_LIT:0> ] ) : <EOL> arg1_methods . append ( "<STR_LIT>" ) <EOL> elif test == str ( args [ <NUM_LIT:0> ] ) : <EOL> arg1_methods . append ( "<STR_LIT:str>" ) <EOL> else : <EOL> break <EOL> if len ( args ) >= <NUM_LIT:2> and test != repr ( args ) : <EOL> break <EOL> else : <EOL> simple = arg1_methods and min ( arg1_methods ) == max ( arg1_methods ) <EOL> yield "<STR_LIT>" <EOL> if simple : <EOL> yield "<STR_LIT>" <EOL> yield "<STR_LIT>" <EOL> yield "<STR_LIT>" <EOL> yield "<STR_LIT>" <EOL> yield "<STR_LIT>" <EOL> yield "<STR_LIT>" <EOL> yield "<STR_LIT>" % arg1_methods . pop ( ) <EOL> yield "<STR_LIT>" <EOL> yield "<STR_LIT>" <EOL> return <EOL> probe = exc ( * working [ <NUM_LIT:0> ] ) <EOL> dic = probe . __dict__ <EOL> for key in dic . keys ( ) : <EOL> if key . startswith ( "<STR_LIT>" ) and key . endswith ( "<STR_LIT>" ) : <EOL> del dic [ key ] <EOL> yield "<STR_LIT>" <EOL> yield "<STR_LIT>" <EOL> yield "<STR_LIT>" <EOL> for key in dic . keys ( ) : <EOL> yield "<STR_LIT>" % ( key , key ) <EOL> yield "<STR_LIT>" <EOL> yield "<STR_LIT>" <EOL> known__str__ = { } <EOL> def __str__ ( self ) : <EOL> if type ( self . msg ) is not str : <EOL> return self . msg <EOL> buffer = self . msg <EOL> have_filename = type ( self . filename ) is str <EOL> have_lineno = type ( self . lineno ) is int <EOL> if have_filename or have_lineno : <EOL> import os <EOL> fname = os . path . basename ( self . filename or "<STR_LIT>" ) <EOL> if have_filename and have_lineno : <EOL> buffer = "<STR_LIT>" % ( self . msg , fname , self . lineno ) <EOL> elif have_filename : <EOL> buffer = "<STR_LIT>" % ( self . msg , fname ) <EOL> elif have_lineno : <EOL> buffer = "<STR_LIT>" % ( self . msg , self . lineno ) <EOL> return buffer <EOL> known__str__ [ SyntaxError ] = __str__ <EOL> def __str__ ( self ) : <EOL> if self . filename is not None : <EOL> return "<STR_LIT>" % ( self . errno , <EOL> self . strerror , <EOL> self . filename ) <EOL> if self . errno and self . strerror : <EOL> return "<STR_LIT>" % ( self . errno , self . strerror ) <EOL> return StandardError . __str__ ( self ) <EOL> known__str__ [ EnvironmentError ] = __str__ <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> import pypy , os <EOL> prefix = os . path . dirname ( pypy . __file__ ) <EOL> libdir = os . path . join ( prefix , "<STR_LIT>" ) <EOL> fname = "<STR_LIT>" <EOL> fpath = os . path . join ( libdir , fname ) <EOL> makeExceptionsTemplate ( file ( fpath , "<STR_LIT:w>" ) ) </s>
