@@ -69,7 +69,7 @@ def to_basestring(s):
6969# --------------------------------- Main classes -------------------------------
7070
7171
72- class VBase ( object ) :
72+ class VBase :
7373 """
7474 Base class for ContentLine and Component.
7575
@@ -88,7 +88,7 @@ class VBase(object):
8888 """
8989
9090 def __init__ (self , group = None , * args , ** kwds ):
91- super (VBase , self ).__init__ (* args , ** kwds )
91+ super ().__init__ (* args , ** kwds )
9292 self .name = None
9393 self .group = group
9494 self .behavior = None
@@ -176,11 +176,11 @@ def transformToNative(self):
176176 if lineNumber is not None :
177177 e .lineNumber = lineNumber
178178 raise
179- else :
180- msg = "In transformToNative, unhandled exception on line {0}: {1}: {2}"
181- msg = msg .format (lineNumber , sys .exc_info ()[0 ], sys .exc_info ()[1 ])
182- msg = msg + " (" + str (self_orig ) + ")"
183- raise ParseError (msg , lineNumber )
179+
180+ msg = "In transformToNative, unhandled exception on line {0}: {1}: {2}"
181+ msg = msg .format (lineNumber , sys .exc_info ()[0 ], sys .exc_info ()[1 ])
182+ msg = msg + " (" + str (self_orig ) + ")"
183+ raise ParseError (msg , lineNumber )
184184
185185 def transformFromNative (self ):
186186 """
@@ -205,10 +205,10 @@ def transformFromNative(self):
205205 if lineNumber is not None :
206206 e .lineNumber = lineNumber
207207 raise
208- else :
209- msg = "In transformFromNative, unhandled exception on line {0} {1}: {2}"
210- msg = msg .format (lineNumber , sys .exc_info ()[0 ], sys .exc_info ()[1 ])
211- raise NativeError (msg , lineNumber )
208+
209+ msg = "In transformFromNative, unhandled exception on line {0} {1}: {2}"
210+ msg = msg .format (lineNumber , sys .exc_info ()[0 ], sys .exc_info ()[1 ])
211+ raise NativeError (msg , lineNumber )
212212 else :
213213 return self
214214
@@ -282,7 +282,7 @@ def __init__(self, name, params, value, group=None, encoded=False, isNative=Fals
282282
283283 Group is used as a positional argument to match parseLine's return
284284 """
285- super (ContentLine , self ).__init__ (group , * args , ** kwds )
285+ super ().__init__ (group , * args , ** kwds )
286286
287287 self .name = name .upper ()
288288 self .encoded = encoded
@@ -330,7 +330,7 @@ def duplicate(cls, copyit):
330330 return newcopy
331331
332332 def copy (self , copyit ):
333- super (ContentLine , self ).copy (copyit )
333+ super ().copy (copyit )
334334 self .name = copyit .name
335335 self .value = copy .copy (copyit .value )
336336 self .encoded = self .encoded
@@ -343,7 +343,7 @@ def copy(self, copyit):
343343 def __eq__ (self , other ):
344344 try :
345345 return (self .name == other .name ) and (self .params == other .params ) and (self .value == other .value )
346- except Exception :
346+ except AttributeError :
347347 return False
348348
349349 def __getattr__ (self , name ):
@@ -449,7 +449,7 @@ class Component(VBase):
449449 """
450450
451451 def __init__ (self , name = None , * args , ** kwds ):
452- super (Component , self ).__init__ (* args , ** kwds )
452+ super ().__init__ (* args , ** kwds )
453453 self .contents = {}
454454 if name :
455455 self .name = name .upper ()
@@ -467,7 +467,7 @@ def duplicate(cls, copyit):
467467 return newcopy
468468
469469 def copy (self , copyit ):
470- super (Component , self ).copy (copyit )
470+ super ().copy (copyit )
471471
472472 # deep copy of contents
473473 self .contents = {}
@@ -627,7 +627,7 @@ def lines(self):
627627 def sortChildKeys (self ):
628628 try :
629629 first = [s for s in self .behavior .sortFirst if s in self .contents ]
630- except Exception :
630+ except AttributeError :
631631 first = []
632632 return first + sorted (k for k in self .contents if k not in first )
633633
@@ -949,13 +949,18 @@ def foldOneLine(outbuf, input_, lineLength=75):
949949
950950 TO-DO: This all seems odd. Is it still needed, especially in python3?
951951 """
952- if len (input_ ) < lineLength :
953- # Optimize for unfolded line case
952+
953+ def write_to_outbuf (text ):
954+ # TODO: remove py2
954955 try :
955- outbuf .write (bytes (input_ , "UTF-8" ))
956- except Exception :
956+ outbuf .write (bytes (text , "UTF-8" ))
957+ except Exception : # pylint: disable=broad-exception-caught
957958 # fall back on py2 syntax
958- outbuf .write (input_ )
959+ outbuf .write (text )
960+
961+ if len (input_ ) < lineLength :
962+ # Optimize for unfolded line case
963+ write_to_outbuf (text = input_ )
959964
960965 else :
961966 # Look for valid utf8 range and write that out
@@ -968,12 +973,7 @@ def foldOneLine(outbuf, input_, lineLength=75):
968973 s = decoded [start ] # take one char
969974 size = len (to_basestring (s )) # calculate it's size in bytes
970975 if counter + size > lineLength :
971- try :
972- outbuf .write (bytes ("\r \n " , "UTF-8" ))
973- except Exception :
974- # fall back on py2 syntax
975- outbuf .write ("\r \n " )
976-
976+ write_to_outbuf (text = "\r \n " )
977977 counter = 1 # one for space
978978
979979 if str is unicode_type :
@@ -985,11 +985,7 @@ def foldOneLine(outbuf, input_, lineLength=75):
985985 written += size
986986 counter += size
987987 start += 1
988- try :
989- outbuf .write (bytes ("\r \n " , "UTF-8" ))
990- except Exception :
991- # fall back on py2 syntax
992- outbuf .write ("\r \n " )
988+ write_to_outbuf (text = "\r \n " )
993989
994990
995991def defaultSerialize (obj , buf , lineLength ):
0 commit comments