@@ -91,47 +91,36 @@ class names, and the use of '+', '|' and '^' operators.
91
91
'indentedBlock' , 'originalTextFor' , 'ungroup' , 'infixNotation' ,
92
92
]
93
93
94
- """
95
- Detect if we are running version 3.X and make appropriate changes
96
- Robert A. Clark
97
- """
98
- _PY3K = sys .version_info [0 ] > 2
99
- if _PY3K :
100
- _MAX_INT = sys .maxsize
101
- basestring = str
102
- unichr = chr
103
- _ustr = str
104
- else :
105
- _MAX_INT = sys .maxint
106
- range = xrange
107
- set = lambda s : dict ( [(c ,0 ) for c in s ] )
108
-
109
- def _ustr (obj ):
110
- """Drop-in replacement for str(obj) that tries to be Unicode friendly. It first tries
111
- str(obj). If that fails with a UnicodeEncodeError, then it tries unicode(obj). It
112
- then < returns the unicode object | encodes it with the default encoding | ... >.
113
- """
114
- if isinstance (obj ,unicode ):
115
- return obj
94
+ _MAX_INT = sys .maxint
95
+ range = xrange
96
+ set = lambda s : dict ( [(c ,0 ) for c in s ] )
97
+
98
+ def _ustr (obj ):
99
+ """Drop-in replacement for str(obj) that tries to be Unicode friendly. It first tries
100
+ str(obj). If that fails with a UnicodeEncodeError, then it tries unicode(obj). It
101
+ then < returns the unicode object | encodes it with the default encoding | ... >.
102
+ """
103
+ if isinstance (obj ,unicode ):
104
+ return obj
116
105
117
- try :
118
- # If this works, then _ustr(obj) has the same behaviour as str(obj), so
119
- # it won't break any existing code.
120
- return str (obj )
121
-
122
- except UnicodeEncodeError :
123
- # The Python docs (http://docs.python.org/ref/customization.html#l2h-182)
124
- # state that "The return value must be a string object". However, does a
125
- # unicode object (being a subclass of basestring) count as a "string
126
- # object"?
127
- # If so, then return a unicode object:
128
- return unicode (obj )
129
- # Else encode it... but how? There are many choices... :)
130
- # Replace unprintables with escape codes?
131
- #return unicode(obj).encode(sys.getdefaultencoding(), 'backslashreplace_errors')
132
- # Replace unprintables with question marks?
133
- #return unicode(obj).encode(sys.getdefaultencoding(), 'replace')
134
- # ...
106
+ try :
107
+ # If this works, then _ustr(obj) has the same behaviour as str(obj), so
108
+ # it won't break any existing code.
109
+ return str (obj )
110
+
111
+ except UnicodeEncodeError :
112
+ # The Python docs (http://docs.python.org/ref/customization.html#l2h-182)
113
+ # state that "The return value must be a string object". However, does a
114
+ # unicode object (being a subclass of basestring) count as a "string
115
+ # object"?
116
+ # If so, then return a unicode object:
117
+ return unicode (obj )
118
+ # Else encode it... but how? There are many choices... :)
119
+ # Replace unprintables with escape codes?
120
+ #return unicode(obj).encode(sys.getdefaultencoding(), 'backslashreplace_errors')
121
+ # Replace unprintables with question marks?
122
+ #return unicode(obj).encode(sys.getdefaultencoding(), 'replace')
123
+ # ...
135
124
136
125
# build list of single arg builtins, tolerant of Python version, that can be used as parse actions
137
126
singleArgBuiltins = []
@@ -860,15 +849,12 @@ def _parseNoCache( self, instring, loc, doActions=True, callPreParse=True ):
860
849
loc ,tokens = self .parseImpl ( instring , preloc , doActions )
861
850
except IndexError :
862
851
raise ParseException ( instring , len (instring ), self .errmsg , self )
863
- except ParseBaseException :
852
+ except ParseBaseException , err :
864
853
#~ print ("Exception raised:", err)
865
854
err = None
866
855
if self .debugActions [2 ]:
867
- err = sys .exc_info ()[1 ]
868
856
self .debugActions [2 ]( instring , tokensStart , self , err )
869
857
if self .failAction :
870
- if err is None :
871
- err = sys .exc_info ()[1 ]
872
858
self .failAction ( instring , tokensStart , self , err )
873
859
raise
874
860
else :
@@ -898,10 +884,9 @@ def _parseNoCache( self, instring, loc, doActions=True, callPreParse=True ):
898
884
self .resultsName ,
899
885
asList = self .saveAsList and isinstance (tokens ,(ParseResults ,list )),
900
886
modal = self .modalResults )
901
- except ParseBaseException :
887
+ except ParseBaseException , err :
902
888
#~ print "Exception raised in user parse action:", err
903
889
if (self .debugActions [2 ] ):
904
- err = sys .exc_info ()[1 ]
905
890
self .debugActions [2 ]( instring , tokensStart , self , err )
906
891
raise
907
892
else :
@@ -940,8 +925,7 @@ def _parseCache( self, instring, loc, doActions=True, callPreParse=True ):
940
925
value = self ._parseNoCache ( instring , loc , doActions , callPreParse )
941
926
ParserElement ._exprArgCache [ lookup ] = (value [0 ],value [1 ].copy ())
942
927
return value
943
- except ParseBaseException :
944
- pe = sys .exc_info ()[1 ]
928
+ except ParseBaseException , pe :
945
929
ParserElement ._exprArgCache [ lookup ] = pe
946
930
raise
947
931
@@ -1011,12 +995,11 @@ def parseString( self, instring, parseAll=False ):
1011
995
loc = self .preParse ( instring , loc )
1012
996
se = Empty () + StringEnd ()
1013
997
se ._parse ( instring , loc )
1014
- except ParseBaseException :
998
+ except ParseBaseException , exc :
1015
999
if ParserElement .verbose_stacktrace :
1016
1000
raise
1017
1001
else :
1018
1002
# catch and re-raise exception from here, clears out pyparsing internal stack trace
1019
- exc = sys .exc_info ()[1 ]
1020
1003
raise exc
1021
1004
else :
1022
1005
return tokens
@@ -1064,12 +1047,11 @@ def scanString( self, instring, maxMatches=_MAX_INT, overlap=False ):
1064
1047
loc = nextLoc
1065
1048
else :
1066
1049
loc = preloc + 1
1067
- except ParseBaseException :
1050
+ except ParseBaseException , exc :
1068
1051
if ParserElement .verbose_stacktrace :
1069
1052
raise
1070
1053
else :
1071
1054
# catch and re-raise exception from here, clears out pyparsing internal stack trace
1072
- exc = sys .exc_info ()[1 ]
1073
1055
raise exc
1074
1056
1075
1057
def transformString ( self , instring ):
@@ -1098,12 +1080,11 @@ def transformString( self, instring ):
1098
1080
out .append (instring [lastE :])
1099
1081
out = [o for o in out if o ]
1100
1082
return "" .join (map (_ustr ,_flatten (out )))
1101
- except ParseBaseException :
1083
+ except ParseBaseException , exc :
1102
1084
if ParserElement .verbose_stacktrace :
1103
1085
raise
1104
1086
else :
1105
1087
# catch and re-raise exception from here, clears out pyparsing internal stack trace
1106
- exc = sys .exc_info ()[1 ]
1107
1088
raise exc
1108
1089
1109
1090
def searchString ( self , instring , maxMatches = _MAX_INT ):
@@ -1113,12 +1094,11 @@ def searchString( self, instring, maxMatches=_MAX_INT ):
1113
1094
"""
1114
1095
try :
1115
1096
return ParseResults ([ t for t ,s ,e in self .scanString ( instring , maxMatches ) ])
1116
- except ParseBaseException :
1097
+ except ParseBaseException , exc :
1117
1098
if ParserElement .verbose_stacktrace :
1118
1099
raise
1119
1100
else :
1120
1101
# catch and re-raise exception from here, clears out pyparsing internal stack trace
1121
- exc = sys .exc_info ()[1 ]
1122
1102
raise exc
1123
1103
1124
1104
def __add__ (self , other ):
@@ -1396,10 +1376,12 @@ def parseFile( self, file_or_filename, parseAll=False ):
1396
1376
f .close ()
1397
1377
try :
1398
1378
return self .parseString (file_contents , parseAll )
1399
- except ParseBaseException :
1400
- # catch and re-raise exception from here, clears out pyparsing internal stack trace
1401
- exc = sys .exc_info ()[1 ]
1402
- raise exc
1379
+ except ParseBaseException , exc :
1380
+ if ParserElement .verbose_stacktrace :
1381
+ raise
1382
+ else :
1383
+ # catch and re-raise exception from here, clears out pyparsing internal stack trace
1384
+ raise exc
1403
1385
1404
1386
def getException (self ):
1405
1387
return ParseException ("" ,0 ,self .errmsg ,self )
@@ -2348,8 +2330,7 @@ def parseImpl( self, instring, loc, doActions=True ):
2348
2330
loc , exprtokens = e ._parse ( instring , loc , doActions )
2349
2331
except ParseSyntaxException :
2350
2332
raise
2351
- except ParseBaseException :
2352
- pe = sys .exc_info ()[1 ]
2333
+ except ParseBaseException , pe :
2353
2334
raise ParseSyntaxException (pe )
2354
2335
except IndexError :
2355
2336
raise ParseSyntaxException ( ParseException (instring , len (instring ), self .errmsg , self ) )
@@ -2401,8 +2382,7 @@ def parseImpl( self, instring, loc, doActions=True ):
2401
2382
for e in self .exprs :
2402
2383
try :
2403
2384
loc2 = e .tryParse ( instring , loc )
2404
- except ParseException :
2405
- err = sys .exc_info ()[1 ]
2385
+ except ParseException , err :
2406
2386
if err .loc > maxExcLoc :
2407
2387
maxException = err
2408
2388
maxExcLoc = err .loc
@@ -2984,7 +2964,7 @@ def __init__(self, *args):
2984
2964
DeprecationWarning ,stacklevel = 2 )
2985
2965
2986
2966
def postParse ( self , instring , loc , tokenlist ):
2987
- return list (map ( string .upper , tokenlist ))
2967
+ return list (map ( str .upper , tokenlist ))
2988
2968
2989
2969
2990
2970
class Combine (TokenConverter ):
@@ -3096,8 +3076,7 @@ def z(*paArgs):
3096
3076
sys .stderr .write ( ">>entering %s(line: '%s', %d, %s)\n " % (thisFunc ,line (l ,s ),l ,t ) )
3097
3077
try :
3098
3078
ret = f (* paArgs )
3099
- except Exception :
3100
- exc = sys .exc_info ()[1 ]
3079
+ except Exception , exc :
3101
3080
sys .stderr .write ( "<<leaving %s (exception: %s)\n " % (thisFunc ,exc ) )
3102
3081
raise
3103
3082
sys .stderr .write ( "<<leaving %s (ret: %s)\n " % (thisFunc ,ret ) )
@@ -3709,8 +3688,7 @@ def test( teststring ):
3709
3688
print ("tokens.columns = " + str (tokens .columns ))
3710
3689
print ("tokens.tables = " + str (tokens .tables ))
3711
3690
print (tokens .asXML ("SQL" ,True ))
3712
- except ParseBaseException :
3713
- err = sys .exc_info ()[1 ]
3691
+ except ParseBaseException , err :
3714
3692
print (teststring + "->" )
3715
3693
print (err .line )
3716
3694
print (" " * (err .column - 1 ) + "^" )
0 commit comments